genalg 0.1.0

A flexible, high-performance genetic algorithm library written in Rust
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
use std::collections::HashSet;

use crate::error::{GeneticError, Result};
use crate::phenotype::Phenotype;
use crate::rng::RandomNumberGenerator;
use crate::selection::selection_strategy::SelectionStrategy;

/// A selection strategy that selects individuals based on their rank in the population.
///
/// Rank-based selection assigns a selection probability to each individual based on its
/// rank in the population, rather than its absolute fitness value. This helps prevent
/// premature convergence when there are a few individuals with much higher fitness than
/// the rest of the population.
///
/// This strategy works well for both maximization and minimization problems, and can
/// handle negative fitness values.
///
/// # Examples
///
/// ```
/// use genalg::selection::rank::RankBasedSelection;
/// 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 },
///         MyPhenotype { value: 4.0 },
///         MyPhenotype { value: 5.0 },
///     ];
///     
///     let fitness = vec![0.5, 0.8, 0.3, 0.9, 0.1];
///     
///     // Create a rank-based selection with default parameters
///     let selection = RankBasedSelection::new(1.5, false, true)?;
///     let selected = selection.select(&population, &fitness, 3)?;
///     
///     assert_eq!(selected.len(), 3);
///     
///     Ok(())
/// }
/// ```
#[derive(Debug, Clone)]
pub struct RankBasedSelection {
    /// Higher values increase selection pressure.
    selection_pressure: f64,
    allow_duplicates: bool,
    higher_is_better: bool,
}

impl RankBasedSelection {
    /// Creates a new RankBasedSelection strategy with default parameters.
    ///
    /// By default:
    /// - Selection pressure is set to 1.5 (a balanced middle ground between 1.0 and 2.0)
    /// - Duplicates are not allowed in the selected individuals
    /// - Higher fitness is considered better
    pub fn new(
        selection_pressure: f64,
        allow_duplicates: bool,
        higher_is_better: bool,
    ) -> Result<Self> {
        if !(1.0..=2.0).contains(&selection_pressure) {
            return Err(GeneticError::Configuration(
                "Selection pressure must be in the range [1.0, 2.0]".to_string(),
            ));
        }

        Ok(Self {
            selection_pressure,
            allow_duplicates,
            higher_is_better,
        })
    }

    /// Creates a new RankBasedSelection strategy with the specified selection pressure.
    ///
    /// # Arguments
    ///
    /// * `selection_pressure` - The selection pressure parameter. Must be in the range [1.0, 2.0].
    ///   Higher values increase selection pressure.
    ///   - At 1.0, all individuals have equal selection probability (no selection pressure)
    ///   - At 2.0, selection pressure is at its maximum
    ///   - The default value of 1.5 provides a balanced selection pressure
    ///
    /// # Returns
    ///
    /// A Result containing the RankBasedSelection instance, or an error if the selection pressure
    /// is outside the valid range.
    ///
    /// # Errors
    ///
    /// Returns a `GeneticError::Configuration` error if `selection_pressure` is not in the range [1.0, 2.0].
    pub fn with_pressure(mut self, selection_pressure: f64) -> Result<Self> {
        if !(1.0..=2.0).contains(&selection_pressure) {
            return Err(GeneticError::Configuration(
                "Selection pressure must be in the range [1.0, 2.0]".to_string(),
            ));
        }

        self.selection_pressure = selection_pressure;

        Ok(self)
    }

    pub fn with_duplicates(mut self) -> Self {
        self.allow_duplicates = true;
        self
    }

    pub fn with_lower_is_better(mut self) -> Self {
        self.higher_is_better = false;
        self
    }

