-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexplore_strategies.rs
143 lines (127 loc) · 4.49 KB
/
explore_strategies.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
use genetic_algorithm::fitness::placeholders::CountTrue;
use genetic_algorithm::strategy::prelude::*;
fn main() {
env_logger::init();
// small enought for permutate
let genotype = BinaryGenotype::builder()
.with_genes_size(16)
.build()
.unwrap();
let builder = StrategyBuilder::new()
.with_genotype(genotype)
.with_target_population_size(20)
.with_max_stale_generations(20)
// .with_target_fitness_score(16) // short-circuits
.with_max_stale_generations(10)
.with_fitness(CountTrue)
.with_mutate(MutateSingleGene::new(0.2))
.with_crossover(CrossoverClone::new())
.with_select(SelectTournament::new(4, 0.9))
.with_reporter(IterationReporter);
let strategies = [
StrategyVariant::Permutate(PermutateVariant::Standard),
StrategyVariant::Evolve(EvolveVariant::Standard),
StrategyVariant::HillClimb(HillClimbVariant::Stochastic),
StrategyVariant::HillClimb(HillClimbVariant::SteepestAscent),
];
strategies.iter().copied().for_each(|variant| {
println!("call: {}", variant);
let strategy = builder.clone().with_variant(variant).call().unwrap();
if let Some(fitness_score) = strategy.best_fitness_score() {
println!(" fitness score: {}", fitness_score);
} else {
println!(" invalid solution with fitness score: None");
}
});
strategies.iter().copied().for_each(|variant| {
println!("call_repeatedly(3): {}", variant);
let (mut strategy, mut others) = builder
.clone()
.with_variant(variant)
.call_repeatedly(3)
.unwrap();
let other_fitness_scores = others
.iter()
.map(|s| s.best_fitness_score())
.collect::<Vec<_>>();
let mut buffer = Vec::<u8>::new();
strategy.flush_reporter(&mut buffer);
others
.iter_mut()
.for_each(|s| s.flush_reporter(&mut buffer));
println!(
" all reporter buffers: {}",
String::from_utf8(buffer).unwrap_or_default()
);
if let Some(fitness_score) = strategy.best_fitness_score() {
println!(
" best fitness score: {}, others: {:?}",
fitness_score, other_fitness_scores
);
} else {
println!(" invalid solution with fitness score: None");
}
});
strategies.iter().copied().for_each(|variant| {
println!("call_par_speciated(3): {}", variant);
let (mut strategy, mut others) = builder
.clone()
.with_variant(variant)
.call_par_speciated(3)
.unwrap();
let other_fitness_scores = others
.iter()
.map(|s| s.best_fitness_score())
.collect::<Vec<_>>();
let mut buffer = Vec::<u8>::new();
strategy.flush_reporter(&mut buffer);
others
.iter_mut()
.for_each(|s| s.flush_reporter(&mut buffer));
println!(
" all reporter buffers: {}",
String::from_utf8(buffer).unwrap_or_default()
);
if let Some(fitness_score) = strategy.best_fitness_score() {
println!(
" best fitness score: {}, others: {:?}",
fitness_score, other_fitness_scores
);
} else {
println!(" invalid solution with fitness score: None");
}
});
}
#[derive(Clone)]
pub struct IterationReporter;
impl StrategyReporter for IterationReporter {
type Genotype = BinaryGenotype;
fn flush(&mut self, output: &mut Vec<u8>) {
output.push(b'.')
}
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());
}
}