-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCCarController.cpp
More file actions
370 lines (304 loc) · 10 KB
/
CCarController.cpp
File metadata and controls
370 lines (304 loc) · 10 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
#include "CCarController.h"
//---------------------------------------constructor---------------------
//
// initialize the cars, their brains and the GA factory
//
//-----------------------------------------------------------------------
CCarController::CCarController(HWND hwndMain,
int cxClient,
int cyClient): m_NumCars(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_iViewThisCar(0)
{
//let's create the cars
for (int i=0; i<m_NumCars; ++i)
{
m_vecCars.push_back(CCar());
}
//and the vector of cars which will hold the best performing cars
for (int i=0; i<CParams::iNumBestSweepers; ++i)
{
m_vecBestCars.push_back(CCar());
}
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_NumCars; i++)
{
m_vecCars[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));
}
//--------------------------------------destructor-------------------------------------
//
//--------------------------------------------------------------------------------------
CCarController::~CCarController()
{
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 CCarController::Update()
{
// evaluate the cars one by one, each running NUM_TICKS cycles
//first add up each cars fitness scores
for (int swp=0; swp<m_vecCars.size(); ++swp)
{
m_vecCars[swp].EndOfRunCalculations();
}
//We have completed another generation so now we need to run the GA
//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
// also sum fitness values, so we can report average of this generation
float avgFitness = 0;
float bestFitness = 0;
int i = 0;
for (i=0; i<m_NumCars; ++i)
{
float sweeperFitness = m_vecCars[i].Fitness();
if(sweeperFitness > bestFitness) bestFitness = sweeperFitness;
avgFitness += sweeperFitness;
m_vecCars[i].InsertNewBrain(pBrains[i]);
m_vecCars[i].Reset();
}
avgFitness /= i;
// Write average fitness and best fitness for this generation out to be appended to a file
std::ofstream outfile;
outfile.open("fitnessValues.txt", std::ios_base::app);
if(m_iGenerations == 1) {
outfile << " -----------\n"; // Signify new run
outfile << "\"Generation\";\"Best\";\"Avg\"\n";
}
else
{
outfile << string("\"" + itos(m_iGenerations-1) + "\"");
outfile << string(";");
outfile << string("\"") << bestFitness << string("\"");
outfile << string(";");
outfile << string("\"") << avgFitness << string("\"");
outfile << "\n";
}
//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 & save them to file
for (int i=0; i<m_vecBestCars.size(); ++i)
{
m_vecBestCars[i].InsertNewBrain(pBestBrains[i]);
m_vecBestCars[i].Reset();
// Index 0 is the best genome, index 1 the second best and so on
string text = string("g") + to_string((long double) (m_iGenerations-1)) + string(".") + to_string((long double) i) + string(".genome");
//const char *text = "test";//m_iGenerations-1+"."+i; //+"Test";
m_pPop->WriteGenome(text.c_str(), i);
}
//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 CCarController::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 m_cars best networks
for(int i = 0; i < CParams::iNumBestSweepers; i++)
{
if(i == 0) m_vecBestCars[0].DrawNet(surface, 0, cxInfo/2, cyInfo/2, 0);
else if(i == 1) m_vecBestCars[1].DrawNet(surface, cxInfo/2, cxInfo, cyInfo/2, 0);
else if(i == 2) m_vecBestCars[2].DrawNet(surface, 0, cxInfo/2, cyInfo, cyInfo/2);
else if(i == 3) m_vecBestCars[3].DrawNet(surface, cxInfo/2, cxInfo, cyInfo, cyInfo/2);
}
}
//------------------------------------Render()--------------------------------------
//
//----------------------------------------------------------------------------------
void CCarController::Render(HDC &surface)
{
// only render stats
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 CCarController::RenderCars(HDC &surface, vector<CCar> &cars)
{
return;
/*
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 CCarController::RenderSensors(HDC &surface, vector<CCar> &cars)
{
/*
//render the sensors
for (int i=0; i<cars.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 CCarController::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> CCarController::GetFitnessScores()const
{
vector<double> scores;
for (int i=0; i<m_vecCars.size(); ++i)
{
scores.push_back(m_vecCars[i].Fitness());
}
return scores;
}