scirs2-optimize 0.4.2

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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
//! Integration tests for scirs2-optimize
//!
//! These tests validate the key functionality of the optimization library
//! across different algorithm categories.

use scirs2_core::ndarray::{Array1, ArrayView1};
use scirs2_core::random::{Rng, RngExt};
use scirs2_optimize::{
    stochastic::{
        minimize_adam, minimize_sgd, AdamOptions, DataProvider, InMemoryDataProvider, SGDOptions,
        StochasticGradientFunction,
    },
    unconstrained::{minimize_bfgs, Options as UnconstrainedOptions},
};

/// Simple quadratic function for testing
struct QuadraticFunction;

impl StochasticGradientFunction for QuadraticFunction {
    fn compute_gradient(&mut self, x: &ArrayView1<f64>, _batchdata: &[f64]) -> Array1<f64> {
        x.mapv(|xi| 2.0 * xi)
    }

    fn compute_value(&mut self, x: &ArrayView1<f64>, _batchdata: &[f64]) -> f64 {
        x.mapv(|xi| xi * xi).sum()
    }
}

#[test]
#[allow(dead_code)]
fn test_stochastic_optimization_integration() {
    // Test SGD
    let grad_func = QuadraticFunction;
    let x0 = Array1::from_vec(vec![1.0, -1.0]);
    let data_provider = Box::new(InMemoryDataProvider::new(vec![1.0; 50]));

    let options = SGDOptions {
        learning_rate: 0.1,
        max_iter: 100,
        tol: 1e-4,
        ..Default::default()
    };

    let result = minimize_sgd(grad_func, x0.clone(), data_provider, options);
    assert!(result.is_ok());
    let result = result.expect("Operation failed");

    // Should converge toward zero
    assert!(result.fun < 1e-2);
    println!(
        "SGD converged to f = {:.2e} in {} iterations",
        result.fun, result.nit
    );
}

#[test]
#[allow(dead_code)]
fn test_adam_optimization_integration() {
    // Test Adam optimizer
    let grad_func = QuadraticFunction;
    let x0 = Array1::from_vec(vec![2.0, -1.5]);
    let data_provider = Box::new(InMemoryDataProvider::new(vec![1.0; 100]));

    let options = AdamOptions {
        learning_rate: 0.1,
        max_iter: 200,
        tol: 1e-6,
        ..Default::default()
    };

    let result = minimize_adam(grad_func, x0, data_provider, options);
    assert!(result.is_ok());
    let result = result.expect("Operation failed");

    // Adam should converge efficiently
    assert!(result.fun < 1e-3);
    println!(
        "Adam converged to f = {:.2e} in {} iterations",
        result.fun, result.nit
    );
}

#[test]
#[allow(dead_code)]
fn test_bfgs_optimization_integration() {
    // Test BFGS on a simple function
    let func = |x: &ArrayView1<f64>| -> f64 { x[0] * x[0] + x[1] * x[1] };

    let x0 = Array1::from_vec(vec![3.0, -2.0]);
    let options = UnconstrainedOptions::default();

    let result = minimize_bfgs(
        func,
        None::<fn(&ArrayView1<f64>) -> Array1<f64>>,
        x0,
        &options,
    );
    assert!(result.is_ok());
    let result = result.expect("Operation failed");

    // BFGS should converge quickly for quadratic functions
    assert!(result.success);
    assert!(result.fun < 1e-6);
    println!(
        "BFGS converged to f = {:.2e} in {} iterations",
        result.fun, result.nit
    );
}

#[test]
#[allow(dead_code)]
fn test_optimization_library_capabilities() {
    println!("\n🔬 scirs2-optimize Library Capabilities Test");
    println!("============================================");

    // Test that we can create different optimizers
    let _sgd_options = SGDOptions::default();
    let _adam_options = AdamOptions::default();
    let _bfgs_options = UnconstrainedOptions::default();

    println!("✅ All optimizer option structs created successfully");

    // Test data provider
    let data = vec![1.0, 2.0, 3.0, 4.0, 5.0];
    let provider = InMemoryDataProvider::new(data.clone());
    assert_eq!(provider.num_samples(), 5);
    assert_eq!(provider.get_full_data(), data);

    println!("✅ Data provider functionality verified");

    // Test gradient function trait
    let mut grad_func = QuadraticFunction;
    let x = Array1::from_vec(vec![1.0, 2.0]);
    let batch_data = vec![1.0];

    let gradient = grad_func.compute_gradient(&x.view(), &batch_data);
    let expected = Array1::from_vec(vec![2.0, 4.0]);
    assert_eq!(gradient, expected);

    let value = grad_func.compute_value(&x.view(), &batch_data);
    assert_eq!(value, 5.0); // 1^2 + 2^2 = 5

    println!("✅ Stochastic gradient function trait verified");
    println!("✅ All core library capabilities working correctly!");
}

