fugue-evo 0.2.0

Evolutionary computation for Rust: classical EC algorithms plus evolutionary inference - evolutionary algorithms as probabilistic programs (tempered SMC in trace space, built on fugue-ppl)
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
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
//! Steady-State Genetic Algorithm
//!
//! This module implements a steady-state genetic algorithm where only a small
//! number of individuals are replaced in each generation (typically 1 or 2),
//! rather than replacing the entire population.

use std::time::Instant;

use rand::Rng;

use crate::diagnostics::{EvolutionResult, EvolutionStats, GenerationStats, TimingStats};
use crate::error::EvolutionError;
use crate::fitness::traits::{Fitness, FitnessValue};
use crate::genome::bounds::MultiBounds;
use crate::genome::traits::EvolutionaryGenome;
use crate::operators::traits::{
    BoundedCrossoverOperator, BoundedMutationOperator, CrossoverOperator, MutationOperator,
    SelectionOperator,
};
use crate::population::individual::Individual;
use crate::population::population::Population;
use crate::termination::{EvolutionState, MaxGenerations, TerminationCriterion};

/// Replacement strategy for steady-state GA.
///
/// EV-42: only [`ReplacementStrategy::ReplaceIfBetter`] is elitist (it accepts an
/// offspring only when it improves on the individual it would replace). Every
/// other strategy performs its replacement **unconditionally**, matching its
/// documented meaning — in particular `ReplaceRandom` is a genuine, diversity-
/// preserving stochastic replacement and may accept a worse offspring.
#[derive(Clone, Debug, Default)]
pub enum ReplacementStrategy {
    /// Replace the worst individual in the population, unconditionally. Because the
    /// current best is never the replacement target, the population best is
    /// preserved, but the population mean may temporarily worsen.
    #[default]
    ReplaceWorst,
    /// Replace a uniformly-random individual, unconditionally (non-elitist). A
    /// worse offspring can enter the population, preserving exploratory diversity.
    ReplaceRandom,
    /// Replace the worse parent, but only if the offspring beats it
    /// (generational replacement).
    ReplaceParent,
    /// Replace the worst individual, but only if the offspring is strictly better
    /// than it (the fully elitist, accept-if-better option).
    ReplaceIfBetter,
    /// Use an inverse tournament to choose a (likely poor) individual to replace,
    /// then replace it unconditionally.
    TournamentWorst(usize),
}

impl ReplacementStrategy {
    /// Whether replacement is gated on the offspring improving on its target
    /// (EV-42). Only `ReplaceIfBetter` is elitist here; `ReplaceParent` carries
    /// its own acceptance test in the main loop.
    fn requires_improvement(&self) -> bool {
        matches!(self, ReplacementStrategy::ReplaceIfBetter)
    }
}

/// Configuration for the Steady-State GA
#[derive(Clone, Debug)]
pub struct SteadyStateConfig {
    /// Population size
    pub population_size: usize,
    /// Number of offspring to generate per step (typically 1 or 2)
    pub offspring_count: usize,
    /// Crossover probability
    pub crossover_probability: f64,
    /// Replacement strategy
    pub replacement: ReplacementStrategy,
    /// Whether to evaluate in parallel
    pub parallel_evaluation: bool,
    /// Number of steps per "generation" for statistics reporting
    pub steps_per_generation: usize,
    /// EV-44: when true, an offspring whose genome equals an existing population
    /// member is rejected instead of inserted, preventing the population from
    /// collapsing to duplicates. This costs an O(population_size) genome
    /// comparison per candidate offspring.
    pub prevent_duplicates: bool,
}

impl Default for SteadyStateConfig {
    fn default() -> Self {
        Self {
            population_size: 100,
            offspring_count: 2,
            crossover_probability: 0.9,
            replacement: ReplacementStrategy::ReplaceWorst,
            parallel_evaluation: false,
            steps_per_generation: 50, // Report stats every 50 replacements
            prevent_duplicates: false,
        }
    }
}

/// Builder for SteadyStateGA
pub struct SteadyStateBuilder<G, F, S, C, M, Fit, Term>
where
    G: EvolutionaryGenome,
    F: FitnessValue,
{
    config: SteadyStateConfig,
    bounds: Option<MultiBounds>,
    selection: Option<S>,
    crossover: Option<C>,
    mutation: Option<M>,
    fitness: Option<Fit>,
    termination: Option<Term>,
    _phantom: std::marker::PhantomData<(G, F)>,
}

