-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdijkstra.cpp
234 lines (219 loc) · 6.42 KB
/
dijkstra.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
#include "bots.hpp"
#include "stdlib.h"
#include "iostream"
using namespace std;
cDijkstra::cDijkstra() {
for (int i = 0; i < GRIDWIDTH; i++)
{
for (int j = 0; j < GRIDHEIGHT; j++)
{
closed[i][j] = false;
cost[i][j] = 1000000.0f;
linkX[i][j] = -1;
linkY[i][j] = -1;
inPath[i][j] = false;
}
}
completed = false;
}
void cDijkstra::UpdateNeighbors(int min_x, int min_y, float base_cost) {
float straight_cost{ 1.0 };
float diagonal_cost{ 1.4 };
// Straight moves
if (gLevel.isValid(min_x - 1, min_y) && !closed[min_x - 1][min_y] &&
(base_cost + straight_cost) < cost[min_x - 1][min_y])
{
cost[min_x - 1][min_y] = cost[min_x][min_y] + 1.0;
linkX[min_x - 1][min_y] = min_x;
linkY[min_x - 1][min_y] = min_y;
}
if (gLevel.isValid(min_x, min_y - 1) && !closed[min_x][min_y - 1] &&
(base_cost + straight_cost) < cost[min_x][min_y - 1])
{
cost[min_x][min_y - 1] = cost[min_x][min_y] + 1.0;
linkX[min_x][min_y - 1] = min_x;
linkY[min_x][min_y - 1] = min_y;
}
if (gLevel.isValid(min_x, min_y + 1) && !closed[min_x][min_y + 1] &&
(base_cost + straight_cost) < cost[min_x][min_y + 1])
{
cost[min_x][min_y + 1] = cost[min_x][min_y] + 1.0;
linkX[min_x][min_y + 1] = min_x;
linkY[min_x][min_y + 1] = min_y;
}
if (gLevel.isValid(min_x + 1, min_y) && !closed[min_x + 1][min_y] &&
(base_cost + straight_cost) < cost[min_x + 1][min_y])
{
cost[min_x + 1][min_y] = cost[min_x][min_y] + 1.0;
linkX[min_x + 1][min_y] = min_x;
linkY[min_x + 1][min_y] = min_y;
}
// Diagonal moves
if (gLevel.isValid(min_x + 1, min_y - 1) && !closed[min_x + 1][min_y - 1] &&
(base_cost + diagonal_cost) < cost[min_x + 1][min_y - 1])
{
cost[min_x + 1][min_y - 1] = cost[min_x][min_y] + 1.4;
linkX[min_x + 1][min_y - 1] = min_x;
linkY[min_x + 1][min_y - 1] = min_y;
}
if (gLevel.isValid(min_x + 1, min_y + 1) && !closed[min_x + 1][min_y + 1] &&
(base_cost + diagonal_cost) < cost[min_x + 1][min_y + 1])
{
cost[min_x + 1][min_y + 1] = cost[min_x][min_y] + 1.4;
linkX[min_x + 1][min_y + 1] = min_x;
linkY[min_x + 1][min_y + 1] = min_y;
}
if (gLevel.isValid(min_x - 1, min_y + 1) && !closed[min_x - 1][min_y + 1] &&
(base_cost + diagonal_cost) < cost[min_x - 1][min_y + 1])
{
cost[min_x - 1][min_y + 1] = cost[min_x][min_y] + 1.4;
linkX[min_x - 1][min_y + 1] = min_x;
linkY[min_x - 1][min_y + 1] = min_y;
}
if (gLevel.isValid(min_x - 1, min_y - 1) && !closed[min_x - 1][min_y - 1] &&
(base_cost + diagonal_cost) < cost[min_x - 1][min_y - 1])
{
cost[min_x - 1][min_y - 1] = cost[min_x][min_y] + 1.4;
linkX[min_x - 1][min_y - 1] = min_x;
linkY[min_x - 1][min_y - 1] = min_y;
}
}
int cDijkstra::TracePath(cBotBase& bot) {
bool done = false; // Set to true when we are back at the bot position
int nextClosedX = gTarget.PositionX(); // Start of path
int nextClosedY = gTarget.PositionY(); // Start of path
int path = 0; // To move in our path
while (!done) {
inPath[nextClosedX][nextClosedY] = true;
path_coordinates[path][0] = nextClosedX;
path_coordinates[path][1] = nextClosedY;
int tmpX = nextClosedX;
int tmpY = nextClosedY;
path += 1;
nextClosedX = linkX[tmpX][tmpY];
nextClosedY = linkY[tmpX][tmpY];
if ((nextClosedX == bot.PositionX()) && (nextClosedY == bot.PositionY())) {
done = true;
}
}
return path;
}
void cDijkstra::Build(cBotBase& bot)
{
// the bot starts the search
cost[bot.PositionX()][bot.PositionY()] = 0;
// finding the lowest cost
while (!closed[gTarget.PositionX()][gTarget.PositionY()])
{
float min_cost = 1000000.0f;
int min_x = 0;
int min_y = 0;
for (int i = 0; i < GRIDWIDTH; i++)
{
for (int j = 0; j < GRIDHEIGHT; j++)
{
if ((cost[i][j] <= min_cost) && gLevel.isValid(i, j) && !closed[i][j])
{
min_cost = cost[i][j];
min_x = i;
min_y = j;
}
}
}
closed[min_x][min_y] = true;
UpdateNeighbors(min_x, min_y, cost[min_x][min_y]);
}
}
// defining the build function for A*, this one has the Manhattan distance implemented in, we'll redifine other functions to use the other
// functions as well.
void cAStar::Build(cBotBase& bot)
{
cost[bot.PositionX()][bot.PositionY()] = 0;
while (!closed[gTarget.PositionX()][gTarget.PositionY()])
{
int min_x = 0;
int min_y = 0;
float min_cost = 1000000.0f;
for (int i = 0; i < GRIDWIDTH; i++)
{
for (int j = 0; j < GRIDHEIGHT; j++)
{
//Manhatan distance
float heuristic = fabs(gTarget.PositionX() - i) + fabs(gTarget.PositionY() - j);
if (((cost[i][j] + heuristic) <= min_cost) && gLevel.isValid(i, j) && !closed[i][j])
{
min_cost = cost[i][j] + heuristic;
min_x = i;
min_y = j;
}
}
}
closed[min_x][min_y] = true;
UpdateNeighbors(min_x, min_y, cost[min_x][min_y]);
}
max_length = TracePath(bot);
completed = true;
}
// for the use of euclidean distance function we redefine the build function
void cAStar2::Build(cBotBase& bot)
{
cost[bot.PositionX()][bot.PositionY()] = 0;
while (!closed[gTarget.PositionX()][gTarget.PositionY()])
{
int min_x = 0;
int min_y = 0;
float min_cost = 1000000.0f;
for (int i = 0; i < GRIDWIDTH; i++)
{
for (int j = 0; j < GRIDHEIGHT; j++)
{
//Euclidean distance
float heuristic = sqrt(pow(fabs(gTarget.PositionX() - i), 2) + pow(fabs(gTarget.PositionY() - j), 2));
if (((cost[i][j] + heuristic) <= min_cost) && gLevel.isValid(i, j) && !closed[i][j])
{
min_cost = cost[i][j] + heuristic;
min_x = i;
min_y = j;
}
}
}
closed[min_x][min_y] = true;
UpdateNeighbors(min_x, min_y, cost[min_x][min_y]);
}
max_length = TracePath(bot);
completed = true;
}
void cAStar3::Build(cBotBase& bot)
{
cost[bot.PositionX()][bot.PositionY()] = 0;
while (!closed[gTarget.PositionX()][gTarget.PositionY()])
{
int min_x = 0;
int min_y = 0;
float min_cost = 1000000.0f;
for (int i = 0; i < GRIDWIDTH; i++)
{
for (int j = 0; j < GRIDHEIGHT; j++)
{
//Diagonal distance
float heuristic = (fabs(gTarget.PositionX() - i) + (fabs(gTarget.PositionY() - j)) -
0.6 * std::min(fabs(gTarget.PositionX() - i), fabs(gTarget.PositionY() - j)));
if (((cost[i][j] + heuristic) <= min_cost) && gLevel.isValid(i, j) && !closed[i][j])
{
min_cost = cost[i][j] + heuristic;
min_x = i;
min_y = j;
}
}
}
closed[min_x][min_y] = true;
UpdateNeighbors(min_x, min_y, cost[min_x][min_y]);
}
max_length = TracePath(bot);
completed = true;
}
// instantiating global objects
cDijkstra gDijkstra;
cAStar gAStar;
cAStar2 gAStar2;
cAStar3 gAStar3;