-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexplore_multithreading.rs
258 lines (229 loc) · 8.28 KB
/
explore_multithreading.rs
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
use genetic_algorithm::fitness::placeholders::CountTrueWithSleep;
use genetic_algorithm::strategy::evolve::prelude::*;
use genetic_algorithm::strategy::hill_climb::prelude::*;
use genetic_algorithm::strategy::permutate::prelude::*;
// const INTERNAL_MULTITHREAD: bool = false;
const INTERNAL_MULTITHREAD: bool = true;
// const EXTERNAL_MULTITHREAD: bool = false;
const EXTERNAL_MULTITHREAD: bool = true;
fn main() {
env_logger::init();
call_evolve();
call_hill_climb();
call_permutate();
call_evolve_repeatedly();
call_hill_climb_repeatedly();
call_evolve_speciated();
}
#[allow(dead_code)]
fn call_evolve() {
let genotype = BinaryGenotype::builder()
.with_genes_size(100)
.build()
.unwrap();
println!("evolve: start");
let mut evolve = Evolve::builder()
.with_genotype(genotype.clone())
.with_target_population_size(100)
.with_max_stale_generations(100)
.with_target_fitness_score(100)
.with_fitness(CountTrueWithSleep::new(1000, true))
.with_par_fitness(INTERNAL_MULTITHREAD)
.with_mutate(MutateSingleGene::new(0.2))
.with_crossover(CrossoverClone::new())
.with_select(SelectTournament::new(4, 0.9))
// .with_reporter(EvolveReporterSimple::new(1000))
.build()
.unwrap();
let now = std::time::Instant::now();
evolve.call();
let duration = now.elapsed();
if let Some(fitness_score) = evolve.best_fitness_score() {
println!("evolve fitness score: {}", fitness_score);
} else {
println!("evolve invalid solution with fitness score: None");
}
println!("evolve: {:?}", duration);
println!();
}
#[allow(dead_code)]
fn call_hill_climb() {
let genotype = BinaryGenotype::builder()
.with_genes_size(100)
.build()
.unwrap();
println!("hill_climb: start");
let mut hill_climb = HillClimb::builder()
.with_genotype(genotype.clone())
.with_variant(HillClimbVariant::SteepestAscent)
.with_max_stale_generations(1)
.with_target_fitness_score(100)
.with_fitness(CountTrueWithSleep::new(1000, true))
.with_par_fitness(INTERNAL_MULTITHREAD)
// .with_reporter(HillClimbReporterSimple::new(1000))
.build()
.unwrap();
let now = std::time::Instant::now();
hill_climb.call();
let duration = now.elapsed();
if let Some(fitness_score) = hill_climb.best_fitness_score() {
println!("hill_climb fitness score: {}", fitness_score);
} else {
println!("hill_climb invalid solution with fitness score: None");
}
println!("hill_climb: {:?}", duration);
println!();
}
#[allow(dead_code)]
fn call_permutate() {
let genotype = BinaryGenotype::builder()
.with_genes_size(12)
.build()
.unwrap();
println!("permutate: start");
let mut permutate = Permutate::builder()
.with_genotype(genotype.clone())
.with_fitness(CountTrueWithSleep::new(1000, true))
.with_par_fitness(INTERNAL_MULTITHREAD)
// .with_reporter(PermutateReporterSimple::new(1000))
.build()
.unwrap();
let now = std::time::Instant::now();
permutate.call();
let duration = now.elapsed();
if let Some(fitness_score) = permutate.best_fitness_score() {
println!("permutate fitness score: {}", fitness_score);
} else {
println!("permutate invalid solution with fitness score: None");
}
println!("permutate: {:?}", duration);
println!();
}
#[allow(dead_code)]
fn call_evolve_repeatedly() {
let genotype = BinaryGenotype::builder()
.with_genes_size(100)
.build()
.unwrap();
println!("evolve_repeatedly: start");
let evolve_builder = Evolve::builder()
.with_genotype(genotype.clone())
.with_target_population_size(100)
.with_max_stale_generations(100)
// .with_target_fitness_score(100) // short-circuit
.with_fitness(CountTrueWithSleep::new(1000, false))
.with_mutate(MutateSingleGene::new(0.2))
.with_crossover(CrossoverClone::new())
.with_select(SelectTournament::new(4, 0.9))
.with_reporter(IterationReporter)
.with_par_fitness(INTERNAL_MULTITHREAD);
let now = std::time::Instant::now();
let (evolve, _) = if EXTERNAL_MULTITHREAD {
evolve_builder.call_par_repeatedly(20).unwrap()
} else {
evolve_builder.call_repeatedly(3).unwrap()
};
let duration = now.elapsed();
if let Some(fitness_score) = evolve.best_fitness_score() {
println!("evolve_repeatedly fitness score: {}", fitness_score);
} else {
println!("evolve_repeatedly invalid solution with fitness score: None");
}
println!("evolve_repeatedly: {:?}", duration);
println!();
}
#[allow(dead_code)]
fn call_evolve_speciated() {
let genotype = BinaryGenotype::builder()
.with_genes_size(100)
.build()
.unwrap();
println!("evolve_speciated: start");
let evolve_builder = Evolve::builder()
.with_genotype(genotype.clone())
.with_target_population_size(100)
.with_max_stale_generations(100)
// .with_target_fitness_score(100) // short-circuit
.with_fitness(CountTrueWithSleep::new(1000, false))
.with_mutate(MutateSingleGene::new(0.2))
.with_crossover(CrossoverClone::new())
.with_select(SelectTournament::new(4, 0.9))
.with_reporter(IterationReporter)
.with_par_fitness(INTERNAL_MULTITHREAD);
let now = std::time::Instant::now();
let (evolve, _) = if EXTERNAL_MULTITHREAD {
evolve_builder.call_par_speciated(20).unwrap()
} else {
evolve_builder.call_speciated(3).unwrap()
};
let duration = now.elapsed();
if let Some(fitness_score) = evolve.best_fitness_score() {
println!("evolve_speciated fitness score: {}", fitness_score);
} else {
println!("evolve_speciated invalid solution with fitness score: None");
}
println!("evolve_speciated: {:?}", duration);
println!();
}
#[allow(dead_code)]
fn call_hill_climb_repeatedly() {
let genotype = BinaryGenotype::builder()
.with_genes_size(100)
.build()
.unwrap();
println!("hill_climb_repeatedly: start");
let hill_climb_builder = HillClimb::builder()
.with_genotype(genotype.clone())
// .with_variant(HillClimbVariant::SteepestAscent) // internal multi-threading
// .with_max_stale_generations(1)
.with_variant(HillClimbVariant::Stochastic) // no internal multi-threading due to sequential nature
.with_max_stale_generations(1000)
// .with_target_fitness_score(100) // short-circuit
.with_fitness(CountTrueWithSleep::new(1000, false))
.with_reporter(IterationReporter)
.with_par_fitness(INTERNAL_MULTITHREAD);
let now = std::time::Instant::now();
let (hill_climb, _) = if EXTERNAL_MULTITHREAD {
hill_climb_builder.call_par_repeatedly(20).unwrap()
} else {
hill_climb_builder.call_repeatedly(3).unwrap()
};
let duration = now.elapsed();
if let Some(fitness_score) = hill_climb.best_fitness_score() {
println!("hill_climb_repeatedly fitness score: {}", fitness_score);
} else {
println!("hill_climb_repeatedly invalid solution with fitness score: None");
}
println!("hill_climb_repeatedly: {:?}", duration);
println!();
}
#[derive(Clone)]
pub struct IterationReporter;
impl StrategyReporter for IterationReporter {
type Genotype = BinaryGenotype;
fn on_start<S: StrategyState<Self::Genotype>, C: StrategyConfig>(
&mut self,
genotype: &Self::Genotype,
state: &S,
_config: &C,
) {
let number_of_seed_genes = genotype.seed_genes_list().len();
if number_of_seed_genes > 0 {
println!(
" start - iteration: {}, number of seed genes: {:?}",
state.current_iteration(),
number_of_seed_genes
);
} else {
println!(" start - iteration: {}", state.current_iteration());
}
}
fn on_finish<S: StrategyState<Self::Genotype>, C: StrategyConfig>(
&mut self,
_genotype: &Self::Genotype,
state: &S,
_config: &C,
) {
println!(" finish - iteration: {}", state.current_iteration());
}
}