Skip to content

Commit a4ebf88

Browse files
committed
fixed issues regarding integration tests of CH, graphhopper#942
1 parent 768c036 commit a4ebf88

File tree

11 files changed

+149
-68
lines changed

11 files changed

+149
-68
lines changed

core/src/main/java/com/graphhopper/routing/AStar.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public int getVisitedNodes() {
168168

169169
@Override
170170
public String getName() {
171-
return Parameters.Algorithms.ASTAR;
171+
return Parameters.Algorithms.ASTAR + "|" + weightApprox;
172172
}
173173

174174
public static class AStarEntry extends SPTEntry {

core/src/main/java/com/graphhopper/routing/AStarBidirection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,6 @@ public void updateBestPath(EdgeIteratorState edgeState, AStarEntry entryCurrent,
280280

281281
@Override
282282
public String getName() {
283-
return Parameters.Algorithms.ASTAR_BI;
283+
return Parameters.Algorithms.ASTAR_BI + "|" + weightApprox;
284284
}
285285
}

core/src/main/java/com/graphhopper/routing/ch/PrepareContractionHierarchies.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ protected Path createAndInitPath() {
671671

672672
@Override
673673
public String getName() {
674-
return "astarbiCH";
674+
return "astarbi|ch";
675675
}
676676

677677
@Override
@@ -707,7 +707,7 @@ protected Path createAndInitPath() {
707707

708708
@Override
709709
public String getName() {
710-
return "dijkstrabiCH";
710+
return "dijkstrabi|ch";
711711
}
712712

713713
@Override

core/src/main/java/com/graphhopper/routing/util/HintsMap.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public HintsMap(String weighting) {
3535
setWeighting(weighting);
3636
}
3737

38+
public HintsMap(HintsMap map) {
39+
super(map.toMap());
40+
}
41+
3842
@Override
3943
public HintsMap put(String key, Object str) {
4044
super.put(key, str);

core/src/main/java/com/graphhopper/routing/util/TestAlgoCollector.java

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,28 @@ public TestAlgoCollector(String name) {
4747

4848
public TestAlgoCollector assertDistance(AlgoHelperEntry algoEntry, List<QueryResult> queryList,
4949
OneRun oneRun) {
50-
List<Path> altPaths = new ArrayList<Path>();
51-
QueryGraph queryGraph = new QueryGraph(algoEntry.getQueryGraph());
50+
List<Path> altPaths = new ArrayList<>();
51+
QueryGraph queryGraph = new QueryGraph(algoEntry.getForQueryGraph());
5252
queryGraph.lookup(queryList);
53-
AlgorithmOptions opts = algoEntry.opts;
53+
AlgorithmOptions opts = algoEntry.getAlgorithmOptions();
5454
FlagEncoder encoder = opts.getWeighting().getFlagEncoder();
55-
if (encoder.supports(TurnWeighting.class))
55+
if (encoder.supports(TurnWeighting.class)) {
56+
if (!opts.getTraversalMode().isEdgeBased()) {
57+
errors.add("Cannot use TurnWeighting with a node based traversal");
58+
return this;
59+
}
5660
algoEntry.setAlgorithmOptions(AlgorithmOptions.start(opts).weighting(new TurnWeighting(opts.getWeighting(), (TurnCostExtension) queryGraph.getExtension())).build());
61+
}
5762

63+
RoutingAlgorithmFactory factory = algoEntry.createRoutingFactory();
5864
for (int i = 0; i < queryList.size() - 1; i++) {
59-
RoutingAlgorithm algo = algoEntry.createAlgo(queryGraph);
65+
RoutingAlgorithm algo = factory.createAlgo(queryGraph, algoEntry.getAlgorithmOptions());
66+
// if (!algoEntry.getExpectedAlgo().equals(algo.toString())) {
67+
// errors.add("Algorithm expected " + algoEntry.getExpectedAlgo() + " but was " + algo.toString());
68+
// return this;
69+
// }
70+
6071
Path path = algo.calcPath(queryList.get(i).getClosestNode(), queryList.get(i + 1).getClosestNode());
61-
// System.out.println(path.calcInstructions().createGPX("temp", 0, "GMT"));
6272
altPaths.add(path);
6373
}
6474

@@ -70,7 +80,7 @@ public TestAlgoCollector assertDistance(AlgoHelperEntry algoEntry, List<QueryRes
7080
pathMerger.doWork(rsp, altPaths, trMap.getWithFallBack(Locale.US));
7181

7282
if (rsp.hasErrors()) {
73-
errors.add(algoEntry + " response contains errors. Expected distance: " + rsp.getDistance()
83+
errors.add("response for " + algoEntry + " contains errors. Expected distance: " + oneRun.getDistance()
7484
+ ", expected points: " + oneRun + ". " + queryList + ", errors:" + rsp.getErrors());
7585
return this;
7686
}
@@ -134,45 +144,45 @@ void printSummary() {
134144
}
135145

136146
public static class AlgoHelperEntry {
137-
private final Graph baseGraph;
138147
private final LocationIndex idx;
139-
private Graph queryGraph;
148+
private Graph forQueryGraph;
149+
private String expectedAlgo;
140150
private AlgorithmOptions opts;
141151

142-
public AlgoHelperEntry(Graph g, Graph baseGraph, AlgorithmOptions opts, LocationIndex idx) {
143-
this.queryGraph = g;
144-
this.baseGraph = baseGraph;
152+
public AlgoHelperEntry(Graph g, AlgorithmOptions opts, LocationIndex idx, String expectedAlgo) {
153+
this.forQueryGraph = g;
145154
this.opts = opts;
146155
this.idx = idx;
156+
this.expectedAlgo = expectedAlgo;
147157
}
148158

149-
public Graph getQueryGraph() {
150-
return queryGraph;
159+
public Graph getForQueryGraph() {
160+
return forQueryGraph;
151161
}
152162

153-
public void setQueryGraph(Graph queryGraph) {
154-
this.queryGraph = queryGraph;
163+
public void setAlgorithmOptions(AlgorithmOptions opts) {
164+
this.opts = opts;
155165
}
156166

157-
public Graph getBaseGraph() {
158-
return baseGraph;
167+
public RoutingAlgorithmFactory createRoutingFactory() {
168+
return new RoutingAlgorithmFactorySimple();
159169
}
160170

161-
public void setAlgorithmOptions(AlgorithmOptions opts) {
162-
this.opts = opts;
171+
public AlgorithmOptions getAlgorithmOptions() {
172+
return opts;
163173
}
164174

165175
public LocationIndex getIdx() {
166176
return idx;
167177
}
168178

169-
public RoutingAlgorithm createAlgo(Graph qGraph) {
170-
return new RoutingAlgorithmFactorySimple().createAlgo(qGraph, opts);
179+
public String getExpectedAlgo() {
180+
return expectedAlgo;
171181
}
172182

173183
@Override
174184
public String toString() {
175-
return opts.getAlgorithm() + (queryGraph instanceof CHGraph ? "CH" : "");
185+
return "algoEntry(" + opts.getAlgorithm() + (forQueryGraph instanceof CHGraph ? "|ch" : "") + ")";
176186
}
177187
}
178188

core/src/main/java/com/graphhopper/routing/weighting/BeelineWeightApproximator.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,9 @@ public BeelineWeightApproximator setDistanceCalc(DistanceCalc distanceCalc) {
6969
this.distanceCalc = distanceCalc;
7070
return this;
7171
}
72+
73+
@Override
74+
public String toString() {
75+
return "beeline";
76+
}
7277
}

core/src/main/java/com/graphhopper/routing/weighting/ConsistentWeightApproximator.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,9 @@ public double approximate(int fromNode, boolean reverse) {
5252

5353
return weightApproximation;
5454
}
55+
56+
@Override
57+
public String toString() {
58+
return uniDirApproximatorForward.toString();
59+
}
5560
}

core/src/main/java/com/graphhopper/routing/weighting/TurnWeighting.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public TurnWeighting(Weighting superWeighting, TurnCostExtension turnCostExt) {
4747
this.turnCostEncoder = (TurnCostEncoder) superWeighting.getFlagEncoder();
4848
this.superWeighting = superWeighting;
4949
this.turnCostExt = turnCostExt;
50-
50+
5151
if (turnCostExt == null)
5252
throw new RuntimeException("No storage set to calculate turn weight");
5353
}
@@ -124,6 +124,11 @@ public boolean matches(HintsMap weightingMap) {
124124
return superWeighting.matches(weightingMap);
125125
}
126126

127+
@Override
128+
public String toString() {
129+
return "turn|" + superWeighting.toString();
130+
}
131+
127132
@Override
128133
public String getName() {
129134
return "turn|" + superWeighting.getName();

core/src/main/java/com/graphhopper/util/PMap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ String get(String key) {
149149
* This method copies the underlying structur into a new Map object
150150
*/
151151
public Map<String, String> toMap() {
152-
return new HashMap<String, String>(map);
152+
return new HashMap<>(map);
153153
}
154154

155155
private Map<String, String> getMap() {

core/src/test/java/com/graphhopper/routing/RoutingAlgorithmIT.java

Lines changed: 76 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,18 @@
1717
*/
1818
package com.graphhopper.routing;
1919

20+
import com.graphhopper.GraphHopper;
2021
import com.graphhopper.reader.PrinctonReader;
21-
import com.graphhopper.routing.ch.PrepareContractionHierarchies;
2222
import com.graphhopper.routing.util.EncodingManager;
2323
import com.graphhopper.routing.util.FlagEncoder;
24+
import com.graphhopper.routing.util.HintsMap;
2425
import com.graphhopper.routing.util.TestAlgoCollector.AlgoHelperEntry;
2526
import com.graphhopper.routing.util.TraversalMode;
26-
import com.graphhopper.routing.weighting.ShortestWeighting;
2727
import com.graphhopper.routing.weighting.Weighting;
28-
import com.graphhopper.storage.*;
28+
import com.graphhopper.storage.CHGraph;
29+
import com.graphhopper.storage.Directory;
30+
import com.graphhopper.storage.GraphBuilder;
31+
import com.graphhopper.storage.GraphHopperStorage;
2932
import com.graphhopper.storage.index.LocationIndex;
3033
import com.graphhopper.storage.index.LocationIndexTree;
3134
import com.graphhopper.util.Parameters;
@@ -44,32 +47,65 @@
4447

4548
/**
4649
* Try algorithms, indices and graph storages with real data
47-
* <p>
4850
*
4951
* @author Peter Karich
5052
*/
5153
public class RoutingAlgorithmIT {
52-
public static List<AlgoHelperEntry> createAlgos(GraphHopperStorage ghStorage, LocationIndex idx, boolean withCH,
53-
TraversalMode tMode, Weighting weighting) {
54-
List<AlgoHelperEntry> prepare = new ArrayList<AlgoHelperEntry>();
55-
prepare.add(new AlgoHelperEntry(ghStorage, ghStorage, new AlgorithmOptions(ASTAR, weighting, tMode), idx));
54+
public static List<AlgoHelperEntry> createAlgos(final GraphHopper hopper, final HintsMap hints, TraversalMode tMode) {
55+
GraphHopperStorage ghStorage = hopper.getGraphHopperStorage();
56+
LocationIndex idx = hopper.getLocationIndex();
57+
58+
String addStr = "";
59+
if (tMode.isEdgeBased())
60+
addStr = "turn|";
61+
62+
FlagEncoder encoder = hopper.getEncodingManager().getEncoder(hints.getVehicle());
63+
Weighting weighting = hopper.createWeighting(hints, encoder, hopper.getGraphHopperStorage());
64+
65+
HintsMap defaultHints = new HintsMap().put(Parameters.CH.DISABLE, true)
66+
.setVehicle(hints.getVehicle()).setWeighting(hints.getWeighting());
67+
68+
AlgorithmOptions defaultOpts = AlgorithmOptions.start(new AlgorithmOptions("", weighting, tMode)).hints(defaultHints).build();
69+
70+
List<AlgoHelperEntry> prepare = new ArrayList<>();
71+
prepare.add(new AlgoHelperEntry(ghStorage, AlgorithmOptions.start(defaultOpts).algorithm(ASTAR).build(), idx, "astar|beeline|" + addStr + weighting));
5672
// later: include dijkstraOneToMany
57-
prepare.add(new AlgoHelperEntry(ghStorage, ghStorage, new AlgorithmOptions(DIJKSTRA, weighting, tMode), idx));
73+
prepare.add(new AlgoHelperEntry(ghStorage, AlgorithmOptions.start(defaultOpts).algorithm(DIJKSTRA).build(), idx, "dijkstra|" + addStr + weighting));
5874

59-
final AlgorithmOptions astarbiOpts = new AlgorithmOptions(ASTAR_BI, weighting, tMode);
75+
AlgorithmOptions astarbiOpts = AlgorithmOptions.start(defaultOpts).algorithm(ASTAR_BI).build();
6076
astarbiOpts.getHints().put(ASTAR_BI + ".approximation", "BeelineSimplification");
61-
final AlgorithmOptions dijkstrabiOpts = new AlgorithmOptions(DIJKSTRA_BI, weighting, tMode);
62-
prepare.add(new AlgoHelperEntry(ghStorage, ghStorage, astarbiOpts, idx));
63-
prepare.add(new AlgoHelperEntry(ghStorage, ghStorage, dijkstrabiOpts, idx));
64-
65-
if (withCH) {
66-
final AlgorithmOptions chOpts = AlgorithmOptions.start(dijkstrabiOpts).build();
67-
chOpts.getHints().put(Parameters.CH.DISABLE, true);
68-
prepare.add(new AlgoHelperEntry(ghStorage, ghStorage, chOpts, idx));
69-
70-
final AlgorithmOptions astarCH = AlgorithmOptions.start(astarbiOpts).build();
71-
astarCH.getHints().put(Parameters.CH.DISABLE, true);
72-
prepare.add(new AlgoHelperEntry(ghStorage, ghStorage, astarCH, idx));
77+
AlgorithmOptions dijkstrabiOpts = AlgorithmOptions.start(defaultOpts).algorithm(DIJKSTRA_BI).build();
78+
prepare.add(new AlgoHelperEntry(ghStorage, astarbiOpts, idx, "astarbi|beeline|" + addStr + weighting));
79+
prepare.add(new AlgoHelperEntry(ghStorage, dijkstrabiOpts, idx, "dijkstrabi|" + addStr + weighting));
80+
81+
// add additional preparations if CH preparation are enabled
82+
if (hopper.getCHFactoryDecorator().isEnabled()) {
83+
final HintsMap chHints = new HintsMap(defaultHints).put(Parameters.CH.DISABLE, false);
84+
Weighting pickedWeighting = null;
85+
for (Weighting tmpWeighting : hopper.getCHFactoryDecorator().getWeightings()) {
86+
if (tmpWeighting.equals(weighting)) {
87+
pickedWeighting = tmpWeighting;
88+
break;
89+
}
90+
}
91+
if (pickedWeighting == null)
92+
throw new IllegalStateException("Didn't find weighting " + hints.getWeighting() + " in " + hopper.getCHFactoryDecorator().getWeightings());
93+
94+
prepare.add(new AlgoHelperEntry(ghStorage.getGraph(CHGraph.class, pickedWeighting),
95+
AlgorithmOptions.start(dijkstrabiOpts).hints(chHints).build(), idx, "dijkstrabi|ch|prepare|" + hints.getWeighting()) {
96+
@Override
97+
public RoutingAlgorithmFactory createRoutingFactory() {
98+
return hopper.getAlgorithmFactory(chHints);
99+
}
100+
});
101+
102+
prepare.add(new AlgoHelperEntry(ghStorage.getGraph(CHGraph.class, pickedWeighting),
103+
AlgorithmOptions.start(astarbiOpts).hints(chHints).build(), idx, "astarbi|ch|prepare|" + hints.getWeighting()) {
104+
@Override
105+
public RoutingAlgorithmFactory createRoutingFactory() {
106+
return hopper.getAlgorithmFactory(chHints);
107+
}
108+
});
73109
}
74110

75111
return prepare;
@@ -81,20 +117,32 @@ public void testPerformance() throws IOException {
81117
int noJvmWarming = N / 4;
82118

83119
Random rand = new Random(0);
84-
EncodingManager eManager = new EncodingManager("car");
85-
FlagEncoder encoder = eManager.getEncoder("car");
86-
GraphHopperStorage graph = new GraphBuilder(eManager).create();
120+
final EncodingManager eManager = new EncodingManager("car");
121+
final GraphHopperStorage graph = new GraphBuilder(eManager).create();
87122

88123
String bigFile = "10000EWD.txt.gz";
89124
new PrinctonReader(graph).setStream(new GZIPInputStream(PrinctonReader.class.getResourceAsStream(bigFile))).read();
90-
Collection<AlgoHelperEntry> prepares = createAlgos(graph, null, false, TraversalMode.NODE_BASED,
91-
new ShortestWeighting(encoder));
125+
GraphHopper hopper = new GraphHopper() {
126+
{
127+
setCHEnabled(false);
128+
setEncodingManager(eManager);
129+
loadGraph(graph);
130+
}
131+
132+
@Override
133+
protected LocationIndex createLocationIndex(Directory dir) {
134+
return new LocationIndexTree(graph, dir);
135+
}
136+
};
137+
138+
Collection<AlgoHelperEntry> prepares = createAlgos(hopper, new HintsMap().setWeighting("shortest").setVehicle("car"), TraversalMode.NODE_BASED);
139+
92140
for (AlgoHelperEntry entry : prepares) {
93141
StopWatch sw = new StopWatch();
94142
for (int i = 0; i < N; i++) {
95143
int node1 = Math.abs(rand.nextInt(graph.getNodes()));
96144
int node2 = Math.abs(rand.nextInt(graph.getNodes()));
97-
RoutingAlgorithm d = entry.createAlgo(graph);
145+
RoutingAlgorithm d = entry.createRoutingFactory().createAlgo(graph, entry.getAlgorithmOptions());
98146
if (i >= noJvmWarming)
99147
sw.start();
100148

0 commit comments

Comments
 (0)