impl<G, F> SteadyStateBuilder<G, F, (), (), (), (), ()>
where
    G: EvolutionaryGenome,
    F: FitnessValue,
{
    /// Create a new builder with default configuration
    pub fn new() -> Self {
        Self {
            config: SteadyStateConfig::default(),
            bounds: None,
            selection: None,
            crossover: None,
            mutation: None,
            fitness: None,
            termination: None,
            _phantom: std::marker::PhantomData,
        }
    }
}

impl<G, F> Default for SteadyStateBuilder<G, F, (), (), (), (), ()>
where
    G: EvolutionaryGenome,
    F: FitnessValue,
{
    fn default() -> Self {
        Self::new()
    }
}

impl<G, F, S, C, M, Fit, Term> SteadyStateBuilder<G, F, S, C, M, Fit, Term>
where
    G: EvolutionaryGenome,
    F: FitnessValue,
{
    /// Set the population size
    pub fn population_size(mut self, size: usize) -> Self {
        self.config.population_size = size;
        self
    }

    /// Set the number of offspring per step
    pub fn offspring_count(mut self, count: usize) -> Self {
        self.config.offspring_count = count;
        self
    }

    /// Set the crossover probability
    pub fn crossover_probability(mut self, probability: f64) -> Self {
        self.config.crossover_probability = probability;
        self
    }

    /// Set the replacement strategy
    pub fn replacement(mut self, strategy: ReplacementStrategy) -> Self {
        self.config.replacement = strategy;
        self
    }

    /// Enable or disable parallel evaluation
    pub fn parallel_evaluation(mut self, enabled: bool) -> Self {
        self.config.parallel_evaluation = enabled;
        self
    }

    /// Set the number of steps per generation (for statistics)
    pub fn steps_per_generation(mut self, steps: usize) -> Self {
        self.config.steps_per_generation = steps;
        self
    }

    /// Enable or disable duplicate avoidance (EV-44).
    ///
    /// When enabled, an offspring whose genome equals any current population
    /// member is rejected rather than inserted. This preserves diversity but adds
    /// an O(population_size) genome comparison per candidate offspring, and
    /// requires the genome type to implement [`PartialEq`].
    pub fn prevent_duplicates(mut self, enabled: bool) -> Self {
        self.config.prevent_duplicates = enabled;
        self
    }

    /// Set the search space bounds
    pub fn bounds(mut self, bounds: MultiBounds) -> Self {
        self.bounds = Some(bounds);
        self
    }

    /// Set the selection operator
    pub fn selection<NewS>(self, selection: NewS) -> SteadyStateBuilder<G, F, NewS, C, M, Fit, Term>
    where
        NewS: SelectionOperator<G>,
    {
        SteadyStateBuilder {
            config: self.config,
            bounds: self.bounds,
            selection: Some(selection),
            crossover: self.crossover,
            mutation: self.mutation,
            fitness: self.fitness,
            termination: self.termination,
            _phantom: std::marker::PhantomData,
        }
    }

    /// Set the crossover operator
    pub fn crossover<NewC>(self, crossover: NewC) -> SteadyStateBuilder<G, F, S, NewC, M, Fit, Term>
    where
        NewC: CrossoverOperator<G>,
    {
        SteadyStateBuilder {
            config: self.config,
            bounds: self.bounds,
            selection: self.selection,
            crossover: Some(crossover),
            mutation: self.mutation,
            fitness: self.fitness,
            termination: self.termination,
            _phantom: std::marker::PhantomData,
        }
    }

    /// Set the mutation operator
    pub fn mutation<NewM>(self, mutation: NewM) -> SteadyStateBuilder<G, F, S, C, NewM, Fit, Term>
    where
        NewM: MutationOperator<G>,
    {
        SteadyStateBuilder {
            config: self.config,
            bounds: self.bounds,
            selection: self.selection,
            crossover: self.crossover,
            mutation: Some(mutation),
            fitness: self.fitness,
            termination: self.termination,
            _phantom: std::marker::PhantomData,
        }
    }

    /// Set the fitness function
    pub fn fitness<NewFit>(self, fitness: NewFit) -> SteadyStateBuilder<G, F, S, C, M, NewFit, Term>
    where
        NewFit: Fitness<Genome = G, Value = F>,
    {
        SteadyStateBuilder {
            config: self.config,
            bounds: self.bounds,
            selection: self.selection,
            crossover: self.crossover,
            mutation: self.mutation,
            fitness: Some(fitness),
            termination: self.termination,
            _phantom: std::marker::PhantomData,
        }
    }

    /// Set the termination criterion
    pub fn termination<NewTerm>(
        self,
        termination: NewTerm,
    ) -> SteadyStateBuilder<G, F, S, C, M, Fit, NewTerm>
    where
        NewTerm: TerminationCriterion<G, F>,
    {
        SteadyStateBuilder {
            config: self.config,
            bounds: self.bounds,
            selection: self.selection,
            crossover: self.crossover,
            mutation: self.mutation,
            fitness: self.fitness,
            termination: Some(termination),
            _phantom: std::marker::PhantomData,
        }
    }

    /// Set max generations (convenience method)
    pub fn max_generations(
        self,
        max: usize,
    ) -> SteadyStateBuilder<G, F, S, C, M, Fit, MaxGenerations> {
        SteadyStateBuilder {
            config: self.config,
            bounds: self.bounds,
            selection: self.selection,
            crossover: self.crossover,
            mutation: self.mutation,
            fitness: self.fitness,
            termination: Some(MaxGenerations::new(max)),
            _phantom: std::marker::PhantomData,
        }
    }
}

