scirs2-optimize 0.5.1

Optimization module for SciRS2 (scirs2-optimize)
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
//! Tests for constrained optimization module

use crate::constrained::*;
use crate::error::OptimizeError;
use scirs2_core::ndarray::array;

#[allow(dead_code)]
fn objective(x: &[f64]) -> f64 {
    (x[0] - 1.0).powi(2) + (x[1] - 2.5).powi(2)
}

#[allow(dead_code)]
fn constraint(x: &[f64]) -> f64 {
    3.0 - x[0] - x[1] // Should be >= 0
}

#[test]
#[allow(dead_code)]
fn test_minimize_constrained_placeholder() {
    // We're now using the real implementation, so this test needs to be adjusted
    let x0 = array![0.0, 0.0];
    let constraints = vec![Constraint::new(constraint, Constraint::INEQUALITY)];

    // Use minimal iterations to check basic algorithm behavior
    let options = Options {
        maxiter: Some(1), // Just a single iteration
        ..Options::default()
    };

    let result = minimize_constrained(
        objective,
        &x0.view(),
        &constraints,
        Method::SLSQP,
        Some(options),
    )
    .expect("Operation failed");

    // With limited iterations, we expect it not to converge
    assert!(!result.success);

    // Check that constraint value was computed
    assert!(result.constr.is_some());
    let constr = result.constr.expect("Operation failed");
    assert_eq!(constr.len(), 1);
}

// Test the SLSQP algorithm on a simple constrained problem
#[test]
#[allow(dead_code)]
fn test_minimize_slsqp() {
    // Problem:
    // Minimize (x-1)^2 + (y-2.5)^2
    // Subject to: x + y <= 3

    let x0 = array![0.0, 0.0];
    let constraints = vec![Constraint::new(constraint, Constraint::INEQUALITY)];

    let options = Options {
        maxiter: Some(100),
        gtol: Some(1e-6),
        ftol: Some(1e-6),
        ctol: Some(1e-6),
        ..Options::default()
    };

    let result = minimize_constrained(
        objective,
        &x0.view(),
        &constraints,
        Method::SLSQP,
        Some(options),
    )
    .expect("Operation failed");

    // For the purpose of this test, we're just checking that the algorithm runs
    // and produces reasonable output. The convergence may vary.

    // Check that we're moving in the right direction
    assert!(result.x[0] >= 0.0);
    assert!(result.x[1] >= 0.0);

    // Function value should be decreasing from initial point
    let initial_value = objective(&[0.0, 0.0]);
    assert!(result.fun <= initial_value);

    // Check that constraint values are computed
    assert!(result.constr.is_some());

    // Output the result for inspection
    println!(
        "SLSQP result: x = {:?}, f = {}, iterations = {}",
        result.x, result.fun, result.nit
    );
}

// Test the Trust Region Constrained algorithm
#[test]
#[allow(dead_code)]
fn test_minimize_trust_constr() {
    // Problem:
    // Minimize (x-1)^2 + (y-2.5)^2
    // Subject to: x + y <= 3

    let x0 = array![0.0, 0.0];
    let constraints = vec![Constraint::new(constraint, Constraint::INEQUALITY)];

    let options = Options {
        maxiter: Some(500), // Increased iterations for convergence
        gtol: Some(1e-6),
        ftol: Some(1e-6),
        ctol: Some(1e-6),
        ..Options::default()
    };

    let result = minimize_constrained(
        objective,
        &x0.view(),
        &constraints,
        Method::TrustConstr,
        Some(options.clone()),
    )
    .expect("Operation failed");

    // Check that we're moving in the right direction
    assert!(result.x[0] >= 0.0);
    assert!(result.x[1] >= 0.0);

    // Function value should be decreasing from initial point
    let initial_value = objective(&[0.0, 0.0]);
    assert!(result.fun <= initial_value);

    // Check that constraint values are computed
    assert!(result.constr.is_some());

    // Output the result for inspection
    println!(
        "TrustConstr result: x = {:?}, f = {}, iterations = {}",
        result.x, result.fun, result.nit
    );
}

