optirs-learned 0.3.1

OptiRS learned optimizers and meta-learning
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
use std::fmt::Debug;
// Multi-objective evaluation for neural architecture search
//
// This module provides methods to evaluate architectures across multiple
// conflicting objectives such as accuracy, latency, memory usage, and energy consumption.

#[allow(dead_code)]

use scirs2_core::ndarray::{Array1, Array2};
use scirs2_core::numeric::Float;
use std::collections::{HashMap, HashSet};

use crate::error::{OptimError, Result};

/// Configuration for multi-objective evaluation
#[derive(Debug, Clone)]
pub struct MultiObjectiveConfig<T: Float + Debug + Send + Sync + 'static> {
    /// Objectives to optimize
    pub objectives: Vec<Objective<T>>,
    
    /// Pareto front approximation method
    pub pareto_method: ParetoMethod,
    
    /// Scalarization method for combining objectives
    pub scalarization_method: ScalarizationMethod<T>,
    
    /// Number of reference points for decomposition
    pub num_reference_points: usize,
    
    /// Population size for evolutionary methods
    pub population_size: usize,
    
    /// Normalization method
    pub normalization_method: NormalizationMethod,
    
    /// Constraint handling method
    pub constraint_handling: ConstraintHandling<T>,
}

/// Individual objective definition
#[derive(Debug, Clone)]
pub struct Objective<T: Float + Debug + Send + Sync + 'static> {
    /// Objective identifier
    pub id: String,
    
    /// Objective name
    pub name: String,
    
    /// Optimization direction
    pub direction: ObjectiveDirection,
    
    /// Objective weight
    pub weight: T,
    
    /// Objective priority
    pub priority: usize,
    
    /// Acceptable range
    pub range: Option<(T, T)>,
    
    /// Constraint type
    pub constraint_type: ConstraintType<T>,
    
    /// Evaluation function identifier
    pub evaluator: ObjectiveEvaluator,
}

/// Optimization directions
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ObjectiveDirection {
    /// Maximize objective
    Maximize,
    /// Minimize objective
    Minimize,
}

/// Constraint types for objectives
#[derive(Debug, Clone)]
pub enum ConstraintType<T: Float + Debug + Send + Sync + 'static> {
    /// No constraint
    None,
    /// Hard constraint (must satisfy)
    Hard { threshold: T },
    /// Soft constraint (penalty if violated)
    Soft { threshold: T, penalty: T },
    /// Range constraint
    Range { min: T, max: T },
}

/// Objective evaluators
#[derive(Debug, Clone)]
pub enum ObjectiveEvaluator {
    /// Accuracy/Performance evaluator
    Accuracy,
    /// Latency evaluator
    Latency,
    /// Memory usage evaluator
    Memory,
    /// Energy consumption evaluator
    Energy,
    /// Model size evaluator
    ModelSize,
    /// FLOPS evaluator
    FLOPS,
    /// Custom evaluator
    Custom(String),
}

/// Methods for Pareto front approximation
#[derive(Debug, Clone, Copy)]
pub enum ParetoMethod {
    /// Non-dominated sorting
    NSGA2,
    /// Strength Pareto evolutionary algorithm
    SPEA2,
    /// Multi-objective evolutionary algorithm based on decomposition
    MOEAD,
    /// Indicator-based evolutionary algorithm
    IBEA,
    /// Reference point based method
    NSGA3,
}

/// Scalarization methods
#[derive(Debug, Clone)]
pub enum ScalarizationMethod<T: Float + Debug + Send + Sync + 'static> {
    /// Weighted sum
    WeightedSum,
    /// Weighted product
    WeightedProduct,
    /// Tchebycheff scalarization
    Tchebycheff { reference_point: Array1<T> },
    /// Achievement scalarizing function
    Achievement { reference_point: Array1<T>, weights: Array1<T> },
    /// Penalty boundary intersection
    PBI { reference_point: Array1<T>, penalty_parameter: T },
}

/// Normalization methods
#[derive(Debug, Clone, Copy)]
pub enum NormalizationMethod {
    /// Min-max normalization
    MinMax,
    /// Z-score normalization
    ZScore,
    /// Robust normalization (median and MAD)
    Robust,
    /// No normalization
    None,
}

