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
use std::time::Instant;
use jobsteal::make_pool;
use individual::{Individual, IndividualWrapper};
use population::Population;
#[derive(Debug,Clone)]
pub enum SimulationType {
EndIteration(u32),
EndFitness(f64),
EndFactor(f64),
}
pub struct Simulation<T: Individual + Send + Sync> {
pub type_of_simulation: SimulationType,
pub num_of_threads: usize,
pub habitat: Vec<Population<T>>,
pub total_time_in_ms: f64,
pub simulation_result: SimulationResult<T>,
pub share_fittest: bool
}
#[derive(Clone)]
pub struct SimulationResult<T: Individual + Send + Sync> {
pub improvement_factor: f64,
pub original_fitness: f64,
pub fittest: Vec<IndividualWrapper<T>>,
pub iteration_counter: u32
}
impl<T: Individual + Send + Sync + Clone> Simulation<T> {
pub fn run(&mut self) {
let start_time = Instant::now();
for population in &mut self.habitat {
population.calculate_fitness();
}
let mut iteration_counter = 0;
let mut pool = make_pool(self.num_of_threads).unwrap();
self.simulation_result = SimulationResult {
improvement_factor: 0.0,
original_fitness: self.habitat[0].population[0].fitness,
fittest: vec![self.habitat[0].population[0].clone()],
iteration_counter: 0
};
info!("original_fitness: {}", self.simulation_result.original_fitness);
match self.type_of_simulation {
SimulationType::EndIteration(end_iteration) => {
for _ in 0..end_iteration {
pool.scope(|scope|
for population in &mut self.habitat {
scope.submit(move || { population.run_body() });
});
self.update_results();
};
self.simulation_result.iteration_counter = end_iteration;
}
SimulationType::EndFactor(end_factor) => {
loop {
iteration_counter += 1;
pool.scope(|scope|
for population in &mut self.habitat {
scope.submit(move || { population.run_body() });
});
self.update_results();
if self.simulation_result.improvement_factor <= end_factor {
break;
}
};
self.simulation_result.iteration_counter = iteration_counter;
}
SimulationType::EndFitness(end_fitness) => {
loop {
iteration_counter += 1;
pool.scope(|scope|
for population in &mut self.habitat {
scope.submit(move || { population.run_body() });
});
self.update_results();
if self.simulation_result.fittest[0].fitness <= end_fitness {
break;
}
};
self.simulation_result.iteration_counter = iteration_counter;
}
}
let elapsed = start_time.elapsed();
self.total_time_in_ms = elapsed.as_secs() as f64 * 1000.0 + elapsed.subsec_nanos() as f64 / 1000_000.0;
}
pub fn print_fitness(&self) {
for wrapper in &self.simulation_result.fittest {
info!("fitness: {}, num_of_mutations: {}, population: {}",
wrapper.fitness, wrapper.num_of_mutations, wrapper.id);
}
}
fn update_results(&mut self) {
let mut new_fittest_found = false;
for population in &mut self.habitat {
if population.population[0].fitness < self.simulation_result.fittest[0].fitness {
new_fittest_found = true;
self.simulation_result.fittest.insert(0, population.population[0].clone());
self.simulation_result.fittest.truncate(10);
population.fitness_counter += 1;
info!("new fittest: fitness: {}, population id: {}, counter: {}", population.population[0].fitness,
population.id, population.fitness_counter);
population.population[0].individual.new_fittest_found();
}
}
if self.share_fittest && new_fittest_found {
for population in &mut self.habitat {
population.population[0] = self.simulation_result.fittest[0].clone();
}
}
self.simulation_result.improvement_factor =
self.simulation_result.fittest[0].fitness /
self.simulation_result.original_fitness;
}
}