-
Notifications
You must be signed in to change notification settings - Fork 0
/
genalg.cpp
152 lines (134 loc) · 3.25 KB
/
genalg.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
#include "genalg.h"
#include "globals.h"
#include "utils.h"
#include <algorithm>
Genome::Genome(Genome&& other)
{
neurons = std::move(other.neurons);
fitness = other.fitness;
}
Genome::Genome(const Genome& other)
{
neurons = other.neurons;
fitness = other.fitness;
}
Genome& Genome::operator=(const Genome& other)
{
if(this != &other)
{
neurons = other.neurons;
fitness = other.fitness;
}
return *this;
}
GenAlg::GenAlg(double mutation_rate, double crossover_rate, int population_size)
: m_mutation_rate(mutation_rate),
m_crossover_rate(crossover_rate),
m_population_size(population_size),
m_total_fitness(0),
m_best_fitness(0),
m_average_fitness(0),
m_generation(0),
m_population()
{
}
void GenAlg::reset()
{
m_total_fitness = 0;
m_best_fitness = 0;
m_average_fitness = 0;
}
QVector<Genome> GenAlg::epoch(QVector<Genome> prev_generation)
{
m_population = prev_generation;
reset();
std::sort(m_population.begin(), m_population.end());
calculate_stats();
QVector<Genome> new_pop;
grab_N_best(Globs::NUM_ELITES, Globs::NUM_COPIES, new_pop);
while(new_pop.size() < m_population_size)
{
Genome mom = select_roulette();
Genome dad = select_roulette();
Genome baby = cross_over(mom, dad);
mutate(baby);
new_pop.append(baby);
}
return std::move(new_pop);
}
void GenAlg::mutate(Genome& genome)
{
for(Neuron neuron : genome.neurons)
{
for(int i = 0; i < neuron.num_inputs + 1; ++i)
{
if(rand_float() < m_mutation_rate)
{
neuron.weights[i] += (rand_float_n1_to_1() * Globs::MAX_PERTURBATION);
}
}
}
}
Genome& GenAlg::select_roulette()
{
double slice = rand_float() * m_total_fitness;
double fitness_accum;
for(Genome& g : m_population)
{
fitness_accum += g.fitness;
if(fitness_accum >= slice)
{
return g;
}
}
}
void GenAlg::grab_N_best(int N, int copies, QVector<Genome> &population) const
{
while(N--)
{
for(int i = 0; i < copies; ++i)
{
// Genomes are sorted in ascending order based on their fitness.
// Grab genomes from end, which holds the best genomes.
population.append(m_population[m_population_size - 1 - N]);
}
}
}
void GenAlg::calculate_stats()
{
m_total_fitness = 0.0;
double best_so_far = 0.0;
for(Genome& g : m_population)
{
if(g.fitness > best_so_far)
{
best_so_far = g.fitness;
m_best_fitness = g.fitness;
}
m_total_fitness += g.fitness;
}
m_average_fitness = m_total_fitness / m_population_size;
}
Genome GenAlg::cross_over(Genome &mom, Genome &dad)
{
Genome baby;
if(rand_float() > m_crossover_rate || (&mom == &dad))
{
baby = mom;
return baby;
}
int cp1 = rand_int(0, m_population.size() - 2);
int cp2 = rand_int(cp1, m_population.size() - 1);
for(int i = 0; i < mom.neurons.size(); ++i)
{
if(i < cp1 || i >= cp2)
{
baby.neurons.append(mom.neurons[i]);
}
else
{
baby.neurons.append(dad.neurons[i]);
}
}
return baby;
}