-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
AStar.java
84 lines (69 loc) · 2.39 KB
/
AStar.java
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
package dsa.ai.graph.path.astar;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Set;
/**
* Helper class containing pathfinding algorithms.
*
*/
public class AStar {
/**
* A Star pathfinding. Note that the heuristic has to be monotonic:
* {@code h(x) <= d(x, y) + h(y)}.
*
* @param <T>
* @param start Starting node
* @param goal Goal node
* @return Shortest path from start to goal, or null if none found
*/
public static <T extends Node<T>> List<T> doAStar(T start, T goal) {
Set<T> closed = new HashSet<>();
Map<T, T> fromMap = new HashMap<>();
List<T> route = new LinkedList<>();
Map<T, Double> gScore = new HashMap<>();
final Map<T, Double> fScore = new HashMap<>();
PriorityQueue<T> open = new PriorityQueue<>(11, new Comparator<T>() {
@Override
public int compare(T nodeA, T nodeB) {
return Double.compare(fScore.get(nodeA), fScore.get(nodeB));
}
});
gScore.put(start, 0.0);
fScore.put(start, start.getHeuristic(goal));
open.offer(start);
while (!open.isEmpty()) {
T current = open.poll();
if (current.equals(goal)) {
while (current != null) {
route.add(0, current);
current = fromMap.get(current);
}
return route;
}
closed.add(current);
for (T neighbour : current.getNeighbours()) {
if (closed.contains(neighbour)) {
continue;
}
double tentG = gScore.get(current)
+ current.getTraversalCost(neighbour);
boolean contains = open.contains(neighbour);
if (!contains || tentG < gScore.get(neighbour)) {
gScore.put(neighbour, tentG);
fScore.put(neighbour, tentG + neighbour.getHeuristic(goal));
if (contains) {
open.remove(neighbour);
}
open.offer(neighbour);
fromMap.put(neighbour, current);
}
}
}
return null;
}
}