aprender-core 0.29.3

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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439

    // ==========================================================
    // RED PHASE: Tests written first (Extreme TDD)
    // ==========================================================

    // --- Tabu Search Tests ---

    #[test]
    fn test_tabu_search_permutation() {
        // Minimize sum of |position - value| (optimal: identity permutation)
        let space = SearchSpace::Permutation { size: 5 };

        let objective = |perm: &Vec<usize>| {
            perm.iter()
                .enumerate()
                .map(|(i, &v)| (i as i64 - v as i64).unsigned_abs() as f64)
                .sum()
        };

        let mut ts = TabuSearch::new(5).with_seed(42);
        let result = ts.optimize(&objective, &space, Budget::Iterations(100));

        // Should find identity or close
        assert!(
            result.objective_value <= 4.0,
            "Should find good permutation"
        );
        assert_eq!(result.solution.len(), 5);
    }

    #[test]
    fn test_tabu_search_parameters() {
        let ts = TabuSearch::new(10)
            .with_tenure(15)
            .with_max_neighbors(500)
            .with_seed(123);

        assert_eq!(ts.tenure, 15);
        assert_eq!(ts.max_neighbors, 500);
    }

    #[test]
    fn test_tabu_search_reset() {
        let space = SearchSpace::Permutation { size: 3 };
        let mut ts = TabuSearch::new(3).with_seed(42);

        let _ = ts.optimize(&|_: &Vec<usize>| 1.0, &space, Budget::Iterations(5));
        assert!(!ts.history.is_empty());

        ts.reset();
        assert!(ts.history.is_empty());
        assert!(ts.best_solution.is_empty());
    }

    #[test]
    fn test_tabu_search_tsp() {
        // Small TSP
        let space = SearchSpace::Permutation { size: 4 };

        // Distance matrix (symmetric)
        let dist = vec![
            vec![0.0, 10.0, 15.0, 20.0],
            vec![10.0, 0.0, 35.0, 25.0],
            vec![15.0, 35.0, 0.0, 30.0],
            vec![20.0, 25.0, 30.0, 0.0],
        ];

        let objective = |tour: &Vec<usize>| {
            let mut total = 0.0;
            for i in 0..tour.len() {
                let from = tour[i];
                let to = tour[(i + 1) % tour.len()];
                total += dist[from][to];
            }
            total
        };

        let mut ts = TabuSearch::new(5).with_seed(42);
        let result = ts.optimize(&objective, &space, Budget::Iterations(50));

        assert!(
            result.objective_value < 120.0,
            "Should find reasonable tour"
        );
    }

    #[test]
    fn test_tabu_search_convergence() {
        let space = SearchSpace::Permutation { size: 6 };

        let objective = |perm: &Vec<usize>| {
            // Quadratic assignment-like objective
            perm.iter()
                .enumerate()
                .map(|(i, &v)| ((i as f64) - (v as f64)).powi(2))
                .sum()
        };

        let mut ts = TabuSearch::new(7).with_seed(42);
        let result = ts.optimize(&objective, &space, Budget::Iterations(100));

        // Should show improvement
        let history = &result.history;
        assert!(history.len() > 1);
        assert!(
            history.last().unwrap() <= &history[0],
            "Should improve or stay same"
        );
    }

    // --- ACO Tests ---

    #[test]
    fn test_aco_tsp_4_cities() {
        // Simple TSP: 4 cities in a square
        // Optimal tour: 0->1->2->3->0 or reverse, length = 40
        let distances = vec![
            vec![(1, 10.0), (2, 15.0), (3, 20.0)],
            vec![(0, 10.0), (2, 35.0), (3, 25.0)],
            vec![(0, 15.0), (1, 35.0), (3, 30.0)],
            vec![(0, 20.0), (1, 25.0), (2, 30.0)],
        ];

        let space = SearchSpace::Graph {
            num_nodes: 4,
            adjacency: distances.clone(),
            heuristic: None,
        };

        let objective = |tour: &Vec<usize>| {
            let mut total = 0.0;
            for i in 0..tour.len() {
                let from = tour[i];
                let to = tour[(i + 1) % tour.len()];
                // Find distance
                for &(j, d) in &distances[from] {
                    if j == to {
                        total += d;
                        break;
                    }
                }
            }
            total
        };

        let mut aco = AntColony::new(10).with_seed(42);
        let result = aco.optimize(&objective, &space, Budget::Iterations(50));

        // Should find a reasonable tour (not necessarily optimal with limited budget)
        assert!(result.objective_value < 100.0, "Should find decent tour");
        assert_eq!(result.solution.len(), 4, "Tour should visit all cities");
    }

    #[test]
    fn test_aco_parameters() {
        let aco = AntColony::new(20)
            .with_alpha(2.0)
            .with_beta(3.0)
            .with_rho(0.2)
            .with_seed(123);

        assert_eq!(aco.num_ants, 20);
        assert!((aco.alpha - 2.0).abs() < 1e-10);
        assert!((aco.beta - 3.0).abs() < 1e-10);
        assert!((aco.rho - 0.2).abs() < 1e-10);
    }

    #[test]
    fn test_aco_reset() {
        let distances = vec![vec![(1, 10.0)], vec![(0, 10.0)]];
        let space = SearchSpace::Graph {
            num_nodes: 2,
            adjacency: distances,
            heuristic: None,
        };

        let mut aco = AntColony::new(5).with_seed(42);
        let _ = aco.optimize(&|_: &Vec<usize>| 10.0, &space, Budget::Iterations(5));

        assert!(!aco.history.is_empty());
        aco.reset();
        assert!(aco.history.is_empty());
        assert!(aco.best_tour.is_empty());
    }

    #[test]
    fn test_aco_permutation_space() {
        // ACO can also work with permutation space
        let space = SearchSpace::Permutation { size: 5 };

        let objective = |perm: &Vec<usize>| {
            // Minimize sum of (position * value)
            perm.iter().enumerate().map(|(i, &v)| (i * v) as f64).sum()
        };

        let mut aco = AntColony::new(10).with_seed(42);
        let result = aco.optimize(&objective, &space, Budget::Iterations(20));

        assert_eq!(result.solution.len(), 5);
    }

    #[test]
    fn test_constructive_trait_best() {
        let mut aco = AntColony::new(5);
        assert!(aco.best().is_none(), "No best before optimization");

        let space = SearchSpace::Permutation { size: 3 };
        let _ = aco.optimize(&|_: &Vec<usize>| 1.0, &space, Budget::Iterations(1));

        assert!(aco.best().is_some(), "Should have best after optimization");
    }

    #[test]
    fn test_aco_convergence_improves() {
        // Test that ACO improves over iterations
        let distances = vec![
            vec![(1, 10.0), (2, 20.0), (3, 30.0)],
            vec![(0, 10.0), (2, 15.0), (3, 25.0)],
            vec![(0, 20.0), (1, 15.0), (3, 10.0)],
            vec![(0, 30.0), (1, 25.0), (2, 10.0)],
        ];

        let space = SearchSpace::Graph {
            num_nodes: 4,
            adjacency: distances.clone(),
            heuristic: None,
        };

        let objective = |tour: &Vec<usize>| {
            let mut total = 0.0;
            for i in 0..tour.len() {
                let from = tour[i];
                let to = tour[(i + 1) % tour.len()];
                for &(j, d) in &distances[from] {
                    if j == to {
                        total += d;
                        break;
                    }
                }
            }
            total
        };

        let mut aco = AntColony::new(15).with_seed(42);
        let result = aco.optimize(&objective, &space, Budget::Iterations(30));

        // History should show improvement (non-increasing for minimization)
        let history = result.history;
        assert!(history.len() > 1);

        // Final should be <= initial (or at least close)
        let first = history[0];
        let last = history[history.len() - 1];
        assert!(last <= first + 1e-10, "Should improve or stay same");
    }

    // --- Tabu Search edge-case tests ---

    #[test]
    fn test_tabu_search_graph_space() {
        // Verify TabuSearch handles Graph search spaces correctly.
        let space = SearchSpace::Graph {
            num_nodes: 4,
            adjacency: vec![
                vec![(1, 10.0), (2, 20.0), (3, 30.0)],
                vec![(0, 10.0), (2, 15.0), (3, 25.0)],
                vec![(0, 20.0), (1, 15.0), (3, 10.0)],
                vec![(0, 30.0), (1, 25.0), (2, 10.0)],
            ],
            heuristic: None,
        };

        let objective = |perm: &Vec<usize>| {
            perm.iter()
                .enumerate()
                .map(|(i, &v)| ((i as f64) - (v as f64)).abs())
                .sum()
        };

        let mut ts = TabuSearch::new(5).with_seed(42);
        let result = ts.optimize(&objective, &space, Budget::Iterations(20));
        assert_eq!(result.solution.len(), 4);
        assert!(result.evaluations > 0);
    }

    #[test]
    fn test_tabu_search_best_before_optimize() {
        let ts = TabuSearch::new(5);
        assert!(ts.best().is_none());
        assert!(ts.history().is_empty());
    }

    #[test]
    fn test_tabu_search_large_neighborhood_sampling() {
        // With a large permutation space, the number of swap moves exceeds
        // max_neighbors, triggering the random subset sampling branch.
        let space = SearchSpace::Permutation { size: 50 }; // 50*49/2 = 1225 moves
        let objective = |perm: &Vec<usize>| {
            perm.iter()
                .enumerate()
                .map(|(i, &v)| ((i as f64) - (v as f64)).powi(2))
                .sum()
        };

        // max_neighbors = 100 which is < 1225 total moves
        let mut ts = TabuSearch::new(7).with_max_neighbors(100).with_seed(42);
        let result = ts.optimize(&objective, &space, Budget::Iterations(20));
        assert_eq!(result.solution.len(), 50);
        assert!(result.evaluations > 0);
    }

    #[test]
    fn test_tabu_aspiration_override() {
        // Set up a scenario where a tabu move produces a globally best solution,
        // triggering the aspiration criterion.
        let space = SearchSpace::Permutation { size: 3 };

        // Objective that strongly rewards a specific permutation
        let objective = |perm: &Vec<usize>| {
            if perm == &vec![0, 1, 2] {
                0.0 // Global best
            } else if perm == &vec![0, 2, 1] {
                1.0
            } else {
                10.0
            }
        };

        // Long tenure to ensure moves stay tabu
        let mut ts = TabuSearch::new(100).with_seed(42);
        let result = ts.optimize(&objective, &space, Budget::Iterations(50));
        // Should find the optimal despite tabu constraints via aspiration
        assert!(result.objective_value <= 10.0);
    }

    #[test]
    fn test_tabu_search_convergence_budget() {
        // Test TabuSearch with convergence budget to exercise the convergence
        // termination path.
        let space = SearchSpace::Permutation { size: 4 };
        let objective = |_perm: &Vec<usize>| 5.0; // Flat - no improvement possible

        let mut ts = TabuSearch::new(3).with_seed(42);
        let budget = Budget::Convergence {
            patience: 5,
            min_delta: 1e-6,
            max_evaluations: 100_000,
        };
        let result = ts.optimize(&objective, &space, budget);
        assert_eq!(result.termination, TerminationReason::Converged);
    }

    #[test]
    fn test_tabu_is_tabu_expired() {
        // Verify is_tabu returns false when the move has expired.
        let mv = SwapMove { i: 0, j: 1 };
        let tabu_list = vec![(mv, 5)]; // Expires at iteration 5
        assert!(TabuSearch::is_tabu(&mv, &tabu_list, 4)); // Still active
        assert!(!TabuSearch::is_tabu(&mv, &tabu_list, 5)); // Expired
        assert!(!TabuSearch::is_tabu(&mv, &tabu_list, 6)); // Expired
    }

    #[test]
    fn test_tabu_is_tabu_different_move() {
        // Verify a different move is not tabu.
        let mv1 = SwapMove { i: 0, j: 1 };
        let mv2 = SwapMove { i: 2, j: 3 };
        let tabu_list = vec![(mv1, 100)];
        assert!(!TabuSearch::is_tabu(&mv2, &tabu_list, 0));
    }

    #[test]
    fn test_tabu_generate_swap_moves() {
        let moves = TabuSearch::generate_swap_moves(4);
        // C(4,2) = 6 swap pairs
        assert_eq!(moves.len(), 6);
        assert!(moves.contains(&SwapMove { i: 0, j: 1 }));
        assert!(moves.contains(&SwapMove { i: 0, j: 3 }));
        assert!(moves.contains(&SwapMove { i: 2, j: 3 }));
    }

    #[test]
    fn test_tabu_apply_swap() {
        let sol = vec![0, 1, 2, 3];
        let mv = SwapMove { i: 1, j: 3 };
        let new_sol = TabuSearch::apply_swap(&sol, &mv);
        assert_eq!(new_sol, vec![0, 3, 2, 1]);
    }

    // --- ACO edge-case tests ---

    #[test]
    fn test_aco_zero_tour_length_deposit_skipped() {
        // When objective returns 0.0 for a tour, the deposit should be skipped
        // (the length <= 0.0 branch in update_pheromones).
        let space = SearchSpace::Permutation { size: 3 };
        let objective = |_tour: &Vec<usize>| 0.0; // Always zero

        let mut aco = AntColony::new(5).with_seed(42);
        let result = aco.optimize(&objective, &space, Budget::Iterations(5));
        // Should still complete without errors
        assert_eq!(result.solution.len(), 3);
        assert!((result.objective_value - 0.0).abs() < 1e-10);
    }

    #[test]
    fn test_aco_with_provided_heuristic() {
        // Provide an explicit heuristic matrix to exercise the Some(h) branch.
        let heuristic = vec![
            vec![0.0, 0.1, 0.05],
            vec![0.1, 0.0, 0.02],
            vec![0.05, 0.02, 0.0],
        ];
        let space = SearchSpace::Graph {
            num_nodes: 3,
            adjacency: vec![
                vec![(1, 10.0), (2, 20.0)],
                vec![(0, 10.0), (2, 50.0)],
                vec![(0, 20.0), (1, 50.0)],
            ],
            heuristic: Some(heuristic),
        };

        let objective = |tour: &Vec<usize>| {
            let dists = [[0.0, 10.0, 20.0], [10.0, 0.0, 50.0], [20.0, 50.0, 0.0]];
            let mut total = 0.0;
            for i in 0..tour.len() {
                let from = tour[i];
                let to = tour[(i + 1) % tour.len()];
                total += dists[from][to];
            }
            total
        };

        let mut aco = AntColony::new(5).with_seed(42);
        let result = aco.optimize(&objective, &space, Budget::Iterations(10));
        assert_eq!(result.solution.len(), 3);
        assert!(result.objective_value.is_finite());
    }