-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment.cpp
334 lines (307 loc) · 10.2 KB
/
environment.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include "environment.hpp"
#include <algorithm>
#include <iostream>
#include <stdexcept>
#include <vector>
#include "overloads.hpp"
template <typename T>
std::vector<T> operator+(const std::vector<T>& A, const std::vector<T>& B) {
std::vector<T> AB;
AB.reserve(A.size() + B.size());
AB.insert(AB.end(), A.begin(), A.end());
AB.insert(AB.end(), B.begin(), B.end());
return AB;
}
template <typename T>
std::vector<T>& operator+=(std::vector<T>& A, const std::vector<T>& B) {
A.reserve(A.size() + B.size());
A.insert(A.end(), B.begin(), B.end());
return A;
}
Environment::Environment(int width, int height, int food, int bugs)
: width(width), height(height) {
this->state.generation = 0;
for (int i = 0; i < food; i++) {
Food food(width, height, 100);
this->foods.push_back(food);
}
for (int i = 0; i < bugs; i++) {
Bug* bug = new Bug;
bug->x = this->width / 2;
bug->y = this->height / 2;
bug->generate_brain(5, 4, 50);
this->bugs.push_back(bug);
}
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
throw std::runtime_error("SDL_Init Error: " + std::string(SDL_GetError()));
}
SDL_CreateWindowAndRenderer(width, height, 0, &this->win, &this->rend);
SDL_SetRenderDrawColor(this->rend, 0, 0, 0, 255);
SDL_RenderClear(this->rend);
}
void Environment::draw() {
SDL_SetRenderDrawColor(this->rend, 0, 0, 0, 255);
SDL_RenderClear(this->rend);
for (Food food : foods) {
food.draw(this->rend);
}
for (Bug* bug : this->bugs) {
bug->draw(this->rend);
}
}
void Environment::cycle() {
for (Bug* bug : this->new_bugs) {
this->bugs.push_back(bug);
}
this->new_bugs.clear();
this->state.total_population = 0;
this->state.total_energy = 0;
this->state.total_age = 0;
this->state.total_generation = 0;
this->state.total_neurons = 0;
this->state.max_energy = 0;
this->state.max_age = 0;
this->state.max_generation = 0;
this->state.max_neurons = 0;
this->state.min_energy = std::numeric_limits<int>::max();
this->state.min_age = std::numeric_limits<int>::max();
this->state.min_generation = std::numeric_limits<int>::max();
this->state.min_neurons = std::numeric_limits<int>::max();
for (Food food : this->foods) {
food.energy -= 1000;
if (food.energy <= 0) {
food.summon(this->width, this->height);
}
}
for (Bug* bug : this->bugs) {
bug->cycle(this);
if (!bug->dead) {
this->state.total_population++;
this->state.total_energy += bug->energy;
this->state.total_age += bug->age;
this->state.total_generation += bug->generation;
if (bug->energy > this->state.max_energy) {
this->state.max_energy = bug->energy;
}
if (bug->age > this->state.max_age) {
this->state.max_age = bug->age;
}
if (bug->generation > this->state.max_generation) {
this->state.max_generation = bug->generation;
}
if (bug->energy < this->state.min_energy) {
this->state.min_energy = bug->energy;
}
if (bug->age < this->state.min_age) {
this->state.min_age = bug->age;
}
if (bug->generation < this->state.min_generation) {
this->state.min_generation = bug->generation;
}
this->state.total_neurons += bug->brain.hidden.size();
if (bug->brain.hidden.size() > this->state.max_neurons) {
this->state.max_neurons = bug->brain.hidden.size();
}
if (bug->brain.hidden.size() < this->state.min_neurons) {
this->state.min_neurons = bug->brain.hidden.size();
}
}
}
this->state.generation++;
}
Bug::Bug() {
this->x = 0;
this->y = 0;
this->generation = 0;
this->age = 0;
this->energy = 1000;
this->dead = false;
}
void Bug::generate_brain(int inputs, int outputs, int hidden_neurons) {
for (int i = 0; i < inputs; i++) {
Neuron* input_neuron = new Neuron();
this->brain.inputs.push_back(input_neuron);
}
for (int i = 0; i < hidden_neurons; i++) {
Neuron* hidden_neuron = new Neuron();
do {
Connection* connection = new Connection((
this->brain.inputs +
this->brain.hidden)[rand() % (this->brain.inputs + this->brain.hidden)
.size()]);
connection->assign_random_weight();
hidden_neuron->add_connection(connection);
} while (rand() % 5 == 0);
this->brain.hidden.push_back(hidden_neuron);
}
for (int i = 0; i < outputs; i++) {
Neuron* output_neuron = new Neuron();
do {
Connection* connection = new Connection((
this->brain.inputs +
this->brain.hidden)[rand() % (this->brain.inputs + this->brain.hidden)
.size()]);
connection->assign_random_weight();
output_neuron->add_connection(connection);
} while (rand() % 5 == 0);
this->brain.outputs.push_back(output_neuron);
}
}
void Bug::mutate_brain(double mutation_factor) {
for (Neuron* neuron : (this->brain.hidden + this->brain.outputs)) {
for (Connection* connection : neuron->connections) {
if (rand() % (int)floor(1 * (1 / mutation_factor)) == 0)
connection->weight += ((rand() % 100) / 100) * mutation_factor;
}
// sometimes will generate a loop which we check after mutating
if (rand() % (int)floor(1 * (1 / mutation_factor)) == 0) {
Neuron* selected_neuron = (this->brain.inputs + this->brain.hidden)
[rand() % (this->brain.inputs + this->brain.hidden).size()];
Connection* connection = new Connection(selected_neuron);
connection->assign_random_weight();
neuron->add_connection(connection);
std::vector<double> test_input;
for (int i = 0; i < this->brain.inputs.size(); i++) {
test_input.push_back(i);
}
try {
this->brain.calculate(test_input);
} catch (std::runtime_error& e) {
neuron->connections.pop_back();
delete connection;
}
}
if (rand() % (int)floor(2 * (1 / mutation_factor)) == 0) {
if (neuron->connections.size() > 0) {
delete neuron->connections.back();
neuron->connections.pop_back();
}
}
}
while (rand() % (int)floor(3 * (1 / mutation_factor)) == 0) {
Neuron* new_neuron = new Neuron();
Neuron* selected_neuron =
(this->brain.inputs +
this->brain.hidden)[rand() %
(this->brain.inputs + this->brain.hidden).size()];
Connection* connection = new Connection(selected_neuron);
connection->assign_random_weight();
new_neuron->add_connection(connection);
this->brain.hidden.push_back(new_neuron);
}
}
void Bug::draw(SDL_Renderer* rend) {
if (this->dead) return;
SDL_SetRenderDrawColor(rend, 255, 0, 0, 255);
SDL_RenderDrawPoint(rend, this->x, this->y);
}
void Bug::clear_brain() {
for (Neuron* neuron :
(this->brain.inputs + this->brain.hidden + this->brain.outputs)) {
for (Connection* connection : neuron->connections) {
delete connection;
}
delete neuron;
}
this->brain.inputs.clear();
this->brain.hidden.clear();
this->brain.outputs.clear();
}
void Bug::cycle(Environment* env) {
if (this->dead) return;
// get nearest food
int nearest_food_index = -1;
double nearest_food_distance = std::numeric_limits<double>::max();
for (int i = 0; i < env->foods.size(); i++) {
double distance = sqrt(pow(this->x - env->foods[i].x, 2) +
pow(this->y - env->foods[i].y, 2));
if (distance < nearest_food_distance) {
nearest_food_distance = distance;
nearest_food_index = i;
}
}
// inputs to the bug:
// 0: distance to nearest food
// 1: x distance to nearest food
// 2: y distance to nearest food
// 3: amount of energy to nearest food
// 4: amount of energy left
std::vector<double> inputs = {
nearest_food_distance,
(double)(this->x - env->foods[nearest_food_index].x),
(double)(this->y - env->foods[nearest_food_index].y),
(double)(env->foods[nearest_food_index].energy), (double)(this->energy)};
std::vector<double> outputs = this->brain.calculate(inputs);
// outputs from the bug:
// 0: move left/right
// 1: move up/down
// 2: eat nearest
// 3: reproduce
double move_limit = std::clamp(this->energy * 0.005, 1.0, 100.0);
int delta_x = std::clamp(outputs[0], -move_limit,
move_limit); // fatter creatures move slower
int delta_y = std::clamp(outputs[1], -move_limit, move_limit);
this->x += delta_x;
this->y += delta_y;
this->energy -= sqrt(pow(delta_x, 2) + pow(delta_y, 2));
if (outputs[2] >
0.5 /* || nearest_food_distance < 1.57 */) { // require them to be clever
// enough to eat
if (nearest_food_distance < 2) {
this->energy += env->foods[nearest_food_index].consume(
outputs[2]);
} else {
this->energy -= 5; // if they are too far away, they lose energy
}
}
if (outputs[3] > 0.5 && this->energy > 1000) {
// we will reuse a dead bug
bool not_found = true;
for (Bug* bug : env->bugs) {
if (bug->dead) {
bug->clear_brain();
bug->brain = *this->brain.copy();
bug->mutate_brain(0.5);
bug->generation = this->generation + 1;
bug->dead = false;
bug->age = 0;
bug->x = this->x;
bug->y = this->y;
bug->energy = 100;
this->energy -= 100;
not_found = false;
break;
}
}
if (not_found) {
// mob cap
Bug* bug = new Bug;
bug->brain = *this->brain.copy();
bug->mutate_brain(0.5);
bug->x = this->x;
bug->y = this->y;
bug->energy = 100;
this->energy -= 100;
env->new_bugs.push_back(bug);
}
}
if (this->x < 0) this->x = env->width + this->x;
if (this->y < 0) this->y = env->height + this->y;
if (this->x > env->width) this->x = this->x - env->width;
if (this->y > env->height) this->y = this->y - env->height;
this->energy -= 1; // every cycle
this->age++;
if (this->energy <= 0) {
this->energy = 0;
this->dead = true;
}
// if (this->energy > 100000) { // i dont know HOW the fuck they are having
// this
// // much energy, but here you go. now die
// bitches
// this->dead = true;
// }
// if (this->age > 10000) {
// this->dead = true;
// }
}