-
Notifications
You must be signed in to change notification settings - Fork 1
/
CMinesweeper.cpp
402 lines (314 loc) · 8.96 KB
/
CMinesweeper.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
#include "CMinesweeper.h"
//-----------------------------------constructor-------------------------
//
//-----------------------------------------------------------------------
CMinesweeper::CMinesweeper():
m_dRotation(0),
m_lTrack(0),
//m_rTrack(0),
m_dFitness(0),
m_dScale(CParams::iSweeperScale),
m_bCollided(false)
{
m_Color = CreatePen(PS_SOLID, 1, RGB(rand() % 205 + 50, rand() % 205 + 50, rand() % 205 + 50));
//create a static start position
m_vPosition = SVector2D(180, 50);
//create the sensors
CreateSensors(m_Sensors, CParams::iNumSensors, CParams::dSensorRange);
//initialize its memory
m_MemoryMap.Init(CParams::WindowWidth,
CParams::WindowHeight);
}
//-------------------------------- CreateSensors ------------------------
//
// This function returns a vector of points which make up the segments of
// the sweepers sensors.
//------------------------------------------------------------------------
void CMinesweeper::CreateSensors(vector<SPoint> &sensors,
int NumSensors,
double range)
{
//make sure vector of sensors is empty before proceeding
sensors.clear();
//double SegmentAngle = CParams::dPi / (NumSensors-1);
////going clockwise from 90deg left of position calculate the fan of
////points radiating out (not including the origin)
//for (int i=0; i<CParams::iNumSensors; i++)
//{
// //calculate vertex position
// SPoint point;
// point.x = -sin(i * SegmentAngle - CParams::dHalfPi) * range;
// point.y = cos(i * SegmentAngle - CParams::dHalfPi) * range;
// sensors.push_back(point);
//}//next segment
SPoint point;
point.x = -sin((double)-0.5235) * range;
point.y = cos((double)-0.5235) * range;
sensors.push_back(point);
SPoint point2;
point2.x = -sin((double)0.5235) * range;
point2.y = cos((double)0.5235) * range;
sensors.push_back(point2);
}
//-----------------------------Reset()------------------------------------
//
// Resets the sweepers position, fitness and rotation
//
//------------------------------------------------------------------------
void CMinesweeper::Reset()
{
//reset the sweepers positions
m_vPosition = SVector2D(180, 50);
//and the fitness
m_dFitness = 0;
//and the rotation
m_dRotation = 0;
m_bReverse = false;
//reset its memory
m_MemoryMap.Reset();
m_bActive = true;
}
void CMinesweeper::SetReverse(bool reverse)
{
m_bReverse = reverse;
}
int CMinesweeper::ResetTrial(int generation)
{
double reward = m_MemoryMap.TMazeRewardF(m_bReverse, m_vPosition.x, m_vPosition.y);
m_dFitness += generation > 0 ? reward : 0;
//m_dFitness += generation > 0 ? m_MemoryMap.TMazeReward(m_bReverse) : 0;
m_vPosition = SVector2D(180, 50);
m_dRotation = 0;
//reset its memory
m_MemoryMap.Reset();
m_bActive = true;
if (reward < 0.01) {
return 0;
}
if (m_bReverse) {
if (reward > 0.9) {
return -1;
}
else {
return 1;
}
} else {
if (reward > 0.9) {
return 1;
}
else {
return -1;
}
}
}
//------------------------- RenderMemory ---------------------------------
//
//------------------------------------------------------------------------
void CMinesweeper::Render(HDC surface)
{
//render the memory
m_MemoryMap.Render(surface);
string s = itos(m_MemoryMap.NumCellsVisited());
s = "Num Cells Visited: " + s;
TextOut(surface, 220,0,s.c_str(), s.size());
}
//---------------------WorldTransform--------------------------------
//
// sets up a translation matrix for the sweeper according to its
// scale, rotation and position. Returns the transformed vertices.
//-------------------------------------------------------------------
void CMinesweeper::WorldTransform(vector<SPoint> &sweeper, double scale)
{
//create the world transformation matrix
C2DMatrix matTransform;
//scale
matTransform.Scale(scale, scale);
//rotate
matTransform.Rotate(m_dRotation);
//and translate
matTransform.Translate(m_vPosition.x, m_vPosition.y);
//now transform the ships vertices
matTransform.TransformSPoints(sweeper);
}
//-------------------------------Update()--------------------------------
//
// First we take sensor readings and feed these into the sweepers brain.
//
// The inputs are:
//
// The readings from the minesweepers sensors
//
// We receive two outputs from the brain.. lTrack & rTrack.
// So given a force for each track we calculate the resultant rotation
// and acceleration and apply to current velocity vector.
//
//-----------------------------------------------------------------------
bool CMinesweeper::Update(vector<SPoint> &objects)
{
if (m_bActive) {
//this will store all the inputs for the NN
vector<double> inputs;
//grab sensor readings
TestSensors(objects);
//input sensors into net
for (int sr=0; sr<m_vecdSensors.size(); ++sr)
{
inputs.push_back(m_vecdSensors[sr]);
// inputs.push_back(m_vecFeelers[sr]); // No need for feelers
}
// inputs.push_back(m_bCollided);
double reward = m_MemoryMap.CheckReward(m_vPosition.x, m_vPosition.y, m_bReverse);
if (reward > 0.9) {
reward = 0.1;
}
else if (reward > 0) {
reward = 1;
}
inputs.push_back(reward);
double turningPoint = m_MemoryMap.CheckTurningPoint(m_vPosition.x, m_vPosition.y);
inputs.push_back(turningPoint);
vector<double> output;
if (reward < 0.01) {
//update the brain and get feedback
output = m_pItsBrain->Update(inputs, CNeuralNet::active);
} else {
output = m_pItsBrain->Update(inputs, CNeuralNet::snapshot);
}
//make sure there were no errors in calculating the
//output
if (output.size() < CParams::iNumOutputs)
{
return false;
}
//assign the outputs to the sweepers left & right tracks
// m_lTrack = output[0] * 2 - 1;
//m_rTrack = output[1];
m_lTrack = output[0] * 2 - 1;
//calculate steering forces
//double RotForce = m_lTrack - m_rTrack;
//clamp rotation
//Clamp(RotForce, -CParams::dMaxTurnRate, CParams::dMaxTurnRate);
//if(m_lTrack < -0.3) m_dRotation = 0;//3.14159265358979;//3.14159265358979f * (3.0 / 2.0);
//else if(m_lTrack > 0.3) m_dRotation = 3.1415926358979f * 0.5;
//else m_dRotation = 3.1415926358979f * 1.5;
m_dRotation += m_lTrack;
//update Look At
m_vLookAt.x = -sin(m_dRotation);
m_vLookAt.y = cos(m_dRotation);
//if the sweepers haven't collided with an obstacle
//update their position
if (!m_bCollided)
{
m_dSpeed = 3;// + m_rTrack;
//m_dSpeed *= 2;
//update position
m_vPosition += (m_vLookAt * m_dSpeed);
//test range of x,y values - because of 'cheap' collision detection
//this can go into error when using < 4 sensors
TestRange();
}
//update the memory map
m_MemoryMap.Update(m_vPosition.x, m_vPosition.y);
m_bActive = reward < 0.05;
return true;
}
return true;
}
//----------------------- TestSensors ------------------------------------
//
// This function checks for any intersections between the sweeper's
// sensors and the objects in its environment
//------------------------------------------------------------------------
void CMinesweeper::TestSensors(vector<SPoint> &objects)
{
m_bCollided = false;
//first we transform the sensors into world coordinates
m_tranSensors = m_Sensors;
WorldTransform(m_tranSensors, 1); //scale is 1
//flush the sensors
m_vecdSensors.clear();
m_vecFeelers.clear();
//now to check each sensor against the objects in the world
for (int sr=0; sr<m_tranSensors.size(); ++sr)
{
bool bHit = false;
double dist = 0;
for (int seg=0; seg<objects.size(); seg+=2)
{
if (LineIntersection2D(SPoint(m_vPosition.x, m_vPosition.y),
m_tranSensors[sr],
objects[seg],
objects[seg+1],
dist))
{
bHit = true;
break;
}
}
if (bHit)
{
m_vecdSensors.push_back(dist);
//implement very simple collision detection
if (dist < CParams::dCollisionDist)
{
m_bCollided = true;
}
}
else
{
m_vecdSensors.push_back(-1);
}
}//next sensor
if (!m_bCollided) {
if (m_vPosition.y < 15) {
m_bCollided = true;
return;
}
if (m_vPosition.y > 170) {
m_bCollided = true;
return;
}
if (m_vPosition.y < 100) {
if (m_vPosition.x > 230) {
m_bCollided = true;
return;
}
if (m_vPosition.x < 150) {
m_bCollided = true;
return;
}
}
}
}
//-------------------------------- TestRange -----------------------------
//
//------------------------------------------------------------------------
void CMinesweeper::TestRange()
{
if (m_vPosition.x < 0)
{
m_vPosition.x = 0;
}
if (m_vPosition.x > CParams::WindowWidth)
{
m_vPosition.x = CParams::WindowWidth;
}
if (m_vPosition.y < 0)
{
m_vPosition.y = 0;
}
if (m_vPosition.y > CParams::WindowHeight)
{
m_vPosition.y = CParams::WindowHeight;
}
}
//------------------------- EndOfRunCalculations() -----------------------
//
//------------------------------------------------------------------------
void CMinesweeper::EndOfRunCalculations()
{
//m_dFitness += m_MemoryMap.NumCellsVisited();
//m_dFitness += m_MemoryMap.TMazeReward();
// Fitness is the average reward/fitness over number of trials
m_dFitness = m_dFitness / CParams::iNumTrials;
}