scivex-optim 0.1.1

Scivex — Optimization, root finding, and numerical integration
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//! Active set method for convex quadratic programming.
//!
//! Implements the primal active set algorithm for problems with inequality
//! constraints. The Hessian `H` must be symmetric positive semi-definite.

use scivex_core::Float;

use crate::error::{OptimError, Result};

use super::QpResult;

/// Solve a convex quadratic program using the active set method.
///
/// Minimizes `0.5 x^T H x + c^T x` subject to `A_ub x <= b_ub` where `H` is
/// symmetric positive semi-definite.
///
/// # Arguments
///
/// * `h` - `n x n` symmetric PSD Hessian matrix (row-major `Vec<Vec<T>>`)
/// * `c` - `n`-vector linear cost term
/// * `a_ub` - `m x n` inequality constraint matrix
/// * `b_ub` - `m`-vector inequality right-hand side
/// * `max_iter` - maximum number of active set iterations
///
/// # Errors
///
/// Returns [`OptimError::InvalidParameter`] if dimensions are inconsistent.
/// Returns [`OptimError::ConvergenceFailure`] if `max_iter` is exhausted.
///
/// # Examples
///
/// ```
/// # use scivex_optim::quadprog::quadprog;
/// // Minimize 0.5 * x^2 subject to x >= 1 (i.e. -x <= -1)
/// let h = vec![vec![1.0_f64]];
/// let c = vec![0.0];
/// let a_ub = vec![vec![-1.0]];
/// let b_ub = vec![-1.0];
/// let result = quadprog(&h, &c, &a_ub, &b_ub, 100).unwrap();
/// assert!((result.x[0] - 1.0).abs() < 1e-6);
/// ```
#[allow(clippy::too_many_lines)]
pub fn quadprog<T: Float>(
    h: &[Vec<T>],
    c: &[T],
    a_ub: &[Vec<T>],
    b_ub: &[T],
    max_iter: usize,
) -> Result<QpResult<T>> {
    let n = c.len();
    let m = a_ub.len();

    validate_inputs(h, c, a_ub, b_ub, n, m)?;

    // Find an initial feasible point.
    let mut x = find_feasible_point(h, c, a_ub, b_ub, n, m)?;

    // Build the initial working set: indices of constraints active at x.
    let mut working_set: Vec<usize> = Vec::new();
    let tol = T::from_f64(1e-10);
    for i in 0..m {
        let ai_x = dot_row(&a_ub[i], &x);
        if (ai_x - b_ub[i]).abs() < tol {
            working_set.push(i);
        }
    }

    let mut iterations = 0usize;

    while iterations < max_iter {
        iterations += 1;

        // Gradient at current point: g = H*x + c
        let g = hessian_times_vec(h, &x, n, c);

        // Solve the equality-constrained QP subproblem for step p and
        // Lagrange multipliers lambda via the KKT system.
        let w_size = working_set.len();
        let kkt_dim = n + w_size;

        // Build KKT matrix:
        // [ H        A_w^T ]   [ p ]   [ -g ]
        // [ A_w      0     ] * [ l ] = [  0  ]
        let mut kkt = vec![T::zero(); kkt_dim * kkt_dim];
        let mut rhs = vec![T::zero(); kkt_dim];

        // Fill H block
        for i in 0..n {
            for j in 0..n {
                kkt[i * kkt_dim + j] = h[i][j];
            }
        }

        // Fill A_w^T block (top-right) and A_w block (bottom-left)
        for (wi, &ci) in working_set.iter().enumerate() {
            for j in 0..n {
                kkt[j * kkt_dim + (n + wi)] = a_ub[ci][j];
                kkt[(n + wi) * kkt_dim + j] = a_ub[ci][j];
            }
        }

        // RHS: [ -g; 0 ]
        for i in 0..n {
            rhs[i] = -g[i];
        }

        // Solve KKT system
        let solution = solve_linear_system(&kkt, &rhs, kkt_dim);

        let (p, lambda) = match solution {
            Some(sol) => {
                let p_vec: Vec<T> = sol[..n].to_vec();
                let l_vec: Vec<T> = sol[n..].to_vec();
                (p_vec, l_vec)
            }
            None => {
                // Singular KKT — treat as zero step
                (vec![T::zero(); n], vec![T::zero(); w_size])
            }
        };

        let p_norm: T = p.iter().map(|&pi| pi * pi).sum();

        if p_norm < tol * tol {
            // p is essentially zero — check Lagrange multipliers.
            if working_set.is_empty() {
                // Unconstrained optimum, done.
                let fun = objective(h, c, &x, n);
                return Ok(QpResult {
                    x,
                    fun,
                    iterations,
                    converged: true,
                });
            }

            // Find most negative multiplier.
            let mut min_lambda = T::zero();
            let mut min_idx: Option<usize> = None;
            for (wi, &li) in lambda.iter().enumerate() {
                if li < min_lambda {
                    min_lambda = li;
                    min_idx = Some(wi);
                }
            }

            if let Some(idx) = min_idx {
                // Drop the constraint with the most negative multiplier.
                working_set.remove(idx);
            } else {
                // All multipliers >= 0, we are optimal.
                let fun = objective(h, c, &x, n);
                return Ok(QpResult {
                    x,
                    fun,
                    iterations,
                    converged: true,
                });
            }
        } else {
            // p != 0: compute maximum step size alpha staying feasible.
            let mut alpha = T::one();

            let mut blocking_constraint: Option<usize> = None;

            for i in 0..m {
                // Skip constraints already in the working set.
                if working_set.contains(&i) {
                    continue;
                }

                let a_p: T = dot_row(&a_ub[i], &p);
                if a_p <= tol {
                    // Constraint is not tightened by this step.
                    continue;
                }

                let a_x: T = dot_row(&a_ub[i], &x);
                let slack = b_ub[i] - a_x;
                let step = slack / a_p;

                if step < alpha {
                    alpha = step;
                    blocking_constraint = Some(i);
                }
            }

            // Clamp alpha to avoid negative due to numerical noise.
            if alpha < T::zero() {
                alpha = T::zero();
            }

            // Update x += alpha * p
            for j in 0..n {
                x[j] += alpha * p[j];
            }

            // If a constraint blocked, add it to the working set.
            if let Some(bc) = blocking_constraint.filter(|_| alpha < T::one()) {
                working_set.push(bc);
            }
        }
    }

    // Did not converge within max_iter.
    Err(OptimError::ConvergenceFailure {
        iterations: max_iter,
    })
}