/// Constraint handling methods
#[derive(Debug, Clone)]
pub enum ConstraintHandling<T: Float + Debug + Send + Sync + 'static> {
    /// Penalty function approach
    Penalty { penalty_factor: T },
    /// Feasibility-based ranking
    FeasibilityBased,
    /// Constraint domination
    ConstraintDomination,
    /// Epsilon-constrained method
    EpsilonConstrained { epsilon: T },
}

/// Multi-objective evaluator
#[derive(Debug)]
pub struct MultiObjectiveEvaluator<T: Float + Debug + Send + Sync + 'static> {
    /// Configuration
    config: MultiObjectiveConfig<T>,
    
    /// Pareto front approximation
    pareto_front: ParetoFront<T>,
    
    /// Archive of non-dominated solutions
    archive: Archive<T>,
    
    /// Objective statistics
    objective_stats: HashMap<String, ObjectiveStats<T>>,
    
    /// Evaluation history
    evaluation_history: Vec<MultiObjectiveEvaluation<T>>,
    
    /// Reference points for decomposition
    reference_points: Array2<T>,
    
    /// Scalarization functions
    scalarization_functions: Vec<ScalarizationFunction<T>>,
}

/// Pareto front representation
#[derive(Debug, Clone)]
pub struct ParetoFront<T: Float + Debug + Send + Sync + 'static> {
    /// Solutions on the Pareto front
    pub solutions: Vec<MultiObjectiveSolution<T>>,
    
    /// Hypervolume indicator
    pub hypervolume: T,
    
    /// Diversity metrics
    pub diversity: DiversityMetrics<T>,
    
    /// Convergence metrics
    pub convergence: ConvergenceMetrics<T>,
}

/// Multi-objective solution
#[derive(Debug, Clone)]
pub struct MultiObjectiveSolution<T: Float + Debug + Send + Sync + 'static> {
    /// Solution identifier
    pub id: String,
    
    /// Architecture representation
    pub architecture: String, // Simplified as string identifier
    
    /// Objective values
    pub objective_values: HashMap<String, T>,
    
    /// Normalized objective values
    pub normalized_values: HashMap<String, T>,
    
    /// Constraint violations
    pub constraint_violations: HashMap<String, T>,
    
    /// Dominance rank
    pub dominance_rank: usize,
    
    /// Crowding distance
    pub crowding_distance: T,
    
    /// Feasibility status
    pub is_feasible: bool,
}

/// Archive of non-dominated solutions
#[derive(Debug)]
pub struct Archive<T: Float + Debug + Send + Sync + 'static> {
    /// Maximum archive size
    pub max_size: usize,
    
    /// Current solutions in archive
    pub solutions: Vec<MultiObjectiveSolution<T>>,
    
    /// Archive management strategy
    pub management_strategy: ArchiveManagementStrategy,
}

/// Archive management strategies
#[derive(Debug, Clone, Copy)]
pub enum ArchiveManagementStrategy {
    /// Maintain fixed size, remove worst solutions
    FixedSize,
    /// Adaptive size based on diversity
    Adaptive,
    /// Crowding-based removal
    CrowdingBased,
}

/// Statistics for individual objectives
#[derive(Debug, Clone)]
pub struct ObjectiveStats<T: Float + Debug + Send + Sync + 'static> {
    /// Minimum observed value
    pub min_value: T,
    
    /// Maximum observed value
    pub max_value: T,
    
    /// Mean value
    pub mean_value: T,
    
    /// Standard deviation
    pub std_deviation: T,
    
    /// Median value
    pub median_value: T,
    
    /// Number of evaluations
    pub num_evaluations: usize,
}

/// Multi-objective evaluation result
#[derive(Debug, Clone)]
pub struct MultiObjectiveEvaluation<T: Float + Debug + Send + Sync + 'static> {
    /// Solution being evaluated
    pub solution: MultiObjectiveSolution<T>,
    
    /// Scalarized fitness value
    pub scalarized_fitness: T,
    
    /// Pareto dominance information
    pub dominance_info: DominanceInfo<T>,
    
    /// Evaluation timestamp
    pub timestamp: usize,
}

