rust_monster/ga/
ga_simple.rs1use super::ga_core::{GAConfig, GAFactory, GAFlags, GeneticAlgorithm, GASolution};
3use super::ga_population::GAPopulation;
4use super::ga_random::{GARandomCtx, GASeed};
5
6#[derive(Copy, Clone, Default, Debug)]
8pub struct SimpleGeneticAlgorithmCfg
10{
11 pub d_seed : GASeed,
12 pub pconv : f32,
13 pub is_min : bool,
14
15 pub max_generations : i32,
17 pub flags : GAFlags,
18 pub probability_crossover : f32,
19 pub probability_mutation : f32,
20
21 pub elitism : bool,
23}
24impl GAConfig for SimpleGeneticAlgorithmCfg
25{
26 fn flags(&self) -> GAFlags
27 {
28 self.flags
29 }
30 fn max_generations(&self) -> i32
31 {
32 self.max_generations
33 }
34 fn probability_crossover(&self) -> f32
35 {
36 self.probability_crossover
37 }
38 fn probability_mutation(&self) -> f32
39 {
40 self.probability_mutation
41 }
42}
43impl SimpleGeneticAlgorithmCfg
44{
45 fn elitism(&self) -> bool
46 {
47 self.elitism
48 }
49}
50
51pub struct SimpleGeneticAlgorithm<T: GASolution>
72{
73 current_generation : i32,
74 config : SimpleGeneticAlgorithmCfg,
75 population : GAPopulation<T>,
76 rng_ctx : GARandomCtx,
77}
78impl<T: GASolution> SimpleGeneticAlgorithm<T>
79{
80 pub fn new(cfg: SimpleGeneticAlgorithmCfg,
83 factory: Option<&mut GAFactory<T>>,
84 population: Option<GAPopulation<T>>) -> SimpleGeneticAlgorithm<T>
85 {
86 let p : GAPopulation<T>;
87 match factory
88 {
89 Some(f) => {
90 p = f.initial_population();
91 },
92 None => {
93 match population
94 {
95 Some(p_) =>
96 {
97 p = p_;
98 },
99 None =>
100 {
101 panic!("Simple Genetic Algorithm - either factory or population need to be provided");
102 }
103 }
104 }
105 }
106
107 SimpleGeneticAlgorithm { current_generation: 0, config : cfg, population : p, rng_ctx : GARandomCtx::from_seed(cfg.d_seed, String::from("")) }
109 }
110}
111impl<T: GASolution> GeneticAlgorithm<T> for SimpleGeneticAlgorithm <T>
112{
113 fn config(&mut self) -> &GAConfig
114 {
115 &self.config
116 }
117
118 fn population(&mut self) -> &mut GAPopulation<T>
119 {
120 &mut self.population
121 }
122
123 fn initialize_internal(&mut self)
124 {
125 assert!(self.population().size() > 0);
126 self.population.sort();
127 }
128
129 fn step_internal(&mut self) -> i32
130 {
131 let mut new_individuals : Vec<T> = vec![];
132
133 for _ in 0..self.population.size()
135 {
136 let ind = self.population.select();
137 let mut new_ind = ind.clone();
138 if self.rng_ctx.test_value(self.config.probability_crossover())
139 {
140 let ind_2 = self.population.select();
141 new_ind = ind.crossover(ind_2);
142 }
143
144 new_ind.mutate(self.config.probability_mutation());
145
146 new_individuals.push(new_ind);
147 }
148
149 self.population.evaluate();
152 self.population.sort();
153
154 let best_old_individual = self.population.best().clone();
155
156 if self.config.elitism()
157 {
158 if best_old_individual.fitness() > self.population.worst().fitness()
159 {
160 }
162 }
163
164 self.current_generation += 1;
165 self.current_generation
166 }
167
168 fn done_internal(&mut self) -> bool
169 {
170 self.current_generation >= self.config().max_generations()
171 }
172}