#[test]
#[allow(dead_code)]
fn test_comprehensive_optimization_workflows() {
    println!("\n🔧 Comprehensive Optimization Workflows Test");
    println!("===========================================");

    // Test 1: Basic unconstrained optimization workflow
    println!("Testing unconstrained optimization workflow...");
    test_unconstrained_workflow();

    // Test 2: Stochastic optimization workflow
    println!("Testing stochastic optimization workflow...");
    test_stochastic_workflow();

    // Test 3: Different problem types
    println!("Testing various problem types...");
    test_problem_types();

    println!("✅ All optimization workflows working correctly!");
}

#[allow(dead_code)]
fn test_unconstrained_workflow() {
    use scirs2_optimize::unconstrained::{minimize_lbfgs, minimize_powell};

    // Rosenbrock function: f(x,y) = (1-x)² + 100(y-x²)²
    let rosenbrock = |x: &ArrayView1<f64>| -> f64 {
        let x1 = x[0];
        let x2 = x[1];
        (1.0 - x1).powi(2) + 100.0 * (x2 - x1 * x1).powi(2)
    };

    let x0 = Array1::from_vec(vec![-1.2, 1.0]);
    let options = UnconstrainedOptions {
        max_iter: 1000,
        gtol: 1e-6,
        ftol: 1e-12,
        ..Default::default()
    };

    // Test L-BFGS
    let result_lbfgs = minimize_lbfgs(
        rosenbrock,
        None::<fn(&ArrayView1<f64>) -> Array1<f64>>,
        x0.clone(),
        &options,
    )
    .expect("Operation failed");
    assert!(result_lbfgs.success, "L-BFGS failed to converge");
    assert!(
        result_lbfgs.fun < 1e-1,
        "L-BFGS didn't reach target accuracy (got {:.2e})",
        result_lbfgs.fun
    );

    // Test Powell's method (derivative-free)
    let result_powell =
        minimize_powell(rosenbrock, x0.clone(), &options).expect("Operation failed");
    assert!(
        result_powell.fun < 1e-3,
        "Powell's method didn't reach reasonable accuracy"
    );

    println!("  ✅ Unconstrained optimization algorithms working");
}

#[allow(dead_code)]
fn test_stochastic_workflow() {
    use scirs2_optimize::stochastic::{
        minimize_adamw, minimize_rmsprop, AdamWOptions, RMSPropOptions,
    };

    // Test with a noisy quadratic function (simulating ML scenario)
    struct NoisyQuadratic;
    impl StochasticGradientFunction for NoisyQuadratic {
        fn compute_gradient(&mut self, x: &ArrayView1<f64>, _batchdata: &[f64]) -> Array1<f64> {
            // Add small noise to simulate stochastic gradients
            let mut rng = scirs2_core::random::rng();
            x.mapv(|xi| 2.0 * xi + rng.random_range(-0.01..0.01))
        }

        fn compute_value(&mut self, x: &ArrayView1<f64>, _batchdata: &[f64]) -> f64 {
            x.mapv(|xi| xi * xi).sum()
        }
    }

    let x0 = Array1::from_vec(vec![2.0, -1.5, 0.5]);
    let data_provider = Box::new(InMemoryDataProvider::new(vec![1.0; 200]));

    // Test RMSProp
    let rmsprop_options = RMSPropOptions {
        learning_rate: 0.01,
        max_iter: 500,
        tol: 1e-4,
        ..Default::default()
    };
    let result_rmsprop =
        minimize_rmsprop(NoisyQuadratic, x0.clone(), data_provider, rmsprop_options)
            .expect("Operation failed");
    assert!(
        result_rmsprop.fun < 1e-2,
        "RMSProp didn't converge adequately"
    );

    // Test AdamW
    let data_provider2 = Box::new(InMemoryDataProvider::new(vec![1.0; 200]));
    let adamw_options = AdamWOptions {
        learning_rate: 0.01,
        max_iter: 500,
        weight_decay: 0.001,
        tol: 1e-4,
        ..Default::default()
    };
    let result_adamw = minimize_adamw(NoisyQuadratic, x0, data_provider2, adamw_options)
        .expect("Operation failed");
    assert!(result_adamw.fun < 1e-2, "AdamW didn't converge adequately");

    println!("  ✅ Stochastic optimization algorithms working");
}

