-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPAIDesert.cpp
More file actions
751 lines (623 loc) · 15 KB
/
Copy pathPAIDesert.cpp
File metadata and controls
751 lines (623 loc) · 15 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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
// ---------------------------------------------------------------------
// PAIDesert Implementation
// ---------------------------------------------------------------------
#include "PAIDesert.h"
// Other includes
#include "random.h"
#include <time.h>
#include <iostream>
#include <CoreStructures\GUVector4.h>
// Namespace use
using namespace CoreStructures;
using namespace std;
// Static varibles (for fitness evaluation)
static int startMap[20][20];
static int map[20][20];
static int renderMap[20][20];
static GUVector4 antPos[20];
// Data collection
static std::vector<char> actions[20];
static int collectIndex = 0;
static bool collectData = 0;
// ---------------------------------------------------------------------
// Fitness Functions/Problem specific functions
// ---------------------------------------------------------------------
// Termination Criteria
// Precondition: GP setup/this function set as termination criteria
// Postcondition: GP will stop generating once a program has a hit
int desertTermination(GP* g)
{
if(g->bestofgen_hits > 0)
return 1;
else
return 0;
}
// Init map
// Precondition: startMap setup
// Postcondition: map is reset
void initialiseMap()
{
for(int i = 0; i < 20; i += 1)
{
for(int j = 0; j < 10; j += 1)
{
map[j][i] = startMap[j][i];
}
}
}
// Fitness function for ant problem
// Precondition: GP setup and this function added as fitness function
// Postcondition: Fitness tested
float antFitness(S_Expression* s, int* hits)
{
// Reset Map
initialiseMap();
// Setup fitness variable
float fitness = 0;
for(int i = 0; i < 20; i += 1)
{
// Set x and y to ant positions
Tset.modify("X", antPos[i].x);
Tset.modify("Y", antPos[i].z);
// Loop to run program - 300 moves
for(int j = 0; j < 300; j += 1)
{
// Run the program
s->eval();
// Ant should have dropped sand
if(Tset.get("CARRYING") > 0)
{
fitness += 40;
// Set carrying back to default
Tset.modify("CARRYING", -1);
}
}
}
// Nested loop checks positions of sand
for(int i = 0; i < 20; i += 1)
{
for(int j = 0; j < 20; j += 1)
{
switch(map[j][i])
{
case 1:
if(j != 0)
fitness += j;
break;
case 2:
if(j == 0)
fitness += 1;
else if(j != 1)
fitness += j;
break;
case 3:
if(j == 0)
fitness += 2;
else if(j == 1)
fitness += 1;
else if(j != 2)
fitness += j;
break;
}
}
}
if(fitness == 0)
*hits += 1;
return fitness;
}
// Function Set
// Update Colour
// Precondition: GP setup and "COLOUR" added as terminal
// Postcondition: "COLOUR" set to colour of grain at terminal positon x/y
float checkColour()
{
// Get current position
int X = (int)Tset.get("X");
int Y = (int)Tset.get("Y");
// If there is something at map positon
if(map[X][Y])
{
Tset.modify("COLOUR", map[X][Y]);
return map[X][Y];
}
else
{
Tset.modify("COLOUR", -1);
return -1;
}
}
// Movement
// Precondition: GP setup and terminals "X" and "Y" have been added
// Postcondition: "Y" altered to move north
float goNorth(S_Expression** params)
{
// Update data collection
if(collectData)
actions[collectIndex].push_back('N');
// Get current position
int X = (int)Tset.get("X");
int Y = (int)Tset.get("Y");
// Check if the ant moves off the grid
if(Y <= 0)
Tset.modify("Y", 19);
else
Tset.modify("Y", Y - 1);
return checkColour();
}
// Precondition: GP setup and terminals "X" and "Y" have been added
// Postcondition: "X" altered to move east
float goEast(S_Expression** params)
{
// Update data collection
if(collectData)
actions[collectIndex].push_back('E');
// Get current position
int X = (int)Tset.get("X");
int Y = (int)Tset.get("Y");
// Check if the ant moves off the grid
if(X >= 19)
Tset.modify("X", 0);
else
Tset.modify("X", X + 1);
return checkColour();
}
// Precondition: GP setup and terminals "X" and "Y" have been added
// Postcondition: "Y" altered to move south
float goSouth(S_Expression** param)
{
// Update data collection
if(collectData)
actions[collectIndex].push_back('S');
// Get current position
int X = (int)Tset.get("X");
int Y = (int)Tset.get("Y");
// Check if the ant moves off the grid
if(Y >= 19)
Tset.modify("Y", 0);
else
Tset.modify("Y", Y + 1);
return checkColour();
}
// Precondition: GP setup and terminals "X" and "Y" have been added
// Postcondition: "X" altered to move west
float goWest(S_Expression** params)
{
// Update data collection
if(collectData)
actions[collectIndex].push_back('W');
// Get current position
int X = (int)Tset.get("X");
int Y = (int)Tset.get("Y");
// Check if the ant moves off the grid
if(X <= 0)
Tset.modify("X", 19);
else
Tset.modify("X", X - 1);
return checkColour();
}
// Precondition: n/a
// Postcondition: One of the four functions above are called
float goRandom(S_Expression** params)
{
int ran = rand() % 4;
switch(ran)
{
case 0:
return goNorth(params);
case 1:
return goEast(params);
case 2:
return goSouth(params);
case 3:
return goWest(params);
}
}
// Pick up
// Precondition: GP setup and terminals "X", "Y" and "CARRYING" have been added
// Postcondition: "CARRYING" updated to sand grain colour
float pickUp(S_Expression** params)
{
// Update data collection
if(collectData)
actions[collectIndex].push_back('P');
// Get current position
int X = (int)Tset.get("X");
int Y = (int)Tset.get("Y");
// Check if the ant is carrying sand
if(Tset.get("CARRYING") > 0)
return map[X][Y];
// Check if there is something at the position
if(map[X][Y] > 0)
{
// Pick up the sand
Tset.modify("CARRYING", map[X][Y]);
// Remove the sand from the map
map[X][Y] = 0;
// Return what's left, -1
return -1;
}
// If there isn't anything at the x, y pos
// Return -1
return -1;
}
// IFLTE - If less then or equal
float iflte(S_Expression** params)
{
if (params[0]->eval() <= params[1]->eval())
return (params[2]->eval());
else
return (params[3]->eval());
}
// IFLTZ - If less than zero
float ifltz(S_Expression** params)
{
if (params[0]->eval() < 0)
return (params[1]->eval());
else
return (params[2]->eval());
}
// IF-DROP
// Precondition: GP setup and terminals "X", "Y" and "CARRYING" have been added
// Postcondition: "CARRYING" updated to sand grain colour/map position updated
float ifDrop(S_Expression** params)
{
// Update data collection
if(collectData)
actions[collectIndex].push_back('D');
// Get current position
int X = (int)Tset.get("X");
int Y = (int)Tset.get("Y");
// Check if sand is being carried
if(Tset.get("CARRYING") > 0)
{
// Check that the current x, y is empty
if(map[X][Y] == 0)
{
// Drops the sand on the map
map[X][Y] == Tset.get("CARRYING");
Tset.modify("CARRYING", -1);
return params[0]->eval();
}
}
return params[1]->eval();
}
// ---------------------------------------------------------------------
// PAIDesert class functions
// ---------------------------------------------------------------------
// PRIVATE
// ---------------------------------------------------------------------
// Problem specific functions
// Movement
// Pre and post conditions apply for each of the movement functions below
// Precondition: An ant exists
// Postcondition: ant[antIndex]'s x and y positions are adjusted
void PAIDesert::AIMoveNorth(int antIndex)
{
// Get current position
int X = ant[antIndex].getPosition().x;
int Y = ant[antIndex].getPosition().z;
// Check if the ant moves off the grid
if(Y <= 0)
ant[antIndex].setPosition(X, 0, 19);
else
ant[antIndex].setPosition(X, 0, Y - 1);
}
void PAIDesert::AIMoveEast(int antIndex)
{
// Get current position
int X = ant[antIndex].getPosition().x;
int Y = ant[antIndex].getPosition().z;
// Check if the ant moves off the grid
if(X >= 19)
ant[antIndex].setPosition(0, 0, Y);
else
ant[antIndex].setPosition(X + 1, 0.0, Y);
}
void PAIDesert::AIMoveSouth(int antIndex)
{
// Get current position
int X = ant[antIndex].getPosition().x;
int Y = ant[antIndex].getPosition().z;
// Check if the ant moves off the grid
if(Y >= 19)
ant[antIndex].setPosition(X, 0, 0);
else
ant[antIndex].setPosition(X, 0, Y + 1);
}
void PAIDesert::AIMoveWest(int antIndex)
{
// Get current position
int X = ant[antIndex].getPosition().x;
int Y = ant[antIndex].getPosition().z;
// Check if the ant moves off the grid
if(X <= 0)
ant[antIndex].setPosition(19, 0, Y);
else
ant[antIndex].setPosition(X - 1, 0, Y);
}
// Precondition: An ant exists
// Postcondition: ant[antIndex]'s carrying and rendermap values adjusted
void PAIDesert::AIPickUp(int antIndex)
{
// Get current position
int X = ant[antIndex].getPosition().x;
int Y = ant[antIndex].getPosition().z;
// Check if the ant is carrying sand
if(ant[antIndex].carrying > 0)
return;
// Check if there is something at the position
if(renderMap[X][Y])
{
// Pick up the sand
ant[antIndex].carrying = renderMap[X][Y];
// Remove the sand from the map
renderMap[X][Y] = 0;
}
}
// Precondition: An ant exists
// Postcondition: ant[antIndex]'s carrying and rendermap values adjusted
void PAIDesert::AIIfDrop(int antIndex)
{
// Get current position
int X = ant[antIndex].getPosition().x;
int Y = ant[antIndex].getPosition().z;
// Check if sand is being carried
if(ant[antIndex].carrying > 0)
{
// Check that the current x, y is empty
if(renderMap[X][Y] == 0)
{
// Drops the sand on the map
renderMap[X][Y] = ant[antIndex].carrying;
ant[antIndex].carrying = 0;
}
}
}
// Setup Objects
// Precondition: n/a
// Postcondition: All ants and grains positions set
void PAIDesert::setupObjects()
{
// Initialise all of the boxes
black.Initialise();
grey.Initialise();
white.Initialise();
// Set correct textures
black.setTexture(L"Resources\\Textures\\black.png");
grey.setTexture(L"Resources\\Textures\\grey.png");
white.setTexture(L"Resources\\Textures\\white.png");
for(int i = 0; i < 20; i += 1)
{
// int for whether positions are set
int set = 0;
// Initialise ant
ant[i].Initialise();
// Set random initial positions
ant[i].setPosition(GUVector4((int)(rand() % 20), 0.0, (int)(rand() % 20)));
antPos[i] = ant[i].getPosition();
while(set < 3)
{
// Generate random Vector
GUVector4 randVec = GUVector4( (int)(rand() % 20), 0.0, (int)(rand() % 20));
// Check if it has already been taken
if(startMap[(int)(randVec.x)][(int)randVec.z] == 0)
{
// Assign if it's not taken
switch(set)
{
case 0:
startMap[(int)(randVec.x)][(int)randVec.z] = 1;
set += 1;
break;
case 1:
startMap[(int)(randVec.x)][(int)randVec.z] = 2;
set += 1;
break;
case 2:
startMap[(int)(randVec.x)][(int)randVec.z] = 3;
set += 1;
break;
}
}
}
}
}
// Run best individual
// Precondition: GP setup and best individual acquired
// Postcondition: Data in the form of character string collected
void PAIDesert::runBestIndividual()
{
collectData = 1;
for(collectIndex = 0; collectIndex < 20; collectIndex += 1)
{
actions[collectIndex].clear();
// Set x and y to ant positions
Tset.modify("X", antPos[collectIndex].x);
Tset.modify("Y", antPos[collectIndex].z);
// Loop to run program - 50 moves
for(int i = 0; i < 300; i += 1)
{
best.s->eval();
}
}
collectData = 0;
}
// Initialise render map
// Precondition: startMap setup
// Postcondition: rendermap is reset
void PAIDesert::initialiseRendermap()
{
for(int i = 0; i < 20; i += 1)
{
for(int j = 0; j < 20; j += 1)
{
renderMap[j][i] = startMap[j][i];
}
}
}
// ---------------------------------------------------------------------
// PUBLIC
// ---------------------------------------------------------------------
// Constructor
PAIDesert::PAIDesert()
{
// Set initial values
current = 0;
moveTime = 0;
move = 0;
gp = NULL;
}
// Deconstructor
PAIDesert::~PAIDesert()
{
// EMPTY
}
// Initialise
// Precondition: N/A
// Postcondition: GP/Desert AI setup complete
void PAIDesert::initialise()
{
// Init objects
setupObjects();
// Init fitness based variables
initialiseMap();
initialiseRendermap();
// 1. Specify terminal set
myTSet.add("X");
myTSet.add("Y");
myTSet.add("CARRYING", -1.0);
myTSet.add("COLOUR", -1.0);
// 2. Specify function set
myFSet.add("GO-N", 0, *goNorth, NULL, 1);
myFSet.add("GO-E", 0, *goEast, NULL, 1);
myFSet.add("GO-S", 0, *goSouth, NULL, 1);
myFSet.add("GO-W", 0, *goWest, NULL, 1);
myFSet.add("GO-Rand", 0, *goRandom, NULL, 1);
myFSet.add("PICK-UP", 0, *pickUp, NULL, 1);
myFSet.add("IFLTE", 4, *iflte, NULL);
myFSet.add("IFLTZ", 3, *ifltz, NULL);
myFSet.add("IF-DROP", 2, *ifDrop, NULL);
// 3. Precalculate your set of fitness test cases
// NONE
// 4. Declare the GP object
if(!gp)
gp = new GP((*antFitness), 100);
// 5. Specify any non-default GP parameters
gp->verbose = DEBUG | END_REPORT;
gp->termination_criteria = *desertTermination;
}
// Run GP and get best individual so far
// Precondition: GP setup
// Postcondition: A run of 10 generations is started (and data collected)
void PAIDesert::run()
{
// Reset variables
initialiseMap();
initialiseRendermap();
current = 0;
// Set ants to default positions and variables
for(int i = 0; i < 20; i += 1)
{
ant[i].carrying = 0;
ant[i].setPosition(antPos[i]);
}
// 1. Start the evolution
gp->go(gp->gen + 10);
// 2. Run best and collect data
best = gp->best_of_run;
runBestIndividual();
// 3. Examine results
move = 1;
}
// Generate new population
// Precondition: GP setup
// Postcondition: GP is restarted, forced to generate a new population
void PAIDesert::refreshPop()
{
// Set GP generation to 0
// Forces a regeneration on next run
gp->gen = 0;
// Run
run();
}
// Update
// Precondition: n/a
// Postcondition: All ants and objects updated
void PAIDesert::update(float delta)
{
if(move)
{
moveTime += delta;
if(moveTime > 0.01)
{
moveTime = 0;
current += 1;
// Move everything to the next place
for(int i = 0; i < 20; i += 1)
{
if(current < actions[i].size())
{
switch(actions[i][current])
{
case 'N': // North
AIMoveNorth(i);
break;
case 'E': // East
AIMoveEast(i);
break;
case 'S': // South
AIMoveSouth(i);
break;
case 'W': // West
AIMoveWest(i);
break;
case 'P': // Pickup
AIPickUp(i);
break;
case 'D': // Drop
AIIfDrop(i);
break;
}
}
}
}
}
}
// Render
// Precondition: n/a
// Postcondition: All ants and objects rendered
void PAIDesert::render(const CoreStructures::GUMatrix4& T)
{
// Render each of the boxes
for(int i = 0; i < 20; i += 1)
{
ant[i].Render(T);
for(int j = 0; j < 20; j += 1)
{
if(renderMap[j][i] == 1)
{
black.setPosition(j, 0.0, i);
black.Render(T);
}
else if(renderMap[j][i] == 2)
{
grey.setPosition(j, 0.0, i);
grey.Render(T);
}
else if(renderMap[j][i] == 3)
{
white.setPosition(j, 0.0, i);
white.Render(T);
}
}
}
}
// Set in use
// Precondition: FunctionSet setup
// Postcondition: Active functions set is the DesertAI function set
void PAIDesert::setInUse()
{
// Set the terminal and function sets
Fset = myFSet;
Tset = myTSet;
}