sklears-multioutput 0.1.1

Multi-output regression and classification
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
//! Comprehensive Test Suite for Multi-Output Optimization
//!
//! This module contains integration tests for all optimization algorithms in the multi-output
//! learning framework, including joint loss optimization, multi-objective optimization,
//! scalarization methods, and NSGA-II evolutionary algorithms.

use super::joint_loss_optimization::{
    JointLossConfig, JointLossOptimizer, LossCombination, LossFunction,
};
use super::multi_objective_optimization::{
    MultiObjectiveConfig, MultiObjectiveOptimizer, ParetoSolution,
};
use super::nsga2_algorithms::{NSGA2Algorithm, NSGA2Config, NSGA2Optimizer};
use sklears_core::traits::{Estimator, Fit, Predict};

use approx::assert_abs_diff_eq;
// Use SciRS2-Core for arrays and random number generation (SciRS2 Policy)
use scirs2_core::ndarray::array;

#[test]
fn test_joint_loss_optimizer_basic() {
    let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];
    let y = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];

    let config = JointLossConfig {
        output_losses: vec![LossFunction::MSE, LossFunction::MSE],
        combination: LossCombination::Sum,
        max_iter: 100,
        ..Default::default()
    };

    let optimizer = JointLossOptimizer::new().config(config);
    let trained = optimizer
        .fit(&X.view(), &y.view())
        .expect("model fitting should succeed");

    let predictions = trained
        .predict(&X.view())
        .expect("prediction should succeed");
    assert_eq!(predictions.shape(), &[3, 2]);
}

#[test]
fn test_joint_loss_optimizer_weighted_sum() {
    let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];
    let y = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];

    let config = JointLossConfig {
        output_losses: vec![LossFunction::MSE, LossFunction::MAE],
        combination: LossCombination::WeightedSum(vec![0.7, 0.3]),
        max_iter: 100,
        ..Default::default()
    };

    let optimizer = JointLossOptimizer::new().config(config);
    let trained = optimizer
        .fit(&X.view(), &y.view())
        .expect("model fitting should succeed");

    assert_eq!(trained.n_outputs(), 2);
    assert_eq!(trained.n_features(), 2);
    assert!(!trained.loss_history().is_empty());
}

#[test]
fn test_joint_loss_optimizer_huber_loss() {
    let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];
    let y = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];

    let config = JointLossConfig {
        output_losses: vec![LossFunction::Huber(1.0), LossFunction::Huber(1.0)],
        combination: LossCombination::GeometricMean,
        max_iter: 50,
        ..Default::default()
    };

    let optimizer = JointLossOptimizer::new().config(config);
    let trained = optimizer
        .fit(&X.view(), &y.view())
        .expect("model fitting should succeed");

    let predictions = trained
        .predict(&X.view())
        .expect("prediction should succeed");
    assert_eq!(predictions.shape(), &[3, 2]);
}

#[test]
fn test_multi_objective_optimizer_basic() {
    let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];
    let y = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];

    let config = MultiObjectiveConfig {
        population_size: 20,
        generations: 10,
        objectives: vec!["accuracy".to_string(), "complexity".to_string()],
        random_state: Some(42),
        ..Default::default()
    };

    let optimizer = MultiObjectiveOptimizer::new().config(config);
    let trained = optimizer
        .fit(&X.view(), &y.view())
        .expect("model fitting should succeed");

    assert!(!trained.pareto_solutions().is_empty());
    assert!(!trained.convergence_history().is_empty());
}

#[test]
fn test_multi_objective_optimizer_multiple_objectives() {
    let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];
    let y = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];

    let config = MultiObjectiveConfig {
        population_size: 15,
        generations: 5,
        objectives: vec![
            "mse".to_string(),
            "mae".to_string(),
            "complexity".to_string(),
        ],
        random_state: Some(123),
        ..Default::default()
    };

    let optimizer = MultiObjectiveOptimizer::new().config(config);
    let trained = optimizer
        .fit(&X.view(), &y.view())
        .expect("model fitting should succeed");

    let predictions = trained
        .predict(&X.view())
        .expect("prediction should succeed");
    assert_eq!(predictions.shape(), &[3, 2]);
}

