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
use std::fmt::Debug;
// Evolutionary search strategies for neural architecture search
//
// This module implements evolutionary algorithms for NAS, including genetic algorithms,
// evolution strategies, and other population-based optimization methods.

use std::collections::HashMap;
use scirs2_core::numeric::Float;

use super::super::architecture::{ArchitectureSpec, ArchitectureCandidate};
use super::strategies::{MutationOperator, CrossoverOperator, SelectionMethod};

/// Evolutionary search state
#[derive(Debug)]
pub struct EvolutionarySearchState<T: Float + Debug + Send + Sync + 'static> {
    /// Current population
    pub population: Vec<ArchitectureCandidate>,

    /// Generation number
    pub generation: usize,

    /// Phantom data for type parameter
    _phantom: std::marker::PhantomData<T>,

    /// Fitness history
    pub fitness_history: Vec<Vec<f64>>,

    /// Selection pressure
    pub selection_pressure: f64,

    /// Diversity metrics
    pub diversity_metrics: DiversityMetrics,

    /// Evolutionary operators
    pub mutation_operators: Vec<Box<dyn MutationOperator>>,
    pub crossover_operators: Vec<Box<dyn CrossoverOperator>>,
    pub selection_methods: Vec<Box<dyn SelectionMethod<T>>>,

    /// Evolutionary parameters
    pub parameters: EvolutionaryParameters,
}

/// Population diversity metrics
#[derive(Debug, Clone, Default)]
pub struct DiversityMetrics {
    /// Structural diversity
    pub structural_diversity: f64,

    /// Performance diversity
    pub performance_diversity: f64,

    /// Genotypic diversity
    pub genotypic_diversity: f64,

    /// Phenotypic diversity
    pub phenotypic_diversity: f64,
}

/// Evolutionary algorithm parameters
#[derive(Debug, Clone)]
pub struct EvolutionaryParameters {
    /// Population size
    pub population_size: usize,

    /// Elite size (number of best individuals to keep)
    pub elite_size: usize,

    /// Mutation rate
    pub mutation_rate: f64,

    /// Crossover rate
    pub crossover_rate: f64,

    /// Selection pressure
    pub selection_pressure: f64,

    /// Maximum generations
    pub max_generations: usize,

    /// Diversity maintenance
    pub maintain_diversity: bool,

    /// Minimum diversity threshold
    pub min_diversity_threshold: f64,

    /// Fitness sharing enabled
    pub fitness_sharing: bool,

    /// Speciation enabled
    pub speciation: bool,

    /// Multi-objective optimization
    pub multi_objective: bool,
}

impl Default for EvolutionaryParameters {
    fn default() -> Self {
        Self {
            population_size: 50,
            elite_size: 5,
            mutation_rate: 0.1,
            crossover_rate: 0.8,
            selection_pressure: 2.0,
            max_generations: 100,
            maintain_diversity: true,
            min_diversity_threshold: 0.1,
            fitness_sharing: false,
            speciation: false,
            multi_objective: false,
        }
    }
}

impl<T: Float + Debug + Default + std::fmt::Debug + Send + Sync> EvolutionarySearchState<T> {
    /// Create new evolutionary search state
    pub fn new(population_size: usize) -> Self {
        Self {
            population: Vec::with_capacity(population_size),
            generation: 0,
            _phantom: std::marker::PhantomData,
            fitness_history: Vec::new(),
            selection_pressure: 2.0,
            diversity_metrics: DiversityMetrics::default(),
            mutation_operators: Vec::new(),
            crossover_operators: Vec::new(),
            selection_methods: Vec::new(),
            parameters: EvolutionaryParameters::default(),
        }
    }

    /// Initialize population with random architectures
    pub fn initialize_population(&mut self, search_space: &super::super::space::ArchitectureSearchSpace) {
        self.population.clear();
        
        for i in 0..self.parameters.population_size {
            let arch_spec = self.generate_random_architecture(search_space, i);
            let candidate = ArchitectureCandidate::new(
                format!("gen0_ind{}", i),
                arch_spec,
            );
            self.population.push(candidate);
        }
    }