/// Dominance relationship information
#[derive(Debug, Clone)]
pub struct DominanceInfo<T: Float + Debug + Send + Sync + 'static> {
    /// Number of solutions this solution dominates
    pub dominates_count: usize,
    
    /// Number of solutions that dominate this solution
    pub dominated_by_count: usize,
    
    /// Solutions dominated by this solution
    pub dominated_solutions: HashSet<String>,
    
    /// Solutions that dominate this solution
    pub dominating_solutions: HashSet<String>,
    
    /// Non-domination rank
    pub non_domination_rank: usize,
}

/// Diversity metrics for Pareto front
#[derive(Debug, Clone)]
pub struct DiversityMetrics<T: Float + Debug + Send + Sync + 'static> {
    /// Spacing metric
    pub spacing: T,
    
    /// Spread metric
    pub spread: T,
    
    /// Maximum spread
    pub max_spread: T,
    
    /// Diversity index
    pub diversity_index: T,
}

/// Convergence metrics
#[derive(Debug, Clone)]
pub struct ConvergenceMetrics<T: Float + Debug + Send + Sync + 'static> {
    /// Generational distance
    pub generational_distance: T,
    
    /// Inverted generational distance
    pub inverted_generational_distance: T,
    
    /// Epsilon indicator
    pub epsilon_indicator: T,
    
    /// R2 indicator
    pub r2_indicator: T,
}

/// Scalarization function
#[derive(Debug, Clone)]
pub struct ScalarizationFunction<T: Float + Debug + Send + Sync + 'static> {
    /// Function identifier
    pub id: String,
    
    /// Scalarization method
    pub method: ScalarizationMethod<T>,
    
    /// Reference direction/point
    pub reference_direction: Array1<T>,
    
    /// Associated weight vector
    pub weight_vector: Array1<T>,
}

