-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCController.cpp
More file actions
502 lines (406 loc) · 12.3 KB
/
CController.cpp
File metadata and controls
502 lines (406 loc) · 12.3 KB
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#include "CController.h"
//these hold the geometry of the sweepers and the mines
const int NumSweeperVerts = 16;
const SPoint sweeper[NumSweeperVerts] = {SPoint(-1, -1),
SPoint(-1, 1),
SPoint(-0.5, 1),
SPoint(-0.5, -1),
SPoint(0.5, -1),
SPoint(1, -1),
SPoint(1, 1),
SPoint(0.5, 1),
SPoint(-0.5, -0.5),
SPoint(0.5, -0.5),
SPoint(-0.5, 0.5),
SPoint(-0.25, 0.5),
SPoint(-0.25, 1.75),
SPoint(0.25, 1.75),
SPoint(0.25, 0.5),
SPoint(0.5, 0.5)};
const int NumMineVerts = 4;
const SPoint mine[NumMineVerts] = {SPoint(-1, -1),
SPoint(-1, 1),
SPoint(1, 1),
SPoint(1, -1)};
const int NumObjectVerts = 42;
const SPoint objects[NumObjectVerts] = {SPoint(80, 60),
SPoint(200,60),
SPoint(200,60),
SPoint(200,100),
SPoint(200,100),
SPoint(160,100),
SPoint(160,100),
SPoint(160,200),
SPoint(160,200),
SPoint(80,200),
SPoint(80,200),
SPoint(80,60),
SPoint(250,100),
SPoint(300,40),
SPoint(300,40),
SPoint(350,100),
SPoint(350,100),
SPoint(250, 100),
SPoint(220,180),
SPoint(320,180),
SPoint(320,180),
SPoint(320,300),
SPoint(320,300),
SPoint(220,300),
SPoint(220,300),
SPoint(220,180),
SPoint(12,15),
SPoint(380, 15),
SPoint(380,15),
SPoint(380,360),
SPoint(380,360),
SPoint(12,360),
SPoint(12,360),
SPoint(12,340),
SPoint(12,340),
SPoint(100,290),
SPoint(100,290),
SPoint(12,240),
SPoint(12,240),
SPoint(12,15)};
//---------------------------------------constructor---------------------
//
// initilaize the sweepers, their brains and the GA factory
//
//-----------------------------------------------------------------------
CController::CController(HWND hwndMain,
int cxClient,
int cyClient): m_NumSweepers(CParams::iPopSize),
m_bFastRender(false),
m_bRenderBest(false),
m_iTicks(0),
m_hwndMain(hwndMain),
m_hwndInfo(NULL),
m_iGenerations(0),
m_cxClient(cxClient),
m_cyClient(cyClient),
m_iViewThisSweeper(0)
{
//let's create the mine sweepers
for (int i=0; i<m_NumSweepers; ++i)
{
m_vecSweepers.push_back(CMinesweeper());
}
//and the vector of sweepers which will hold the best performing sweeprs
for (int i=0; i<CParams::iNumBestSweepers; ++i)
{
m_vecBestSweepers.push_back(CMinesweeper());
}
m_pPop = new Cga(CParams::iPopSize,
CParams::iNumInputs,
CParams::iNumOutputs);
//create the phenotypes
vector<CNeuralNet*> pBrains = m_pPop->CreatePhenotypes();
//assign the phenotypes
for (int i=0; i<m_NumSweepers; i++)
{
m_vecSweepers[i].InsertNewBrain(pBrains[i]);
}
//create a pen for the graph drawing
m_BluePen = CreatePen(PS_SOLID, 1, RGB(0, 0, 255));
m_RedPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
m_GreenPen = CreatePen(PS_SOLID, 1, RGB(0, 255, 0));
m_GreyPenDotted = CreatePen(PS_DOT, 1, RGB(100, 100, 100));
m_RedPenDotted = CreatePen(PS_DOT, 1, RGB(200, 0, 0));
m_OldPen = NULL;
//and the brushes
m_BlueBrush = CreateSolidBrush(RGB(0,0,244));
m_RedBrush = CreateSolidBrush(RGB(150,0,0));
//fill the vertex buffers
for (int i=0; i<NumSweeperVerts; ++i)
{
m_SweeperVB.push_back(sweeper[i]);
}
for (int i=0; i<NumObjectVerts; ++i)
{
m_ObjectsVB.push_back(objects[i]);
}
}
//--------------------------------------destructor-------------------------------------
//
//--------------------------------------------------------------------------------------
CController::~CController()
{
if (m_pPop)
{
delete m_pPop;
}
DeleteObject(m_BluePen);
DeleteObject(m_RedPen);
DeleteObject(m_GreenPen);
DeleteObject(m_OldPen);
DeleteObject(m_GreyPenDotted);
DeleteObject(m_RedPenDotted);
DeleteObject(m_BlueBrush);
DeleteObject(m_RedBrush);
}
//-------------------------------------Update---------------------------------------
//
// This is the main workhorse. The entire simulation is controlled from here.
//
//--------------------------------------------------------------------------------------
bool CController::Update()
{
//run the sweepers through NUM_TICKS amount of cycles. During this loop each
//sweepers NN is constantly updated with the appropriate information from its
//surroundings. The output from the NN is obtained and the sweeper is moved.
if (m_iTicks++ < CParams::iNumTicks)
{
for (int i=0; i<m_NumSweepers; ++i)
{
//update the NN and position
if (!m_vecSweepers[i].Update(m_ObjectsVB))
{
//error in processing the neural net
MessageBox(m_hwndMain, "Wrong amount of NN inputs!", "Error", MB_OK);
return false;
}
}
//update the NNs of the last generations best performers
if (m_iGenerations > 0)
{
for (int i=0; i<m_vecBestSweepers.size(); ++i)
{
//update the NN and position
if (!m_vecBestSweepers[i].Update(m_ObjectsVB))
{
//error in processing the neural net
MessageBox(m_hwndMain, "Wrong amount of NN inputs!", "Error", MB_OK);
return false;
}
}
}
}
//We have completed another generation so now we need to run the GA
else
{
//first add up each sweepers fitness scores. (remember for this task
//there are many different sorts of penalties the sweepers may incur
//and each one has a coefficient)
for (int swp=0; swp<m_vecSweepers.size(); ++swp)
{
m_vecSweepers[swp].EndOfRunCalculations();
}
//increment the generation counter
++m_iGenerations;
//reset cycles
m_iTicks = 0;
//perform an epoch and grab the new brains
vector<CNeuralNet*> pBrains = m_pPop->Epoch(GetFitnessScores());
//insert the new brains back into the sweepers and reset their
//positions
for (int i=0; i<m_NumSweepers; ++i)
{
m_vecSweepers[i].InsertNewBrain(pBrains[i]);
m_vecSweepers[i].Reset();
}
//grab the NNs of the best performers from the last generation
vector<CNeuralNet*> pBestBrains = m_pPop->GetBestPhenotypesFromLastGeneration();
//put them into our record of the best sweepers
for (int i=0; i<m_vecBestSweepers.size(); ++i)
{
m_vecBestSweepers[i].InsertNewBrain(pBestBrains[i]);
m_vecBestSweepers[i].Reset();
}
//this will call WM_PAINT which will render our scene
InvalidateRect(m_hwndInfo, NULL, TRUE);
UpdateWindow(m_hwndInfo);
}
return true;
}
//---------------------------------- RenderNetworks ----------------------
//
// Renders the best four phenotypes from the previous generation
//------------------------------------------------------------------------
void CController::RenderNetworks(HDC &surface)
{
if (m_iGenerations < 1)
{
return;
}
//draw the network of the best 4 genomes. First get the dimensions of the
//info window
RECT rect;
GetClientRect(m_hwndInfo, &rect);
int cxInfo = rect.right;
int cyInfo = rect.bottom;
//now draw the 4 best networks
m_vecBestSweepers[0].DrawNet(surface, 0, cxInfo/2, cyInfo/2, 0);
//m_vecBestSweepers[1].DrawNet(surface, cxInfo/2, cxInfo, cyInfo/2, 0);
//m_vecBestSweepers[2].DrawNet(surface, 0, cxInfo/2, cyInfo, cyInfo/2);
//m_vecBestSweepers[3].DrawNet(surface, cxInfo/2, cxInfo, cyInfo, cyInfo/2);
}
//------------------------------------Render()--------------------------------------
//
//----------------------------------------------------------------------------------
void CController::Render(HDC &surface)
{
//do not render if running at accelerated speed
if (!m_bFastRender)
{
string s = "Generation: " + itos(m_iGenerations);
TextOut(surface, 5, 0, s.c_str(), s.size());
//select in the blue pen
m_OldPen = (HPEN)SelectObject(surface, m_BluePen);
if (m_bRenderBest)
{
//render the best sweepers memory cells
m_vecBestSweepers[m_iViewThisSweeper].Render(surface);
//render the best sweepers from the last generation
RenderSweepers(surface, m_vecBestSweepers);
//render the best sweepers sensors
RenderSensors(surface, m_vecBestSweepers);
}
else
{
//render the sweepers
RenderSweepers(surface, m_vecSweepers);
}
SelectObject(surface, m_OldPen);
//render the objects
for (int i=0; i<NumObjectVerts; i+=2)
{
MoveToEx(surface, m_ObjectsVB[i].x, m_ObjectsVB[i].y, NULL);
LineTo(surface, m_ObjectsVB[i+1].x, m_ObjectsVB[i+1].y);
}
}//end if
else
{
PlotStats(surface);
RECT sr;
sr.top = m_cyClient-50;
sr.bottom = m_cyClient;
sr.left = 0;
sr.right = m_cxClient;
//render the species chart
m_pPop->RenderSpeciesInfo(surface, sr);
}
}
//------------------------- RenderSweepers -------------------------------
//
// given a vector of sweepers this function renders them to the screen
//------------------------------------------------------------------------
void CController::RenderSweepers(HDC &surface, vector<CMinesweeper> &sweepers)
{
for (int i=0; i<sweepers.size(); ++i)
{
//if they have crashed into an obstacle draw
if ( sweepers[i].Collided())
{
SelectObject(surface, m_RedPen);
}
else
{
SelectObject(surface, m_BluePen);
}
//grab the sweeper vertices
vector<SPoint> sweeperVB = m_SweeperVB;
//transform the vertex buffer
sweepers[i].WorldTransform(sweeperVB, sweepers[i].Scale());
//draw the sweeper left track
MoveToEx(surface, (int)sweeperVB[0].x, (int)sweeperVB[0].y, NULL);
for (int vert=1; vert<4; ++vert)
{
LineTo(surface, (int)sweeperVB[vert].x, (int)sweeperVB[vert].y);
}
LineTo(surface, (int)sweeperVB[0].x, (int)sweeperVB[0].y);
//draw the sweeper right track
MoveToEx(surface, (int)sweeperVB[4].x, (int)sweeperVB[4].y, NULL);
for (int vert=5; vert<8; ++vert)
{
LineTo(surface, (int)sweeperVB[vert].x, (int)sweeperVB[vert].y);
}
LineTo(surface, (int)sweeperVB[4].x, (int)sweeperVB[4].y);
MoveToEx(surface, (int)sweeperVB[8].x, (int)sweeperVB[8].y, NULL);
LineTo(surface, (int)sweeperVB[9].x, (int)sweeperVB[9].y);
MoveToEx(surface, (int)sweeperVB[10].x, (int)sweeperVB[10].y, NULL);
for (int vert=11; vert<16; ++vert)
{
LineTo(surface, (int)sweeperVB[vert].x, (int)sweeperVB[vert].y);
}
}//next sweeper
}
//----------------------------- RenderSensors ----------------------------
//
// renders the sensors of a given vector of sweepers
//------------------------------------------------------------------------
void CController::RenderSensors(HDC &surface, vector<CMinesweeper> &sweepers)
{
//render the sensors
for (int i=0; i<sweepers.size(); ++i)
{
//grab each sweepers sensor data
vector<SPoint> tranSensors = sweepers[i].Sensors();
vector<double> SensorReadings = sweepers[i].SensorReadings();
vector<double> MemoryReadings = sweepers[i].MemoryReadings();
for (int sr=0; sr<tranSensors.size(); ++sr)
{
if (SensorReadings[sr] > 0)
{
SelectObject(surface, m_RedPen);
}
else
{
SelectObject(surface, m_GreyPenDotted);
}
//make sure we clip the drawing of the sensors or we will get
//unwanted artifacts appearing
if (!((fabs(sweepers[i].Position().x - tranSensors[sr].x) >
(CParams::dSensorRange+1))||
(fabs(sweepers[i].Position().y - tranSensors[sr].y) >
(CParams::dSensorRange+1))))
{
MoveToEx(surface,
(int)sweepers[i].Position().x,
(int)sweepers[i].Position().y,
NULL);
LineTo(surface, (int)tranSensors[sr].x, (int)tranSensors[sr].y);
//render the cell sensors
RECT rect;
rect.left = (int)tranSensors[sr].x - 2;
rect.right = (int)tranSensors[sr].x + 2;
rect.top = (int)tranSensors[sr].y - 2;
rect.bottom= (int)tranSensors[sr].y + 2;
if (MemoryReadings[sr] < 0)
{
FillRect(surface, &rect, m_BlueBrush);
}
else
{
FillRect(surface, &rect, m_RedBrush);
}
}
}
}
}
//--------------------------PlotStats-------------------------------------
//
// Given a surface to draw on this function displays some simple stats
//------------------------------------------------------------------------
void CController::PlotStats(HDC surface)const
{
string s = "Generation: " + itos(m_iGenerations);
TextOut(surface, 5, 25, s.c_str(), s.size());
s = "Num Species: " + itos(m_pPop->NumSpecies());
TextOut(surface, 5, 45, s.c_str(), s.size());
s = "Best Fitness so far: " + ftos(m_pPop->BestEverFitness());
TextOut(surface, 5, 5, s.c_str(), s.size());
}
//------------------------------- GetFitnessScores -----------------------
//
// returns a std::vector containing the genomes fitness scores
//------------------------------------------------------------------------
vector<double> CController::GetFitnessScores()const
{
vector<double> scores;
for (int i=0; i<m_vecSweepers.size(); ++i)
{
scores.push_back(m_vecSweepers[i].Fitness());
}
return scores;
}