aprender-core 0.31.2

Next-generation machine learning library in pure Rust
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
use crate::optim::*;
use crate::primitives::Vector;

// Test ProjectedGradientDescent line search with actual backtracking
#[test]
fn test_projected_gd_line_search_backtracking() {
    // Use a large initial step size that will require backtracking
    // Problem: minimize x² starting far from optimum with huge step
    let objective = |x: &Vector<f32>| x[0] * x[0];
    let gradient = |x: &Vector<f32>| Vector::from_slice(&[2.0 * x[0]]);
    let project = |x: &Vector<f32>| x.clone(); // No projection

    // Initial step=10.0 is too large for this problem (gradient = 2*10 = 20, step = 200!)
    // This will cause f(x_new) > f(x) triggering backtracking
    let mut pgd = ProjectedGradientDescent::new(100, 10.0, 1e-6).with_line_search(0.5);
    let x0 = Vector::from_slice(&[10.0]);
    let result = pgd.minimize(objective, gradient, project, x0);

    // Should still converge due to line search
    assert_eq!(result.status, ConvergenceStatus::Converged);
    // Solution should be near 0
    assert!(result.solution[0].abs() < 1e-3);
}

// Test ProjectedGradientDescent line search max backtracking iterations
#[test]
fn test_projected_gd_line_search_max_backtrack() {
    // Create a problem where backtracking won't help much
    // but the solver still needs to try multiple backtracking steps
    use std::cell::Cell;
    use std::rc::Rc;

    let call_count = Rc::new(Cell::new(0));
    let count_clone = Rc::clone(&call_count);

    // Objective that oscillates based on call count
    let objective = move |x: &Vector<f32>| {
        count_clone.set(count_clone.get() + 1);
        // Large value initially, then small
        x[0] * x[0] + if count_clone.get() < 5 { 1000.0 } else { 0.0 }
    };
    let gradient = |x: &Vector<f32>| Vector::from_slice(&[2.0 * x[0]]);
    let project = |x: &Vector<f32>| x.clone();

    // Large step will cause issues
    let mut pgd = ProjectedGradientDescent::new(100, 5.0, 1e-6).with_line_search(0.5);
    let x0 = Vector::from_slice(&[5.0]);
    let result = pgd.minimize(objective, gradient, project, x0);

    // Should eventually converge or hit max iterations
    assert!(result.iterations > 0);
    assert!(call_count.get() > 5); // Objective was called multiple times due to line search
}

// Test ConvergenceStatus variants
#[test]
fn test_convergence_status_all_variants() {
    // Test all variants exist and can be compared
    let statuses = [
        ConvergenceStatus::Converged,
        ConvergenceStatus::MaxIterations,
        ConvergenceStatus::Stalled,
        ConvergenceStatus::NumericalError,
        ConvergenceStatus::Running,
        ConvergenceStatus::UserTerminated,
    ];

    for status in &statuses {
        // Test Clone
        let cloned = *status;
        assert_eq!(*status, cloned);

        // Test Debug
        let debug_str = format!("{:?}", status);
        assert!(!debug_str.is_empty());
    }
}

// Test OptimizationResult fields
#[test]
fn test_optimization_result_fields() {
    let result = OptimizationResult::converged(Vector::from_slice(&[1.0, 2.0]), 10);
    assert_eq!(result.iterations, 10);
    assert_eq!(result.status, ConvergenceStatus::Converged);
    assert_eq!(result.solution.len(), 2);
    assert!((result.objective_value - 0.0).abs() < 1e-6);
    assert!((result.gradient_norm - 0.0).abs() < 1e-6);
    assert!((result.constraint_violation - 0.0).abs() < 1e-6);
    assert_eq!(result.elapsed_time, std::time::Duration::ZERO);

    let result2 = OptimizationResult::max_iterations(Vector::from_slice(&[3.0]));
    assert_eq!(result2.status, ConvergenceStatus::MaxIterations);
    assert_eq!(result2.iterations, 0);
}

// Test nonnegative projection with all negative
#[test]
fn test_nonnegative_all_negative() {
    let x = Vector::from_slice(&[-1.0, -2.0, -3.0, -0.1]);
    let result = prox::nonnegative(&x);

    for i in 0..result.len() {
        assert!(result[i].abs() < 1e-6);
    }
}

// Test nonnegative projection with all positive
#[test]
fn test_nonnegative_all_positive() {
    let x = Vector::from_slice(&[1.0, 2.0, 3.0, 0.1]);
    let result = prox::nonnegative(&x);

    for i in 0..result.len() {
        assert!((result[i] - x[i]).abs() < 1e-6);
    }
}

// Test project_l2_ball with zero vector
#[test]
fn test_project_l2_ball_zero_vector() {
    let x = Vector::from_slice(&[0.0, 0.0, 0.0]);
    let result = prox::project_l2_ball(&x, 1.0);

    // Zero vector should stay zero
    for i in 0..result.len() {
        assert!(result[i].abs() < 1e-6);
    }
}

