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
use crate::GeneticEngineBuilder;
use radiate_core::{Alterer, Chromosome, Crossover, Mutate};
use radiate_utils::intern;
impl<C, T> GeneticEngineBuilder<C, T>
where
C: Chromosome + PartialEq + Clone,
T: Clone + Send,
{
/// Set the alterer of the genetic engine. This is the alterer that will be used to
/// alter the offspring of the population. The alterer is used to apply mutations
/// and crossover operations to the offspring and will be used to create the next
/// generation of the population. **Note**: the order of the alterers is important - the
/// alterers will be applied in the order they are provided.
pub fn alter(mut self, alterers: Vec<Alterer<C>>) -> Self {
self.params.alterers = alterers.into_iter().map(|alt| alt.into()).collect();
self
}
/// Define a single mutator for the genetic engine - this will be converted to
/// a `Box<dyn Alter<C>>` and added to the list of alterers. Note: The order in which
/// mutators and crossovers are added is the order in which they will be applied during
/// the evolution process.
pub fn mutator<M: Mutate<C> + 'static>(mut self, mutator: M) -> Self {
self.params.alterers.push(mutator.alterer());
self
}
/// Define a list of mutators for the genetic engine - this will be converted to a list
/// of `Box<dyn Alter<C>>` and added to the list of alterers. Just like adding a single mutator,
/// the order in which mutators and crossovers are added is the order in which they will be applied
/// during the evolution process.s
pub fn mutators(mut self, mutators: Vec<Box<dyn Mutate<C>>>) -> Self {
let mutate_actions = mutators
.into_iter()
.map(|m| Alterer::Mutate(intern!(m.name()), m.rate(), m.into()))
.collect::<Vec<_>>();
self.params.alterers.extend(mutate_actions);
self
}
/// Define a single crossover for the genetic engine - this will be converted to
/// a `Box<dyn Alter<C>>` and added to the list of alterers. Note: The order in which
/// mutators and crossovers are added is the order in which they will be applied during
/// the evolution process.s
pub fn crossover<R: Crossover<C> + 'static>(mut self, crossover: R) -> Self {
self.params.alterers.push(crossover.alterer());
self
}
/// Define a list of crossovers for the genetic engine - this will be converted to a list
/// of `Box<dyn Alter<C>>` and added to the list of alterers. Just like adding a single crossover,
/// the order in which mutators and crossovers are added is the order in which they will be applied
/// during the evolution process.
pub fn crossovers(mut self, crossovers: Vec<Box<dyn Crossover<C>>>) -> Self {
let crossover_actions = crossovers
.into_iter()
.map(|c| Alterer::Crossover(c.name().leak(), c.rate(), c.into()))
.collect::<Vec<_>>();
self.params.alterers.extend(crossover_actions);
self
}
}