-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pathfindder_sfml.cpp
executable file
·431 lines (383 loc) · 18.4 KB
/
test_pathfindder_sfml.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
#include "lib/SimpleNEAT.hpp"
int main() {
// Window
sf::RenderWindow window(sf::VideoMode(800, 800), "path findder", sf::Style::Titlebar | sf::Style::Close);
window.setFramerateLimit(120);
sf::Event ev{};
bool isKeepLeft = false;
std::map<std::vector<long>, sf::RectangleShape> blocks;
std::map<std::vector<long>, int> linePos;
std::vector<long> beginPos = {30, 30};
std::vector<long> endPos = {760, 760};
static float bestDistance = std::sqrt(float(std::pow(beginPos[0] - endPos[0], 2) + std::pow(beginPos[1] - endPos[1], 2)));
struct Player {
std::map<std::vector<long>, int> passedPath;
std::vector<long> statusPos;
bool isDone = false;
uint stepCount = 0;
float distanceLeft = bestDistance;
};
std::map<znn::NetworkGenome *, Player> players;
Player Champion;
bool isStart = false;
bool isAllDie = false;
bool isCrow = false;
bool canDrawChampion = false;
std::function<bool(Player &, std::vector<long> &)> isCrash = [&linePos, &endPos](Player &player, std::vector<long> &nextMove) {
if (nextMove[0] < 0 || nextMove[0] >= 800 || nextMove[1] < 0 || nextMove[1] >= 800 || player.passedPath.contains(nextMove) || linePos.contains(nextMove) || nextMove == endPos) {
return true;
}
return false;
};
for (long i = 0; i < 80; ++i) {
for (long ii = 0; ii < 80; ++ii) {
sf::RectangleShape box(sf::Vector2f(10.f, 10.f));
box.setPosition(float(i * 10), float(ii * 10));
box.setFillColor(sf::Color(0, 0, 0, 255));
blocks[{i * 10, ii * 10}] = box;
}
}
/*
* 0 1 2
* 3 4
* 5 6 7
*/
std::function<std::vector<float>(std::vector<long> &)> getAround = [&linePos](std::vector<long> &status) {
std::vector<float> around = {0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f};
if (linePos.contains({status[0] - 10, status[1] - 10}) || status[0] - 10 < 0 || status[1] - 10 < 0) {
around[0] = 1.f;
}
if (linePos.contains({status[0], status[1] - 10}) || status[1] - 10 < 0) {
around[1] = 1.f;
}
if (linePos.contains({status[0] + 10, status[1] - 10}) || status[0] + 10 >= 800 || status[1] - 10 < 0) {
around[2] = 1.f;
}
if (linePos.contains({status[0] - 10, status[1]}) || status[0] - 10 < 0) {
around[3] = 1.f;
}
if (linePos.contains({status[0] + 10, status[1]}) || status[0] + 10 >= 800) {
around[4] = 1.f;
}
if (linePos.contains({status[0] - 10, status[1] + 10}) || status[0] - 10 < 0 || status[1] + 10 >= 800) {
around[5] = 1.f;
}
if (linePos.contains({status[0], status[1] + 10}) || status[1] + 10 >= 800) {
around[6] = 1.f;
}
if (linePos.contains({status[0] + 10, status[1] + 10}) || status[0] + 10 >= 800 || status[1] + 10 >= 800) {
around[7] = 1.f;
}
return around;
};
std::function<std::vector<long>(std::vector<long> &, uint &)> getNextMove = [](std::vector<long> &status, uint &action) {
switch (action) {
case 0:
return std::vector<long>{status[0] - 10, status[1] - 10};
case 1:
return std::vector<long>{status[0], status[1] - 10};
case 2:
return std::vector<long>{status[0] + 10, status[1] - 10};
case 3:
return std::vector<long>{status[0] - 10, status[1]};
case 4:
return std::vector<long>{status[0] + 10, status[1]};
case 5:
return std::vector<long>{status[0] - 10, status[1] + 10};
case 6:
return std::vector<long>{status[0], status[1] + 10};
case 7:
return std::vector<long>{status[0] + 10, status[1] + 10};
}
};
znn::SimpleNeat sneat;
auto createPop = [&]() { // 构建基因种群
znn::Opts.InputSize = 12;
znn::Opts.OutputSize = 8;
znn::Opts.ActiveFunction = znn::Sigmoid;
znn::Opts.IterationTimes = 900;
znn::Opts.FitnessThreshold = 0.f;
znn::Opts.IterationCheckPoint = 0;
znn::Opts.ThreadCount = 16;
znn::Opts.MutateAddNeuronRate = 0.1f;
znn::Opts.MutateAddConnectionRate = 0.99f;
znn::Opts.PopulationSize = 300;
znn::Opts.ChampionKeepSize = 30;
znn::Opts.NewSize = 20;
znn::Opts.KeepWorstSize = 30;
znn::Opts.ChampionToNewSize = 90;
znn::Opts.KeepComplexSize = 1;
znn::Opts.WeightRange = 32.f;
znn::Opts.BiasRange = 16.f;
znn::Opts.MutateWeightDirectOrNear = 0.5f;
znn::Opts.MutateWeightNearRange = 6;
znn::Opts.MutateBiasDirectOrNear = 0.5f;
znn::Opts.MutateWeightNearRange = 6;
znn::Opts.Enable3dNN = true;
sneat.StartNew();
isCrow = true;
};
auto initPlayers = [&]() {
for (auto &g: sneat.population.NeuralNetworks) {
Player player = Player{};
player.statusPos = beginPos;
player.passedPath[beginPos] = 0;
players[&g] = player;
}
};
uint rounds = 1;
uint realRounds = 1;
auto getFitness = [&]() {
std::unordered_map<znn::NetworkGenome *, float> popFitness;
for (auto &p: players) {
popFitness[p.first] = (bestDistance - p.second.distanceLeft) * 0.5f + float(p.second.stepCount) * 0.5f; // TODO 分数评判有问题
if (p.second.distanceLeft < 15) {
popFitness[p.first] = popFitness[p.first] / float(p.second.stepCount) + bestDistance;
}
}
return popFitness;
};
auto drawChampion = [&]() {
// std::cout << p.distanceLeft << "\n";
for (auto &pp: Champion.passedPath) {
blocks[pp.first].setFillColor(sf::Color(200, 200, 255, 200));
window.draw(blocks[pp.first]);
}
blocks[Champion.statusPos].setFillColor(sf::Color(250, 0, 0, 255));
window.draw(blocks[Champion.statusPos]);
};
auto singleFromLoop = [&]() {
using namespace znn;
auto populationFitness = getFitness();
auto orderedPopulation = sneat.OrderByFitness(populationFitness);
auto orderedByComplex = sneat.OrderByComplex();
Champion = players[orderedPopulation[0]];
std::cout << "gen: " << realRounds << " " << orderedPopulation[0] << " " << orderedPopulation[0]->Neurons.size() << " " << orderedPopulation[0]->Connections.size() << " steps: "
<< players[orderedPopulation[0]].stepCount << " distance left: " << players[orderedPopulation[0]].distanceLeft << " fitness: " << populationFitness[orderedPopulation[0]]
<< " most complex: " << orderedByComplex[0]->Neurons.size() << std::endl;
++realRounds;
if (rounds >= Opts.IterationTimes) {
auto simplifiedBestNN = sneat.population.generation.neuralNetwork.SimplifyRemoveDisable(*orderedPopulation[0]);
auto compressedLeftBestNN = sneat.population.generation.neuralNetwork.SimplifyRemoveUselessConnectionLeft(simplifiedBestNN);
auto compressedRightBestNN = sneat.population.generation.neuralNetwork.SimplifyRemoveUselessConnectionRight(compressedLeftBestNN);
sneat.population.generation.neuralNetwork.ExportNN(compressedRightBestNN, "./champion");
isStart = false;
isAllDie = false;
canDrawChampion = true;
rounds = 1;
std::cout << "Training done.\n";
return;
}
if (players[orderedPopulation[0]].distanceLeft < 15) {
canDrawChampion = true;
}
std::vector<NetworkGenome> tmpPopulation(Opts.PopulationSize);
std::vector<std::future<void>> thisFuture; // 如果用这个线程池的push_task函数,后面需要wait_for_tasks(),会卡死
uint indexOutside = 0;
for (auto &nn: tmpPopulation) {
thisFuture.push_back(tPool.submit([&]() {
mtx.lock();
uint index = indexOutside;
++indexOutside;
mtx.unlock();
if (index < Opts.ChampionToNewSize) {
nn = *orderedPopulation[index % Opts.ChampionKeepSize]; // 选取ChampionKeepSize个个体填满前ChampionToNewSize个
if (index >= Opts.ChampionKeepSize) {
sneat.population.generation.MutateNetworkGenome(nn); // 除开原始冠军,他们的克隆体进行变异
}
} else if (index < Opts.PopulationSize - Opts.NewSize - Opts.KeepWorstSize - Opts.KeepComplexSize) {
auto nn0 = orderedPopulation[random() % Opts.ChampionKeepSize];
auto nn1 = orderedPopulation[Opts.ChampionKeepSize + random() % (Opts.PopulationSize - Opts.ChampionKeepSize)];
nn = sneat.population.generation.GetChildByCrossing(nn0, nn1);
if ((index % 2 == 0 || nn0 == nn1) && nn0->Neurons.size() < orderedByComplex[0]->Neurons.size() && nn1->Neurons.size() < orderedByComplex[0]->Neurons.size()) {
sneat.population.generation.MutateNetworkGenome(nn); // 繁殖以后进行变异
}
++index;
} else if (index < Opts.PopulationSize - Opts.KeepWorstSize - Opts.KeepComplexSize) {
nn = sneat.population.generation.neuralNetwork.NewNN();
} else if (index < Opts.PopulationSize - Opts.KeepWorstSize) {
nn = *orderedByComplex[index % Opts.KeepComplexSize];
sneat.population.generation.EnableAllConnections(nn);
} else {
nn = *orderedPopulation[index];
sneat.population.generation.MutateNetworkGenome(nn);
}
}));
}
// exit(0);
for (auto &f: thisFuture) {
f.wait();
}
sneat.population.NeuralNetworks = tmpPopulation;
initPlayers();
++rounds;
};
//Gmae Loop
for (;;) {
if (!isStart) {
while (window.pollEvent(ev)) {
switch (ev.type) {
case sf::Event::Closed:
window.close();
exit(0);
case sf::Event::KeyPressed:
if (ev.key.code == sf::Keyboard::Space) {
std::cout << "Start training\n";
isAllDie = false;
isStart = true;
canDrawChampion = false;
if (!isCrow) {
createPop();
}
initPlayers();
break;
}
if (ev.key.code == sf::Keyboard::Escape) {
isCrow = false;
realRounds = 0;
std::cout << "Clear.\n";
players.clear();
canDrawChampion = false;
window.clear(sf::Color(0, 0, 0, 255)); // Clear old frame
break;
}
{
auto thisMouse = sf::Mouse::getPosition(window);
if (!isCrow && ev.key.code == sf::Keyboard::Num1) {
beginPos = {thisMouse.x / 10 * 10, thisMouse.y / 10 * 10};
bestDistance = std::sqrt(float(std::pow(beginPos[0] - endPos[0], 2) + std::pow(beginPos[1] - endPos[1], 2)));
std::cout << "begin set: " << beginPos[0] << " " << beginPos[1] << "\n";
}
if (!isCrow && ev.key.code == sf::Keyboard::Num2) {
endPos = {thisMouse.x / 10 * 10, thisMouse.y / 10 * 10};
bestDistance = std::sqrt(float(std::pow(beginPos[0] - endPos[0], 2) + std::pow(beginPos[1] - endPos[1], 2)));
std::cout << "end set: " << endPos[0] << " " << endPos[1] << "\n";
}
window.clear(sf::Color(0, 0, 0, 255)); // Clear old frame
break;
}
break;
case sf::Event::MouseButtonPressed:
if (ev.mouseButton.button == sf::Mouse::Left) {
isKeepLeft = true;
}
if (ev.mouseButton.button == sf::Mouse::Right) {
linePos.clear();
window.clear(sf::Color(0, 0, 0, 255)); // Clear old frame
}
break;
case sf::Event::MouseButtonReleased:
if (ev.mouseButton.button == sf::Mouse::Left) {
isKeepLeft = false;
}
break;
}
}
} else {
while (window.pollEvent(ev)) {
switch (ev.type) {
case sf::Event::Closed:
window.close();
exit(0);
case sf::Event::KeyPressed:
if (ev.key.code == sf::Keyboard::Space) {
isStart = false;
isAllDie = false;
canDrawChampion = true;
rounds = 1;
std::cout << "Training cancel.\n";
break;
}
}
}
}
// Update
if (isKeepLeft && !isStart) {
auto pos = sf::Mouse::getPosition(window);
linePos[{pos.x / 10 * 10, pos.y / 10 * 10}] = 0;
linePos[{pos.x / 10 * 10 - 10, pos.y / 10 * 10}] = 0;
linePos[{pos.x / 10 * 10, pos.y / 10 * 10 - 10}] = 0;
linePos[{pos.x / 10 * 10 - 10, pos.y / 10 * 10 - 10}] = 0;
}
if (isStart && !isAllDie) {
isAllDie = true;
for (auto &p: players) {
if (!p.second.isDone) {
std::vector<float> around = getAround(p.second.statusPos);
auto predictActions = sneat.population.generation.neuralNetwork.FeedForwardPredict(p.first, {float(p.second.statusPos[0]) / 800.f, float(p.second.statusPos[1]) / 800.f,
float(endPos[0]) / 800.f, float(endPos[1]) / 800.f, around[0], around[1], around[2],
around[3], around[4], around[5], around[6], around[7]}, false);
std::map<float, uint> actions;
for (uint i = 0; i < 8; ++i) {
actions[predictActions[i]] = i;
}
std::sort(predictActions.begin(), predictActions.end());
uint choseAction = actions[predictActions[predictActions.size() - 1]];
std::vector<long> nextMove = getNextMove(p.second.statusPos, choseAction);
if (isCrash(p.second, nextMove)) {
p.second.isDone = true;
p.second.distanceLeft = std::sqrt(std::pow(p.second.statusPos[0] - endPos[0], 2) + std::pow(p.second.statusPos[1] - endPos[1], 2));
// std::cout << &p.second << " died step: " << p.second.stepCount << " left: " << p.second.distanceLeft << "\n";
if (nextMove[0] < 0 || nextMove[0] >= 800 || nextMove[1] < 0 || nextMove[1] >= 800 || linePos.contains(nextMove)) {
p.second.distanceLeft += 50.f;
}
} else {
++p.second.stepCount;
p.second.statusPos = nextMove;
p.second.passedPath[nextMove] = 0;
isAllDie = false;
}
}
}
}
if (isAllDie && isStart) {
isAllDie = false;
// std::cout << "All died\n";
singleFromLoop();
}
if (!isStart || rounds % 10 == 0 || canDrawChampion || isAllDie) {
if (canDrawChampion && (rounds % 10 == 1 || !isStart)) {
window.clear(sf::Color(0, 0, 0, 255)); // Clear old frame
drawChampion();
} else if ((isStart || isAllDie) && rounds % 10 == 0) {
window.clear(sf::Color(0, 0, 0, 255)); // Clear old frame
std::vector<std::vector<long>> tmpDiedPoints;
for (auto &p: players) {
for (auto &pp: p.second.passedPath) {
blocks[pp.first].setFillColor(sf::Color(50, 50, 200, 60));
window.draw(blocks[pp.first]);
}
if (p.second.isDone) {
tmpDiedPoints.push_back(p.second.statusPos);
} else {
blocks[p.second.statusPos].setFillColor(sf::Color(0, 200, 0, 100));
}
window.draw(blocks[p.second.statusPos]);
}
for (auto &dp: tmpDiedPoints) {
blocks[dp].setFillColor(sf::Color(250, 0, 0, 255));
window.draw(blocks[dp]);
}
// }
}
for (auto &lp: linePos) {
blocks[lp.first].setFillColor(sf::Color(255, 255, 255, 255));
window.draw(blocks[lp.first]);
}
blocks[beginPos].setFillColor(sf::Color(255, 255, 0, 255));
window.draw(blocks[beginPos]);
blocks[endPos].setFillColor(sf::Color(0, 150, 255, 255));
window.draw(blocks[endPos]);
// Draw game
window.display(); // Tell app window is done drawing
}
if (isStart) {
std::thread update3d(znn::Update3dNN, sneat.population.NeuralNetworks[0], false);
update3d.detach();
}
}
return 0;
}