// ---------------------------------------------------------------------------
// Helper functions
// ---------------------------------------------------------------------------

/// Validate that all dimensions are consistent.
fn validate_inputs<T: Float>(
    h: &[Vec<T>],
    _c: &[T],
    a_ub: &[Vec<T>],
    b_ub: &[T],
    n: usize,
    m: usize,
) -> Result<()> {
    if n == 0 {
        return Err(OptimError::InvalidParameter {
            name: "c",
            reason: "must be non-empty",
        });
    }

    if h.len() != n {
        return Err(OptimError::InvalidParameter {
            name: "h",
            reason: "must have n rows matching length of c",
        });
    }
    for row in h {
        if row.len() != n {
            return Err(OptimError::InvalidParameter {
                name: "h",
                reason: "all rows must have length n",
            });
        }
    }

    if b_ub.len() != m {
        return Err(OptimError::InvalidParameter {
            name: "b_ub",
            reason: "length must match number of rows in a_ub",
        });
    }

    for row in a_ub {
        if row.len() != n {
            return Err(OptimError::InvalidParameter {
                name: "a_ub",
                reason: "all rows must have length equal to c",
            });
        }
    }

    Ok(())
}

/// Compute `H * x + c`.
fn hessian_times_vec<T: Float>(h: &[Vec<T>], x: &[T], n: usize, c: &[T]) -> Vec<T> {
    let mut g = vec![T::zero(); n];
    for i in 0..n {
        let mut sum = T::zero();
        for j in 0..n {
            sum += h[i][j] * x[j];
        }
        g[i] = sum + c[i];
    }
    g
}

/// Dot product of a row vector with x.
fn dot_row<T: Float>(row: &[T], x: &[T]) -> T {
    let mut s = T::zero();
    for i in 0..row.len() {
        s += row[i] * x[i];
    }
    s
}

/// Compute objective 0.5 * x^T H x + c^T x.
fn objective<T: Float>(h: &[Vec<T>], c: &[T], x: &[T], n: usize) -> T {
    let half = T::from_f64(0.5);
    let mut quad = T::zero();
    for i in 0..n {
        for j in 0..n {
            quad += x[i] * h[i][j] * x[j];
        }
    }
    let mut lin = T::zero();
    for i in 0..n {
        lin += c[i] * x[i];
    }
    half * quad + lin
}

