aprender-core 0.30.0

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
440
441
442
443
444
445
use super::*;

#[test]
fn test_active_learning_with_de_search() {
    let space: SearchSpace<RF> = SearchSpace::new()
        .add_continuous(RF::NEstimators, 10.0, 500.0)
        .add_continuous(RF::MaxDepth, 2.0, 20.0);

    // Active learning wrapping DE
    let base = DESearch::new(100).with_seed(42);
    let mut search = ActiveLearningSearch::new(base).with_uncertainty_threshold(0.1);

    let trials = search.suggest(&space, 20);
    assert_eq!(trials.len(), 20);

    // All values should be within bounds
    for trial in &trials {
        let n_est = trial
            .get_f64(&RF::NEstimators)
            .expect("NEstimators should exist");
        assert!((10.0..=500.0).contains(&n_est));
    }
}

// =========================================================================
// Additional coverage tests
// =========================================================================

#[test]
fn test_log_scale_struct() {
    let ls = LogScale {
        low: 1e-4,
        high: 1.0,
    };
    assert_eq!(ls.low, 1e-4);
    assert_eq!(ls.high, 1.0);

    // Test Clone/Copy via Debug
    let _cloned = ls;
    let debug_str = format!("{:?}", ls);
    assert!(debug_str.contains("LogScale"));
}

#[test]
fn test_generic_param_display() {
    let param = GenericParam("learning_rate");
    let display = format!("{}", param);
    assert_eq!(display, "learning_rate");
}

#[test]
fn test_search_space_iter() {
    let space: SearchSpace<RF> = SearchSpace::new()
        .add(RF::NEstimators, 10..500)
        .add(RF::MaxDepth, 2..20);

    let count = space.iter().count();
    assert_eq!(count, 2);
}

#[test]
fn test_search_space_is_empty() {
    let empty: SearchSpace<RF> = SearchSpace::new();
    assert!(empty.is_empty());

    let non_empty = empty.add(RF::NEstimators, 10..100);
    assert!(!non_empty.is_empty());
}

#[test]
fn test_search_space_add_param() {
    let param = HyperParam::continuous(0.0, 1.0);
    let space: SearchSpace<RF> = SearchSpace::new().add_param(RF::NEstimators, param);

    assert!(space.get(&RF::NEstimators).is_some());
}

#[test]
fn test_trial_get_usize() {
    let mut values = HashMap::new();
    values.insert(RF::NEstimators, ParamValue::Int(100));

    let trial = Trial { values };
    assert_eq!(trial.get_usize(&RF::NEstimators), Some(100));
}

#[test]
fn test_param_value_as_str_none() {
    let pv = ParamValue::Int(42);
    assert!(pv.as_str().is_none());

    let pv2 = ParamValue::Float(1.0);
    assert!(pv2.as_str().is_none());
}

#[test]
fn test_param_value_as_bool_none() {
    let pv = ParamValue::Int(42);
    assert!(pv.as_bool().is_none());
}

#[test]
fn test_param_value_display() {
    assert_eq!(format!("{}", ParamValue::Float(1.5)), "1.500000");
    assert_eq!(format!("{}", ParamValue::Int(42)), "42");
    assert_eq!(format!("{}", ParamValue::Bool(true)), "true");
    assert_eq!(
        format!("{}", ParamValue::String("test".to_string())),
        "test"
    );
}

#[test]
fn test_param_value_from_usize() {
    let pv = ParamValue::from(42_usize);
    assert_eq!(pv.as_i64(), Some(42));
}

#[test]
fn test_param_value_from_string() {
    let pv = ParamValue::from("hello".to_string());
    assert_eq!(pv.as_str(), Some("hello"));
}

#[test]
fn test_hyperparam_clone() {
    let hp = HyperParam::continuous(0.0, 1.0);
    let cloned = hp.clone();
    if let HyperParam::Continuous { low, high, .. } = cloned {
        assert_eq!(low, 0.0);
        assert_eq!(high, 1.0);
    } else {
        panic!("Should be continuous");
    }
}

#[test]
fn test_grid_points_single_point() {
    let param = HyperParam::continuous(5.0, 10.0);
    let points = param.grid_points(1);

    assert_eq!(points.len(), 1);
    assert_eq!(points[0].as_f64(), Some(5.0));
}

#[test]
fn test_grid_points_log_scale() {
    let param = HyperParam::continuous_log(1.0, 100.0);
    let points = param.grid_points(3);

    assert_eq!(points.len(), 3);
    // First and last should be at bounds
    assert!((points[0].as_f64().unwrap() - 1.0).abs() < 0.01);
    assert!((points[2].as_f64().unwrap() - 100.0).abs() < 0.01);
}