#[allow(dead_code)]
fn test_problem_types() {
    // Test different problem characteristics

    // 1. Ill-conditioned quadratic
    let ill_conditioned = |x: &ArrayView1<f64>| -> f64 {
        1000.0 * x[0] * x[0] + x[1] * x[1] // Condition number = 1000
    };

    let x0 = Array1::from_vec(vec![1.0, 1.0]);
    let options = UnconstrainedOptions {
        max_iter: 500,
        gtol: 1e-6,
        ..Default::default()
    };

    let result = minimize_bfgs(
        ill_conditioned,
        None::<fn(&ArrayView1<f64>) -> Array1<f64>>,
        x0.clone(),
        &options,
    )
    .expect("Operation failed");
    assert!(
        result.fun < 1e-3,
        "Failed on ill-conditioned problem (got {:.2e})",
        result.fun
    );

    // 2. High-dimensional problem
    let high_dim_size = 50;
    let high_dim_quad = |x: &ArrayView1<f64>| -> f64 { x.mapv(|xi| xi * xi).sum() };

    let x0_high_dim = Array1::ones(high_dim_size);
    let result_high_dim = minimize_bfgs(
        high_dim_quad,
        None::<fn(&ArrayView1<f64>) -> Array1<f64>>,
        x0_high_dim,
        &options,
    )
    .expect("Operation failed");
    assert!(
        result_high_dim.success,
        "Failed on high-dimensional problem"
    );
    assert!(
        result_high_dim.fun < 1e-6,
        "High-dimensional problem didn't reach target accuracy"
    );

    println!("  ✅ Various problem types handled correctly");
}

#[test]
#[allow(dead_code)]
fn test_algorithm_robustness() {
    println!("\n🛡️  Testing Algorithm Robustness");
    println!("===============================");

    // Test algorithms on challenging problems
    test_challenging_problems();
    test_edge_cases();

    println!("✅ All robustness tests passed!");
}

#[allow(dead_code)]
fn test_challenging_problems() {
    use scirs2_optimize::unconstrained::minimize_nelder_mead;

    // Himmelblau's function (multiple global minima)
    let himmelblau = |x: &ArrayView1<f64>| -> f64 {
        let x1 = x[0];
        let x2 = x[1];
        (x1 * x1 + x2 - 11.0).powi(2) + (x1 + x2 * x2 - 7.0).powi(2)
    };

    let x0 = Array1::from_vec(vec![0.0, 0.0]);
    let options = UnconstrainedOptions {
        max_iter: 1000,
        ftol: 1e-6,
        ..Default::default()
    };

    // Test with derivative-free method
    let result = minimize_nelder_mead(himmelblau, x0, &options).expect("Operation failed");
    assert!(
        result.fun < 1e-3,
        "Failed to find good minimum for Himmelblau's function"
    );

    println!("  ✅ Challenging multi-modal problems handled");
}

#[allow(dead_code)]
fn test_edge_cases() {
    // Test with very small problems
    let simple_1d = |x: &ArrayView1<f64>| -> f64 { x[0] * x[0] };
    let x0_1d = Array1::from_vec(vec![5.0]);
    let options = UnconstrainedOptions {
        max_iter: 100,
        gtol: 1e-8,
        ..Default::default()
    };

    let result_1d = minimize_bfgs(
        simple_1d,
        None::<fn(&ArrayView1<f64>) -> Array1<f64>>,
        x0_1d,
        &options,
    )
    .expect("Operation failed");
    assert!(result_1d.success, "Failed on 1D problem");
    assert!(
        (result_1d.x[0]).abs() < 1e-6,
        "1D minimum not found accurately"
    );

    // Test with starting point at optimum
    let x0_optimal = Array1::from_vec(vec![0.0, 0.0]);
    let quad_2d = |x: &ArrayView1<f64>| -> f64 { x[0] * x[0] + x[1] * x[1] };
    let result_optimal = minimize_bfgs(
        quad_2d,
        None::<fn(&ArrayView1<f64>) -> Array1<f64>>,
        x0_optimal,
        &UnconstrainedOptions::default(),
    )
    .expect("Operation failed");
    assert!(result_optimal.success, "Failed when starting at optimum");

    println!("  ✅ Edge cases handled correctly");
}

#[test]
#[allow(dead_code)]
fn test_stochastic_integration_comprehensive() {
    println!("\n📊 Comprehensive Stochastic Integration Test");
    println!("==========================================");

    // Test complete stochastic optimization pipeline
    test_machine_learning_workflow();
    test_large_scale_optimization();

    println!("✅ Comprehensive stochastic integration completed!");
}