/// Find an initial feasible point satisfying all `A x <= b`.
///
/// Tries the origin first. If the origin is infeasible, projects towards
/// feasibility by iteratively adjusting the point along violated constraint
/// normals.
fn find_feasible_point<T: Float>(
    _h: &[Vec<T>],
    _c: &[T],
    a_ub: &[Vec<T>],
    b_ub: &[T],
    n: usize,
    m: usize,
) -> Result<Vec<T>> {
    if m == 0 {
        return Ok(vec![T::zero(); n]);
    }

    let mut x = vec![T::zero(); n];

    // Check if origin is feasible.
    let origin_feasible = (0..m).all(|i| dot_row(&a_ub[i], &x) <= b_ub[i]);
    if origin_feasible {
        return Ok(x);
    }

    // Iterative projection to find a feasible point.
    // For each violated constraint a_i^T x > b_i, push x to satisfy it.
    let tol = T::from_f64(1e-10);
    let max_proj_iter = 1000usize;

    for _ in 0..max_proj_iter {
        let mut feasible = true;
        for i in 0..m {
            let ai_x = dot_row(&a_ub[i], &x);
            let violation = ai_x - b_ub[i];
            if violation > tol {
                feasible = false;
                // Project x onto the half-space a_i^T x <= b_i:
                // x_new = x - ((a_i^T x - b_i) / ||a_i||^2) * a_i
                let norm_sq: T = a_ub[i].iter().map(|&v| v * v).sum();
                if norm_sq > tol {
                    let factor = violation / norm_sq;
                    for j in 0..n {
                        x[j] -= factor * a_ub[i][j];
                    }
                }
            }
        }
        if feasible {
            return Ok(x);
        }
    }

    Err(OptimError::InvalidParameter {
        name: "constraints",
        reason: "could not find a feasible starting point",
    })
}