    /// Generate random architecture from search space
    fn generate_random_architecture(
        &self,
        _search_space: &super::super::space::ArchitectureSearchSpace,
        seed: usize,
    ) -> ArchitectureSpec {
        // Simplified random architecture generation
        use super::super::architecture::{LayerSpec, LayerDimensions, LayerType, ActivationType, GlobalArchitectureConfig};
        
        let num_layers = 1 + (seed % 5); // 1-5 layers
        let mut layers = Vec::new();

        for i in 0..num_layers {
            let layer_type = match i % 3 {
                0 => LayerType::Linear,
                1 => LayerType::LSTM,
                _ => LayerType::Attention,
            };

            let dimensions = LayerDimensions {
                input_dim: if i == 0 { 128 } else { 64 },
                output_dim: 64,
                hidden_dims: vec![],
            };

            let activation = match seed % 3 {
                0 => ActivationType::ReLU,
                1 => ActivationType::GELU,
                _ => ActivationType::Tanh,
            };

            layers.push(LayerSpec::new(layer_type, dimensions, activation));
        }

        ArchitectureSpec::new(layers, GlobalArchitectureConfig::default())
    }

    /// Evolve population for one generation
    pub fn evolve_generation(&mut self) -> Result<(), super::SearchError> {
        // Calculate fitness for all individuals
        let fitnesses = self.calculate_population_fitness();

        // Update diversity metrics
        self.update_diversity_metrics();

        // Record fitness history
        self.fitness_history.push(fitnesses.clone());

        // Create new population
        let mut new_population = Vec::new();

        // Elite selection - keep best individuals
        let elite_indices = self.select_elite(&fitnesses);
        for &idx in &elite_indices {
            new_population.push(self.population[idx].clone());
        }

        // Generate offspring to fill rest of population
        while new_population.len() < self.parameters.population_size {
            if scirs2_core::random::random::<f64>() < self.parameters.crossover_rate {
                // Crossover
                let offspring = self.generate_crossover_offspring(&fitnesses)?;
                new_population.extend(offspring);
            } else {
                // Mutation only
                let offspring = self.generate_mutation_offspring(&fitnesses)?;
                new_population.push(offspring);
            }
        }

        // Truncate if we generated too many offspring
        new_population.truncate(self.parameters.population_size);

        // Replace population
        self.population = new_population;
        self.generation += 1;

        Ok(())
    }

    /// Calculate fitness for all individuals in population
    fn calculate_population_fitness(&self) -> Vec<f64> {
        self.population
            .iter()
            .map(|individual| individual.performance.optimization_performance)
            .collect()
    }

    /// Select elite individuals
    fn select_elite(&self, fitnesses: &[f64]) -> Vec<usize> {
        let mut indexed_fitnesses: Vec<(usize, f64)> = fitnesses
            .iter()
            .enumerate()
            .map(|(i, &f)| (i, f))
            .collect();

        indexed_fitnesses.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        indexed_fitnesses
            .into_iter()
            .take(self.parameters.elite_size)
            .map(|(idx, _)| idx)
            .collect()
    }

    /// Generate offspring through crossover
    fn generate_crossover_offspring(&self, fitnesses: &[f64]) -> Result<Vec<ArchitectureCandidate>, super::SearchError> {
        // Select parents
        let parent1_idx = self.select_parent(fitnesses)?;
        let parent2_idx = self.select_parent(fitnesses)?;

        if parent1_idx == parent2_idx {
            // If same parent selected, just mutate
            return Ok(vec![self.generate_mutation_offspring(fitnesses)?]);
        }

        let parent1 = &self.population[parent1_idx].architecture;
        let parent2 = &self.population[parent2_idx].architecture;

        // Apply crossover (use first crossover operator if available)
        if let Some(crossover_op) = self.crossover_operators.first() {
            let (child1_arch, child2_arch) = crossover_op.crossover(parent1, parent2)?;

            let mut offspring = Vec::new();

            // Create candidates from offspring architectures
            let child1 = ArchitectureCandidate::new(
                format!("gen{}_cross1", self.generation + 1),
                child1_arch,
            );
            offspring.push(child1);

            let child2 = ArchitectureCandidate::new(
                format!("gen{}_cross2", self.generation + 1),
                child2_arch,
            );
            offspring.push(child2);

            // Apply mutation to offspring if enabled
            for child in &mut offspring {
                if scirs2_core::random::random::<f64>() < self.parameters.mutation_rate {
                    self.apply_mutations(&mut child.architecture)?;
                }
            }

            Ok(offspring)
        } else {
            // No crossover operator available, fall back to mutation
            Ok(vec![self.generate_mutation_offspring(fitnesses)?])
        }
    }