impl<G, F, S, C, M, Fit, Term> SteadyStateBuilder<G, F, S, C, M, Fit, Term>
where
    G: EvolutionaryGenome + Send + Sync,
    F: FitnessValue + Send,
    S: SelectionOperator<G>,
    C: CrossoverOperator<G>,
    M: MutationOperator<G>,
    Fit: Fitness<Genome = G, Value = F> + Sync,
    Term: TerminationCriterion<G, F>,
{
    /// Build the SteadyStateGA instance
    #[allow(clippy::type_complexity)]
    pub fn build(self) -> Result<SteadyStateGA<G, F, S, C, M, Fit, Term>, EvolutionError> {
        let bounds = self
            .bounds
            .ok_or_else(|| EvolutionError::Configuration("Bounds must be specified".to_string()))?;

        let selection = self.selection.ok_or_else(|| {
            EvolutionError::Configuration("Selection operator must be specified".to_string())
        })?;

        let crossover = self.crossover.ok_or_else(|| {
            EvolutionError::Configuration("Crossover operator must be specified".to_string())
        })?;

        let mutation = self.mutation.ok_or_else(|| {
            EvolutionError::Configuration("Mutation operator must be specified".to_string())
        })?;

        let fitness = self.fitness.ok_or_else(|| {
            EvolutionError::Configuration("Fitness function must be specified".to_string())
        })?;

        let termination = self.termination.ok_or_else(|| {
            EvolutionError::Configuration("Termination criterion must be specified".to_string())
        })?;

        Ok(SteadyStateGA {
            config: self.config,
            bounds,
            selection,
            crossover,
            mutation,
            fitness,
            termination,
            _phantom: std::marker::PhantomData,
        })
    }
}

/// Steady-State Genetic Algorithm
///
/// A GA variant that replaces only a few individuals per generation step,
/// maintaining a more continuous population transition.
pub struct SteadyStateGA<G, F, S, C, M, Fit, Term>
where
    G: EvolutionaryGenome,
    F: FitnessValue,
{
    config: SteadyStateConfig,
    bounds: MultiBounds,
    selection: S,
    crossover: C,
    mutation: M,
    fitness: Fit,
    termination: Term,
    _phantom: std::marker::PhantomData<(G, F)>,
}