// Test both constrained optimization methods on a more complex problem
#[test]
#[allow(dead_code)]
fn test_constrained_rosenbrock() {
    // Rosenbrock function with a constraint
    fn rosenbrock(x: &[f64]) -> f64 {
        100.0 * (x[1] - x[0].powi(2)).powi(2) + (1.0 - x[0]).powi(2)
    }

    // Constraint: x[0]^2 + x[1]^2 <= 1.5
    fn circle_constraint(x: &[f64]) -> f64 {
        1.5 - (x[0].powi(2) + x[1].powi(2)) // Should be >= 0
    }

    let x0 = array![0.0, 0.0];
    let constraints = vec![Constraint::new(circle_constraint, Constraint::INEQUALITY)];

    let options = Options {
        maxiter: Some(1000), // More iterations for this harder problem
        gtol: Some(1e-4),    // Relaxed tolerances
        ftol: Some(1e-4),
        ctol: Some(1e-4),
        ..Options::default()
    };

    // For this test, we'll clone options at each stage to avoid move issues
    let options_copy1 = options.clone();
    let options_copy2 = options.clone();

    // Test SLSQP
    let result_slsqp = minimize_constrained(
        rosenbrock,
        &x0.view(),
        &constraints,
        Method::SLSQP,
        Some(options_copy1),
    )
    .expect("Operation failed");

    // Test TrustConstr
    let result_trust = minimize_constrained(
        rosenbrock,
        &x0.view(),
        &constraints,
        Method::TrustConstr,
        Some(options_copy2),
    )
    .expect("Operation failed");

    // Check that both methods find a reasonable solution
    println!(
        "SLSQP Rosenbrock result: x = {:?}, f = {}, iterations = {}",
        result_slsqp.x, result_slsqp.fun, result_slsqp.nit
    );
    println!(
        "TrustConstr Rosenbrock result: x = {:?}, f = {}, iterations = {}",
        result_trust.x, result_trust.fun, result_trust.nit
    );

    // Check that function value is better than initial point
    let initial_value = rosenbrock(&[0.0, 0.0]);
    assert!(result_slsqp.fun < initial_value);
    assert!(result_trust.fun < initial_value);

    // Check that constraint is satisfied
    let constr_slsqp = result_slsqp.constr.expect("Operation failed");
    let constr_trust = result_trust.constr.expect("Operation failed");
    assert!(constr_slsqp[0] >= -0.01); // Relaxed tolerance for the test
    assert!(constr_trust[0] >= -0.01); // Relaxed tolerance for the test
}

#[test]
#[allow(dead_code)]
fn test_cobyla_not_implemented() {
    // Test that COBYLA returns a NotImplementedError
    let x0 = array![0.0, 0.0];
    let constraints = vec![Constraint::new(constraint, Constraint::INEQUALITY)];

    let result = minimize_constrained(objective, &x0.view(), &constraints, Method::COBYLA, None);

    // COBYLA is now implemented, so it should succeed
    assert!(result.is_ok());
    let opt_result = result.expect("Operation failed");
    assert!(opt_result.success || opt_result.nit > 0); // Should make progress or succeed
}

/// Verify that Method::AugmentedLagrangian is wired up and returns a valid result.
/// The inequality constraint is x0 + x1 <= 3, minimising (x0-1)^2 + (x1-2.5)^2.
/// Unconstrained minimum at (1, 2.5) satisfies the constraint, so we expect success
/// near that point when running enough iterations.
#[test]
fn test_augmented_lagrangian_wired_up() {
    let x0 = array![0.5, 0.5];
    let constraints = vec![Constraint::new(constraint, Constraint::INEQUALITY)];

    let options = Options {
        maxiter: Some(50),
        ..Options::default()
    };

    let result = minimize_constrained(
        objective,
        &x0.view(),
        &constraints,
        Method::AugmentedLagrangian,
        Some(options),
    );

    // The method must succeed without error (no longer returns NotImplementedError)
    assert!(
        result.is_ok(),
        "AugmentedLagrangian returned an error: {:?}",
        result.err()
    );
    let opt_result = result.expect("AugmentedLagrangian failed");
    // Must make at least one iteration
    assert!(opt_result.nit > 0 || opt_result.success);
    // Objective must be finite
    assert!(opt_result.fun.is_finite());
}

/// Test with equality constraint: x0 + x1 = 2, minimise x0^2 + x1^2.
/// Optimal solution is x0 = x1 = 1 with f = 2.
#[test]
fn test_augmented_lagrangian_equality_wired_up() {
    fn obj_sum_sq(x: &[f64]) -> f64 {
        x[0].powi(2) + x[1].powi(2)
    }
    // Equality g(x) = 0, expressed as g(x) = x0 + x1 - 2 (must be 0)
    // The constraint API uses "fun >= 0" for inequality and "fun == 0" for equality.
    fn eq_con(x: &[f64]) -> f64 {
        x[0] + x[1] - 2.0
    }

    let x0 = array![0.5, 1.5];
    let constraints = vec![Constraint::new(eq_con, Constraint::EQUALITY)];
    let options = Options {
        maxiter: Some(100),
        ..Options::default()
    };

    let result = minimize_constrained(
        obj_sum_sq,
        &x0.view(),
        &constraints,
        Method::AugmentedLagrangian,
        Some(options),
    );

    assert!(
        result.is_ok(),
        "AugmentedLagrangian equality failed: {:?}",
        result.err()
    );
    let opt_result = result.expect("AugmentedLagrangian equality failed");
    assert!(opt_result.fun.is_finite());
}