    /// Generate offspring through mutation
    fn generate_mutation_offspring(&self, fitnesses: &[f64]) -> Result<ArchitectureCandidate, super::SearchError> {
        let parent_idx = self.select_parent(fitnesses)?;
        let mut child_arch = self.population[parent_idx].architecture.clone();

        // Apply mutations
        self.apply_mutations(&mut child_arch)?;

        let child = ArchitectureCandidate::new(
            format!("gen{}_mut", self.generation + 1),
            child_arch,
        );

        Ok(child)
    }

    /// Select parent for reproduction
    fn select_parent(&self, fitnesses: &[f64]) -> Result<usize, super::SearchError> {
        if let Some(selection_method) = self.selection_methods.first() {
            let selected = selection_method.select(&self.population, &fitnesses.iter().map(|&f| scirs2_core::numeric::NumCast::from(f).unwrap_or_else(|| T::zero())).collect::<Vec<_>>(), 1);
            selected.first().copied().ok_or_else(|| {
                super::SearchError::GenerationFailed("No parent selected".to_string())
            })
        } else {
            // Default to random selection
            Ok(scirs2_core::random::random::<usize>() % self.population.len())
        }
    }

    /// Apply mutations to architecture
    fn apply_mutations(&self, architecture: &mut ArchitectureSpec) -> Result<(), super::SearchError> {
        for mutation_op in &self.mutation_operators {
            if scirs2_core::random::random::<f64>() < self.parameters.mutation_rate {
                mutation_op.mutate(architecture, self.parameters.mutation_rate);
            }
        }
        Ok(())
    }

    /// Update diversity metrics
    fn update_diversity_metrics(&mut self) {
        self.diversity_metrics = calculate_diversity_metrics(&self.population);
    }

    /// Check if evolution should terminate
    pub fn should_terminate(&self) -> bool {
        // Terminate if max generations reached
        if self.generation >= self.parameters.max_generations {
            return true;
        }

        // Terminate if diversity too low (premature convergence)
        if self.parameters.maintain_diversity
            && self.diversity_metrics.structural_diversity < self.parameters.min_diversity_threshold
        {
            return true;
        }

        // Terminate if no improvement for many generations
        if self.fitness_history.len() >= 20 {
            let recent_best = self.fitness_history
                .iter()
                .rev()
                .take(10)
                .filter_map(|gen| gen.iter().max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)))
                .max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

