-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenetic.cpp
180 lines (151 loc) · 4.87 KB
/
genetic.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
#include "genetic.h"
#include "bird.h"
#include "obstacles.h"
#include "env.h"
#include <iostream>
#include <algorithm>
#include <iterator>
#include <time.h>
GA::GA(int pop_size, int num_winners, ObstacleList ol, int num_layers, int* layers)
{
this->pop_size = pop_size;
this->num_winners = num_winners;
this->num_layers = num_layers;
this->layers = layers;
this->population = new NeuralBird*[pop_size];
this->mutation_rate = mut_rate;
this->ol = ol;
this->iteration = 0;
}
void GA::init_rand_pop()
{
for (int i = 0; i < this->pop_size; i++) {
this->population[i] = new NeuralBird(this->ol, this->num_layers, this->layers);
}
struct fann* ann = fann_create_from_file("good_network");
this->population[0]->ann = fann_copy(ann);
this->population[1]->ann = fann_copy(ann);
}
int GA::simulate_game(NeuralBird *nb, ObstacleList ol)
{
ol.reset_list();
nb->reset(ol);
while (!nb->game_over && nb->score < 100) {
ol.update_pos();
nb->Bird::update_pos();
if (nb->should_jump()) {
nb->Bird::jump();
}
}
// nb->avg_fitness = (this->iteration * nb->avg_fitness + nb->score)/(this->iteration + 1);
}
void GA::simulate_game_gen()
{
for (int i = 0; i < pop_size; i++) {
this->simulate_game(population[i], this->ol);
}
this->iteration++;
std::sort(this->population, this->population + pop_size, comp_fitness);
display_fitness(this->population);
}
void GA::display_fitness(NeuralBird** population)
{
std::cout << "Score\tFitness\tAverage Fitness\tTotal Distance" << std::endl;
for(int i=0; i < this->pop_size; i++) {
std::cout << population[i]->score << "\t" << population[i]->distance << "\t" << population[i]->avg_fitness << "\t" << population[i]->total_fitness << std::endl;
}
}
void GA::crossover(NeuralBird *nb1, NeuralBird *nb2, NeuralBird **child)
{
/* Extract the ANNs of the parent birds */
struct fann* ann1 = fann_copy(nb1->ann);
struct fann* ann2 = fann_copy(nb2->ann);
struct fann* child_ann = fann_copy(ann1);
/* Initialize child bird */
*child = new NeuralBird(*nb1);
(*child)->turns = 0;
(*child)->total_fitness = 0;
(*child)->avg_fitness = 0;
/*
* struct fann_connection is a structure that holds the connection data
* of an ANN
*/
int total_conn = fann_get_total_connections(ann1);
struct fann_connection *connection1 = new struct fann_connection[total_conn];
struct fann_connection *connection2 = new struct fann_connection[total_conn];
struct fann_connection *child_conn = new struct fann_connection[total_conn];
srand(time(0));
fann_get_connection_array(ann1, connection1);
fann_get_connection_array(ann2, connection2);
fann_get_connection_array(ann1, child_conn);
int crossover_pt = rand() % total_conn;
/*
* During crossover, combine the biases of parent1 and parent2 to create the
* new child
*/
for (int i=0; i<total_conn; i++) {
if ((rand() % 100) < mutation_rate) {
/* Perform a random mutation based on mutation_rate */
float rand_weight = rand_float(-1.0, 1.0);
child_conn[i].weight = rand_weight;
} else if ( (connection1[i].from_neuron = 2 || connection1[i].from_neuron == 9) && i > crossover_pt ) {
child_conn[i].weight = connection2[i].weight;
} else {
child_conn[i].weight = connection1[i].weight;
}
fann_set_weight_array(child_ann, child_conn, total_conn);
(*child)->ann = fann_copy(child_ann);
}
}
void GA::evolve()
{
NeuralBird *parent1, *parent2;
this->last_population = new NeuralBird*[this->pop_size];
for(int i=0; i<this->pop_size; i++)
{
this->last_population[i] = new NeuralBird(*(this->population[i]));
}
int index=0, curr_index=0;
// copy 4 best winners to new generation
for(int i=index; i<0.4*this->pop_size; i++)
{
this->population[i] = new NeuralBird(*(this->last_population[i]));
index++;
}
curr_index = index;
// 1 offspring from 2 best winners
for(int i=index; i<curr_index + 0.1*this->pop_size; i++)
{
parent1 = this->last_population[0];
parent2 = this->last_population[1];
this->crossover(parent1, parent2, &this->population[i]);
index++;
}
curr_index = index;
// 3 offsprings from 2 random winners
for(int i=index; i<curr_index + 0.3*this->pop_size; i++)
{
int index1 = rand() % num_winners;
int index2 = rand() % num_winners;
parent1 = this->last_population[index1];
parent2 = this->last_population[index2];
this->crossover(parent1, parent2, &this->population[i]);
index++;
}
// 2 direct copies of random winners
for(int i=index; i<(this->pop_size); i++)
{
int winner = rand() % num_winners;
this->population[i] = this->last_population[winner];
}
}
bool comp_fitness(NeuralBird *nb1, NeuralBird *nb2)
{
return nb1->score > nb2->score;
}
float rand_float(float a, float b) {
float random = ((float) rand()) / (float) RAND_MAX;
float diff = b - a;
float r = random * diff;
return a + r;
}