-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAStar.cpp
490 lines (405 loc) · 14.7 KB
/
AStar.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
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
/*
* AStar.cpp
*
* Created on: 11 Mar 2014
* Author: chris
*/
#include <iostream>
#include <ostream>
#include <vector>
#include <queue>
#include <stdlib.h>
#include <math.h>
#include <cmath>
#include <list>
#include "Node.h"
#include "AStar.h"
/*
* This is an A* (A star) search, which uses the combined hueristic costs and
* the path goal of each node in order to select the optimal path for a given
* state space.
*
* This code was written by me, Chris Edwards ([email protected]) with some help from
* online resources, and using the knowledge gained from the Aberyswyth CS26110 Artificial
* Inteligence module.
*
* The heuristic used in this A* is currently the Manhattan distance which is:
*
* given two coordinates in terms of [x1][y2] and [x2][y2] then the manhattan distance between
* [0][1] and [2][1] is calculated using:
*
* abs((x1 - x2) + (y1 - y2)) thus, 0 - 2 + 1-1 = -2 + 0 = abs(-2) = 2.
*
* This is an admissable heurisic so A* will find the optimal path...
*
* This A* does not currently allow diagonal movements which will obviously impact the optimal
* of the result.
*
* Some resources used for development and resolving problems can be found:
*
* udefined reference error on Node*:
* http://stackoverflow.com/questions/22382566/c-i-change-my-listand-code-so-it-now-handles-node-rather-than-node-objects
* printing the path problems:
* http://stackoverflow.com/questions/22361308/c-after-finding-the-path-from-an-a-search-printing-the-path-continually-prin
* Eclpise CDT compilation problems:
* http://stackoverflow.com/questions/22392540/c-compiling-my-code-in-eclpise-cdt-it-works-fine-but-doing-so-from-the-comman
*
* All of the above, were questions asked me by under the user name Chris Edwards. The code in those
* questions was written by me and only me.
*
* The parameter to this function is a list where the Node* for the optimal path to goal
* will be placed.
*
*/
int StartSearch(std::list<Node*>& path){
/*
* This is a hardcoded representation of th map which will be searched
* as the path finding process. This could potental be loaded from a file
* or #define'd but in all honesty it is not of vital importance, but in terms
* of maintainability for future projects this should be reviewed.
*/
int map[20][20] = {{0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,2},
{0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0},
{0,0,0,0,0,1,1,1,0,0,0,0,1,0,0,0,0,0,0,0},
{0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,0,0,0},
{0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,0,0,0},
{0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,0,0,0},
{0,0,3,0,0,1,1,1,1,1,1,1,0,0,0,0,1,0,0,0},
{0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,1,1,0,0,0},
{0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,1,1,1,1,1,1,0,0,0,1,0,0,0,0,0,0},
{0,0,0,1,1,1,1,1,1,1,0,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0}};
using namespace std;
//we need to store the unexplored nodes, this is also effective a 'priority queue'
list<Node*> openList;
//store the explored nodes, the ordering doesnt really matter here
vector<Node*> closedList;
//list<Node*> path;
Node *end;
Node *start = initiateStart(map);
openList.push_front(start);
cout <<"Start index: x " << start->getX() << " y " <<start->getY() << endl;
/*
* The general algorithm behind this process is:
*
* Put the starting node on the open list
*
*While the open list is not empty
* find the node with the lowest function cost (f = g+h)
* pop the top of the open list
* generate the current bests successors/children
* for each successor
* if successor is the goal, exit and return path
* child's g is equal to the parents g + 1 (each move costs 1)
* child's h = mahattan distance from this point to goal
* child's f = child's g + child's h
* if the child's X and Y are not already in the open list,
* AND
* the child's X and Y are not in the closed list, add to open list
* put the parent on the closed list
*finishe return the path
*
*/
while (!openList.empty()) {
Node *best = openList.front();
//pop_front doesnt return anything, so we have to get the front the pop
openList.pop_front();
if(!checkInClosedList(closedList, best->getX(), best->getY())){
calcManhattanDistance(best, map);
if(best->getValue() == 3){
//path has been found
end = best;
cout <<"end index: x " << end->getX() << " y " <<end->getY() << endl;
checkPath(end, map, path);
}
/*
* We need to check if we can move left. we can only move left
* is the cell in the given occupancy grid to the left of the current
* position is not a 1, and if the current Y value -1 is 0 or greater.
*
* This is because a vaue of 1 for a cell signifies a wall which cant be moved to
* and a Y value less than 0 means we are outside the bounds of the grid
*
*/
if(map[best->getX()][best->getY()-1] != 1 && best->getY() - 1 > -1){
//check this X and Y is not in the open list
if(placeInOpen(openList,best->getX(), best->getY() - 1)){
openList.push_front(generateLeftChild(best, map));
}
}
/*
* We need to check if we can move right. we can only move right
* is the cell in the given occupancy grid to the right of the current
* position is not a 1, and if the current Y value +1 is 19 or less.
*
* This is because a vaue of 1 for a cell signifies a wall which cant be moved to
* and a Y value greater than 19 means we are outside the bounds of the grid
*
*/
if(map[best->getX()][best->getY()+1] != 1 && best->getY() + 1 < 20){
if(placeInOpen(openList,best->getX(), best->getY() + 1)){
openList.push_front(generateRightChild(best, map));
}
}
/*
* We need to check if we can move up. we can only move up
* is the cell in the given occupancy grid to above the current
* position is not a 1, and if the current X value -1 is 0 or greater.
*
* This is because a vaue of 1 for a cell signifies a wall which cant be moved to
* and a Y value less than 0 means we are outside the bounds of the grid
*
*/
if(map[best->getX()-1][best->getY()] != 1 && best->getX() - 1 > -1){
if(placeInOpen(openList,best->getX()-1, best->getY())){
openList.push_front(generateAboveChild(best, map));
}
}
/*
* We need to check if we can move down. we can only move down
* is the cell in the given occupancy grid below the current
* position is not a 1, and if the current X value +1 is 19 or less.
*
* This is because a vaue of 1 for a cell signifies a wall which cant be moved to
* and a Y value above 19 means we are outside the bounds of the grid
*
*/
if(map[best->getX()+1][best->getY()] != 1 && best->getX() + 1 < 20){
if(placeInOpen(openList,best->getX()+1, best->getY())){
openList.push_front(generateBelowChild(best, map));
}
}
//add the current best to the closed list
closedList.push_back(best);
}
/* We need to sort the list now we have added new Nodes. The reason a priority
* queue is not used is because you cannot iterate a priority queue in C++.
*
* I wanted to iterate through the lists to check the indexs of each Node current in
* the open list to prevent the duplicates arrising in the list.
*
* I ended up using the list contatiner with a custom comparator struct for the Node
* Objects, which generates the effect of having an iterateable priority queue.
*/
openList.sort(NodeComparator());
}
return 0;
}
/*
* This is used to locate the starting Node.
*
* Parse the given grid untill a cell has the value 2. When this is
* located create a new Node with the X and Y coordinates of this value
* on the heap and return a pointer to this memory address.
*
*/
Node* initiateStart(int m[20][20]){
Node *start;
for(int i = 0; i < 20; i++){
for(int j = 0; j < 20; j++){
if(m[i][j] == 2){
start = new Node(i, j, m[i][j], 0, NULL, 's');
}
}
}
return start;
}
/*
* Generate the left child of the current parent.
* This is only called if a left move is in fact possible thus there are no checks
* to see if a left move is valid inside this actual method itself.
*
* Create a new Node on the heap with the values:
*
* X = the same X as the parent
* Y = the parents Y - 1
* value = the value of the occupance grid at grid[X][Y]
* g = parent g + 1
* parent = parent
* direction = 'l'
* h = manhattan distance
*
* return a pointer to the memory address on the heap of this new Node.
*/
Node* generateLeftChild(Node *parent, int m[20][20]){
Node *child;
child = new Node(parent->getX(), parent->getY() - 1, m[parent->getX()][parent->getY() - 1],
parent->getGCost() + 1, parent, 'l');
calcManhattanDistance(child, m);
return child;
}
/*
* Generate the right child of the current parent.
* This is only called if a left move is in fact possible thus there are no checks
* to see if a right move is valid inside this actual method itself.
*
* Create a new Node on the heap with the values:
*
* X = the same X as the parent
* Y = the parents Y + 1
* value = the value of the occupance grid at grid[X][Y]
* g = parent g + 1
* parent = parent
* direction = 'r'
* h = manhattan distance
*
* return a pointer to the memory address on the heap of this new Node.
*/
Node* generateRightChild(Node *parent, int m[20][20]){
Node *child;
child = new Node(parent->getX() , parent->getY() + 1, m[parent->getX()][parent->getY() + 1],
parent->getGCost() + 1, parent, 'r');
calcManhattanDistance(child, m);
return child;
}
/*
* Generate the above child of the current parent.
* This is only called if an above move is in fact possible thus there are no checks
* to see if an above move is valid inside this actual method itself.
*
* Create a new Node on the heap with the values:
*
* X = the same X - 1 as the parent
* Y = the parents Y
* value = the value of the occupance grid at grid[X][Y]
* g = parent g + 1
* parent = parent
* direction = 'u'
* h = manhattan distance
*
* return a pointer to the memory address on the heap of this new Node.
*/
Node* generateAboveChild(Node *parent, int m[20][20]){
Node *child;
child = new Node(parent->getX() - 1, parent->getY(), m[parent->getX() - 1][parent->getY()],
parent->getGCost() + 1, parent, 'u');
calcManhattanDistance(child, m);
return child;
}
/*
* Generate the below child of the current parent.
* This is only called if a below move is in fact possible thus there are no checks
* to see if a below move is valid inside this actual method itself.
*
* Create a new Node on the heap with the values:
*
* X = the same X + 1 as the parent
* Y = the parents Y
* value = the value of the occupance grid at grid[X][Y]
* g = parent g + 1
* parent = parent
* direction = 'u'
* h = manhattan distance
*
* return a pointer to the memory address on the heap of this new Node.
*/
Node* generateBelowChild(Node *parent, int m[20][20]){
Node *child;
child = new Node(parent->getX() + 1, parent->getY(), m[parent->getX() + 1][parent->getY()],
parent->getGCost() + 1, parent, 'd');
calcManhattanDistance(child, m);
return child;
}
/*
* This is the heruitic function for this A* search algorithm.
*
* It uses the mahattan distance which is defined as:
*
* definition from: http://xlinux.nist.gov/dads/HTML/manhattanDistance.html
*
* "The distance between two points measured along axes at right angles.
* In a plane with p1 at (x1, y1) and p2 at (x2, y2), it is |x1 - x2| + |y1 - y2|."
*
* This herutitic is admissible because it does not overestimate the cost to the goal, so an
* optimal solution will always be found.
*
*/
void calcManhattanDistance(Node *node, int m[20][20]){
int tempX;
int tempY;
double manhattanDistance;
int differenceX;
int differenceY;
//std::cout << "node x: " << node->getX() << " node y: " << node->getY() << std::endl;
for(int i = 0; i < 20; i++){
for(int j = 0; j < 20; j++){
if(m[i][j] == 3){
tempX = i;
tempY = j;
}
}
}
//sum of term difference, none of these can be negative hense the std::abs
differenceX = tempX - node->getX();
differenceY = tempY - node->getY();
manhattanDistance = std::abs(differenceX) + std::abs(differenceY);
//std::cout << "Manhattan distance: " << manhattanDistance << std::endl;
node->setHCost(manhattanDistance);
}
/*
* This function returns a boolean value which is directly related to the contents of
* the current closed list vector.
*
* Iterate through the members of the closed list and check the values of the
* current Node* and it's X and Y against the X argument and Y argument of the function.
*
* If the current Node* X and Y values are identical to the argument X and Y, then return true.
* If there is no matched values for BOTH X and Y, then return false.
*/
bool checkInClosedList(std::vector<Node*>& v,int x, int y){
for (std::vector<Node*>::iterator iter = v.begin(); iter != v.end(); ++iter) {
if((*iter)->getX() == x && (*iter)->getY() == y){
return true;
}
}
return false;
}
/*
* This function returns a boolean value which is directly related to the contents of
* the current open list container.
*
* Iterate through the members of the open list and check the values of the
* current Node* and it's X and Y against the X argument and Y argument of the function.
*
* If the current Node* X and Y values are identical to the argument X and Y, then return true.
* If there is no matched values for BOTH X and Y, then return false.
*
* The list<Node*> argument is simply a reference to the current openList and its elements.
*/
bool placeInOpen(std::list<Node*>& v,int x, int y){
for (std::list<Node*>::iterator iter = v.begin(); iter != v.end(); ++iter) {
if((*iter)->getX() == x && (*iter)->getY() == y){
return false;
}
}
return true;
}
/*
* This function is used to check the route returned from the A* search algorithm.
* Also this function is used to populate a list which contains a list of Node* which
* represent the goal state. This can then be used and transvered in order to pass the
* required movements from start to goal to the robot or another medium.
*
* The passed in arguments are the a Node* which points to the goal node, the current occupancy grid
* which is of type int[20][20] and the list<Node*> where to path will be stored.
*/
void checkPath(Node *end, int m[20][20], std::list<Node*>& path){
int tempX, tempY;
Node *temp = end;
while(temp != NULL){
path.push_front(temp);
tempX = temp->getX();
tempY = temp->getY();
m[tempX][tempY] = 4;
temp = temp->getParent();
}
}