// ---------------------------------------------------------------------------
// Issue #126 — closures (capturing outer variables) accepted as constraints
// ---------------------------------------------------------------------------

/// A closure capturing a local `threshold` is used as an inequality constraint.
///
/// Problem: minimise (x0 - 1)^2 + (x1 - 2.5)^2 subject to
///   threshold - x0 - x1 >= 0  with threshold = 3.0.
/// The unconstrained optimum (1.0, 2.5) has x0 + x1 = 3.5 > 3, so it is
/// infeasible; the constrained optimum lies on x0 + x1 = 3 at (0.75, 2.25).
///
/// Starting from a feasible interior point (1.5, 1.5) the SLSQP solver reaches
/// the boundary. Before issue #126, a captured-variable closure could not be
/// stored in a `Constraint` (only `fn` pointers were accepted), so this test
/// would not even compile.
#[test]
fn test_issue_126_constraint_captures_variable() {
    let threshold = 3.0_f64;

    let obj = |x: &[f64]| (x[0] - 1.0).powi(2) + (x[1] - 2.5).powi(2);
    // Closure captures `threshold` from the enclosing scope.
    let cons = vec![Constraint::new(
        move |x: &[f64]| threshold - x[0] - x[1],
        Constraint::INEQUALITY,
    )];

    let x0 = array![1.5, 1.5];
    let result = minimize_constrained(obj, &x0, &cons, Method::SLSQP, None)
        .expect("constrained minimisation should not error");

    assert!(result.success, "SLSQP did not converge: {}", result.message);
    // The constrained solution lies on x0 + x1 = threshold.
    let sum = result.x[0] + result.x[1];
    assert!(
        (sum - threshold).abs() < 1e-2,
        "x0 + x1 = {} (expected ~{})",
        sum,
        threshold
    );
    assert!(result.fun.is_finite());
    // Objective at (0.75, 2.25) is 0.125.
    assert!(
        (result.fun - 0.125).abs() < 1e-2,
        "objective = {} (expected ~0.125)",
        result.fun
    );
}

/// Two closures capturing *different* local variables live in a single
/// `Vec<Constraint>`. This is only possible because the constraint callable is
/// stored as a boxed trait object (issue #126); distinct closure types would
/// otherwise be incompatible elements of one `Vec`.
#[test]
fn test_issue_126_heterogeneous_closures_in_vec() {
    let upper = 3.0_f64; // captured by closure A
    let lower = 0.1_f64; // captured by a *different* closure B

    let obj = |x: &[f64]| (x[0] - 1.0).powi(2) + (x[1] - 2.5).powi(2);

    // closure_a captures `upper`; closure_b captures `lower` — different types.
    let closure_a = move |x: &[f64]| upper - x[0] - x[1];
    let closure_b = move |x: &[f64]| x[0] - lower;

    let cons = vec![
        Constraint::new(closure_a, Constraint::INEQUALITY),
        Constraint::new(closure_b, Constraint::INEQUALITY),
    ];

    let x0 = array![1.5, 1.5];
    // The point of this test is that the heterogeneous `Vec` compiles and the
    // solver runs without error.
    let result = minimize_constrained(obj, &x0, &cons, Method::SLSQP, None)
        .expect("constrained minimisation should not error");
    assert!(result.fun.is_finite());
    assert_eq!(result.constr.expect("constraint values present").len(), 2);
}

// ---------------------------------------------------------------------------
// Issue #127 — optional analytical objective gradient / constraint Jacobians
// ---------------------------------------------------------------------------

