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
pub mod genome;
pub mod network;
pub mod population;
mod util;
use std::collections::HashMap;
pub struct NeatParams {
pub population_size: usize,
pub delta_c1: f64,
pub delta_c2: f64,
pub delta_c3: f64,
pub delta_threshold: f64,
pub species_death_time: usize,
pub champion_copy_size: usize,
pub weight_mutation_chance: f64,
pub weight_reset_chance: f64,
pub disable_chance: f64,
pub no_crossover_chance: f64,
pub interspecies_mating_rate: f64,
pub new_neuron_chance: f64,
pub new_weight_chance: f64,
pub survival_threshold: f64,
}
impl Default for NeatParams {
fn default() -> Self {
Self {
population_size: 150,
delta_c1: 1.0,
delta_c2: 1.0,
delta_c3: 1.0,
delta_threshold: 3.0,
species_death_time: 15,
champion_copy_size: 5,
weight_mutation_chance: 0.8,
weight_reset_chance: 0.1,
disable_chance: 0.75,
no_crossover_chance: 0.25,
interspecies_mating_rate: 0.05,
new_neuron_chance: 0.08,
new_weight_chance: 0.1,
survival_threshold: 0.4,
}
}
}
pub struct Neat {
params: NeatParams,
innovations: HashMap<(usize, usize), usize>,
innovation: usize,
}
impl Neat {
pub fn new() -> Self {
Self::from_params(NeatParams::default())
}
pub fn from_params(params: NeatParams) -> Neat {
Neat {
params,
innovations: HashMap::new(),
innovation: 0,
}
}
fn innovation(&mut self, input: usize, output: usize) -> usize {
match self.innovations.get(&(input, output)) {
Some(result) => *result,
None => {
let result = self.innovation;
self.innovations.insert((input, output), result);
self.innovation += 1;
result
}
}
}
pub fn population_size(&self) -> usize {
self.params.population_size
}
pub fn delta_c1(&self) -> f64 {
self.params.delta_c1
}
pub fn delta_c2(&self) -> f64 {
self.params.delta_c2
}
pub fn delta_c3(&self) -> f64 {
self.params.delta_c3
}
pub fn delta_threshold(&self) -> f64 {
self.params.delta_threshold
}
pub fn species_death_time(&self) -> usize {
self.params.species_death_time
}
pub fn champion_copy_size(&self) -> usize {
self.params.champion_copy_size
}
pub fn weight_mutation_chance(&self) -> f64 {
self.params.weight_mutation_chance
}
pub fn weight_reset_chance(&self) -> f64 {
self.params.weight_reset_chance
}
pub fn disable_chance(&self) -> f64 {
self.params.disable_chance
}
pub fn no_crossover_chance(&self) -> f64 {
self.params.no_crossover_chance
}
pub fn interspecies_mating_rate(&self) -> f64 {
self.params.interspecies_mating_rate
}
pub fn new_neuron_chance(&self) -> f64 {
self.params.new_neuron_chance
}
pub fn new_weight_chance(&self) -> f64 {
self.params.new_weight_chance
}
pub fn survival_threshold(&self) -> f64 {
self.params.survival_threshold
}
}
#[cfg(test)]
mod tests {}