#[test]
fn test_grid_empty_space() {
    let space: SearchSpace<RF> = SearchSpace::new();
    let grid = space.grid(5);

    assert_eq!(grid.len(), 1);
    assert!(grid[0].values.is_empty());
}

#[test]
fn test_de_search_with_jade() {
    let search = DESearch::new(50).with_jade();
    assert!(search.use_jade);
}

#[test]
fn test_de_search_with_mutation_factor() {
    let search = DESearch::new(50).with_mutation_factor(0.5);
    assert_eq!(search.mutation_factor, 0.5);
}

#[test]
fn test_de_search_with_crossover_rate() {
    let search = DESearch::new(50).with_crossover_rate(0.7);
    assert_eq!(search.crossover_rate, 0.7);
}

#[test]
fn test_de_search_with_population_size() {
    let search = DESearch::new(50).with_population_size(100);
    assert_eq!(search.population_size, 100);
}

#[test]
fn test_de_search_rand2bin_strategy() {
    use crate::metaheuristics::DEStrategy;

    let space: SearchSpace<RF> = SearchSpace::new()
        .add_continuous(RF::NEstimators, 10.0, 500.0)
        .add_continuous(RF::MaxDepth, 2.0, 20.0);

    let mut search = DESearch::new(100)
        .with_strategy(DEStrategy::Rand2Bin)
        .with_seed(42);

    let trials = search.suggest(&space, 20);
    assert_eq!(trials.len(), 20);

    // Simulate results to trigger update with Rand2Bin mutation
    let results: Vec<TrialResult<RF>> = trials
        .iter()
        .enumerate()
        .map(|(i, t)| TrialResult {
            trial: t.clone(),
            score: i as f64,
            metrics: HashMap::new(),
        })
        .collect();

    search.update(&results);

    // Get next batch after evolution
    let trials2 = search.suggest(&space, 20);
    assert!(trials2.len() <= 20);
}

#[test]
fn test_de_search_with_categorical() {
    let space: SearchSpace<RF> =
        SearchSpace::new().add_categorical(RF::MaxFeatures, ["sqrt", "log2", "auto"]);

    let mut search = DESearch::new(50).with_seed(42);
    let trials = search.suggest(&space, 10);

    for trial in &trials {
        let val = trial.get(&RF::MaxFeatures).expect("should have value");
        let s = val.as_str().expect("should be string");
        assert!(["sqrt", "log2", "auto"].contains(&s));
    }
}

#[test]
fn test_active_learning_sample_count() {
    let space: SearchSpace<RF> = SearchSpace::new().add_continuous(RF::NEstimators, 10.0, 500.0);

    let base = RandomSearch::new(100).with_seed(42);
    let mut search = ActiveLearningSearch::new(base);

    assert_eq!(search.sample_count(), 0);

    let trials = search.suggest(&space, 5);
    let results: Vec<TrialResult<RF>> = trials
        .iter()
        .map(|t| TrialResult {
            trial: t.clone(),
            score: 0.5,
            metrics: HashMap::new(),
        })
        .collect();

    search.update(&results);
    assert_eq!(search.sample_count(), 5);
}

#[test]
fn test_active_learning_returns_empty_when_should_stop() {
    let space: SearchSpace<RF> = SearchSpace::new().add_continuous(RF::NEstimators, 10.0, 500.0);

    let base = RandomSearch::new(1000).with_seed(42);
    let mut search = ActiveLearningSearch::new(base)
            .with_uncertainty_threshold(1.0) // Very high threshold - easy to satisfy
            .with_min_samples(5);

    // Get enough samples
    let trials = search.suggest(&space, 10);
    let results: Vec<TrialResult<RF>> = trials
        .iter()
        .map(|t| TrialResult {
            trial: t.clone(),
            score: 0.5, // Same score = zero variance
            metrics: HashMap::new(),
        })
        .collect();

    search.update(&results);

    // Now should_stop() returns true, so suggest() should return empty
    assert!(search.should_stop());
    let empty_trials = search.suggest(&space, 10);
    assert!(empty_trials.is_empty());
}