    /// Calculates the selection probabilities for each individual based on their rank.
    ///
    /// # Arguments
    ///
    /// * `fitness` - The fitness scores of all individuals.
    ///
    /// # Returns
    ///
    /// A vector of cumulative probabilities for each individual.
    fn calculate_probabilities(&self, fitness: &[f64]) -> Vec<f64> {
        let population_size = fitness.len();

        let mut indices: Vec<usize> = (0..population_size).collect();

        if self.higher_is_better {
            indices.sort_by(|&a, &b| {
                let fa = fitness[a];
                let fb = fitness[b];

                if fa.is_nan() {
                    return std::cmp::Ordering::Less;
                }
                if fb.is_nan() {
                    return std::cmp::Ordering::Greater;
                }

                fb.partial_cmp(&fa).unwrap_or(std::cmp::Ordering::Equal)
            });
        } else {
            indices.sort_by(|&a, &b| {
                let fa = fitness[a];
                let fb = fitness[b];

                if fa.is_nan() {
                    return std::cmp::Ordering::Greater;
                }
                if fb.is_nan() {
                    return std::cmp::Ordering::Less;
                }

                fa.partial_cmp(&fb).unwrap_or(std::cmp::Ordering::Equal)
            });
        }

        let mut rank_map = vec![0; population_size];
        for (rank, &idx) in indices.iter().enumerate() {
            rank_map[idx] = rank;
        }

        let mut probs: Vec<f64> = Vec::with_capacity(population_size);
        let mut cumulative = 0.0;

        for &rank in rank_map.iter() {
            let rank = rank as f64;
            let prob = (2.0 - self.selection_pressure) / population_size as f64
                + (2.0 * rank * (self.selection_pressure - 1.0))
                    / (population_size as f64 * (population_size as f64 - 1.0));

            cumulative += prob;
            probs.push(cumulative);
        }

        if let Some(last) = probs.last() {
            if *last > 0.0 && (*last - 1.0).abs() > f64::EPSILON {
                let last = *last;
                for prob in &mut probs {
                    *prob /= last;
                }
            }
        }

        if let Some(last) = probs.last_mut() {
            *last = 1.0;
        }

        probs
    }

    /// Selects an individual using rank-based selection.
    ///
    /// # Arguments
    ///
    /// * `cumulative_probs` - The cumulative probabilities for each individual.
    /// * `rng` - A random number generator.
    ///
    /// # Returns
    ///
    /// The index of the selected individual.
    ///
    /// # Errors
    ///
    /// Returns an error if random number generation fails.
    fn select_individual(
        &self,
        cumulative_probs: &[f64],
        rng: &mut RandomNumberGenerator,
    ) -> Result<usize> {
        let uniform = rng.fetch_uniform(0.0, 1.0, 1);
        let r = match uniform.front() {
            Some(val) => *val as f64,
            None => {
                return Err(GeneticError::RandomGeneration(
                    "Failed to generate random value for rank-based selection".to_string(),
                ))
            }
        };

        for (i, &prob) in cumulative_probs.iter().enumerate() {
            if r <= prob {
                return Ok(i);
            }
        }

        Ok(cumulative_probs.len() - 1)
    }
}

impl Default for RankBasedSelection {
    fn default() -> Self {
        Self {
            selection_pressure: 1.5,
            allow_duplicates: false,
            higher_is_better: true,
        }
    }
}

impl<P> SelectionStrategy<P> for RankBasedSelection
where
    P: Phenotype,
{
    fn select(&self, population: &[P], fitness: &[f64], num_to_select: usize) -> Result<Vec<P>> {
        if population.is_empty() {
            return Err(GeneticError::EmptyPopulation);
        }

        if fitness.len() != population.len() {
            return Err(GeneticError::Selection(format!(
                "Fitness vector length ({}) doesn't match population length ({})",
                fitness.len(),
                population.len()
            )));
        }

        let cumulative_probs = self.calculate_probabilities(fitness);

        let mut selected = Vec::with_capacity(num_to_select);
        let mut selected_indices = HashSet::new();
        let mut rng = RandomNumberGenerator::new();

        while selected.len() < num_to_select {
            if !self.allow_duplicates && selected_indices.len() >= population.len() {
                break;
            }

            let idx = self.select_individual(&cumulative_probs, &mut rng)?;

            if self.allow_duplicates || selected_indices.insert(idx) {
                selected.push(population[idx].clone());
            }
        }

        Ok(selected)
    }
}

