-
Notifications
You must be signed in to change notification settings - Fork 37
/
GeneticAlgorithm.cpp
227 lines (189 loc) · 7.21 KB
/
GeneticAlgorithm.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
//
// Created by Иван Ильин on 25.07.2020.
//
#include "GeneticAlgorithm.h"
#include "Time.h"
#include <thread>
#include <iostream>
#include <fstream>
#include "Point3D.h"
#include "Point2D.h"
#include <sstream>
#include <thread>
GeneticAlgorithm::GeneticAlgorithm(World& world, ServerUDP& server, int number) : W_world(world), serverUdp(server) {
for(int i = 0; i < number; i++) {
v_generation.insert({i, std::shared_ptr<Enemy>(new Enemy(world, {0, 0}))});
//v_generation[i]->network().randomlyInitNetwork();
}
//_spawns.push_back({ 1.5, 1.5 });
//_spawns.push_back({ 15.5, 2.5 });
//_spawns.push_back({ 27.5, -12.5 });
//_spawns.push_back({ 21.5, -15.0 });
//_spawns.push_back({ 23.0, 27.0 });
//_spawns.push_back({9.5, 26.0});
//_spawns.push_back({0.6, 8.5});
//_spawns.push_back({15, 8});
//_spawns.push_back({20, -5});
//////
//_spawns.push_back({22, 19});
//_spawns.push_back({13, 20});
//_spawns.push_back({23, 14});
//_spawns.push_back({28, 8});
//_spawns.push_back({20, 20});
//_spawns.push_back({23, 3});
//_spawns.push_back({15, 6});
//_spawns.push_back({20, 14});
//_spawns.push_back({12, 19});
//
//
//
//
//_spawns.push_back({0.5, -0.5});
//_spawns.push_back({12.5, -12.5});
//_spawns.push_back({30, 5});
//_spawns.push_back({6, 26});
////
//_spawns.push_back({9, -2});
//_spawns.push_back({26, -9});
//_spawns.push_back({25, 11});
//_spawns.push_back({24, 27});
}
void GeneticAlgorithm::connect(ClientUDP& camera) {
for(int i = 0; i < v_generation.size(); i++) {
v_generation[i]->connect("127.0.0.1", 54000);
while (v_generation[i]->client->isWorking() && !v_generation[i]->client->connected())
{
serverUdp.update();
for(auto& p : v_generation)
if(p.second->client != nullptr)
p.second->client->update();
camera.update();
Time::update();
}
// If connect fail - return to menu
if (!v_generation[i]->client->isWorking())
{
throw "ENEMY CONNECTION ERROR";
}
if(v_generation[i]->client->localPlayer() != nullptr) {
v_generation[i]->client->localPlayer()->client = v_generation[i]->client;
v_generation[i]->setPosition(v_generation[i]->client->localPlayer()->position());
v_generation[i]->client->localPlayer()->setFieldOfView(v_generation[i]->fov());
v_generation[i]->setName(v_generation[i]->client->localPlayer()->getName());
}
}
setInitStats();
}
void GeneticAlgorithm::updateFormTo(int from, int to, double dt, double elapsedTime) {
for(int i = from; i < to; i++) {
v_generation[i]->update(dt, elapsedTime);
}
}
void GeneticAlgorithm::update(double dt, double elapsedTime) {
if(m) {
std::vector<std::shared_ptr<std::thread>> threads;
if(v_generation.size() <= std::thread::hardware_concurrency()) {
for(auto& player : v_generation)
threads.emplace_back(new std::thread(&Enemy::update, player.second.get(), dt, elapsedTime));
} else {
int onOneThread = 1 + v_generation.size() / std::thread::hardware_concurrency();
for(int i = 0; i < std::thread::hardware_concurrency(); i++) {
int from = i*onOneThread;
int to = (i+1)*onOneThread;
if(i == std::thread::hardware_concurrency() - 1 || to >= v_generation.size()) {
to = v_generation.size();
i = std::thread::hardware_concurrency() - 1;
}
threads.emplace_back(new std::thread(&GeneticAlgorithm::updateFormTo, this, from, to, dt, elapsedTime));
}
}
for(auto& t : threads)
if(t->joinable())
t->join();
} else {
for(auto& player : v_generation)
player.second->update(dt, elapsedTime);
}
// ========================= //
for(auto& p : v_generation) {
if(p.second->client->localPlayer() == nullptr)
return;
if(p.second->client->localPlayer()->deaths() != p.second->deaths()) {
if (b_oneDeathMode) {
p.second->client->localPlayer()->setPosition({2000, 0});
} else {
p.second->setKills(p.second->client->localPlayer()->kills());
p.second->setDeaths(p.second->client->localPlayer()->deaths());
}
}
}
}
void GeneticAlgorithm::newGeneration() {
// select AI's with the highest score = kills - deaths
std::vector<std::pair<double, NeuralNetwork>> theBest;
for(auto& p : v_generation)
//theBest.emplace_back(p.second->kills(), std::move(p.second->network()));
theBest.emplace_back(p.second->kills() * 4 - 1 * p.second->deaths() + p.second->pathLength(), std::move(p.second->network()));
std::sort(theBest.begin(), theBest.end(), [](const std::pair<int, NeuralNetwork>& lh, const std::pair<int, NeuralNetwork>& rh) { return lh.first > rh.first; });
d_score = theBest[0].first;
// crossbreed the best networks and share to all AI's
for(auto& p : v_generation) {
int n1 = rand() % 10;
int n2 = rand() % 10;
p.second->network().crossbreeding(theBest[n1].second, theBest[n2].second);
}
// make mutations of all network (10% of all weight and bias we change on +- 10%)
mutate();
i_generation++;
setInitStats();
}
void GeneticAlgorithm::mutate() {
for(auto& p : v_generation)
p.second->network().mutateNetwork();
}
void GeneticAlgorithm::saveNetwork(std::string filename) {
int maxKillsPlayer = 0;
// select AI with the highest score = kills - deaths
for(auto& p : v_generation)
if (p.second->kills() > v_generation[maxKillsPlayer]->kills())
maxKillsPlayer = p.first;
// save the network of this AI
v_generation[maxKillsPlayer]->network().saveNetwork(filename, i_generation);
}
void GeneticAlgorithm::loadNetwork(std::string filename) {
// load the network from 'filename'
i_generation = v_generation[0]->network().loadNetwork(filename);
for(auto& p : v_generation)
p.second->network().sameNet(v_generation[0]->network());
}
void GeneticAlgorithm::setInitStats() {
for(auto& p : v_generation) {
if(p.second->client->isWorking()) {
p.second->setDeaths(0);
p.second->setKills(0);
p.second->setHeight(100);
//p.second->setPosition(serverUdp.spawns()[p.first % serverUdp.spawns().size()]);
//p.second->client->localPlayer()->setPosition(p.second->position());
p.second->fullAmmunition();
p.second->setInitStats();
p.second->client->localPlayer()->setKills(0);
p.second->client->localPlayer()->setDeaths(0);
}
}
serverUdp.reInit();
}
void GeneticAlgorithm::logScore(std::string filename) {
std::ofstream file;
file.open(filename, std::ios::app);
if(!file.is_open())
return;
// the number of current generation
file << i_generation << '\t' << d_score << std::endl;
file.close();
}
struct Point4D {
double x = 0;
double y = 0;
double z = 0;
double w = 0;
};