// ============================================================================
// Projected Gradient Descent Tests (Coverage for projected_gradient.rs)
// ============================================================================

#[test]
fn test_pgd_with_line_search_converges() {
    // Simple quadratic: minimize ½‖x - c‖² subject to x ≥ 0
    let c = Vector::from_slice(&[1.0, -2.0, 3.0, -1.0]);

    let objective = |x: &Vector<f32>| {
        let mut obj = 0.0;
        for i in 0..x.len() {
            let diff = x[i] - c[i];
            obj += 0.5 * diff * diff;
        }
        obj
    };

    let gradient = |x: &Vector<f32>| {
        let mut grad = Vector::zeros(x.len());
        for i in 0..x.len() {
            grad[i] = x[i] - c[i];
        }
        grad
    };

    let project = |x: &Vector<f32>| prox::nonnegative(x);

    // Enable line search
    let mut pgd = ProjectedGradientDescent::new(100, 1.0, 1e-6).with_line_search(0.5);
    let x0 = Vector::zeros(4);
    let result = pgd.minimize(&objective, &gradient, &project, x0);

    assert_eq!(result.status, ConvergenceStatus::Converged);
    // Solution should be max(c, 0) = [1.0, 0.0, 3.0, 0.0]
    assert!((result.solution[0] - 1.0).abs() < 1e-3);
    assert!(result.solution[1].abs() < 1e-3);
    assert!((result.solution[2] - 3.0).abs() < 1e-3);
    assert!(result.solution[3].abs() < 1e-3);
}

#[test]
fn test_pgd_line_search_triggers_backtracking() {
    // Quadratic with large initial step size to trigger backtracking
    let c = Vector::from_slice(&[2.0, 3.0]);

    let objective = |x: &Vector<f32>| {
        let mut obj = 0.0;
        for i in 0..x.len() {
            let diff = x[i] - c[i];
            obj += 0.5 * diff * diff;
        }
        obj
    };

    let gradient = |x: &Vector<f32>| {
        let mut grad = Vector::zeros(x.len());
        for i in 0..x.len() {
            grad[i] = x[i] - c[i];
        }
        grad
    };

    let project = |x: &Vector<f32>| prox::nonnegative(x);

    // Large step size to trigger backtracking line search
    let mut pgd = ProjectedGradientDescent::new(200, 10.0, 1e-6).with_line_search(0.5);
    let x0 = Vector::zeros(2);
    let result = pgd.minimize(&objective, &gradient, &project, x0);

    // Should converge to [2.0, 3.0]
    assert!((result.solution[0] - 2.0).abs() < 1e-2);
    assert!((result.solution[1] - 3.0).abs() < 1e-2);
}

#[test]
fn test_pgd_max_iterations_reached() {
    // Poorly conditioned problem with tiny tolerance that won't converge in 5 iterations
    let c = Vector::from_slice(&[100.0, 100.0, 100.0, 100.0]);

    let objective = |x: &Vector<f32>| {
        let mut obj = 0.0;
        for i in 0..x.len() {
            let diff = x[i] - c[i];
            obj += 0.5 * diff * diff;
        }
        obj
    };

    let gradient = |x: &Vector<f32>| {
        let mut grad = Vector::zeros(x.len());
        for i in 0..x.len() {
            grad[i] = x[i] - c[i];
        }
        grad
    };

    let project = |x: &Vector<f32>| prox::nonnegative(x);

    // Very few iterations with very small step size - won't converge
    let mut pgd = ProjectedGradientDescent::new(5, 0.01, 1e-10);
    let x0 = Vector::zeros(4);
    let result = pgd.minimize(&objective, &gradient, &project, x0);

    // Should reach max iterations
    assert_eq!(result.status, ConvergenceStatus::MaxIterations);
    assert_eq!(result.iterations, 5);
}

#[test]
fn test_pgd_reset() {
    let mut pgd = ProjectedGradientDescent::new(100, 0.1, 1e-6);
    // Reset should not panic
    Optimizer::reset(&mut pgd);
}

#[test]
#[should_panic(expected = "Projected Gradient Descent does not support stochastic updates")]
fn test_pgd_step_not_implemented() {
    let mut pgd = ProjectedGradientDescent::new(100, 0.1, 1e-6);
    let mut params = Vector::zeros(4);
    let grads = Vector::zeros(4);
    pgd.step(&mut params, &grads);
}

#[test]
fn test_pgd_struct_debug() {
    let pgd = ProjectedGradientDescent::new(100, 0.1, 1e-6);
    let debug = format!("{:?}", pgd);
    assert!(debug.contains("ProjectedGradientDescent"));
}

#[test]
fn test_pgd_struct_clone() {
    let pgd = ProjectedGradientDescent::new(100, 0.1, 1e-6);
    let cloned = pgd.clone();
    let debug1 = format!("{:?}", pgd);
    let debug2 = format!("{:?}", cloned);
    assert_eq!(debug1, debug2);
}

