radiate_rust/engines/alterers/
alter.rs

1use crate::engines::genome::genes::gene::Gene;
2use crate::engines::genome::population::Population;
3use crate::engines::optimize::Optimize;
4
5use super::crossovers::crossover::Crossover;
6use super::mutators::mutate::Mutate;
7
8pub trait Alter<G, A>
9where
10    G: Gene<G, A>
11{
12    fn alter(&self, population: &mut Population<G, A>, optimize: &Optimize, generation: i32);
13}
14
15
16pub struct AlterWrap<G, A> 
17where
18    G: Gene<G, A>
19{
20    pub rate: f32,
21    pub mutator: Option<Box<dyn Mutate<G, A>>>,
22    pub crossover: Option<Box<dyn Crossover<G, A>>>,
23    pub alterer: Option<Box<dyn Alter<G, A>>>,
24}
25
26
27pub enum Alterer<G, A> 
28where
29    G: Gene<G, A>
30{
31    Mutator(f32),
32    UniformCrossover(f32),
33    MultiPointCrossover(f32, usize),
34    SinglePointCrossover(f32),
35    SwapMutator(f32),
36    Mutation(Box<dyn Mutate<G, A>>),
37    Crossover(Box<dyn Crossover<G, A>>),
38    Alterer(Box<dyn Alter<G, A>>)
39}
40
41impl<G, A> Alterer<G, A>
42where
43    G: Gene<G, A>
44{
45    pub fn alterer<T>(alterer: T) -> Self
46    where
47        T: Alter<G, A> + 'static
48     {
49        Alterer::Alterer(Box::new(alterer))
50    }
51
52    pub fn crossover<T>(crossover: T) -> Self
53    where
54        T: Crossover<G, A> + 'static
55    {
56        Alterer::Crossover(Box::new(crossover))
57    }
58
59    pub fn mutation<T>(mutation: T) -> Self
60    where
61        T: Mutate<G, A> + 'static
62    {
63        Alterer::Mutation(Box::new(mutation))
64    }
65}