-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenome.cpp
305 lines (260 loc) · 8.01 KB
/
Genome.cpp
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Genome.cpp
// I. Ahmed
//
// This defines the Genome class, which represents a genome (neural
// network) in the NEAT algorithm.
#include <algorithm>
#include <vector>
#include "Genome.h"
#include "random.h"
// Finds a neuron.
std::vector<Neuron_gene>::const_iterator Genome::find_neuron_const(
const int id) const
{
return std::find_if(neuron_genes.begin(), neuron_genes.end(),
[id](const Neuron_gene &neuron_gene)
{
return neuron_gene.id == id;
});
}
// Finds a neuron.
std::vector<Neuron_gene>::iterator Genome::find_neuron(
const int id)
{
return std::find_if(neuron_genes.begin(), neuron_genes.end(),
[id](const Neuron_gene &neuron_gene)
{
return neuron_gene.id == id;
});
}
// Finds a link.
std::vector<Link_gene>::const_iterator Genome::find_link_const(
const int innov_id) const
{
return std::find_if(link_genes.begin(), link_genes.end(),
[innov_id](const Link_gene &link_gene)
{
return link_gene.innov_id == innov_id;
});
}
// Finds a link.
std::vector<Link_gene>::iterator Genome::find_link(const int innov_id)
{
return std::find_if(link_genes.begin(), link_genes.end(),
[innov_id](const Link_gene &link_gene)
{
return link_gene.innov_id == innov_id;
});
}
// Finds a link.
std::vector<Link_gene>::const_iterator Genome::find_link_const(
const Link_gene &link_gene) const
{
return find_link_const(link_gene.innov_id);
}
// Determines if a neuron is in this genome.
bool Genome::contains_neuron(const int id) const
{
return find_neuron_const(id) != neuron_genes.end();
}
bool Genome::contains_neuron(Neuron_gene &neuron_gene) const
{
return contains_neuron(neuron_gene.id);
}
// Determines if a link is in this genome.
bool Genome::contains_link(const int innov_id) const
{
return find_link_const(innov_id) != link_genes.end();
}
// Determines if a link is in this genome.
bool Genome::contains_link(const Link_gene &link_gene) const
{
return contains_link(link_gene.innov_id);
}
// Determines if a link is in this genome and is enabled.
bool Genome::contains_enabled_link(const int innov_id) const
{
// Find the link.
auto found_link = find_link_const(innov_id);
// Make sure the link has been found and is enabled.
return found_link != link_genes.end() && found_link->enabled;
}
// Sets the neurons' inputs and activations to 0.
void Genome::flush()
{
// Go through each neuron gene and flush.
for (auto &neuron_gene : neuron_genes)
{
neuron_gene.flush();
}
}
// Sorts link genes by the to neuron's depth.
void Genome::sort_link_genes()
{
struct Depth_and_link_gene
{
Depth_and_link_gene() = default;
double depth;
Link_gene link_gene;
// Overload the < operator for std::sort.
bool operator<(const Depth_and_link_gene& right_val) const
{
return depth < right_val.depth;
}
};
std::vector<Depth_and_link_gene> depths_and_links;
// Create a list of the links and their corresponding to neuron depths.
for (const auto &link_gene : link_genes)
{
Depth_and_link_gene depth_and_link;
depth_and_link.link_gene = link_gene;
depth_and_link.depth = find_neuron_const(
link_gene.to_neuron_id)->depth;
depths_and_links.push_back(depth_and_link);
}
// Sort the list.
std::sort(depths_and_links.begin(), depths_and_links.end());
link_genes.clear();
// Extract the link genes from the list to set the link gene list.
for (const auto &depth_and_link : depths_and_links)
{
link_genes.push_back(depth_and_link.link_gene);
}
}
// Calculate the output of the genome given an input.
void Genome::activate(const std::vector<double> &input)
{
// Sort the link genes in order of the to neuron's depth.
sort_link_genes();
// Set neurons' inputs to 0.
for (auto &neuron_gene : neuron_genes)
{
neuron_gene.input = 0.0;
}
// Set the input neurons.
for (int i = 0; i < input_length; i++)
{
neuron_genes[i].activation = input[i];
}
// Activate hidden and output neurons.
for (auto link = link_genes.begin(); link != link_genes.end(); link++)
{
auto to_neuron = find_neuron(link->to_neuron_id);
auto from_neuron = find_neuron(link->from_neuron_id);
// If the link is enabled, update the to neuron's input.
if (link->enabled)
{
to_neuron->input += (from_neuron->activation * link->weight);
}
// If the next link does not exist or has a different to neuron,
// calculate the activation of the to neuron of this link and set it.
auto next_link = link + 1;
if (next_link == link_genes.end() ||
next_link->to_neuron_id != link->to_neuron_id)
{
to_neuron->calc_activation();
}
}
}
// Returns the output of the genome.
std::vector<double> Genome::output()
{
std::vector<double> result;
// Loop through the output genomes, adding the activation of each into
// a vector.
for (int i = input_length; i < (input_length + output_length); i++)
{
result.push_back(neuron_genes[i].activation);
}
return result;
}
// Randomizes link weights between a low value and a high value.
void Genome::randomize_weights(const double low, const double high)
{
// Assign a random weight between the 2 values for each link gene.
for (auto &link_gene : link_genes)
{
link_gene.weight = random_real_num(low, high);
}
}
// Compares the fitnesses of 2 genomes (a higher fitness is better).
// If the fitnesses are the same, compare the sizes (fewer links is better).
bool Genome::operator<(const Genome &right_val) const
{
// Higher fitness is better.
if (fitness != right_val.fitness)
{
return fitness < right_val.fitness;
}
// Fewer links is better.
else if (link_genes.size() != right_val.link_genes.size())
{
return link_genes.size() > right_val.link_genes.size();
}
else // Pick randomly.
{
return true_or_false(0.5);
}
}
// Compares the fitnesses of 2 genomes (a higher fitness is better).
// If the fitnesses are the same, compare the sizes (fewer links is better).
bool Genome::operator>(const Genome &right_val) const
{
return right_val < *this;
}
// Adds a link gene.
void Genome::add_link_gene(const Link_gene &link_gene)
{
link_genes.push_back(link_gene);
}
// Adds a neuron gene.
void Genome::add_neuron_gene(const Neuron_gene &neuron_gene)
{
neuron_genes.push_back(neuron_gene);
}
// Inherits a link gene from another genome.
void Genome::inherit_link_gene(const Genome &genome,
const Link_gene &link_gene)
{
add_link_gene(link_gene);
// Make sure the from and to neurons are present in the child.
if (!contains_neuron(link_gene.from_neuron_id))
{
// Add the from neuron gene to the child.
auto &from_neuron_gene = *(genome.find_neuron_const(
link_gene.from_neuron_id));
add_neuron_gene(from_neuron_gene);
}
if (!contains_neuron(link_gene.to_neuron_id))
{
// Add the to neuron gene to the child.
auto &to_neuron_gene = *(genome.find_neuron_const(
link_gene.to_neuron_id));
add_neuron_gene(to_neuron_gene);
}
}
// Counts the number of links present in this genome that are not present
// in another genome. This means that excess links are included.
int Genome::links_not_present_in(const Genome &genome) const
{
int result = 0;
// Go through each link and check if it is present in the other genome.
for (const auto &link_gene : link_genes)
{
if (!genome.contains_link(link_gene))
{
result++;
}
}
return result;
}
// Finds a genome.
std::vector<Genome>::const_iterator find_genome(
const std::vector<Genome> &genomes, const Genome &genome)
{
return std::find_if(genomes.begin(), genomes.end(),
[genome](const Genome ¤t_genome)
{
return current_genome.id == genome.id;
});
}