            let older_best = self.fitness_history
                .iter()
                .rev()
                .skip(10)
                .take(10)
                .filter_map(|gen| gen.iter().max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)))
                .max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

            if let (Some(&recent), Some(&older)) = (recent_best, older_best) {
                if (recent - older).abs() < 0.001 {
                    return true; // No significant improvement
                }
            }
        }

        false
    }

    /// Get best individuals in current population
    pub fn get_best_individuals(&self, n: usize) -> Vec<&ArchitectureCandidate> {
        let mut indexed_individuals: Vec<(usize, &ArchitectureCandidate)> = self
            .population
            .iter()
            .enumerate()
            .collect();

        indexed_individuals.sort_by(|a, b| {
            b.1.performance
                .optimization_performance
                .partial_cmp(&a.1.performance.optimization_performance)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        indexed_individuals
            .into_iter()
            .take(n)
            .map(|(_, individual)| individual)
            .collect()
    }

    /// Add mutation operator
    pub fn add_mutation_operator(&mut self, operator: Box<dyn MutationOperator>) {
        self.mutation_operators.push(operator);
    }

    /// Add crossover operator
    pub fn add_crossover_operator(&mut self, operator: Box<dyn CrossoverOperator>) {
        self.crossover_operators.push(operator);
    }

    /// Add selection method
    pub fn add_selection_method(&mut self, method: Box<dyn SelectionMethod<T>>) {
        self.selection_methods.push(method);
    }

    /// Set parameters
    pub fn set_parameters(&mut self, parameters: EvolutionaryParameters) {
        self.parameters = parameters;
    }
}

/// Calculate diversity metrics for a population
pub fn calculate_diversity_metrics(population: &[ArchitectureCandidate]) -> DiversityMetrics {
    if population.len() < 2 {
        return DiversityMetrics::default();
    }

    let structural_diversity = calculate_structural_diversity(population);
    let performance_diversity = calculate_performance_diversity(population);
    let genotypic_diversity = calculate_genotypic_diversity(population);
    let phenotypic_diversity = calculate_phenotypic_diversity(population);

    DiversityMetrics {
        structural_diversity,
        performance_diversity,
        genotypic_diversity,
        phenotypic_diversity,
    }
}

fn calculate_structural_diversity(population: &[ArchitectureCandidate]) -> f64 {
    let mut total_distance = 0.0;
    let mut comparisons = 0;

    for i in 0..population.len() {
        for j in (i + 1)..population.len() {
            let distance = structural_distance(&population[i].architecture, &population[j].architecture);
            total_distance += distance;
            comparisons += 1;
        }
    }

    if comparisons > 0 {
        total_distance / comparisons as f64
    } else {
        0.0
    }
}

fn calculate_performance_diversity(population: &[ArchitectureCandidate]) -> f64 {
    let performances: Vec<f64> = population
        .iter()
        .map(|ind| ind.performance.optimization_performance)
        .collect();

    if performances.len() < 2 {
        return 0.0;
    }

    let mean = performances.iter().sum::<f64>() / performances.len() as f64;
    let variance = performances
        .iter()
        .map(|&x| (x - mean).powi(2))
        .sum::<f64>()
        / performances.len() as f64;

    variance.sqrt()
}

fn calculate_genotypic_diversity(population: &[ArchitectureCandidate]) -> f64 {
    // Simplified genotypic diversity based on architecture encoding differences
    calculate_structural_diversity(population)
}

fn calculate_phenotypic_diversity(population: &[ArchitectureCandidate]) -> f64 {
    // Phenotypic diversity based on performance characteristics
    calculate_performance_diversity(population)
}

fn structural_distance(arch1: &ArchitectureSpec, arch2: &ArchitectureSpec) -> f64 {
    // Calculate structural distance between architectures
    let layer_count_diff = (arch1.layers.len() as i32 - arch2.layers.len() as i32).abs() as f64;
    let param_count_diff = (arch1.parameter_count() as i64 - arch2.parameter_count() as i64).abs() as f64;
    
    // Normalize and combine
    layer_count_diff + param_count_diff / 10000.0
}

/// NSGA-II implementation for multi-objective optimization
pub struct NSGA2State<T: Float + Debug + Send + Sync + 'static> {
    /// Population
    pub population: Vec<ArchitectureCandidate>,
    
    /// Objectives
    pub objectives: Vec<String>,
    
    /// Non-dominated sorting
    pub fronts: Vec<Vec<usize>>,
    
    /// Crowding distances
    pub crowding_distances: HashMap<usize, f64>,
    
    /// Parameters
    _phantom: std::marker::PhantomData<T>,
}

impl<T: Float + Debug + Default + std::fmt::Debug + Send + Sync> NSGA2State<T> {
    pub fn new(objectives: Vec<String>) -> Self {
        Self {
            population: Vec::new(),
            objectives,
            fronts: Vec::new(),
            crowding_distances: HashMap::new(),
            _phantom: std::marker::PhantomData,
        }
    }

    /// Perform non-dominated sorting
    pub fn non_dominated_sort(&mut self) {
        let n = self.population.len();
        let mut domination_count = vec![0; n];
        let mut dominated_solutions = vec![Vec::new(); n];
        self.fronts.clear();
        self.fronts.push(Vec::new());

        // Calculate domination relationships
        for i in 0..n {
            for j in 0..n {
                if i != j && self.dominates(i, j) {
                    dominated_solutions[i].push(j);
                } else if i != j && self.dominates(j, i) {
                    domination_count[i] += 1;
                }
            }

            if domination_count[i] == 0 {
                self.fronts[0].push(i);
            }
        }

        // Build subsequent fronts
        let mut front_idx = 0;
        while !self.fronts[front_idx].is_empty() {
            let mut next_front = Vec::new();
            
            for &individual in &self.fronts[front_idx] {
                for &dominated in &dominated_solutions[individual] {
                    domination_count[dominated] -= 1;
                    if domination_count[dominated] == 0 {
                        next_front.push(dominated);
                    }
                }
            }
            
            if !next_front.is_empty() {
                self.fronts.push(next_front);
            }
            front_idx += 1;
        }
    }

