Repository navigation
Expand file tree
/
Copy pathPathfindingAlgorithms.cpp
More file actions
285 lines (233 loc) · 10.9 KB
/
Copy pathPathfindingAlgorithms.cpp
File metadata and controls
285 lines (233 loc) · 10.9 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
#include "PathfindingAlgorithms.h"
#include <queue>
#include <set>
#include <algorithm>
#include <limits>
#include <cmath>
using namespace std;
double PathfindingAlgorithms::calculateHeuristic(const Position& start, const Position& goal) {
// Manhattan distance for grid-based movement
return abs(start.x - goal.x) + abs(start.y - goal.y);
}
double PathfindingAlgorithms::calculateFireDanger(const Position& pos, const Grid& grid, const vector<Position>& firePositions) {
double minFireDistance = numeric_limits<double>::max();
for (const Position& firePos : firePositions) {
double distance = pos.distanceTo(firePos);
minFireDistance = min(minFireDistance, distance);
}
// Higher danger when closer to fire
if (minFireDistance < 3.0) {
return 10.0 / (minFireDistance + 0.1); // Avoid division by zero
}
return 0.0;
}
vector<Position> PathfindingAlgorithms::reconstructPath(const map<Position, Position>& cameFrom,
const Position& start, const Position& goal) {
vector<Position> path;
Position current = goal;
while (!(current == start)) {
path.push_back(current);
auto it = cameFrom.find(current);
if (it == cameFrom.end()) break;
current = it->second;
}
reverse(path.begin(), path.end());
return path;
}
vector<Position> PathfindingAlgorithms::findPathBFS(const Grid& grid, const Position& start, const Position& goal) {
if (start == goal) return {start};
queue<Position> q;
map<Position, Position> cameFrom;
set<Position> visited;
q.push(start);
visited.insert(start);
while (!q.empty()) {
Position current = q.front();
q.pop();
if (current == goal) {
return reconstructPath(cameFrom, start, goal);
}
for (const Position& neighbor : grid.getNeighbors(current)) {
if (visited.find(neighbor) == visited.end() &&
(grid.isWalkable(neighbor) || neighbor == goal)) {
visited.insert(neighbor);
cameFrom[neighbor] = current;
q.push(neighbor);
}
}
}
return {};
}
vector<Position> PathfindingAlgorithms::findPathDijkstra(const Grid& grid, const Position& start, const Position& goal,
const vector<Position>& firePositions) {
if (start == goal) return {start};
priority_queue<PathNode, vector<PathNode>, greater<PathNode>> pq;
map<Position, double> distances;
map<Position, Position> cameFrom;
set<Position> visited;
pq.push(PathNode(start, 0, 0));
distances[start] = 0;
while (!pq.empty()) {
PathNode current = pq.top();
pq.pop();
if (visited.find(current.pos) != visited.end()) continue;
visited.insert(current.pos);
if (current.pos == goal) {
return reconstructPath(cameFrom, start, goal);
}
for (const Position& neighbor : grid.getNeighbors(current.pos)) {
if (visited.find(neighbor) != visited.end() ||
(!grid.isWalkable(neighbor) && neighbor != goal)) continue;
double moveCost = 1.0;
double fireDanger = calculateFireDanger(neighbor, grid, firePositions);
double newDistance = current.gCost + moveCost + fireDanger;
if (distances.find(neighbor) == distances.end() || newDistance < distances[neighbor]) {
distances[neighbor] = newDistance;
cameFrom[neighbor] = current.pos;
pq.push(PathNode(neighbor, newDistance, 0));
}
}
}
return {};
}
vector<Position> PathfindingAlgorithms::findPathAStar(const Grid& grid, const Position& start, const Position& goal,
const vector<Position>& firePositions) {
if (start == goal) return {start};
priority_queue<PathNode, vector<PathNode>, greater<PathNode>> openSet;
map<Position, double> gScore;
map<Position, Position> cameFrom;
set<Position> closedSet;
double startH = calculateHeuristic(start, goal);
openSet.push(PathNode(start, 0, startH));
gScore[start] = 0;
while (!openSet.empty()) {
PathNode current = openSet.top();
openSet.pop();
if (closedSet.find(current.pos) != closedSet.end()) continue;
closedSet.insert(current.pos);
if (current.pos == goal) {
return reconstructPath(cameFrom, start, goal);
}
for (const Position& neighbor : grid.getNeighbors(current.pos)) {
if (closedSet.find(neighbor) != closedSet.end() ||
(!grid.isWalkable(neighbor) && neighbor != goal)) continue;
double moveCost = 1.0;
double fireDanger = calculateFireDanger(neighbor, grid, firePositions);
double tentativeG = current.gCost + moveCost + fireDanger * 0.5; // Weight fire danger less in A*
if (gScore.find(neighbor) == gScore.end() || tentativeG < gScore[neighbor]) {
gScore[neighbor] = tentativeG;
cameFrom[neighbor] = current.pos;
double h = calculateHeuristic(neighbor, goal);
openSet.push(PathNode(neighbor, tentativeG, h));
}
}
}
return {};
}
vector<Position> PathfindingAlgorithms::findPathHybridAdaptive(const Grid& grid, const Position& start, const Position& goal,
const vector<Position>& firePositions, int stepCount) {
// Adaptive algorithm selection based on situation
double fireProximity = calculateFireDanger(start, grid, firePositions);
double distanceToGoal = calculateHeuristic(start, goal);
// Very early game with no immediate fire threat and short distance: use simple BFS
if (stepCount < 5 && fireProximity < 1.0 && distanceToGoal <= 6.0) {
return findPathBFS(grid, start, goal);
}
// Early game or far from fire: use A* for speed
else if (stepCount < 20 || fireProximity < 2.0) {
return findPathAStar(grid, start, goal, firePositions);
}
// High fire danger: use Dijkstra for safety
else if (fireProximity > 5.0) {
return findPathDijkstra(grid, start, goal, firePositions);
}
// Medium danger: use multi-objective
else {
return findPathMultiObjective(grid, start, goal, firePositions, 0.6, 0.4);
}
}
vector<Position> PathfindingAlgorithms::findPathMultiObjective(const Grid& grid, const Position& start, const Position& goal,
const vector<Position>& firePositions,
double safetyWeight, double speedWeight) {
if (start == goal) return {start};
priority_queue<PathNode, vector<PathNode>, greater<PathNode>> openSet;
map<Position, double> gScore;
map<Position, Position> cameFrom;
set<Position> closedSet;
double startH = calculateHeuristic(start, goal);
openSet.push(PathNode(start, 0, startH));
gScore[start] = 0;
while (!openSet.empty()) {
PathNode current = openSet.top();
openSet.pop();
if (closedSet.find(current.pos) != closedSet.end()) continue;
closedSet.insert(current.pos);
if (current.pos == goal) {
return reconstructPath(cameFrom, start, goal);
}
for (const Position& neighbor : grid.getNeighbors(current.pos)) {
if (closedSet.find(neighbor) != closedSet.end() ||
(!grid.isWalkable(neighbor) && neighbor != goal)) continue;
double moveCost = 1.0;
double fireDanger = calculateFireDanger(neighbor, grid, firePositions);
double heuristic = calculateHeuristic(neighbor, goal);
// Multi-objective cost function
double safetyCost = fireDanger;
double speedCost = moveCost + heuristic * 0.1;
double totalCost = safetyWeight * safetyCost + speedWeight * speedCost;
double tentativeG = current.gCost + totalCost;
if (gScore.find(neighbor) == gScore.end() || tentativeG < gScore[neighbor]) {
gScore[neighbor] = tentativeG;
cameFrom[neighbor] = current.pos;
openSet.push(PathNode(neighbor, tentativeG, heuristic));
}
}
}
return {};
}
PathfindingType PathfindingAlgorithms::selectOptimalAlgorithm(const Grid& grid, const Position& start, const Position& goal,
const vector<Position>& firePositions, int stepCount) {
double fireProximity = calculateFireDanger(start, grid, firePositions);
double distanceToGoal = calculateHeuristic(start, goal);
int fireCount = firePositions.size();
// Decision tree for algorithm selection
if (fireCount == 0 && distanceToGoal <= 5.0) {
return PathfindingType::BFS; // No fire and short distance, use simple BFS
}
else if (fireCount == 0) {
return PathfindingType::A_STAR; // No fire but longer distance, use A* for speed
}
else if (stepCount < 10 && fireProximity < 3.0 && distanceToGoal <= 8.0) {
return PathfindingType::BFS; // Early game, low fire threat, short distance
}
else if (fireProximity > 8.0) {
return PathfindingType::DIJKSTRA; // High danger, prioritize safety
}
else if (stepCount > 50 && fireCount > 5) {
return PathfindingType::MULTI_OBJECTIVE; // Late game with lots of fire
}
else if (distanceToGoal > 15) {
return PathfindingType::HYBRID_ADAPTIVE; // Long distance, adapt as needed
}
else {
return PathfindingType::A_STAR; // Default to A* for efficiency
}
}
vector<Position> PathfindingAlgorithms::findPath(PathfindingType type, const Grid& grid, const Position& start,
const Position& goal, const vector<Position>& firePositions,
int stepCount) {
switch (type) {
case PathfindingType::BFS:
return findPathBFS(grid, start, goal);
case PathfindingType::DIJKSTRA:
return findPathDijkstra(grid, start, goal, firePositions);
case PathfindingType::A_STAR:
return findPathAStar(grid, start, goal, firePositions);
case PathfindingType::HYBRID_ADAPTIVE:
return findPathHybridAdaptive(grid, start, goal, firePositions, stepCount);
case PathfindingType::MULTI_OBJECTIVE:
return findPathMultiObjective(grid, start, goal, firePositions);
default:
return findPathAStar(grid, start, goal, firePositions);
}
}