Skip to main content

optlib/genetic/selection/
vec_float.rs

1//! The module with selection algorithms for type chromosomes of Vec<Float>.
2
3use num::Float;
4
5use crate::genetic::{Population, Selection};
6
7/// Kill individuals if theirs gene does not lie in the specified intevals.
8///
9/// `G` - type of gene.
10/// Returns count of the killed individuals.
11pub struct CheckChromoInterval<G: Float> {
12    intervals: Vec<(G, G)>,
13}
14
15impl<G: Float> CheckChromoInterval<G> {
16    /// Constructor.
17    ///
18    /// # Parameters
19    /// * `intervals` - allowed interval for every gene. Count of the genes and count of the
20    /// interval must be equal.
21    pub fn new(intervals: Vec<(G, G)>) -> Self {
22        Self { intervals }
23    }
24}
25
26impl<G: Float> Selection<Vec<G>> for CheckChromoInterval<G> {
27    fn kill(&mut self, population: &mut Population<Vec<G>>) {
28        for individual in population.iter_mut() {
29            assert_eq!(individual.get_chromosomes().len(), self.intervals.len());
30
31            for (chromo, interval) in individual
32                .get_chromosomes()
33                .iter()
34                .zip(self.intervals.iter())
35            {
36                if !chromo.is_finite() || *chromo < interval.0 || *chromo > interval.1 {
37                    individual.kill();
38                    break;
39                }
40            }
41        }
42    }
43}