    /// Check if individual i dominates individual j
    fn dominates(&self, i: usize, j: usize) -> bool {
        let perf_i = &self.population[i].performance;
        let perf_j = &self.population[j].performance;

        let better_in_any = perf_i.optimization_performance > perf_j.optimization_performance
            || perf_i.convergence_speed > perf_j.convergence_speed
            || perf_i.generalization > perf_j.generalization;

        let worse_in_any = perf_i.optimization_performance < perf_j.optimization_performance
            || perf_i.convergence_speed < perf_j.convergence_speed
            || perf_i.generalization < perf_j.generalization;

        better_in_any && !worse_in_any
    }

    /// Calculate crowding distances
    pub fn calculate_crowding_distances(&mut self) {
        self.crowding_distances.clear();

        for front in &self.fronts {
            if front.len() <= 2 {
                for &individual in front {
                    self.crowding_distances.insert(individual, f64::INFINITY);
                }
                continue;
            }

            // Initialize distances
            for &individual in front {
                self.crowding_distances.insert(individual, 0.0);
            }

            // For each objective
            for obj_idx in 0..self.objectives.len() {
                let mut sorted_front = front.clone();
                sorted_front.sort_by(|&a, &b| {
                    self.get_objective_value(a, obj_idx)
                        .partial_cmp(&self.get_objective_value(b, obj_idx))
                        .unwrap_or(std::cmp::Ordering::Equal)
                });

                // Set boundary points to infinite distance
                *self.crowding_distances.get_mut(&sorted_front[0]).expect("unwrap failed") = f64::INFINITY;
                *self.crowding_distances.get_mut(&sorted_front[sorted_front.len() - 1]).expect("unwrap failed") = f64::INFINITY;

                // Calculate distances for intermediate points
                let obj_range = self.get_objective_value(sorted_front[sorted_front.len() - 1], obj_idx)
                    - self.get_objective_value(sorted_front[0], obj_idx);

                if obj_range > 0.0 {
                    for i in 1..sorted_front.len() - 1 {
                        let distance_contrib = (self.get_objective_value(sorted_front[i + 1], obj_idx)
                            - self.get_objective_value(sorted_front[i - 1], obj_idx))
                            / obj_range;
                        
                        *self.crowding_distances.get_mut(&sorted_front[i]).expect("unwrap failed") += distance_contrib;
                    }
                }
            }
        }
    }

    /// Get objective value for individual
    fn get_objective_value(&self, individual: usize, objective_idx: usize) -> f64 {
        let performance = &self.population[individual].performance;
        match objective_idx {
            0 => performance.optimization_performance,
            1 => performance.convergence_speed,
            2 => performance.generalization,
            3 => performance.robustness,
            _ => 0.0,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use super::super::architecture::*;

    #[test]
    fn test_evolutionary_state_creation() {
        let state: EvolutionarySearchState<f64> = EvolutionarySearchState::new(10);
        assert_eq!(state.parameters.population_size, 10);
        assert_eq!(state.generation, 0);
    }

    #[test]
    fn test_diversity_calculation() {
        let population = vec![
            ArchitectureCandidate::new(
                "arch1".to_string(),
                ArchitectureSpec::new(vec![], GlobalArchitectureConfig::default()),
            ),
            ArchitectureCandidate::new(
                "arch2".to_string(),
                ArchitectureSpec::new(vec![], GlobalArchitectureConfig::default()),
            ),
        ];

        let diversity = calculate_diversity_metrics(&population);
        assert!(diversity.structural_diversity >= 0.0);
        assert!(diversity.performance_diversity >= 0.0);
    }

    #[test]
    fn test_nsga2_state_creation() {
        let objectives = vec!["performance".to_string(), "efficiency".to_string()];
        let state: NSGA2State<f64> = NSGA2State::new(objectives);
        assert_eq!(state.objectives.len(), 2);
    }

    #[test]
    fn test_elite_selection() {
        let mut state: EvolutionarySearchState<f64> = EvolutionarySearchState::new(5);
        
        // Add some individuals
        for i in 0..5 {
            let mut candidate = ArchitectureCandidate::new(
                format!("arch{}", i),
                ArchitectureSpec::new(vec![], GlobalArchitectureConfig::default()),
            );
            candidate.performance.optimization_performance = i as f64 / 10.0;
            state.population.push(candidate);
        }

        let fitnesses: Vec<f64> = (0..5).map(|i| i as f64 / 10.0).collect();
        let elite = state.select_elite(&fitnesses);

        assert_eq!(elite.len(), state.parameters.elite_size);
        // Best individuals should be selected (highest indices have highest fitness)
        assert!(elite.contains(&4)); // Highest fitness
    }
}