/// Solve a dense linear system `A x = b` via Gaussian elimination with
/// partial pivoting. Returns `None` if the matrix is singular.
fn solve_linear_system<T: Float>(a: &[T], b: &[T], n: usize) -> Option<Vec<T>> {
    let mut aug = vec![T::zero(); n * (n + 1)];
    for i in 0..n {
        for j in 0..n {
            aug[i * (n + 1) + j] = a[i * n + j];
        }
        aug[i * (n + 1) + n] = b[i];
    }

    // Forward elimination with partial pivoting
    for col in 0..n {
        // Find pivot
        let mut max_val = aug[col * (n + 1) + col].abs();
        let mut max_row = col;
        for row in (col + 1)..n {
            let val = aug[row * (n + 1) + col].abs();
            if val > max_val {
                max_val = val;
                max_row = row;
            }
        }

        if max_val < T::from_f64(1e-14) {
            return None; // Singular
        }

        // Swap rows
        if max_row != col {
            for j in 0..=n {
                aug.swap(col * (n + 1) + j, max_row * (n + 1) + j);
            }
        }

        // Eliminate
        let pivot = aug[col * (n + 1) + col];
        for row in (col + 1)..n {
            let factor = aug[row * (n + 1) + col] / pivot;
            for j in col..=n {
                let val = aug[col * (n + 1) + j];
                aug[row * (n + 1) + j] -= factor * val;
            }
        }
    }

    // Back substitution
    let mut x = vec![T::zero(); n];
    for i in (0..n).rev() {
        let mut sum = aug[i * (n + 1) + n];
        for j in (i + 1)..n {
            sum -= aug[i * (n + 1) + j] * x[j];
        }
        x[i] = sum / aug[i * (n + 1) + i];
    }

    Some(x)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn approx_eq(a: f64, b: f64, tol: f64) -> bool {
        (a - b).abs() < tol
    }

    fn approx_vec(a: &[f64], b: &[f64], tol: f64) -> bool {
        a.len() == b.len()
            && a.iter()
                .zip(b.iter())
                .all(|(&ai, &bi)| approx_eq(ai, bi, tol))
    }

    #[test]
    fn test_qp_unconstrained() {
        // minimize 0.5*(x1^2 + x2^2) + (-2)*x1 + (-1)*x2
        // H = I, c = [-2, -1]
        // Optimal: x = [2, 1], fun = -2.5
        let h = vec![vec![1.0, 0.0], vec![0.0, 1.0]];
        let c = vec![-2.0, -1.0];
        let a_ub: Vec<Vec<f64>> = vec![];
        let b_ub: Vec<f64> = vec![];

        let res = quadprog(&h, &c, &a_ub, &b_ub, 100).expect("should converge");
        assert!(res.converged);
        assert!(approx_vec(&res.x, &[2.0, 1.0], 1e-6));
        assert!(approx_eq(res.fun, -2.5, 1e-6));
    }

    #[test]
    fn test_qp_with_constraints() {
        // minimize 0.5*(x1^2 + x2^2) - 2*x1 - x2
        // subject to x1 + x2 <= 1
        // Unconstrained optimum is [2, 1] which violates x1+x2<=1.
        // Constrained optimum: x1 + x2 = 1, with x1 = 1.5 - 0.5 = ?
        // KKT: x1 - 2 + lambda = 0, x2 - 1 + lambda = 0, x1+x2=1
        // => x1 = 2 - lambda, x2 = 1 - lambda, (2-lambda)+(1-lambda)=1 => lambda=1
        // => x1 = 1, x2 = 0, fun = 0.5 - 2 = -1.5
        let h = vec![vec![1.0, 0.0], vec![0.0, 1.0]];
        let c = vec![-2.0, -1.0];
        let a_ub = vec![vec![1.0, 1.0]];
        let b_ub = vec![1.0];

        let res = quadprog(&h, &c, &a_ub, &b_ub, 100).expect("should converge");
        assert!(res.converged);
        assert!(approx_vec(&res.x, &[1.0, 0.0], 1e-6));
        assert!(approx_eq(res.fun, -1.5, 1e-6));
    }

    #[test]
    fn test_qp_box_constraints() {
        // minimize 0.5*(x1^2 + x2^2) - 5*x1 - 5*x2
        // subject to x1 <= 2, x2 <= 3, -x1 <= 0, -x2 <= 0
        // Unconstrained: [5, 5]. With bounds: [2, 3].
        let h = vec![vec![1.0, 0.0], vec![0.0, 1.0]];
        let c = vec![-5.0, -5.0];
        let a_ub = vec![
            vec![1.0, 0.0],  // x1 <= 2
            vec![0.0, 1.0],  // x2 <= 3
            vec![-1.0, 0.0], // -x1 <= 0 (i.e. x1 >= 0)
            vec![0.0, -1.0], // -x2 <= 0 (i.e. x2 >= 0)
        ];
        let b_ub = vec![2.0, 3.0, 0.0, 0.0];

        let res = quadprog(&h, &c, &a_ub, &b_ub, 200).expect("should converge");
        assert!(res.converged);
        assert!(approx_vec(&res.x, &[2.0, 3.0], 1e-6));
        // fun = 0.5*(4+9) - 10 - 15 = 6.5 - 25 = -18.5
        assert!(approx_eq(res.fun, -18.5, 1e-6));
    }

    #[test]
    fn test_qp_infeasible_start() {
        // minimize 0.5*(x1^2 + x2^2)
        // subject to x1 + x2 >= 2 (i.e. -x1 - x2 <= -2)
        //            x1 >= 0, x2 >= 0
        // Origin is NOT feasible. Optimal: x1=1, x2=1, fun=1.
        let h = vec![vec![1.0, 0.0], vec![0.0, 1.0]];
        let c = vec![0.0, 0.0];
        let a_ub = vec![
            vec![-1.0, -1.0], // -x1-x2 <= -2
            vec![-1.0, 0.0],  // -x1 <= 0
            vec![0.0, -1.0],  // -x2 <= 0
        ];
        let b_ub = vec![-2.0, 0.0, 0.0];

        let res = quadprog(&h, &c, &a_ub, &b_ub, 200).expect("should converge");
        assert!(res.converged);
        assert!(approx_vec(&res.x, &[1.0, 1.0], 1e-6));
        assert!(approx_eq(res.fun, 1.0, 1e-6));
    }

    #[test]
    fn test_qp_convergence() {
        // Simple problem that must converge.
        // minimize 0.5 * x^2 - x, optimal at x=1, fun=-0.5
        let h = vec![vec![1.0]];
        let c = vec![-1.0];
        let a_ub: Vec<Vec<f64>> = vec![];
        let b_ub: Vec<f64> = vec![];

        let res = quadprog(&h, &c, &a_ub, &b_ub, 50).expect("should converge");
        assert!(res.converged);
        assert!(approx_eq(res.x[0], 1.0, 1e-8));
        assert!(approx_eq(res.fun, -0.5, 1e-8));
        assert!(res.iterations <= 50);
    }
}