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
use Debug;
use crateResult;
use cratePhenotype;
/// Trait for selection strategies in genetic algorithms.
///
/// Selection strategies are responsible for choosing individuals from a population
/// based on their fitness scores. Different selection strategies can be used to
/// achieve different evolutionary behaviors.
///
/// # Examples
///
/// ```
/// use genalg::selection::elitist::ElitistSelection;
/// use genalg::selection::SelectionStrategy;
/// use genalg::phenotype::Phenotype;
/// use genalg::rng::RandomNumberGenerator;
/// use genalg::error::Result;
///
/// #[derive(Clone, Debug)]
/// struct MyPhenotype {
/// value: f64,
/// }
///
/// impl Phenotype for MyPhenotype {
/// fn crossover(&mut self, other: &Self) {
/// self.value = (self.value + other.value) / 2.0;
/// }
///
/// fn mutate(&mut self, _rng: &mut RandomNumberGenerator) {
/// self.value += 0.1;
/// }
/// }
///
/// fn main() -> Result<()> {
/// let population = vec![
/// MyPhenotype { value: 1.0 },
/// MyPhenotype { value: 2.0 },
/// MyPhenotype { value: 3.0 },
/// ];
///
/// let fitness = vec![0.5, 0.8, 0.3];
///
/// // Create an elitist selection with default parameters (higher is better, no duplicates)
/// let selection = ElitistSelection::new(true, false);
/// let selected = selection.select(&population, &fitness, 2)?;
///
/// assert_eq!(selected.len(), 2);
///
/// Ok(())
/// }
/// ```