impl<G, F, S, C, M, Fit, Term> SteadyStateGA<G, F, S, C, M, Fit, Term>
where
    G: EvolutionaryGenome + Send + Sync,
    F: FitnessValue + Send,
    S: SelectionOperator<G>,
    C: CrossoverOperator<G>,
    M: MutationOperator<G>,
    Fit: Fitness<Genome = G, Value = F> + Sync,
    Term: TerminationCriterion<G, F>,
{
    /// Create a builder for SteadyStateGA
    pub fn builder() -> SteadyStateBuilder<G, F, (), (), (), (), ()> {
        SteadyStateBuilder::new()
    }

    /// Find the index of the individual to replace for the configured strategy.
    fn find_replacement_index<R: Rng>(&self, population: &Population<G, F>, rng: &mut R) -> usize {
        match &self.config.replacement {
            // Both worst-targeting strategies aim at the least-fit individual; the
            // accept-if-better gate for `ReplaceIfBetter` is applied by the caller.
            ReplacementStrategy::ReplaceWorst | ReplacementStrategy::ReplaceIfBetter => {
                // Find index of worst fitness
                population
                    .iter()
                    .enumerate()
                    .min_by(|(_, a), (_, b)| {
                        a.fitness_value()
                            .partial_cmp(b.fitness_value())
                            .unwrap_or(std::cmp::Ordering::Equal)
                    })
                    .map(|(i, _)| i)
                    .unwrap_or(0)
            }
            ReplacementStrategy::ReplaceRandom => rng.gen_range(0..population.len()),
            ReplacementStrategy::ReplaceParent => {
                // This is handled specially in the main loop
                0
            }
            ReplacementStrategy::TournamentWorst(tournament_size) => {
                // Inverse tournament: select worst from tournament
                let mut worst_idx = rng.gen_range(0..population.len());
                let mut worst_fitness = population[worst_idx].fitness_value().to_f64();

                for _ in 1..*tournament_size {
                    let idx = rng.gen_range(0..population.len());
                    let fitness = population[idx].fitness_value().to_f64();
                    if fitness < worst_fitness {
                        worst_idx = idx;
                        worst_fitness = fitness;
                    }
                }
                worst_idx
            }
        }
    }

    /// Generate exactly `offspring_count` evaluated children (EV-43).
    ///
    /// Each returned tuple is `(child, (parent1_idx, parent1_fitness),
    /// (parent2_idx, parent2_fitness))`; the parent data drives `ReplaceParent`.
    /// Fresh parents are selected per crossover pair, and only children that will
    /// actually be considered are mutated and evaluated.
    #[allow(clippy::type_complexity)]
    fn generate_offspring<R: Rng>(
        &self,
        selection_pool: &[(G, f64)],
        rng: &mut R,
    ) -> Vec<(Individual<G, F>, (usize, f64), (usize, f64))> {
        let target = self.config.offspring_count;
        let mut offspring = Vec::with_capacity(target);

        while offspring.len() < target {
            let p1_idx = self.selection.select(selection_pool, rng);
            let p2_idx = self.selection.select(selection_pool, rng);
            let p1 = (p1_idx, selection_pool[p1_idx].1);
            let p2 = (p2_idx, selection_pool[p2_idx].1);
            let parent1 = &selection_pool[p1_idx].0;
            let parent2 = &selection_pool[p2_idx].0;

            let (mut child1, mut child2) = if rng.gen::<f64>() < self.config.crossover_probability {
                match self.crossover.crossover(parent1, parent2, rng).genome() {
                    Some((c1, c2)) => (c1, c2),
                    None => (parent1.clone(), parent2.clone()),
                }
            } else {
                (parent1.clone(), parent2.clone())
            };

            self.mutation.mutate(&mut child1, rng);
            let mut ind1 = Individual::new(child1);
            ind1.set_fitness(self.fitness.evaluate(ind1.genome()));
            offspring.push((ind1, p1, p2));

            if offspring.len() < target {
                self.mutation.mutate(&mut child2, rng);
                let mut ind2 = Individual::new(child2);
                ind2.set_fitness(self.fitness.evaluate(ind2.genome()));
                offspring.push((ind2, p1, p2));
            }
        }

        offspring
    }

    /// Insert an already-evaluated `child` into `population` per the configured
    /// replacement strategy (EV-42) and duplicate policy (EV-44).
    ///
    /// The population may be left unchanged: a duplicate is rejected when
    /// `prevent_duplicates` is set, `ReplaceParent` skips when the child does not
    /// beat its worse parent, and `ReplaceIfBetter` skips when the child does not
    /// beat the individual it targets. All other strategies replace their chosen
    /// index unconditionally.
    fn place_offspring<R: Rng>(
        &self,
        population: &mut Population<G, F>,
        child: Individual<G, F>,
        parent1: (usize, f64),
        parent2: (usize, f64),
        rng: &mut R,
    ) where
        G: PartialEq,
    {
        // EV-44: reject an exact duplicate of an existing genome if configured
        // (O(population_size) genome comparison).
        if self.config.prevent_duplicates
            && population.iter().any(|ind| ind.genome() == child.genome())
        {
            return;
        }

        let replace_idx = match &self.config.replacement {
            ReplacementStrategy::ReplaceParent => {
                let (p1_idx, p1_fit) = parent1;
                let (p2_idx, p2_fit) = parent2;
                let child_fit = child.fitness_value().to_f64();
                if child_fit > p1_fit.min(p2_fit) {
                    if p1_fit < p2_fit {
                        p1_idx
                    } else {
                        p2_idx
                    }
                } else {
                    return; // offspring did not beat its worse parent
                }
            }
            _ => self.find_replacement_index(population, rng),
        };

        // EV-42: only the elitist ReplaceIfBetter gates on improvement. Every
        // other strategy (ReplaceRandom, ReplaceWorst, TournamentWorst) replaces
        // its chosen index unconditionally, as documented.
        if self.config.replacement.requires_improvement()
            && !child
                .fitness_value()
                .is_better_than(population[replace_idx].fitness_value())
        {
            return;
        }

        population[replace_idx] = child;
    }

    /// Run the steady-state genetic algorithm.
    ///
    /// Requires `G: PartialEq` so the optional duplicate-avoidance policy
    /// (`prevent_duplicates`) can compare genomes; all built-in genome types
    /// satisfy this.
    pub fn run<R: Rng>(&self, rng: &mut R) -> Result<EvolutionResult<G, F>, EvolutionError>
    where
        G: PartialEq,
    {
        let start_time = Instant::now();

        // Initialize population
        let mut population: Population<G, F> =
            Population::random(self.config.population_size, &self.bounds, rng);

        // Evaluate initial population
        if self.config.parallel_evaluation {
            population.evaluate_parallel(&self.fitness);
        } else {
            population.evaluate(&self.fitness);
        }

        let mut stats = EvolutionStats::new();
        let mut evaluations = population.len();
        let mut fitness_history: Vec<f64> = Vec::new();
        let mut step_count = 0usize;
        let mut generation = 0usize;

        // Track best individual
        let mut best_individual = population
            .best()
            .ok_or(EvolutionError::EmptyPopulation)?
            .clone();

        // Record initial statistics
        let gen_stats = GenerationStats::from_population(&population, 0, evaluations);
        fitness_history.push(gen_stats.best_fitness);
        stats.record(gen_stats);

        // Main evolution loop
        loop {
            // Check termination
            let state = EvolutionState {
                generation,
                evaluations,
                best_fitness: best_individual.fitness_value().to_f64(),
                population: &population,
                fitness_history: &fitness_history,
            };

            if self.termination.should_terminate(&state) {
                stats.set_termination_reason(self.termination.reason());
                break;
            }

            let step_start = Instant::now();

            // Selection pool
            let selection_pool: Vec<(G, f64)> = population.as_fitness_pairs();

            // EV-43: generate and evaluate EXACTLY offspring_count children — no
            // more, no fewer — so wasted evaluations don't inflate the counter or
            // trip MaxEvaluations early, and offspring_count > 2 is honored.
            let offspring = self.generate_offspring(&selection_pool, rng);
            evaluations += offspring.len();

            // Replace individuals in the population.
            for (child, parent1, parent2) in offspring {
                self.place_offspring(&mut population, child, parent1, parent2, rng);
            }

            // Update best individual
            if let Some(best) = population.best() {
                if best.is_better_than(&best_individual) {
                    best_individual = best.clone();
                }
            }

            step_count += 1;

            // Record statistics at generation boundaries
            if step_count.is_multiple_of(self.config.steps_per_generation) {
                generation += 1;
                population.set_generation(generation);

                let timing = TimingStats::new()
                    .with_total(step_start.elapsed() * self.config.steps_per_generation as u32);

                let gen_stats =
                    GenerationStats::from_population(&population, generation, evaluations)
                        .with_timing(timing);
                fitness_history.push(gen_stats.best_fitness);
                stats.record(gen_stats);
            }
        }

        stats.set_runtime(start_time.elapsed());

        Ok(EvolutionResult::new(
            best_individual.genome,
            best_individual.fitness.unwrap(),
            generation,
            evaluations,
        )
        .with_stats(stats))
    }
}

