/
AStar.java
186 lines (158 loc) · 6.69 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
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
/*
* Licensed to GraphHopper GmbH under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* GraphHopper GmbH licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.graphhopper.routing;
import com.graphhopper.coll.GHIntObjectHashMap;
import com.graphhopper.routing.util.TraversalMode;
import com.graphhopper.routing.weighting.BeelineWeightApproximator;
import com.graphhopper.routing.weighting.WeightApproximator;
import com.graphhopper.routing.weighting.Weighting;
import com.graphhopper.storage.Graph;
import com.graphhopper.storage.SPTEntry;
import com.graphhopper.util.*;
import java.util.PriorityQueue;
/**
* This class implements the A* algorithm according to
* http://en.wikipedia.org/wiki/A*_search_algorithm
* <p>
* Different distance calculations can be used via setApproximation.
* <p>
*
* @author Peter Karich
*/
public class AStar extends AbstractRoutingAlgorithm {
private WeightApproximator weightApprox;
private int visitedCount;
private GHIntObjectHashMap<AStarEntry> fromMap;
private PriorityQueue<AStarEntry> prioQueueOpenSet;
private AStarEntry currEdge;
private int to1 = -1;
public AStar(Graph graph, Weighting weighting, TraversalMode tMode) {
super(graph, weighting, tMode);
int size = Math.min(Math.max(200, graph.getNodes() / 10), 2000);
initCollections(size);
BeelineWeightApproximator defaultApprox = new BeelineWeightApproximator(nodeAccess, weighting);
defaultApprox.setDistanceCalc(Helper.DIST_PLANE);
setApproximation(defaultApprox);
}
/**
* @param approx defines how distance to goal Node is approximated
*/
public AStar setApproximation(WeightApproximator approx) {
weightApprox = approx;
return this;
}
protected void initCollections(int size) {
fromMap = new GHIntObjectHashMap<>();
prioQueueOpenSet = new PriorityQueue<>(size);
}
@Override
public Path calcPath(int from, int to) {
checkAlreadyRun();
to1 = to;
weightApprox.setTo(to);
double weightToGoal = weightApprox.approximate(from);
currEdge = new AStarEntry(EdgeIterator.NO_EDGE, from, 0 + weightToGoal, 0);
if (!traversalMode.isEdgeBased()) {
fromMap.put(from, currEdge);
}
return runAlgo();
}
private Path runAlgo() {
double currWeightToGoal, estimationFullWeight;
EdgeExplorer explorer = outEdgeExplorer;
while (true) {
int currVertex = currEdge.adjNode;
visitedCount++;
if (isMaxVisitedNodesExceeded())
return createEmptyPath();
if (finished())
break;
EdgeIterator iter = explorer.setBaseNode(currVertex);
while (iter.next()) {
if (!accept(iter, currEdge.edge))
continue;
double alreadyVisitedWeight = weighting.calcWeight(iter, false, currEdge.edge)
+ currEdge.weightOfVisitedPath;
if (Double.isInfinite(alreadyVisitedWeight))
continue;
int traversalKey = traversalMode.createTraversalKey(iter, false);
AStarEntry ase = fromMap.get(traversalKey);
if (ase == null || ase.weightOfVisitedPath > alreadyVisitedWeight) {
int neighborNode = iter.getAdjNode();
currWeightToGoal = weightApprox.approximate(neighborNode);
estimationFullWeight = alreadyVisitedWeight + currWeightToGoal;
if (ase == null) {
ase = new AStarEntry(iter.getEdge(), neighborNode, estimationFullWeight, alreadyVisitedWeight);
fromMap.put(traversalKey, ase);
} else {
// assert (ase.weight > 0.9999999 * estimationFullWeight) : "Inconsistent distance estimate. It is expected weight >= estimationFullWeight but was "
// + ase.weight + " < " + estimationFullWeight + " (" + ase.weight / estimationFullWeight + "), and weightOfVisitedPath:"
// + ase.weightOfVisitedPath + " vs. alreadyVisitedWeight:" + alreadyVisitedWeight + " (" + ase.weightOfVisitedPath / alreadyVisitedWeight + ")";
prioQueueOpenSet.remove(ase);
ase.edge = iter.getEdge();
ase.weight = estimationFullWeight;
ase.weightOfVisitedPath = alreadyVisitedWeight;
}
ase.parent = currEdge;
prioQueueOpenSet.add(ase);
updateBestPath(iter, ase, traversalKey);
}
}
if (prioQueueOpenSet.isEmpty())
return createEmptyPath();
currEdge = prioQueueOpenSet.poll();
if (currEdge == null)
throw new AssertionError("Empty edge cannot happen");
}
return extractPath();
}
@Override
protected Path extractPath() {
return new Path(graph, weighting).
setWeight(currEdge.weight).setSPTEntry(currEdge).extract();
}
@Override
protected boolean finished() {
return currEdge.adjNode == to1;
}
@Override
public int getVisitedNodes() {
return visitedCount;
}
protected void updateBestPath(EdgeIteratorState edgeState, SPTEntry bestSPTEntry, int traversalKey) {
}
public static class AStarEntry extends SPTEntry {
double weightOfVisitedPath;
public AStarEntry(int edgeId, int adjNode, double weightForHeap, double weightOfVisitedPath) {
super(edgeId, adjNode, weightForHeap);
this.weightOfVisitedPath = weightOfVisitedPath;
}
@Override
public final double getWeightOfVisitedPath() {
return weightOfVisitedPath;
}
@Override
public AStarEntry getParent() {
return (AStarEntry) parent;
}
}
@Override
public String getName() {
return Parameters.Algorithms.ASTAR + "|" + weightApprox;
}
}