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
use std;
use individual::{Individual, IndividualWrapper};
use population::Population;
pub struct PopulationBuilder<T: Individual> {
population: Population<T>,
}
quick_error! {
#[derive(Debug)]
pub enum PopError {
IndividualsTooLow {}
LimitEndTooLow {}
}
}
pub type Result<T> = std::result::Result<Population<T>, PopError>;
impl<T: Individual + Clone> PopulationBuilder<T> {
pub fn new() -> PopulationBuilder<T> {
PopulationBuilder {
population: Population {
num_of_individuals: 0,
population: Vec::new(),
reset_limit: 0,
reset_limit_start: 1000,
reset_limit_end: 10000,
reset_limit_increment: 1000,
reset_counter: 0,
id: 1,
fitness_counter: 0
}
}
}
pub fn initial_population(mut self, individuals: &[T]) -> PopulationBuilder<T> {
self.population.num_of_individuals = individuals.len() as u32;
for individual in individuals {
self.population.population.push(IndividualWrapper {
individual: (*individual).clone(),
fitness: std::f64::MAX,
num_of_mutations: 1,
id: self.population.id,
});
}
self
}
pub fn increasing_mutation_rate(mut self) -> PopulationBuilder<T> {
let mut mutation_rate = 1;
for wrapper in &mut self.population.population {
wrapper.num_of_mutations = mutation_rate;
mutation_rate += 1;
}
self
}
pub fn increasing_exp_mutation_rate(mut self, base: f64) -> PopulationBuilder<T> {
let mut mutation_rate = 1;
for wrapper in &mut self.population.population {
wrapper.num_of_mutations = base.powi(mutation_rate).floor() as u32;
mutation_rate += 1;
}
self
}
pub fn mutation_rate(mut self, mutation_rate: Vec<u32>) -> PopulationBuilder<T> {
assert!(self.population.population.len() == mutation_rate.len());
for (individual, mutation_rate) in self.population
.population
.iter_mut()
.zip(mutation_rate.into_iter()) {
individual.num_of_mutations = mutation_rate;
}
self
}
pub fn reset_limit_start(mut self, reset_limit_start: u32) -> PopulationBuilder<T> {
self.population.reset_limit_start = reset_limit_start;
self.population.reset_limit = reset_limit_start;
self
}
pub fn reset_limit_end(mut self, reset_limit_end: u32) -> PopulationBuilder<T> {
self.population.reset_limit_end = reset_limit_end;
self
}
pub fn reset_limit_increment(mut self, reset_limit_increment: u32) -> PopulationBuilder<T> {
self.population.reset_limit_increment = reset_limit_increment;
self
}
pub fn set_id(mut self, id: u32) -> PopulationBuilder<T> {
for individual in &mut self.population.population {
individual.id = id;
}
self.population.id = id;
self
}
pub fn finalize(self) -> Result<T> {
match self.population {
Population { num_of_individuals: 0...2, ..} => {
Err(PopError::IndividualsTooLow)
}
Population { reset_limit_start: start,
reset_limit_end: end, ..} if (end > 0) && (start >= end) => {
Err(PopError::LimitEndTooLow)
}
_ => Ok(self.population)
}
}
}