#[test]
fn test_joint_loss_optimizer_adaptive_combination() {
    let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];
    let y = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];

    let config = JointLossConfig {
        output_losses: vec![LossFunction::MSE, LossFunction::MAE],
        combination: LossCombination::Adaptive,
        max_iter: 200,
        learning_rate: 0.01,
        ..Default::default()
    };

    let optimizer = JointLossOptimizer::new().config(config);
    let trained = optimizer
        .fit(&X.view(), &y.view())
        .expect("model fitting should succeed");

    // Check that optimizer ran and produced loss history
    let loss_history = trained.loss_history();
    assert!(!loss_history.is_empty(), "loss history should be non-empty");
    // With adaptive combination, convergence is not strictly guaranteed,
    // so we only assert that losses are finite and non-negative
    for &loss in loss_history {
        assert!(loss.is_finite(), "loss values should be finite");
        assert!(loss >= 0.0, "loss values should be non-negative");
    }
}

#[test]
fn test_joint_loss_optimizer_invalid_input() {
    let X = array![[1.0, 2.0], [2.0, 3.0]];
    let y = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]]; // Different number of samples

    let optimizer = JointLossOptimizer::new();
    let result = optimizer.fit(&X.view(), &y.view());
    assert!(result.is_err());
}

#[test]
fn test_multi_objective_optimizer_invalid_objectives() {
    let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];
    let y = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];

    let config = MultiObjectiveConfig {
        population_size: 10,
        generations: 5,
        objectives: vec!["invalid_objective".to_string()],
        ..Default::default()
    };

    let optimizer = MultiObjectiveOptimizer::new().config(config);
    let result = optimizer.fit(&X.view(), &y.view());
    assert!(result.is_err());
}

#[test]
fn test_joint_loss_optimizer_cross_entropy() {
    let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];
    let y = array![[0.0, 1.0], [1.0, 0.0], [1.0, 1.0]]; // Binary labels

    let config = JointLossConfig {
        output_losses: vec![LossFunction::CrossEntropy, LossFunction::CrossEntropy],
        combination: LossCombination::Sum,
        max_iter: 100,
        learning_rate: 0.01,
        ..Default::default()
    };

    let optimizer = JointLossOptimizer::new().config(config);
    let trained = optimizer
        .fit(&X.view(), &y.view())
        .expect("model fitting should succeed");

    let predictions = trained
        .predict(&X.view())
        .expect("prediction should succeed");
    assert_eq!(predictions.shape(), &[3, 2]);
}

#[test]
fn test_joint_loss_optimizer_max_combination() {
    let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];
    let y = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];

    let config = JointLossConfig {
        output_losses: vec![LossFunction::MSE, LossFunction::MAE],
        combination: LossCombination::Max,
        max_iter: 50,
        ..Default::default()
    };

    let optimizer = JointLossOptimizer::new().config(config);
    let trained = optimizer
        .fit(&X.view(), &y.view())
        .expect("model fitting should succeed");

    assert_eq!(trained.weights().shape(), &[2, 2]);
    assert_eq!(trained.bias().len(), 2);
}

#[test]
fn test_nsga2_optimizer_basic() {
    let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]];
    let y = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]];

    let config = NSGA2Config {
        population_size: 20,
        generations: 10,
        crossover_prob: 0.8,
        mutation_prob: 0.1,
        random_state: Some(42),
        ..Default::default()
    };

    let optimizer = NSGA2Optimizer::new().config(config);
    let trained = optimizer
        .fit(&X.view(), &y.view())
        .expect("model fitting should succeed");

    assert!(!trained.pareto_solutions().is_empty());
    assert!(!trained.convergence_history().is_empty());
    assert_eq!(trained.convergence_history().len(), 10);

    let predictions = trained
        .predict(&X.view())
        .expect("prediction should succeed");
    assert_eq!(predictions.shape(), &[4, 2]);
}

#[test]
fn test_nsga2_optimizer_sbx_algorithm() {
    let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];
    let y = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];

    let config = NSGA2Config {
        population_size: 15,
        generations: 5,
        algorithm: NSGA2Algorithm::SBX,
        eta_c: 15.0,
        eta_m: 15.0,
        random_state: Some(123),
        ..Default::default()
    };

    let optimizer = NSGA2Optimizer::new()
        .config(config)
        .population_size(15)
        .generations(5)
        .crossover_prob(0.9)
        .mutation_prob(0.2)
        .algorithm(NSGA2Algorithm::SBX);

    let trained = optimizer
        .fit(&X.view(), &y.view())
        .expect("model fitting should succeed");

    assert!(!trained.pareto_solutions().is_empty());
    assert_eq!(trained.final_population().len(), 15);

    // Check that best solution has reasonable parameters
    let best = trained.best_solution();
    assert_eq!(best.parameters.len(), 6); // 2 features * 2 outputs + 2 bias
    assert!(best.objectives.len() == 2);
}