#[test]
fn test_active_learning_near_zero_mean() {
    let space: SearchSpace<RF> = SearchSpace::new().add_continuous(RF::NEstimators, 10.0, 500.0);

    let base = RandomSearch::new(100).with_seed(42);
    let mut search = ActiveLearningSearch::new(base);

    let trials = search.suggest(&space, 5);

    // Scores near zero to trigger the mean.abs() < 1e-10 branch
    let results: Vec<TrialResult<RF>> = trials
        .iter()
        .enumerate()
        .map(|(i, t)| TrialResult {
            trial: t.clone(),
            score: (i as f64) * 1e-12, // Very small values
            metrics: HashMap::new(),
        })
        .collect();

    search.update(&results);

    // Should handle near-zero mean without dividing by zero
    let uncertainty = search.uncertainty();
    assert!(uncertainty.is_finite());
}

#[test]
fn test_active_learning_single_sample() {
    let space: SearchSpace<RF> = SearchSpace::new().add_continuous(RF::NEstimators, 10.0, 500.0);

    let base = RandomSearch::new(100).with_seed(42);
    let mut search = ActiveLearningSearch::new(base);

    let trials = search.suggest(&space, 1);
    let results: Vec<TrialResult<RF>> = trials
        .iter()
        .map(|t| TrialResult {
            trial: t.clone(),
            score: 0.5,
            metrics: HashMap::new(),
        })
        .collect();

    search.update(&results);

    // With only 1 sample, uncertainty should be infinite
    assert!(search.uncertainty().is_infinite());
}

#[test]
fn test_xorshift_edge_cases() {
    // Seed of 0 should be treated as 1
    let mut rng = XorShift64::new(0);
    let v = rng.gen_f64();
    assert!((0.0..1.0).contains(&v));

    // gen_i64_range with low >= high should return low
    let mut rng2 = XorShift64::new(42);
    assert_eq!(rng2.gen_i64_range(10, 10), 10);
    assert_eq!(rng2.gen_i64_range(10, 5), 10);

    // gen_usize with len 0 should return 0
    assert_eq!(rng2.gen_usize(0), 0);
}

#[test]
fn test_trial_result_clone() {
    let mut values = HashMap::new();
    values.insert(RF::NEstimators, ParamValue::Int(100));
    let trial = Trial { values };

    let result = TrialResult {
        trial,
        score: 0.95,
        metrics: HashMap::new(),
    };

    let cloned = result.clone();
    assert_eq!(cloned.score, 0.95);
}

#[test]
fn test_de_update_empty_results() {
    let space: SearchSpace<RF> = SearchSpace::new().add_continuous(RF::NEstimators, 10.0, 500.0);

    let mut search = DESearch::new(50).with_seed(42);
    let _trials = search.suggest(&space, 10);

    // Update with empty results - should not panic
    let empty: Vec<TrialResult<RF>> = vec![];
    search.update(&empty);
}

#[test]
fn test_grid_search_position_tracking() {
    let space: SearchSpace<RF> = SearchSpace::new().add_categorical(RF::Bootstrap, [true, false]);

    let mut search = GridSearch::new(5);

    // First batch
    let t1 = search.suggest(&space, 1);
    assert_eq!(t1.len(), 1);

    // Second batch from same position
    let t2 = search.suggest(&space, 1);
    assert_eq!(t2.len(), 1);

    // Total grid is 2, so we've exhausted it
    let t3 = search.suggest(&space, 10);
    assert!(t3.is_empty());
}

#[test]
fn test_search_space_default() {
    let space: SearchSpace<RF> = SearchSpace::default();
    assert!(space.is_empty());
}

#[test]
fn test_random_search_clone() {
    let search = RandomSearch::new(50).with_seed(42);
    let cloned = search.clone();
    assert_eq!(cloned.n_iter, 50);
    assert_eq!(cloned.seed, 42);
}

// =========================================================================
// Coverage gap tests (targeting 32 missed lines)
// =========================================================================

#[test]
fn test_param_value_as_f64_from_int() {
    // Covers ParamValue::as_f64 for Int variant (line 160)
    let pv = ParamValue::Int(42);
    assert_eq!(pv.as_f64(), Some(42.0));

    let pv_neg = ParamValue::Int(-7);
    assert_eq!(pv_neg.as_f64(), Some(-7.0));
}

#[test]
fn test_param_value_as_f64_returns_none_for_non_numeric() {
    // Covers the None branch of as_f64 for Bool and String
    assert_eq!(ParamValue::Bool(true).as_f64(), None);
    assert_eq!(ParamValue::String("x".to_string()).as_f64(), None);
}

#[test]
fn test_param_value_as_i64_from_float() {
    // Covers ParamValue::as_i64 for Float variant (line 170)
    let pv = ParamValue::Float(3.7);
    assert_eq!(pv.as_i64(), Some(3)); // truncation

    let pv_neg = ParamValue::Float(-2.9);
    assert_eq!(pv_neg.as_i64(), Some(-2));
}