-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathastar.cpp
More file actions
80 lines (75 loc) · 2.59 KB
/
Copy pathastar.cpp
File metadata and controls
80 lines (75 loc) · 2.59 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
#include "astar.h"
SearchResult AStar::findPath(Input i)
{
auto t = std::chrono::high_resolution_clock::now();
SearchResult result;
/*
* start.g = 0
* start.f = getHValue(start, goal, dma)
* OPEN = {start}
* CLOSED = {}
* while OPEN is not empty
* current = node from OPEN with minimal g-value
* move current from OPEN to CLOSED
* if current is goal
* result.f = current.f
* return reconstructPath(current)
* neighbors = get neighbors of current node
* for each neighbor in neighbors:
* if neighbor in CLOSED:
* continue
* neighbor.g = current.g + cost(neighbor, current)
* neighbor.f = neighbor.g + getHValue(neighbor, goal, dma)
* neighbor.parent = current
* insert or update neighbor into OPEN
* return path not found
*/
i.start.g = 0;
i.start.f = AStar::getHValue(i.start, i.goal, i.map.diagonal_moves_allowed);
ol.addNode(i.start);
while (ol.getSize() != 0)
{
Node current = ol.getMin();
cl.addClose(current);
ol.popMin();
if ((current.x == i.goal.x) && (current.y == i.goal.y))
{
result.cost = current.f;
result.pathfound = true;
result.path = AStar::reconstructPath(current);
break;
}
std::list<Node> neighbours = i.map.getValidMoves(current);
for (auto n : neighbours)
{
if (cl.inClose(n.x, n.y))
continue;
n.g = current.g + i.map.getCost(n, current);
n.f = n.g + AStar::getHValue(n, i.goal, i.map.diagonal_moves_allowed);
n.parent = cl.getPointer(current.x, current.y);
ol.addNode(n);
}
}
result.createdNodes = cl.getSize() + ol.getSize();
result.steps = cl.getSize();
result.runtime = std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::high_resolution_clock::now() - t).count();
return result;
}
double AStar::getHValue(Node current, Node goal, bool dma)
{
if (dma)
return abs(abs(goal.x - current.x) - abs(goal.y - current.y)) + sqrt(2) * fmin(abs(goal.x - current.x), abs(goal.y - current.y));
else
return abs(goal.x - current.x) + abs(goal.y - current.y);;
}
std::list<Node> AStar::reconstructPath(Node current)
{
std::list<Node> path;
while(current.parent != nullptr)
{
path.push_front(current);
current = *current.parent;
}
path.push_front(current);
return path;
}