#[cfg(test)]
mod tests {
    use crate::phenotype::Phenotype;
    use crate::rng::RandomNumberGenerator;

    use super::*;

    #[derive(Clone, Debug)]
    struct TestPhenotype {
        value: f64,
    }

    impl Phenotype for TestPhenotype {
        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;
        }
    }

    #[test]
    fn test_rank_based_selection() {
        let population = vec![
            TestPhenotype { value: 1.0 },
            TestPhenotype { value: 2.0 },
            TestPhenotype { value: 3.0 },
            TestPhenotype { value: 4.0 },
            TestPhenotype { value: 5.0 },
        ];
        let fitness = vec![0.5, 0.8, 0.3, 0.9, 0.1];

        // Test with default parameters
        let selection = RankBasedSelection::default();
        let selected = selection.select(&population, &fitness, 3).unwrap();
        assert_eq!(selected.len(), 3);

        // Test with custom selection pressure
        let selection = RankBasedSelection::default().with_pressure(1.8).unwrap();
        let selected = selection.select(&population, &fitness, 3).unwrap();
        assert_eq!(selected.len(), 3);

        // Test with duplicates allowed
        let selection = RankBasedSelection::default().with_duplicates();
        let selected = selection.select(&population, &fitness, 10).unwrap();
        assert_eq!(selected.len(), 10);

        // Test with lower is better and no duplicates
        // When duplicates are not allowed, we can only select up to population.len() individuals
        let selection = RankBasedSelection::new(1.2, false, true).unwrap();
        let selected = selection.select(&population, &fitness, 10).unwrap();
        assert_eq!(selected.len(), 5); // Only 5 unique individuals are available
    }

    #[test]
    fn test_rank_based_selection_empty_population() {
        let population: Vec<TestPhenotype> = Vec::new();
        let fitness: Vec<f64> = Vec::new();

        let selection = RankBasedSelection::default();
        let result = selection.select(&population, &fitness, 3);
        assert!(result.is_err());
    }

    #[test]
    fn test_rank_based_selection_mismatched_lengths() {
        let population = vec![TestPhenotype { value: 1.0 }, TestPhenotype { value: 2.0 }];
        let fitness = vec![0.5];

        let selection = RankBasedSelection::default();
        let result = selection.select(&population, &fitness, 1);
        assert!(result.is_err());
    }

    #[test]
    fn test_rank_based_selection_with_nan() {
        let population = vec![
            TestPhenotype { value: 1.0 },
            TestPhenotype { value: 2.0 },
            TestPhenotype { value: 3.0 },
        ];
        let fitness = vec![0.5, f64::NAN, 0.3];

        let selection = RankBasedSelection::default();
        let selected = selection.select(&population, &fitness, 2).unwrap();
        assert_eq!(selected.len(), 2);
    }

    #[test]
    fn test_rank_based_selection_minimization() {
        let population = vec![
            TestPhenotype { value: 1.0 },
            TestPhenotype { value: 2.0 },
            TestPhenotype { value: 3.0 },
            TestPhenotype { value: 4.0 },
            TestPhenotype { value: 5.0 },
        ];
        // Lower values are better in this test
        let fitness = vec![0.5, 0.8, 0.3, 0.9, 0.1];

        // Test with minimization (lower is better)
        let selection = RankBasedSelection::new(1.5, false, false).unwrap();
        let selected = selection.select(&population, &fitness, 3).unwrap();
        assert_eq!(selected.len(), 3);
    }

    #[test]
    fn test_invalid_selection_pressure() {
        // Test with selection pressure below minimum
        let result = RankBasedSelection::default().with_pressure(0.5);
        assert!(result.is_err());

        // Test with selection pressure above maximum
        let result = RankBasedSelection::default().with_pressure(2.5);
        assert!(result.is_err());

        // Test with options and invalid selection pressure
        let result = RankBasedSelection::new(2.5, true, false);
        assert!(result.is_err());
    }
}