/// Supplying an analytical objective gradient via `minimize_constrained_with_jac`
/// reaches the same optimum as the finite-difference path. The analytical
/// gradient must genuinely be used (here it converges in fewer iterations).
#[test]
fn test_issue_127_analytical_objective_gradient() {
    let threshold = 3.0_f64;
    let obj = |x: &[f64]| (x[0] - 1.0).powi(2) + (x[1] - 2.5).powi(2);
    let x0 = array![1.5, 1.5];

    // Finite-difference baseline.
    let cons_fd = vec![Constraint::new(
        move |x: &[f64]| threshold - x[0] - x[1],
        Constraint::INEQUALITY,
    )];
    let fd = minimize_constrained(obj, &x0, &cons_fd, Method::SLSQP, None)
        .expect("FD minimisation should not error");

    // Analytical objective gradient: grad f = [2(x0 - 1), 2(x1 - 2.5)].
    // Wrap the closure so it bumps a shared counter on every invocation. This
    // makes the test tamper-evident: a broken wiring that silently fell back to
    // finite differences would leave the counter at zero (and still match the
    // FD baseline below), so the equivalence asserts alone could not catch it.
    // `Arc<AtomicUsize>` is Clone + Send + Sync + 'static, satisfying the
    // `G: Fn(..) + Clone` bound of `minimize_constrained_with_jac`.
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::Arc;
    let grad_calls = Arc::new(AtomicUsize::new(0));
    let gc = Arc::clone(&grad_calls);
    let obj_grad = move |x: &[f64]| {
        gc.fetch_add(1, Ordering::Relaxed);
        array![2.0 * (x[0] - 1.0), 2.0 * (x[1] - 2.5)]
    };
    let cons_jac = vec![Constraint::new(
        move |x: &[f64]| threshold - x[0] - x[1],
        Constraint::INEQUALITY,
    )];
    let jac =
        minimize_constrained_with_jac(obj, Some(obj_grad), &x0, &cons_jac, Method::SLSQP, None)
            .expect("analytical-gradient minimisation should not error");

    // Tamper-evidence: the user-supplied gradient must actually have been called.
    assert!(
        grad_calls.load(Ordering::Relaxed) > 0,
        "analytical objective gradient was never invoked — the analytical path is broken \
         (a silent finite-difference fallback would still match the FD baseline)"
    );

    assert!(jac.success, "SLSQP (analytical grad) did not converge");
    // Analytical and FD runs converge to the same point.
    assert!(
        (jac.x[0] - fd.x[0]).abs() < 1e-4,
        "x0: analytical {} vs FD {}",
        jac.x[0],
        fd.x[0]
    );
    assert!(
        (jac.x[1] - fd.x[1]).abs() < 1e-4,
        "x1: analytical {} vs FD {}",
        jac.x[1],
        fd.x[1]
    );
    assert!(
        (jac.fun - fd.fun).abs() < 1e-4,
        "objective: analytical {} vs FD {}",
        jac.fun,
        fd.fun
    );
}

/// Attaching an analytical constraint Jacobian via `Constraint::with_jacobian`
/// yields the same solution as the finite-difference path. For the linear
/// constraint `3 - x0 - x1`, the exact Jacobian is `[-1, -1]`.
#[test]
fn test_issue_127_constraint_with_jacobian() {
    let threshold = 3.0_f64;
    let obj = |x: &[f64]| (x[0] - 1.0).powi(2) + (x[1] - 2.5).powi(2);
    let x0 = array![1.5, 1.5];

    // Finite-difference baseline (no analytical constraint Jacobian).
    let cons_fd = vec![Constraint::new(
        move |x: &[f64]| threshold - x[0] - x[1],
        Constraint::INEQUALITY,
    )];
    let fd = minimize_constrained(obj, &x0, &cons_fd, Method::SLSQP, None)
        .expect("FD minimisation should not error");

    // Same constraint, now with an analytical Jacobian attached. The Jacobian
    // closure bumps a shared counter on every call so the test is tamper-evident:
    // if `with_jacobian` wiring were broken and the solver silently used finite
    // differences, the counter would stay at zero (and the run would still match
    // the FD baseline below). `Arc<AtomicUsize>` is Send + Sync + 'static, which
    // satisfies the `J: Fn(..) + Send + Sync + 'static` bound of `with_jacobian`.
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::Arc;
    let jac_calls = Arc::new(AtomicUsize::new(0));
    let jcc = Arc::clone(&jac_calls);
    let cons_jac = vec![Constraint::new(
        move |x: &[f64]| threshold - x[0] - x[1],
        Constraint::INEQUALITY,
    )
    .with_jacobian(move |_x: &[f64]| {
        jcc.fetch_add(1, Ordering::Relaxed);
        array![-1.0, -1.0]
    })];
    let jac = minimize_constrained(obj, &x0, &cons_jac, Method::SLSQP, None)
        .expect("constraint-Jacobian minimisation should not error");

    // Tamper-evidence: the user-supplied constraint Jacobian must actually have
    // been called.
    assert!(
        jac_calls.load(Ordering::Relaxed) > 0,
        "analytical constraint Jacobian was never invoked — `with_jacobian` wiring is broken \
         (a silent finite-difference fallback would still match the FD baseline)"
    );

    assert!(jac.success, "SLSQP (constraint Jacobian) did not converge");
    assert!(
        (jac.x[0] - fd.x[0]).abs() < 1e-4,
        "x0: analytical-jac {} vs FD {}",
        jac.x[0],
        fd.x[0]
    );
    assert!(
        (jac.x[1] - fd.x[1]).abs() < 1e-4,
        "x1: analytical-jac {} vs FD {}",
        jac.x[1],
        fd.x[1]
    );
    assert!(
        (jac.fun - fd.fun).abs() < 1e-4,
        "objective: analytical-jac {} vs FD {}",
        jac.fun,
        fd.fun
    );
}