-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha_star.cpp
More file actions
266 lines (227 loc) · 8.04 KB
/
a_star.cpp
File metadata and controls
266 lines (227 loc) · 8.04 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
#include "a_star.h"
#include <cmath>
#include <cstddef>
#include <cstdio>
using namespace a_star;
static bool isValid(int row, int col, int rows, int cols)
{
return (row >= 0) && (row < rows) &&
(col >= 0) && (col < cols);
}
static bool isUnBlocked(const std::vector<std::vector<int>>& grid, int row, int col)
{
if (grid[row][col] == 1)
return (true);
else
return (false);
}
static bool isDestination(int row, int col, Pair dest)
{
if (row == dest.first && col == dest.second)
return (true);
else
return (false);
}
static double calculateHValue(int row, int col, Pair dest)
{
return ((double)sqrt ((row-dest.first)*(row-dest.first)
+ (col-dest.second)*(col-dest.second)));
}
static std::stack<Pair> tracePath(const std::vector<std::vector<cell>>& cellDetails, Pair dest)
{
int row = dest.first;
int col = dest.second;
std::stack<Pair> Path;
while (!(cellDetails[row][col].parent_i == row &&
cellDetails[row][col].parent_j == col ))
{
Path.push (std::make_pair (row, col));
int temp_row = cellDetails[row][col].parent_i;
int temp_col = cellDetails[row][col].parent_j;
row = temp_row;
col = temp_col;
}
Path.push (std::make_pair (row, col));
return Path;
}
std::stack<Pair> a_star::aStarSearch(const std::vector<std::vector<int>>& grid, Pair src, Pair dest)
{
const size_t rows = grid.size();
const size_t cols = grid[0].size();
// If the source is out of range
if (isValid (src.first, src.second, rows, cols) == false)
{
printf ("Source is invalid\n");
return std::stack<Pair>();
}
// If the destination is out of range
if (isValid (dest.first, dest.second, rows, cols) == false)
{
printf ("Destination is invalid\n");
return std::stack<Pair>();
}
// Either the source or the destination is blocked
if (isUnBlocked(grid, src.first, src.second) == false ||
isUnBlocked(grid, dest.first, dest.second) == false)
{
printf ("Source or the destination is blocked\n");
return std::stack<Pair>();
}
// If the destination cell is the same as source cell
if (isDestination(src.first, src.second, dest) == true)
{
printf ("We are already at the destination\n");
return std::stack<Pair>();
}
std::vector<std::vector<bool>> closedList{rows, std::vector<bool>(cols, false)};
std::vector<std::vector<cell>> cellDetails{rows, std::vector<cell>{cols, {0,0,0,0,0}}};
size_t i, j;
for (i=0; i<rows; i++)
{
for (j=0; j<cols; j++)
{
cellDetails[i][j].f = FLT_MAX;
cellDetails[i][j].g = FLT_MAX;
cellDetails[i][j].h = FLT_MAX;
cellDetails[i][j].parent_i = -1;
cellDetails[i][j].parent_j = -1;
}
}
// Initialising the parameters of the starting node
i = src.first, j = src.second;
cellDetails[i][j].f = 0.0;
cellDetails[i][j].g = 0.0;
cellDetails[i][j].h = 0.0;
cellDetails[i][j].parent_i = i;
cellDetails[i][j].parent_j = j;
std::set<pPair> openList;
openList.insert(std::make_pair (0.0, std::make_pair (i, j)));
bool foundDest = false;
while (!openList.empty())
{
pPair p = *openList.begin();
openList.erase(openList.begin());
i = p.second.first;
j = p.second.second;
closedList[i][j] = true;
double gNew, hNew, fNew;
if (isValid(i-1, j, rows, cols))
{
if (isDestination(i-1, j, dest))
{
cellDetails[i-1][j].parent_i = i;
cellDetails[i-1][j].parent_j = j;
foundDest = true;
return tracePath (cellDetails, dest);
}
else if (closedList[i-1][j] == false &&
isUnBlocked(grid, i-1, j) == true)
{
gNew = cellDetails[i][j].g + 1.0;
hNew = calculateHValue (i-1, j, dest);
fNew = gNew + hNew;
if (cellDetails[i-1][j].f == FLT_MAX ||
cellDetails[i-1][j].f > fNew)
{
openList.insert( std::make_pair(fNew,
std::make_pair(i-1, j)));
cellDetails[i-1][j].f = fNew;
cellDetails[i-1][j].g = gNew;
cellDetails[i-1][j].h = hNew;
cellDetails[i-1][j].parent_i = i;
cellDetails[i-1][j].parent_j = j;
}
}
}
// SOUTH
if (isValid(i+1, j, rows, cols) == true)
{
if (isDestination(i+1, j, dest) == true)
{
cellDetails[i+1][j].parent_i = i;
cellDetails[i+1][j].parent_j = j;
foundDest = true;
return tracePath (cellDetails, dest);;
}
else if (closedList[i+1][j] == false &&
isUnBlocked(grid, i+1, j) == true)
{
gNew = cellDetails[i][j].g + 1.0;
hNew = calculateHValue(i+1, j, dest);
fNew = gNew + hNew;
if (cellDetails[i+1][j].f == FLT_MAX ||
cellDetails[i+1][j].f > fNew)
{
openList.insert( std::make_pair(fNew,
std::make_pair(i+1, j)));
cellDetails[i+1][j].f = fNew;
cellDetails[i+1][j].g = gNew;
cellDetails[i+1][j].h = hNew;
cellDetails[i+1][j].parent_i = i;
cellDetails[i+1][j].parent_j = j;
}
}
}
// EAST
if (isValid(i, j+1, rows, cols))
{
if (isDestination(i, j+1, dest))
{
cellDetails[i][j+1].parent_i = i;
cellDetails[i][j+1].parent_j = j;
foundDest = true;
return tracePath (cellDetails, dest);
}
else if (closedList[i][j+1] == false &&
isUnBlocked(grid, i, j+1) == true)
{
gNew = cellDetails[i][j].g + 1.0;
hNew = calculateHValue (i, j+1, dest);
fNew = gNew + hNew;
if (cellDetails[i][j+1].f == FLT_MAX ||
cellDetails[i][j+1].f > fNew)
{
openList.insert( std::make_pair(fNew,
std::make_pair(i, j+1)));
cellDetails[i][j+1].f = fNew;
cellDetails[i][j+1].g = gNew;
cellDetails[i][j+1].h = hNew;
cellDetails[i][j+1].parent_i = i;
cellDetails[i][j+1].parent_j = j;
}
}
}
// WEST
if (isValid(i, j-1, rows, cols))
{
if (isDestination(i, j-1, dest))
{
cellDetails[i][j-1].parent_i = i;
cellDetails[i][j-1].parent_j = j;
foundDest = true;
return tracePath (cellDetails, dest);
}
else if (closedList[i][j-1] == false &&
isUnBlocked(grid, i, j-1) == true)
{
gNew = cellDetails[i][j].g + 1.0;
hNew = calculateHValue (i, j-1, dest);
fNew = gNew + hNew;
if (cellDetails[i][j-1].f == FLT_MAX ||
cellDetails[i][j-1].f > fNew)
{
openList.insert( std::make_pair(fNew,
std::make_pair(i, j-1)));
cellDetails[i][j-1].f = fNew;
cellDetails[i][j-1].g = gNew;
cellDetails[i][j-1].h = hNew;
cellDetails[i][j-1].parent_i = i;
cellDetails[i][j-1].parent_j = j;
}
}
}
}
if (foundDest == false)
printf("Failed to find the Destination Cell\n");
return std::stack<Pair>();
}