#[test]
fn test_pgd_with_line_search_builder() {
    let pgd = ProjectedGradientDescent::new(100, 1.0, 1e-6).with_line_search(0.3);
    let debug = format!("{:?}", pgd);
    assert!(debug.contains("use_line_search: true"));
    assert!(debug.contains("beta: 0.3"));
}

#[test]
fn test_pgd_without_line_search() {
    let c = Vector::from_slice(&[1.0, 2.0]);

    let objective = |x: &Vector<f32>| {
        let mut obj = 0.0;
        for i in 0..x.len() {
            let diff = x[i] - c[i];
            obj += 0.5 * diff * diff;
        }
        obj
    };

    let gradient = |x: &Vector<f32>| {
        let mut grad = Vector::zeros(x.len());
        for i in 0..x.len() {
            grad[i] = x[i] - c[i];
        }
        grad
    };

    let project = |x: &Vector<f32>| x.clone();

    // Without line search (default)
    let mut pgd = ProjectedGradientDescent::new(1000, 0.5, 1e-6);
    let x0 = Vector::zeros(2);
    let result = pgd.minimize(&objective, &gradient, &project, x0);

    assert_eq!(result.status, ConvergenceStatus::Converged);
    assert!((result.solution[0] - 1.0).abs() < 1e-3);
    assert!((result.solution[1] - 2.0).abs() < 1e-3);
}

#[test]
fn test_pgd_line_search_max_backtracking() {
    // Create a scenario where line search might hit max iterations (20)
    // Using an objective where the gradient points in a direction that doesn't decrease
    // the objective when projected

    let objective = |x: &Vector<f32>| {
        // Objective that increases with any step
        let mut obj = 0.0;
        for i in 0..x.len() {
            obj += x[i] * x[i];
        }
        obj
    };

    let gradient = |x: &Vector<f32>| {
        let mut grad = Vector::zeros(x.len());
        for i in 0..x.len() {
            grad[i] = 2.0 * x[i];
        }
        grad
    };

    // Project to L2 ball - ensures projection changes the point
    let project = |x: &Vector<f32>| prox::project_l2_ball(x, 0.5);

    let mut pgd = ProjectedGradientDescent::new(50, 1.0, 1e-6).with_line_search(0.9);
    let x0 = Vector::from_slice(&[0.5, 0.5]);
    let result = pgd.minimize(&objective, &gradient, &project, x0);

    // Result is valid regardless of convergence
    assert!(result.iterations > 0);
}

#[test]
fn test_pgd_gradient_norm_tracking() {
    let c = Vector::from_slice(&[1.0, 1.0]);

    let objective = |x: &Vector<f32>| {
        let mut obj = 0.0;
        for i in 0..x.len() {
            let diff = x[i] - c[i];
            obj += 0.5 * diff * diff;
        }
        obj
    };

    let gradient = |x: &Vector<f32>| {
        let mut grad = Vector::zeros(x.len());
        for i in 0..x.len() {
            grad[i] = x[i] - c[i];
        }
        grad
    };

    let project = |x: &Vector<f32>| x.clone();

    let mut pgd = ProjectedGradientDescent::new(100, 0.5, 1e-6);
    let x0 = Vector::zeros(2);
    let result = pgd.minimize(&objective, &gradient, &project, x0);

    // At convergence, gradient norm should be small
    assert!(result.gradient_norm < 1e-3);
}

#[test]
fn test_pgd_elapsed_time_recorded() {
    let c = Vector::from_slice(&[1.0]);

    let objective = |x: &Vector<f32>| (x[0] - c[0]).powi(2);
    let gradient = |x: &Vector<f32>| {
        let mut grad = Vector::zeros(1);
        grad[0] = 2.0 * (x[0] - c[0]);
        grad
    };
    let project = |x: &Vector<f32>| x.clone();

    let mut pgd = ProjectedGradientDescent::new(100, 0.5, 1e-6);
    let x0 = Vector::zeros(1);
    let result = pgd.minimize(&objective, &gradient, &project, x0);

    // Elapsed time should be non-zero
    assert!(result.elapsed_time.as_nanos() > 0);
}

#[test]
fn test_pgd_constraint_violation_zero() {
    let c = Vector::from_slice(&[1.0]);

    let objective = |x: &Vector<f32>| (x[0] - c[0]).powi(2);
    let gradient = |x: &Vector<f32>| {
        let mut grad = Vector::zeros(1);
        grad[0] = 2.0 * (x[0] - c[0]);
        grad
    };
    let project = |x: &Vector<f32>| x.clone();

    let mut pgd = ProjectedGradientDescent::new(100, 0.5, 1e-6);
    let x0 = Vector::zeros(1);
    let result = pgd.minimize(&objective, &gradient, &project, x0);

    // Constraint violation should be zero (no constraints violated with identity projection)
    assert_eq!(result.constraint_violation, 0.0);
}