#[allow(dead_code)]
fn test_machine_learning_workflow() {
    use scirs2_optimize::stochastic::{minimize_sgd_momentum, MomentumOptions};

    // Simulate a simple logistic regression scenario
    struct LogisticRegression {
        features: Vec<Vec<f64>>,
        labels: Vec<f64>,
    }

    impl LogisticRegression {
        fn new() -> Self {
            // Simple 2D dataset with linear separability
            let features = vec![
                vec![1.0, 2.0],
                vec![2.0, 3.0],
                vec![3.0, 4.0], // Class 1
                vec![-1.0, -2.0],
                vec![-2.0, -3.0],
                vec![-3.0, -4.0], // Class 0
            ];
            let labels = vec![1.0, 1.0, 1.0, 0.0, 0.0, 0.0];
            Self { features, labels }
        }
    }

    impl StochasticGradientFunction for LogisticRegression {
        fn compute_gradient(
            &mut self,
            params: &ArrayView1<f64>,
            batch_indices: &[f64],
        ) -> Array1<f64> {
            let mut grad = Array1::zeros(params.len());

            for &idx in batch_indices {
                let i = idx as usize % self.features.len();
                let x = &self.features[i];
                let y = self.labels[i];

                let z = params[0] * x[0] + params[1] * x[1] + params[2]; // w1*x1 + w2*x2 + bias
                let pred = 1.0 / (1.0 + (-z).exp());
                let error = pred - y;

                grad[0] += error * x[0];
                grad[1] += error * x[1];
                grad[2] += error; // bias gradient
            }

            grad / batch_indices.len() as f64
        }

        fn compute_value(&mut self, params: &ArrayView1<f64>, batchindices: &[f64]) -> f64 {
            let mut loss = 0.0;

            for &idx in batchindices {
                let i = idx as usize % self.features.len();
                let x = &self.features[i];
                let y = self.labels[i];

                let z = params[0] * x[0] + params[1] * x[1] + params[2];
                let pred = 1.0 / (1.0 + (-z).exp());

                // Cross-entropy loss
                loss += -y * pred.ln() - (1.0 - y) * (1.0 - pred).ln();
            }

            loss / batchindices.len() as f64
        }
    }

    let logreg = LogisticRegression::new();
    let x0 = Array1::zeros(3); // [w1, w2, bias]
    let data_provider = Box::new(InMemoryDataProvider::new(
        (0..6).map(|i| i as f64).collect(),
    ));

    let options = MomentumOptions {
        learning_rate: 0.1,
        momentum: 0.9,
        max_iter: 200,
        tol: 1e-4,
        batch_size: Some(3),
        ..Default::default()
    };

    let result =
        minimize_sgd_momentum(logreg, x0, data_provider, options).expect("Operation failed");
    assert!(
        result.fun < 1.0,
        "Logistic regression didn't converge to reasonable loss"
    );

    println!("  ✅ Machine learning workflow tested successfully");
}

#[allow(dead_code)]
fn test_large_scale_optimization() {
    // Test with larger problem size to verify scalability
    struct LargeQuadratic {
        #[allow(dead_code)]
        dimension: usize,
    }

    impl LargeQuadratic {
        fn new(dim: usize) -> Self {
            Self { dimension: dim }
        }
    }

    impl StochasticGradientFunction for LargeQuadratic {
        fn compute_gradient(&mut self, x: &ArrayView1<f64>, _batchdata: &[f64]) -> Array1<f64> {
            // Scaled quadratic: gradient = 2x with different scales per dimension
            x.iter()
                .enumerate()
                .map(|(i, &xi)| {
                    let scale = 1.0 + i as f64 * 0.1; // Increasing scale
                    2.0 * scale * xi
                })
                .collect()
        }

        fn compute_value(&mut self, x: &ArrayView1<f64>, _batchdata: &[f64]) -> f64 {
            x.iter()
                .enumerate()
                .map(|(i, &xi)| {
                    let scale = 1.0 + i as f64 * 0.1;
                    scale * xi * xi
                })
                .sum()
        }
    }

    let large_dim = 100;
    let large_quad = LargeQuadratic::new(large_dim);
    let x0_large = Array1::ones(large_dim);
    let data_provider = Box::new(InMemoryDataProvider::new(vec![1.0; 500]));

    let adam_options = AdamOptions {
        learning_rate: 0.01,
        max_iter: 300,
        tol: 1e-4,
        batch_size: Some(50),
        ..Default::default()
    };

    let result_large =
        minimize_adam(large_quad, x0_large, data_provider, adam_options).expect("Operation failed");
    assert!(result_large.fun < 1e-2, "Large-scale optimization failed");

    println!(
        "  ✅ Large-scale optimization (dim={}) successful",
        large_dim
    );
}