#[test]
fn test_nsga2_optimizer_convergence() {
    let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]];
    let y = array![[2.0, 4.0], [4.0, 6.0], [6.0, 8.0], [8.0, 10.0]]; // Linear relationship

    let config = NSGA2Config {
        population_size: 30,
        generations: 20,
        crossover_prob: 0.9,
        mutation_prob: 0.1,
        random_state: Some(42),
        ..Default::default()
    };

    let optimizer = NSGA2Optimizer::new().config(config);
    let trained = optimizer
        .fit(&X.view(), &y.view())
        .expect("model fitting should succeed");

    // Check convergence - later generations should have better or similar hypervolume
    let history = trained.convergence_history();
    assert!(!history.is_empty());

    // Check that we have Pareto solutions
    assert!(!trained.pareto_solutions().is_empty());

    // Check that solutions have valid objectives
    for solution in trained.pareto_solutions() {
        assert!(solution.objectives[0] >= 0.0); // MSE should be non-negative
        assert!(solution.objectives[1] >= 0.0); // Complexity should be non-negative
    }
}

#[test]
fn test_nsga2_optimizer_builder_pattern() {
    let config = NSGA2Optimizer::new()
        .population_size(50)
        .generations(25)
        .crossover_prob(0.85)
        .mutation_prob(0.15)
        .algorithm(NSGA2Algorithm::Standard);

    // Access config through Estimator trait method
    let cfg = Estimator::config(&config);
    assert_eq!(cfg.population_size, 50);
    assert_eq!(cfg.generations, 25);
    assert_abs_diff_eq!(cfg.crossover_prob, 0.85, epsilon = 1e-6);
    assert_abs_diff_eq!(cfg.mutation_prob, 0.15, epsilon = 1e-6);
    assert_eq!(cfg.algorithm, NSGA2Algorithm::Standard);
}

#[test]
fn test_nsga2_optimizer_invalid_input() {
    let X = array![[1.0, 2.0], [2.0, 3.0]];
    let y = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]]; // Different number of samples

    let optimizer = NSGA2Optimizer::new();
    let result = optimizer.fit(&X.view(), &y.view());
    assert!(result.is_err());
}

#[test]
fn test_nsga2_dominance_relationships() {
    let solution1 = ParetoSolution {
        parameters: array![1.0, 2.0, 3.0],
        objectives: array![1.0, 2.0], // Better in both objectives
        rank: 0,
        crowding_distance: 0.0,
    };

    let solution2 = ParetoSolution {
        parameters: array![2.0, 3.0, 4.0],
        objectives: array![2.0, 3.0], // Worse in both objectives
        rank: 0,
        crowding_distance: 0.0,
    };

    let optimizer = NSGA2Optimizer::new();
    assert!(optimizer.nsga2_dominates(&solution1, &solution2));
    assert!(!optimizer.nsga2_dominates(&solution2, &solution1));

    // Test non-dominating solutions
    let solution3 = ParetoSolution {
        parameters: array![1.5, 2.5, 3.5],
        objectives: array![0.5, 3.5], // Better in first, worse in second
        rank: 0,
        crowding_distance: 0.0,
    };

    assert!(!optimizer.nsga2_dominates(&solution1, &solution3));
    assert!(!optimizer.nsga2_dominates(&solution3, &solution1));
}

#[test]
fn test_nsga2_optimizer_reproducibility() {
    let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];
    let y = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];

    let config1 = NSGA2Config {
        population_size: 20,
        generations: 5,
        random_state: Some(42),
        ..Default::default()
    };

    let config2 = NSGA2Config {
        population_size: 20,
        generations: 5,
        random_state: Some(42),
        ..Default::default()
    };

    let optimizer1 = NSGA2Optimizer::new().config(config1);
    let optimizer2 = NSGA2Optimizer::new().config(config2);

    let trained1 = optimizer1
        .fit(&X.view(), &y.view())
        .expect("model fitting should succeed");
    let trained2 = optimizer2
        .fit(&X.view(), &y.view())
        .expect("model fitting should succeed");

    // Results should be identical with same random state
    assert_eq!(
        trained1.pareto_solutions().len(),
        trained2.pareto_solutions().len()
    );
    assert_eq!(
        trained1.convergence_history().len(),
        trained2.convergence_history().len()
    );

    // Check that best solutions are the same
    let best1 = trained1.best_solution();
    let best2 = trained2.best_solution();

    for i in 0..best1.parameters.len() {
        assert_abs_diff_eq!(best1.parameters[i], best2.parameters[i], epsilon = 1e-10);
    }
}