// Implement bounded operators version
impl<G, F, S, C, M, Fit, Term> SteadyStateGA<G, F, S, C, M, Fit, Term>
where
    G: EvolutionaryGenome + Send + Sync,
    F: FitnessValue + Send,
    S: SelectionOperator<G>,
    C: BoundedCrossoverOperator<G>,
    M: BoundedMutationOperator<G>,
    Fit: Fitness<Genome = G, Value = F> + Sync,
    Term: TerminationCriterion<G, F>,
{
    /// Generate exactly `offspring_count` evaluated children with bounded
    /// operators (EV-43). See [`SteadyStateGA::generate_offspring`].
    #[allow(clippy::type_complexity)]
    fn generate_offspring_bounded<R: Rng>(
        &self,
        selection_pool: &[(G, f64)],
        rng: &mut R,
    ) -> Vec<(Individual<G, F>, (usize, f64), (usize, f64))> {
        let target = self.config.offspring_count;
        let mut offspring = Vec::with_capacity(target);

        while offspring.len() < target {
            let p1_idx = self.selection.select(selection_pool, rng);
            let p2_idx = self.selection.select(selection_pool, rng);
            let p1 = (p1_idx, selection_pool[p1_idx].1);
            let p2 = (p2_idx, selection_pool[p2_idx].1);
            let parent1 = &selection_pool[p1_idx].0;
            let parent2 = &selection_pool[p2_idx].0;

            let (mut child1, mut child2) = if rng.gen::<f64>() < self.config.crossover_probability {
                match self
                    .crossover
                    .crossover_bounded(parent1, parent2, &self.bounds, rng)
                    .genome()
                {
                    Some((c1, c2)) => (c1, c2),
                    None => (parent1.clone(), parent2.clone()),
                }
            } else {
                (parent1.clone(), parent2.clone())
            };

            self.mutation.mutate_bounded(&mut child1, &self.bounds, rng);
            let mut ind1 = Individual::new(child1);
            ind1.set_fitness(self.fitness.evaluate(ind1.genome()));
            offspring.push((ind1, p1, p2));

            if offspring.len() < target {
                self.mutation.mutate_bounded(&mut child2, &self.bounds, rng);
                let mut ind2 = Individual::new(child2);
                ind2.set_fitness(self.fitness.evaluate(ind2.genome()));
                offspring.push((ind2, p1, p2));
            }
        }

        offspring
    }

    /// Run the steady-state genetic algorithm with bounded operators.
    ///
    /// Requires `G: PartialEq` for the optional `prevent_duplicates` policy.
    pub fn run_bounded<R: Rng>(&self, rng: &mut R) -> Result<EvolutionResult<G, F>, EvolutionError>
    where
        G: PartialEq,
    {
        let start_time = Instant::now();

        // Initialize population
        let mut population: Population<G, F> =
            Population::random(self.config.population_size, &self.bounds, rng);

        // Evaluate initial population
        if self.config.parallel_evaluation {
            population.evaluate_parallel(&self.fitness);
        } else {
            population.evaluate(&self.fitness);
        }

        let mut stats = EvolutionStats::new();
        let mut evaluations = population.len();
        let mut fitness_history: Vec<f64> = Vec::new();
        let mut step_count = 0usize;
        let mut generation = 0usize;

        // Track best individual
        let mut best_individual = population
            .best()
            .ok_or(EvolutionError::EmptyPopulation)?
            .clone();

        // Record initial statistics
        let gen_stats = GenerationStats::from_population(&population, 0, evaluations);
        fitness_history.push(gen_stats.best_fitness);
        stats.record(gen_stats);

        // Main evolution loop
        loop {
            // Check termination
            let state = EvolutionState {
                generation,
                evaluations,
                best_fitness: best_individual.fitness_value().to_f64(),
                population: &population,
                fitness_history: &fitness_history,
            };

            if self.termination.should_terminate(&state) {
                stats.set_termination_reason(self.termination.reason());
                break;
            }

            // Selection pool
            let selection_pool: Vec<(G, f64)> = population.as_fitness_pairs();

            // EV-43: generate and evaluate EXACTLY offspring_count children with
            // bounded operators.
            let offspring = self.generate_offspring_bounded(&selection_pool, rng);
            evaluations += offspring.len();

            // Replace individuals in the population.
            for (child, parent1, parent2) in offspring {
                self.place_offspring(&mut population, child, parent1, parent2, rng);
            }

            // Update best individual
            if let Some(best) = population.best() {
                if best.is_better_than(&best_individual) {
                    best_individual = best.clone();
                }
            }

            step_count += 1;

            // Record statistics at generation boundaries
            if step_count.is_multiple_of(self.config.steps_per_generation) {
                generation += 1;
                population.set_generation(generation);

                let gen_stats =
                    GenerationStats::from_population(&population, generation, evaluations);
                fitness_history.push(gen_stats.best_fitness);
                stats.record(gen_stats);
            }
        }

        stats.set_runtime(start_time.elapsed());

        Ok(EvolutionResult::new(
            best_individual.genome,
            best_individual.fitness.unwrap(),
            generation,
            evaluations,
        )
        .with_stats(stats))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fitness::benchmarks::{OneMax, Sphere};
    use crate::genome::real_vector::RealVector;
    use crate::genome::traits::RealValuedGenome;
    use crate::operators::crossover::{SbxCrossover, UniformCrossover};
    use crate::operators::mutation::{BitFlipMutation, PolynomialMutation};
    use crate::operators::selection::TournamentSelection;
    use crate::termination::MaxEvaluations;
    use rand::SeedableRng;

    #[test]
    fn test_steady_state_builder() {
        let bounds = MultiBounds::symmetric(5.0, 10);
        let ga = SteadyStateBuilder::new()
            .population_size(50)
            .offspring_count(2)
            .bounds(bounds)
            .selection(TournamentSelection::new(3))
            .crossover(SbxCrossover::new(20.0))
            .mutation(PolynomialMutation::new(20.0))
            .fitness(Sphere::new(10))
            .max_generations(10)
            .build();

        assert!(ga.is_ok());
    }

    #[test]
    fn test_steady_state_sphere() {
        let mut rng = rand::thread_rng();
        let bounds = MultiBounds::symmetric(5.12, 10);

        let ga = SteadyStateBuilder::new()
            .population_size(50)
            .offspring_count(2)
            .steps_per_generation(25)
            .bounds(bounds)
            .selection(TournamentSelection::new(3))
            .crossover(SbxCrossover::new(20.0))
            .mutation(PolynomialMutation::new(20.0))
            .fitness(Sphere::new(10))
            .termination(MaxEvaluations::new(2000))
            .build()
            .unwrap();

        let result = ga.run(&mut rng).unwrap();

        // Should find some improvement
        assert!(
            result.best_fitness > -200.0,
            "Expected fitness > -200, got {}",
            result.best_fitness
        );
        assert!(result.evaluations <= 2000);
    }

    #[test]
    fn test_steady_state_onemax() {
        let mut rng = rand::thread_rng();
        let bounds = MultiBounds::uniform(crate::genome::bounds::Bounds::unit(), 20);

        let ga = SteadyStateBuilder::new()
            .population_size(50)
            .offspring_count(2)
            .steps_per_generation(25)
            .bounds(bounds)
            .selection(TournamentSelection::new(3))
            .crossover(UniformCrossover::new())
            .mutation(BitFlipMutation::new())
            .fitness(OneMax::new(20))
            .termination(MaxEvaluations::new(2000))
            .build()
            .unwrap();

        let result = ga.run(&mut rng).unwrap();

        // Should find good solution
        assert!(result.best_fitness >= 15); // At least 75% ones
    }

    #[test]
    fn test_replacement_strategies() {
        let mut rng = rand::thread_rng();
        let bounds = MultiBounds::symmetric(5.12, 5);

        let strategies = vec![
            ReplacementStrategy::ReplaceWorst,
            ReplacementStrategy::ReplaceRandom,
            ReplacementStrategy::TournamentWorst(3),
        ];

        for strategy in strategies {
            let ga = SteadyStateBuilder::new()
                .population_size(30)
                .offspring_count(2)
                .replacement(strategy)
                .bounds(bounds.clone())
                .selection(TournamentSelection::new(3))
                .crossover(SbxCrossover::new(20.0))
                .mutation(PolynomialMutation::new(20.0))
                .fitness(Sphere::new(5))
                .termination(MaxEvaluations::new(500))
                .build()
                .unwrap();

            let result = ga.run(&mut rng);
            assert!(result.is_ok());
        }
    }

    #[test]
    fn test_steady_state_bounded() {
        let mut rng = rand::thread_rng();
        let bounds = MultiBounds::symmetric(5.12, 10);

        let ga = SteadyStateBuilder::new()
            .population_size(50)
            .bounds(bounds.clone())
            .selection(TournamentSelection::new(3))
            .crossover(SbxCrossover::new(20.0))
            .mutation(PolynomialMutation::new(20.0))
            .fitness(Sphere::new(10))
            .termination(MaxEvaluations::new(1000))
            .build()
            .unwrap();

        let result = ga.run_bounded(&mut rng).unwrap();

        // All genes should be within bounds
        for gene in result.best_genome.genes() {
            assert!(*gene >= -5.12 && *gene <= 5.12);
        }
    }

    // regression: EV-43 — with offspring_count = 1, exactly one child is evaluated
    // per step. Starting from a pop of 10 (10 initial evals) under a budget of 15,
    // the fixed code stops at exactly 15; the pre-fix code evaluated 2 per step and
    // overshot to 16.
    #[test]
    fn test_offspring_count_one_evaluates_one_per_step() {
        let mut rng = rand::rngs::StdRng::seed_from_u64(11);
        let bounds = MultiBounds::symmetric(5.12, 5);
        let ga = SteadyStateBuilder::new()
            .population_size(10)
            .offspring_count(1)
            .steps_per_generation(1)
            .bounds(bounds)
            .selection(TournamentSelection::new(2))
            .crossover(SbxCrossover::new(20.0))
            .mutation(PolynomialMutation::new(20.0))
            .fitness(Sphere::new(5))
            .termination(MaxEvaluations::new(15))
            .build()
            .unwrap();

        let result = ga.run(&mut rng).unwrap();
        assert_eq!(result.evaluations, 15);
    }

    // regression: EV-42 — ReplaceRandom replaces unconditionally, so a worse
    // offspring can enter and the population's WORST fitness can degrade across
    // steps. The pre-fix accept-if-better guard kept the worst monotone, so no
    // degradation could ever appear.
    #[test]
    fn test_replace_random_accepts_worse_offspring() {
        let mut rng = rand::rngs::StdRng::seed_from_u64(20);
        let bounds = MultiBounds::symmetric(5.12, 5);
        let ga = SteadyStateBuilder::new()
            .population_size(20)
            .offspring_count(1)
            .steps_per_generation(1)
            .replacement(ReplacementStrategy::ReplaceRandom)
            .bounds(bounds)
            .selection(TournamentSelection::new(2))
            .crossover(SbxCrossover::new(2.0))
            .mutation(PolynomialMutation::new(2.0))
            .fitness(Sphere::new(5))
            .termination(MaxEvaluations::new(400))
            .build()
            .unwrap();

        let result = ga.run(&mut rng).unwrap();
        let worst: Vec<f64> = result
            .stats
            .generations
            .iter()
            .map(|g| g.worst_fitness)
            .collect();
        let degraded = worst.windows(2).any(|w| w[1] < w[0] - 1e-12);
        assert!(
            degraded,
            "ReplaceRandom must let the population worst degrade at least once"
        );
    }

    // regression: EV-44 — with prevent_duplicates, an offspring whose genome equals
    // an existing member is rejected instead of inserted, even if it is fitter.
    #[test]
    fn test_prevent_duplicates_rejects_existing_genome() {
        let mut rng = rand::rngs::StdRng::seed_from_u64(3);
        let bounds = MultiBounds::symmetric(5.0, 3);
        let ga = SteadyStateBuilder::new()
            .population_size(5)
            .offspring_count(1)
            .replacement(ReplacementStrategy::ReplaceRandom)
            .prevent_duplicates(true)
            .bounds(bounds)
            .selection(TournamentSelection::new(2))
            .crossover(SbxCrossover::new(20.0))
            .mutation(PolynomialMutation::new(20.0))
            .fitness(Sphere::new(3))
            .max_generations(1)
            .build()
            .unwrap();

        // A population of distinct genomes.
        let mut pop: Population<RealVector, f64> = Population::new();
        for i in 0..5 {
            let mut ind = Individual::new(RealVector::new(vec![i as f64, 0.0, 0.0]));
            ind.set_fitness(-(i as f64));
            pop.push(ind);
        }

        // A child equal to an existing member is rejected (population unchanged),
        // even though its fitness is better.
        let mut dup = Individual::new(RealVector::new(vec![2.0, 0.0, 0.0]));
        dup.set_fitness(100.0);
        let before: Vec<RealVector> = pop.iter().map(|i| i.genome().clone()).collect();
        ga.place_offspring(&mut pop, dup, (0, 0.0), (0, 0.0), &mut rng);
        let after: Vec<RealVector> = pop.iter().map(|i| i.genome().clone()).collect();
        assert_eq!(before, after, "duplicate offspring must be rejected");

        // A novel genome is accepted (ReplaceRandom, unconditional).
        let mut novel = Individual::new(RealVector::new(vec![42.0, 0.0, 0.0]));
        novel.set_fitness(-999.0);
        let novel_genome = novel.genome().clone();
        ga.place_offspring(&mut pop, novel, (0, 0.0), (0, 0.0), &mut rng);
        assert!(
            pop.iter().any(|i| *i.genome() == novel_genome),
            "a novel genome should be inserted"
        );
    }
}