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
use std::cmp::Ordering;
use itertools::Itertools;
use polytype::Type;
use rand::{seq, Rng};
use Task;
pub struct GPParams {
pub population_size: usize,
pub tournament_size: usize,
pub mutation_prob: f64,
pub n_delta: usize,
}
pub trait GP: Send + Sync + Sized {
type Expression: Clone + Send;
type Params;
fn genesis<R: Rng>(
&self,
params: &Self::Params,
rng: &mut R,
pop_size: usize,
tp: &Type,
) -> Vec<Self::Expression>;
fn mutate<R: Rng>(
&self,
params: &Self::Params,
rng: &mut R,
prog: &Self::Expression,
) -> Self::Expression;
fn crossover<R: Rng>(
&self,
params: &Self::Params,
rng: &mut R,
parent1: &Self::Expression,
parent2: &Self::Expression,
) -> Vec<Self::Expression>;
fn tournament<'a, R: Rng>(
&self,
rng: &mut R,
tournament_size: usize,
population: &'a [(Self::Expression, f64)],
) -> &'a Self::Expression {
seq::sample_iter(rng, 0..population.len(), tournament_size)
.expect("tournament size was bigger than population")
.into_iter()
.map(|i| &population[i])
.max_by(|&&(_, ref x), &&(_, ref y)| x.partial_cmp(y).expect("found NaN"))
.map(|&(ref expr, _)| expr)
.expect("tournament cannot select winner from no contestants")
}
fn init<R: Rng, O: Sync>(
&self,
params: &Self::Params,
rng: &mut R,
gpparams: &GPParams,
task: &Task<Self, Self::Expression, O>,
) -> Vec<(Self::Expression, f64)> {
let exprs = self.genesis(params, rng, gpparams.population_size, &task.tp);
exprs
.into_iter()
.map(|expr| {
let l = (task.oracle)(self, &expr);
(expr, l)
})
.sorted_by(|&(_, ref x), &(_, ref y)| x.partial_cmp(y).expect("found NaN"))
}
fn evolve<R: Rng, O: Sync>(
&self,
params: &Self::Params,
rng: &mut R,
gpparams: &GPParams,
task: &Task<Self, Self::Expression, O>,
population: &mut Vec<(Self::Expression, f64)>,
) {
let mut new_exprs = Vec::with_capacity(gpparams.n_delta);
while new_exprs.len() < gpparams.n_delta {
if rng.gen_range(0f64, 1f64) < gpparams.mutation_prob {
let parent = self.tournament(rng, gpparams.tournament_size, population);
let child = self.mutate(params, rng, parent);
let fitness = (task.oracle)(self, &child);
new_exprs.push((child, fitness));
} else {
let parent1 = self.tournament(rng, gpparams.tournament_size, population);
let parent2 = self.tournament(rng, gpparams.tournament_size, population);
let children = self.crossover(params, rng, parent1, parent2);
let mut scored_children = children
.into_iter()
.map(|child| {
let fitness = (task.oracle)(self, &child);
(child, fitness)
})
.collect();
new_exprs.append(&mut scored_children);
}
}
new_exprs.truncate(gpparams.n_delta);
for child in new_exprs {
sorted_place(child, population)
}
}
fn init_and_evolve<R: Rng, O: Sync>(
&self,
params: &Self::Params,
rng: &mut R,
gpparams: &GPParams,
task: &Task<Self, Self::Expression, O>,
generations: u32,
) -> Vec<(Self::Expression, f64)> {
let mut pop = self.init(params, rng, gpparams, task);
for _ in 0..generations {
self.evolve(params, rng, gpparams, task, &mut pop)
}
pop
}
}
fn sorted_place<T>(child: (T, f64), pop: &mut Vec<(T, f64)>) {
let mut size = pop.len();
let idx = {
if size == 0 {
0
} else {
let mut base = 0usize;
while size > 1 {
let half = size / 2;
let mid = base + half;
let other = unsafe { pop.get_unchecked(mid) };
let cmp = other.1.partial_cmp(&child.1).expect("found NaN");
base = if cmp == Ordering::Greater { base } else { mid };
size -= half;
}
let other = unsafe { pop.get_unchecked(base) };
let cmp = other.1.partial_cmp(&child.1).expect("found NaN");
if cmp == Ordering::Equal {
base
} else {
base + (cmp == Ordering::Less) as usize
}
}
};
pop[idx] = child;
}