impl<T: Float + Debug + Default + Clone + Send + Sync + 'static> MultiObjectiveEvaluator<T> {
    /// Create new multi-objective evaluator
    pub fn new(config: MultiObjectiveConfig<T>) -> Result<Self> {
        let num_objectives = config.objectives.len();
        
        // Initialize reference points
        let reference_points = Self::generate_reference_points(num_objectives, config.num_reference_points)?;
        
        // Create scalarization functions
        let scalarization_functions = Self::create_scalarization_functions(&config, &reference_points)?;
        
        Ok(Self {
            config,
            pareto_front: ParetoFront::new(),
            archive: Archive::new(1000), // Default archive size
            objective_stats: HashMap::new(),
            evaluation_history: Vec::new(),
            reference_points,
            scalarization_functions,
        })
    }
    
    /// Evaluate architecture on multiple objectives
    pub fn evaluate(&mut self, arch_id: &str, architecture_data: &HashMap<String, f64>) -> Result<MultiObjectiveEvaluation<T>> {
        let mut objective_values = HashMap::new();
        let mut constraint_violations = HashMap::new();
        
        // Evaluate each objective
        for objective in &self.config.objectives {
            let value = self.evaluate_single_objective(objective, architecture_data)?;
            objective_values.insert(objective.id.clone(), value);
            
            // Check constraints
            let violation = self.check_constraint(objective, value)?;
            if violation > T::zero() {
                constraint_violations.insert(objective.id.clone(), violation);
            }
            
            // Update statistics
            self.update_objective_stats(&objective.id, value)?;
        }
        
        // Normalize objective values
        let normalized_values = self.normalize_objectives(&objective_values)?;
        
        // Create solution
        let solution = MultiObjectiveSolution {
            id: arch_id.to_string(),
            architecture: arch_id.to_string(),
            objective_values: objective_values.clone(),
            normalized_values,
            constraint_violations: constraint_violations.clone(),
            dominance_rank: 0,
            crowding_distance: T::zero(),
            is_feasible: constraint_violations.is_empty(),
        };
        
        // Compute scalarized fitness
        let scalarized_fitness = self.compute_scalarized_fitness(&solution)?;
        
        // Compute dominance information
        let dominance_info = self.compute_dominance_info(&solution)?;
        
        let evaluation = MultiObjectiveEvaluation {
            solution: solution.clone(),
            scalarized_fitness,
            dominance_info,
            timestamp: self.evaluation_history.len(),
        };
        
        // Update archive and Pareto front
        self.update_archive(solution)?;
        self.update_pareto_front()?;
        
        // Store evaluation
        self.evaluation_history.push(evaluation.clone());
        
        Ok(evaluation)
    }
    
    /// Evaluate single objective
    fn evaluate_single_objective(&self, objective: &Objective<T>, data: &HashMap<String, f64>) -> Result<T> {
        match &objective.evaluator {
            ObjectiveEvaluator::Accuracy => {
                let acc = data.get("accuracy").unwrap_or(&0.0);
                Ok(scirs2_core::numeric::NumCast::from(*acc).unwrap_or_else(|| T::zero()))
            }
            ObjectiveEvaluator::Latency => {
                let latency = data.get("latency_ms").unwrap_or(&100.0);
                Ok(scirs2_core::numeric::NumCast::from(*latency).unwrap_or_else(|| T::zero()))
            }
            ObjectiveEvaluator::Memory => {
                let memory = data.get("memory_mb").unwrap_or(&512.0);
                Ok(scirs2_core::numeric::NumCast::from(*memory).unwrap_or_else(|| T::zero()))
            }
            ObjectiveEvaluator::Energy => {
                let energy = data.get("energy_j").unwrap_or(&10.0);
                Ok(scirs2_core::numeric::NumCast::from(*energy).unwrap_or_else(|| T::zero()))
            }
            ObjectiveEvaluator::ModelSize => {
                let size = data.get("model_size_mb").unwrap_or(&50.0);
                Ok(scirs2_core::numeric::NumCast::from(*size).unwrap_or_else(|| T::zero()))
            }
            ObjectiveEvaluator::FLOPS => {
                let flops = data.get("flops").unwrap_or(&1e9);
                Ok(scirs2_core::numeric::NumCast::from(*flops).unwrap_or_else(|| T::zero()))
            }
            ObjectiveEvaluator::Custom(name) => {
                let value = data.get(name).unwrap_or(&0.0);
                Ok(scirs2_core::numeric::NumCast::from(*value).unwrap_or_else(|| T::zero()))
            }
        }
    }
    
    /// Check constraint violation
    fn check_constraint(&self, objective: &Objective<T>, value: T) -> Result<T> {
        match &objective.constraint_type {
            ConstraintType::None => Ok(T::zero()),
            ConstraintType::Hard { threshold } => {
                match objective.direction {
                    ObjectiveDirection::Maximize => {
                        if value < *threshold {
                            Ok(*threshold - value)
                        } else {
                            Ok(T::zero())
                        }
                    }
                    ObjectiveDirection::Minimize => {
                        if value > *threshold {
                            Ok(value - *threshold)
                        } else {
                            Ok(T::zero())
                        }
                    }
                }
            }
            ConstraintType::Soft { threshold, penalty: _ } => {
                // Similar to hard constraint, but would apply penalty
                self.check_constraint(&Objective {
                    constraint_type: ConstraintType::Hard { threshold: *threshold },
                    ..objective.clone()
                }, value)
            }
            ConstraintType::Range { min, max } => {
                if value < *min {
                    Ok(*min - value)
                } else if value > *max {
                    Ok(value - *max)
                } else {
                    Ok(T::zero())
                }
            }
        }
    }
    
    /// Normalize objective values
    fn normalize_objectives(&self, values: &HashMap<String, T>) -> Result<HashMap<String, T>> {
        let mut normalized = HashMap::new();
        
        match self.config.normalization_method {
            NormalizationMethod::MinMax => {
                for (obj_id, &value) in values {
                    if let Some(stats) = self.objective_stats.get(obj_id) {
                        let range = stats.max_value - stats.min_value;
                        let normalized_value = if range > T::zero() {
                            (value - stats.min_value) / range
                        } else {
                            T::zero()
                        };
                        normalized.insert(obj_id.clone(), normalized_value);
                    } else {
                        normalized.insert(obj_id.clone(), value);
                    }
                }
            }
            NormalizationMethod::ZScore => {
                for (obj_id, &value) in values {
                    if let Some(stats) = self.objective_stats.get(obj_id) {
                        let normalized_value = if stats.std_deviation > T::zero() {
                            (value - stats.mean_value) / stats.std_deviation
                        } else {
                            T::zero()
                        };
                        normalized.insert(obj_id.clone(), normalized_value);
                    } else {
                        normalized.insert(obj_id.clone(), value);
                    }
                }
            }
            _ => {
                // For other methods or None, return original values
                normalized = values.clone();
            }
        }
        
        Ok(normalized)
    }
    
    /// Compute scalarized fitness using configured method
    fn compute_scalarized_fitness(&self, solution: &MultiObjectiveSolution<T>) -> Result<T> {
        match &self.config.scalarization_method {
            ScalarizationMethod::WeightedSum => {
                let mut fitness = T::zero();
                for objective in &self.config.objectives {
                    if let Some(&value) = solution.normalized_values.get(&objective.id) {
                        let contribution = match objective.direction {
                            ObjectiveDirection::Maximize => objective.weight * value,
                            ObjectiveDirection::Minimize => objective.weight * (-value),
                        };
                        fitness = fitness + contribution;
                    }
                }
                Ok(fitness)
            }
            ScalarizationMethod::WeightedProduct => {
                let mut fitness = T::one();
                for objective in &self.config.objectives {
                    if let Some(&value) = solution.normalized_values.get(&objective.id) {
                        let contribution = match objective.direction {
                            ObjectiveDirection::Maximize => value.powf(objective.weight),
                            ObjectiveDirection::Minimize => (T::one() - value).powf(objective.weight),
                        };
                        fitness = fitness * contribution;
                    }
                }
                Ok(fitness)
            }
            ScalarizationMethod::Tchebycheff { reference_point } => {
                let mut max_diff = T::zero();
                for (i, objective) in self.config.objectives.iter().enumerate() {
                    if let Some(&value) = solution.normalized_values.get(&objective.id) {
                        if i < reference_point.len() {
                            let diff = match objective.direction {
                                ObjectiveDirection::Maximize => objective.weight * (reference_point[i] - value).abs(),
                                ObjectiveDirection::Minimize => objective.weight * (value - reference_point[i]).abs(),
                            };
                            max_diff = max_diff.max(diff);
                        }
                    }
                }
                Ok(-max_diff) // Negative because we want to minimize Tchebycheff distance
            }
            _ => {
                // Fallback to weighted sum
                self.compute_scalarized_fitness(&MultiObjectiveSolution {
                    config: MultiObjectiveConfig {
                        scalarization_method: ScalarizationMethod::WeightedSum,
                        ..self.config.clone()
                    },
                    ..solution.clone()
                })
            }
        }
    }
    
    /// Compute dominance information for a solution
    fn compute_dominance_info(&self, solution: &MultiObjectiveSolution<T>) -> Result<DominanceInfo<T>> {
        let mut dominates_count = 0;
        let mut dominated_by_count = 0;
        let mut dominated_solutions = HashSet::new();
        let mut dominating_solutions = HashSet::new();
        
        // Compare with archived solutions
        for archived_solution in &self.archive.solutions {
            if solution.id != archived_solution.id {
                match self.compare_dominance(solution, archived_solution)? {
                    DominanceRelation::Dominates => {
                        dominates_count += 1;
                        dominated_solutions.insert(archived_solution.id.clone());
                    }
                    DominanceRelation::DominatedBy => {
                        dominated_by_count += 1;
                        dominating_solutions.insert(archived_solution.id.clone());
                    }
                    DominanceRelation::NonDominated => {
                        // No action needed
                    }
                }
            }
        }
        
        Ok(DominanceInfo {
            dominates_count,
            dominated_by_count,
            dominated_solutions,
            dominating_solutions,
            non_domination_rank: 0, // Will be computed during NSGA-II sorting
        })
    }
    
    /// Compare dominance between two solutions
    fn compare_dominance(&self, solution1: &MultiObjectiveSolution<T>, solution2: &MultiObjectiveSolution<T>) -> Result<DominanceRelation> {
        let mut solution1_better = false;
        let mut solution2_better = false;
        
        for objective in &self.config.objectives {
            let val1 = solution1.objective_values.get(&objective.id).unwrap_or(&T::zero());
            let val2 = solution2.objective_values.get(&objective.id).unwrap_or(&T::zero());
            
            match objective.direction {
                ObjectiveDirection::Maximize => {
                    if *val1 > *val2 {
                        solution1_better = true;
                    } else if *val2 > *val1 {
                        solution2_better = true;
                    }
                }
                ObjectiveDirection::Minimize => {
                    if *val1 < *val2 {
                        solution1_better = true;
                    } else if *val2 < *val1 {
                        solution2_better = true;
                    }
                }
            }
        }
        
        if solution1_better && !solution2_better {
            Ok(DominanceRelation::Dominates)
        } else if solution2_better && !solution1_better {
            Ok(DominanceRelation::DominatedBy)
        } else {
            Ok(DominanceRelation::NonDominated)
        }
    }
    
    /// Update objective statistics
    fn update_objective_stats(&mut self, obj_id: &str, value: T) -> Result<()> {
        let stats = self.objective_stats.entry(obj_id.to_string()).or_insert_with(|| ObjectiveStats {
            min_value: value,
            max_value: value,
            mean_value: value,
            std_deviation: T::zero(),
            median_value: value,
            num_evaluations: 0,
        });
        
        stats.min_value = stats.min_value.min(value);
        stats.max_value = stats.max_value.max(value);
        stats.num_evaluations += 1;
        
        // Update running mean (simplified)
        let n = scirs2_core::numeric::NumCast::from(stats.num_evaluations as f64).unwrap_or_else(|| T::zero());
        stats.mean_value = (stats.mean_value * (n - T::one()) + value) / n;
        
        Ok(())
    }
    
    /// Update archive with new solution
    fn update_archive(&mut self, solution: MultiObjectiveSolution<T>) -> Result<()> {
        // Add solution to archive
        self.archive.solutions.push(solution);
        
        // Remove dominated solutions
        self.remove_dominated_solutions()?;
        
        // Maintain archive size
        if self.archive.solutions.len() > self.archive.max_size {
            self.reduce_archive_size()?;
        }
        
        Ok(())
    }
    
    /// Remove dominated solutions from archive
    fn remove_dominated_solutions(&mut self) -> Result<()> {
        let mut non_dominated = Vec::new();
        
        for i in 0..self.archive.solutions.len() {
            let mut is_dominated = false;
            for j in 0..self.archive.solutions.len() {
                if i != j {
                    if let Ok(DominanceRelation::DominatedBy) = self.compare_dominance(&self.archive.solutions[i], &self.archive.solutions[j]) {
                        is_dominated = true;
                        break;
                    }
                }
            }
            if !is_dominated {
                non_dominated.push(self.archive.solutions[i].clone());
            }
        }
        
        self.archive.solutions = non_dominated;
        Ok(())
    }
    
    /// Reduce archive size when it exceeds maximum
    fn reduce_archive_size(&mut self) -> Result<()> {
        // Use crowding distance to remove solutions
        self.compute_crowding_distances()?;
        
        // Sort by crowding distance (descending) and keep top solutions
        self.archive.solutions.sort_by(|a, b| b.crowding_distance.partial_cmp(&a.crowding_distance).unwrap_or(std::cmp::Ordering::Equal));
        self.archive.solutions.truncate(self.archive.max_size);
        
        Ok(())
    }
    
    /// Compute crowding distances for archive solutions
    fn compute_crowding_distances(&mut self) -> Result<()> {
        let n = self.archive.solutions.len();
        if n <= 2 {
            return Ok(());
        }
        
        // Initialize crowding distances
        for solution in &mut self.archive.solutions {
            solution.crowding_distance = T::zero();
        }
        
        // For each objective
        for objective in &self.config.objectives {
            // Sort solutions by this objective
            self.archive.solutions.sort_by(|a, b| {
                let val_a = a.objective_values.get(&objective.id).unwrap_or(&T::zero());
                let val_b = b.objective_values.get(&objective.id).unwrap_or(&T::zero());
                val_a.partial_cmp(val_b).unwrap_or(std::cmp::Ordering::Equal)
            });
            
            // Set boundary solutions to infinite distance
            self.archive.solutions[0].crowding_distance = scirs2_core::numeric::NumCast::from(f64::INFINITY).unwrap_or_else(|| T::zero());
            self.archive.solutions[n-1].crowding_distance = scirs2_core::numeric::NumCast::from(f64::INFINITY).unwrap_or_else(|| T::zero());
            
            // Compute distances for interior solutions
            let obj_range = *self.archive.solutions[n-1].objective_values.get(&objective.id).unwrap_or(&T::zero()) -
                          *self.archive.solutions[0].objective_values.get(&objective.id).unwrap_or(&T::zero());
            
            if obj_range > T::zero() {
                for i in 1..n-1 {
                    let prev_val = *self.archive.solutions[i-1].objective_values.get(&objective.id).unwrap_or(&T::zero());
                    let next_val = *self.archive.solutions[i+1].objective_values.get(&objective.id).unwrap_or(&T::zero());
                    let distance = (next_val - prev_val) / obj_range;
                    self.archive.solutions[i].crowding_distance = self.archive.solutions[i].crowding_distance + distance;
                }
            }
        }
        
        Ok(())
    }
    
    /// Update Pareto front
    fn update_pareto_front(&mut self) -> Result<()> {
        // Extract non-dominated solutions for Pareto front
        self.pareto_front.solutions = self.archive.solutions.clone();
        
        // Compute hypervolume
        self.pareto_front.hypervolume = self.compute_hypervolume()?;
        
        // Compute diversity metrics
        self.pareto_front.diversity = self.compute_diversity_metrics()?;
        
        Ok(())
    }
    
    /// Compute hypervolume indicator (simplified)
    fn compute_hypervolume(&self) -> Result<T> {
        // Simplified hypervolume calculation
        // In practice, would use more sophisticated algorithms
        if self.archive.solutions.is_empty() {
            return Ok(T::zero());
        }
        
        let mut volume = T::zero();
        for solution in &self.archive.solutions {
            let mut product = T::one();
            for objective in &self.config.objectives {
                if let Some(&value) = solution.normalized_values.get(&objective.id) {
                    product = product * value.max(T::zero());
                }
            }
            volume = volume + product;
        }
        
        Ok(volume)
    }
    
    /// Compute diversity metrics
    fn compute_diversity_metrics(&self) -> Result<DiversityMetrics<T>> {
        if self.archive.solutions.len() < 2 {
            return Ok(DiversityMetrics {
                spacing: T::zero(),
                spread: T::zero(),
                max_spread: T::zero(),
                diversity_index: T::zero(),
            });
        }
        
        // Simplified diversity computation
        let spacing = scirs2_core::numeric::NumCast::from(0.5).unwrap_or_else(|| T::zero()); // Placeholder
        let spread = scirs2_core::numeric::NumCast::from(0.8).unwrap_or_else(|| T::zero()); // Placeholder
        let max_spread = T::one();
        let diversity_index = scirs2_core::numeric::NumCast::from(0.7).unwrap_or_else(|| T::zero()); // Placeholder
        
        Ok(DiversityMetrics {
            spacing,
            spread,
            max_spread,
            diversity_index,
        })
    }
    
    /// Generate reference points for decomposition methods
    fn generate_reference_points(num_objectives: usize, num_points: usize) -> Result<Array2<T>> {
        let mut points = Array2::zeros((num_points, num_objectives));
        
        // Generate uniform reference points (Das & Dennis method)
        for i in 0..num_points {
            for j in 0..num_objectives {
                let value = T::from((i + j) as f64 / (num_points + num_objectives) as f64).expect("unwrap failed");
                points[[i, j]] = value;
            }
            
            // Normalize to sum to 1
            let sum: T = points.row(i).sum();
            if sum > T::zero() {
                for j in 0..num_objectives {
                    points[[i, j]] = points[[i, j]] / sum;
                }
            }
        }
        
        Ok(points)
    }
    
    /// Create scalarization functions
    fn create_scalarization_functions(config: &MultiObjectiveConfig<T>, reference_points: &Array2<T>) -> Result<Vec<ScalarizationFunction<T>>> {
        let mut functions = Vec::new();
        
        for i in 0..reference_points.nrows() {
            let reference_direction = reference_points.row(i).to_owned();
            let weight_vector = reference_direction.clone();
            
            functions.push(ScalarizationFunction {
                id: format!("scalarization_{}", i),
                method: config.scalarization_method.clone(),
                reference_direction,
                weight_vector,
            });
        }
        
        Ok(functions)
    }
    
    /// Get current Pareto front
    pub fn get_pareto_front(&self) -> &ParetoFront<T> {
        &self.pareto_front
    }
    
    /// Get archive
    pub fn get_archive(&self) -> &Archive<T> {
        &self.archive
    }
    
    /// Get evaluation history
    pub fn get_evaluation_history(&self) -> &[MultiObjectiveEvaluation<T>] {
        &self.evaluation_history
    }
}

/// Dominance relationship between two solutions
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DominanceRelation {
    /// Solution 1 dominates solution 2
    Dominates,
    /// Solution 1 is dominated by solution 2
    DominatedBy,
    /// Solutions are non-dominated with respect to each other
    NonDominated,
}

impl<T: Float + Debug + Default + Clone + Send + Sync + 'static> ParetoFront<T> {
    fn new() -> Self {
        Self {
            solutions: Vec::new(),
            hypervolume: T::zero(),
            diversity: DiversityMetrics {
                spacing: T::zero(),
                spread: T::zero(),
                max_spread: T::zero(),
                diversity_index: T::zero(),
            },
            convergence: ConvergenceMetrics {
                generational_distance: T::zero(),
                inverted_generational_distance: T::zero(),
                epsilon_indicator: T::zero(),
                r2_indicator: T::zero(),
            },
        }
    }
}

impl<T: Float + Debug + Default + Clone + Send + Sync + 'static> Archive<T> {
    fn new(max_size: usize) -> Self {
        Self {
            max_size,
            solutions: Vec::new(),
            management_strategy: ArchiveManagementStrategy::CrowdingBased,
        }
    }
}

impl<T: Float + Debug + Default + Clone + Send + Sync + 'static> Default for MultiObjectiveConfig<T> {
    fn default() -> Self {
        Self {
            objectives: vec![
                Objective {
                    id: "accuracy".to_string(),
                    name: "Validation Accuracy".to_string(),
                    direction: ObjectiveDirection::Maximize,
                    weight: scirs2_core::numeric::NumCast::from(0.4).unwrap_or_else(|| T::zero()),
                    priority: 1,
                    range: Some((T::zero(), T::one())),
                    constraint_type: ConstraintType::None,
                    evaluator: ObjectiveEvaluator::Accuracy,
                },
                Objective {
                    id: "latency".to_string(),
                    name: "Inference Latency".to_string(),
                    direction: ObjectiveDirection::Minimize,
                    weight: scirs2_core::numeric::NumCast::from(0.3).unwrap_or_else(|| T::zero()),
                    priority: 2,
                    range: Some((T::zero(), scirs2_core::numeric::NumCast::from(1000.0).unwrap_or_else(|| T::zero()))),
                    constraint_type: ConstraintType::Hard { threshold: scirs2_core::numeric::NumCast::from(100.0).unwrap_or_else(|| T::zero()) },
                    evaluator: ObjectiveEvaluator::Latency,
                },
                Objective {
                    id: "memory".to_string(),
                    name: "Memory Usage".to_string(),
                    direction: ObjectiveDirection::Minimize,
                    weight: scirs2_core::numeric::NumCast::from(0.3).unwrap_or_else(|| T::zero()),
                    priority: 3,
                    range: Some((T::zero(), scirs2_core::numeric::NumCast::from(2048.0).unwrap_or_else(|| T::zero()))),
                    constraint_type: ConstraintType::Soft { 
                        threshold: scirs2_core::numeric::NumCast::from(1024.0).unwrap_or_else(|| T::zero()),
                        penalty: scirs2_core::numeric::NumCast::from(0.1).unwrap_or_else(|| T::zero())
                    },
                    evaluator: ObjectiveEvaluator::Memory,
                },
            ],
            pareto_method: ParetoMethod::NSGA2,
            scalarization_method: ScalarizationMethod::WeightedSum,
            num_reference_points: 100,
            population_size: 50,
            normalization_method: NormalizationMethod::MinMax,
            constraint_handling: ConstraintHandling::Penalty { penalty_factor: scirs2_core::numeric::NumCast::from(2.0).unwrap_or_else(|| T::zero()) },
        }
    }
}