Skip to content

Commit

Permalink
Remove unused maxRetries mechanism in GraphHopper#calcPaths (#1870)
Browse files Browse the repository at this point in the history
  • Loading branch information
easbar committed Jan 27, 2020
1 parent a155832 commit 4811ff3
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 98 deletions.
141 changes: 66 additions & 75 deletions core/src/main/java/com/graphhopper/GraphHopper.java
Original file line number Diff line number Diff line change
Expand Up @@ -1050,88 +1050,79 @@ else if (ALT_ROUTE.equalsIgnoreCase(algoStr))
else
routingTemplate = new ViaRoutingTemplate(request, ghRsp, locationIndex, encodingManager);

List<Path> altPaths = null;
int maxRetries = routingTemplate.getMaxRetries();
Locale locale = request.getLocale();
Translation tr = trMap.getWithFallBack(locale);
for (int i = 0; i < maxRetries; i++) {
StopWatch sw = new StopWatch().start();
List<QueryResult> qResults = routingTemplate.lookup(points, encoder);
ghRsp.addDebugInfo("idLookup:" + sw.stop().getSeconds() + "s");
if (ghRsp.hasErrors())
return Collections.emptyList();

RoutingAlgorithmFactory tmpAlgoFactory = getAlgorithmFactory(hints);
Weighting weighting;
QueryGraph queryGraph;

if (chFactoryDecorator.isEnabled() && !disableCH) {
if (request.hasFavoredHeading(0))
throw new IllegalArgumentException("The 'heading' parameter is currently not supported for speed mode, you need to disable speed mode with `ch.disable=true`. See issue #483");

if (request.getHints().getBool(Routing.PASS_THROUGH, false)) {
throw new IllegalArgumentException("The '" + Parameters.Routing.PASS_THROUGH + "' parameter is currently not supported for speed mode, you need to disable speed mode with `ch.disable=true`. See issue #1765");
}
// if LM is enabled we have the LMFactory with the CH algo!
RoutingAlgorithmFactory chAlgoFactory = tmpAlgoFactory;
if (tmpAlgoFactory instanceof LMAlgoFactoryDecorator.LMRAFactory)
chAlgoFactory = ((LMAlgoFactoryDecorator.LMRAFactory) tmpAlgoFactory).getDefaultAlgoFactory();

if (chAlgoFactory instanceof CHRoutingAlgorithmFactory) {
CHProfile chProfile = ((CHRoutingAlgorithmFactory) chAlgoFactory).getCHProfile();
queryGraph = QueryGraph.lookup(ghStorage.getCHGraph(chProfile), qResults);
weighting = chProfile.getWeighting();
} else {
throw new IllegalStateException("Although CH was enabled a non-CH algorithm factory was returned " + tmpAlgoFactory);
}
} else {
checkNonChMaxWaypointDistance(points);
queryGraph = QueryGraph.lookup(ghStorage, qResults);
weighting = createWeighting(hints, encoder, queryGraph);
}
ghRsp.addDebugInfo("tmode:" + tMode.toString());

int maxVisitedNodesForRequest = hints.getInt(Routing.MAX_VISITED_NODES, maxVisitedNodes);
if (maxVisitedNodesForRequest > maxVisitedNodes)
throw new IllegalArgumentException("The max_visited_nodes parameter has to be below or equal to:" + maxVisitedNodes);
StopWatch sw = new StopWatch().start();
List<QueryResult> qResults = routingTemplate.lookup(points, encoder);
ghRsp.addDebugInfo("idLookup:" + sw.stop().getSeconds() + "s");
if (ghRsp.hasErrors())
return Collections.emptyList();

int uTurnCostInt = request.getHints().getInt(Routing.U_TURN_COSTS, INFINITE_U_TURN_COSTS);
if (uTurnCostInt != INFINITE_U_TURN_COSTS && !tMode.isEdgeBased()) {
throw new IllegalArgumentException("Finite u-turn costs can only be used for edge-based routing, use `" + Routing.EDGE_BASED + "=true'");
}
double uTurnCosts = uTurnCostInt == INFINITE_U_TURN_COSTS ? Double.POSITIVE_INFINITY : uTurnCostInt;
weighting = createTurnWeighting(queryGraph, weighting, tMode, uTurnCosts);

AlgorithmOptions algoOpts = AlgorithmOptions.start().
algorithm(algoStr).traversalMode(tMode).weighting(weighting).
maxVisitedNodes(maxVisitedNodesForRequest).
hints(hints).
build();

// do the actual route calculation !
altPaths = routingTemplate.calcPaths(queryGraph, tmpAlgoFactory, algoOpts, encoder);

boolean tmpEnableInstructions = hints.getBool(Routing.INSTRUCTIONS, getEncodingManager().isEnableInstructions());
boolean tmpCalcPoints = hints.getBool(Routing.CALC_POINTS, calcPoints);
double wayPointMaxDistance = hints.getDouble(Routing.WAY_POINT_MAX_DISTANCE, 1d);

DouglasPeucker peucker = new DouglasPeucker().setMaxDistance(wayPointMaxDistance);
PathMerger pathMerger = new PathMerger(queryGraph.getBaseGraph(), weighting).
setCalcPoints(tmpCalcPoints).
setDouglasPeucker(peucker).
setEnableInstructions(tmpEnableInstructions).
setPathDetailsBuilders(pathBuilderFactory, request.getPathDetails()).
setSimplifyResponse(simplifyResponse && wayPointMaxDistance > 0);
RoutingAlgorithmFactory tmpAlgoFactory = getAlgorithmFactory(hints);
Weighting weighting;
QueryGraph queryGraph;

if (chFactoryDecorator.isEnabled() && !disableCH) {
if (request.hasFavoredHeading(0))
pathMerger.setFavoredHeading(request.getFavoredHeading(0));
throw new IllegalArgumentException("The 'heading' parameter is currently not supported for speed mode, you need to disable speed mode with `ch.disable=true`. See issue #483");

if (routingTemplate.isReady(pathMerger, tr))
break;
if (request.getHints().getBool(Routing.PASS_THROUGH, false)) {
throw new IllegalArgumentException("The '" + Parameters.Routing.PASS_THROUGH + "' parameter is currently not supported for speed mode, you need to disable speed mode with `ch.disable=true`. See issue #1765");
}
// if LM is enabled we have the LMFactory with the CH algo!
RoutingAlgorithmFactory chAlgoFactory = tmpAlgoFactory;
if (tmpAlgoFactory instanceof LMAlgoFactoryDecorator.LMRAFactory)
chAlgoFactory = ((LMAlgoFactoryDecorator.LMRAFactory) tmpAlgoFactory).getDefaultAlgoFactory();

if (chAlgoFactory instanceof CHRoutingAlgorithmFactory) {
CHProfile chProfile = ((CHRoutingAlgorithmFactory) chAlgoFactory).getCHProfile();
queryGraph = QueryGraph.lookup(ghStorage.getCHGraph(chProfile), qResults);
weighting = chProfile.getWeighting();
} else {
throw new IllegalStateException("Although CH was enabled a non-CH algorithm factory was returned " + tmpAlgoFactory);
}
} else {
checkNonChMaxWaypointDistance(points);
queryGraph = QueryGraph.lookup(ghStorage, qResults);
weighting = createWeighting(hints, encoder, queryGraph);
}
ghRsp.addDebugInfo("tmode:" + tMode.toString());

return altPaths;
int maxVisitedNodesForRequest = hints.getInt(Routing.MAX_VISITED_NODES, maxVisitedNodes);
if (maxVisitedNodesForRequest > maxVisitedNodes)
throw new IllegalArgumentException("The max_visited_nodes parameter has to be below or equal to:" + maxVisitedNodes);

int uTurnCostInt = request.getHints().getInt(Routing.U_TURN_COSTS, INFINITE_U_TURN_COSTS);
if (uTurnCostInt != INFINITE_U_TURN_COSTS && !tMode.isEdgeBased()) {
throw new IllegalArgumentException("Finite u-turn costs can only be used for edge-based routing, use `" + Routing.EDGE_BASED + "=true'");
}
double uTurnCosts = uTurnCostInt == INFINITE_U_TURN_COSTS ? Double.POSITIVE_INFINITY : uTurnCostInt;
weighting = createTurnWeighting(queryGraph, weighting, tMode, uTurnCosts);

AlgorithmOptions algoOpts = AlgorithmOptions.start().
algorithm(algoStr).traversalMode(tMode).weighting(weighting).
maxVisitedNodes(maxVisitedNodesForRequest).
hints(hints).
build();

// do the actual route calculation !
List<Path> altPaths = routingTemplate.calcPaths(queryGraph, tmpAlgoFactory, algoOpts, encoder);

boolean tmpEnableInstructions = hints.getBool(Routing.INSTRUCTIONS, getEncodingManager().isEnableInstructions());
boolean tmpCalcPoints = hints.getBool(Routing.CALC_POINTS, calcPoints);
double wayPointMaxDistance = hints.getDouble(Routing.WAY_POINT_MAX_DISTANCE, 1d);

DouglasPeucker peucker = new DouglasPeucker().setMaxDistance(wayPointMaxDistance);
PathMerger pathMerger = new PathMerger(queryGraph.getBaseGraph(), weighting).
setCalcPoints(tmpCalcPoints).
setDouglasPeucker(peucker).
setEnableInstructions(tmpEnableInstructions).
setPathDetailsBuilders(pathBuilderFactory, request.getPathDetails()).
setSimplifyResponse(simplifyResponse && wayPointMaxDistance > 0);

if (request.hasFavoredHeading(0))
pathMerger.setFavoredHeading(request.getFavoredHeading(0));

routingTemplate.finish(pathMerger, trMap.getWithFallBack(request.getLocale()));
return altPaths;
} catch (IllegalArgumentException ex) {
ghRsp.addError(ex);
return Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public List<Path> calcPaths(QueryGraph queryGraph, RoutingAlgorithmFactory algoF
}

@Override
public boolean isReady(PathMerger pathMerger, Translation tr) {
public void finish(PathMerger pathMerger, Translation tr) {
if (pathList.isEmpty())
throw new RuntimeException("Empty paths for alternative route calculation not expected");

Expand All @@ -82,6 +82,5 @@ public boolean isReady(PathMerger pathMerger, Translation tr) {
ghResponse.add(tmpAltRsp);
pathMerger.doWork(tmpAltRsp, Collections.singletonList(pathList.get(index)), encodingManager, tr);
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,11 @@ public void setPaths(List<Path> pathList) {
}

@Override
public boolean isReady(PathMerger pathMerger, Translation tr) {
public void finish(PathMerger pathMerger, Translation tr) {
altResponse = new PathWrapper();
altResponse.setWaypoints(getWaypoints());
ghResponse.add(altResponse);
pathMerger.doWork(altResponse, pathList, encodingManager, tr);
// with potentially retrying, including generating new route points, for now disabled
return true;
}

private QueryResult generateValidPoint(GHPoint from, double distanceInMeters, double heading,
Expand All @@ -180,9 +178,4 @@ private QueryResult generateValidPoint(GHPoint from, double distanceInMeters, do
}
}

@Override
public int getMaxRetries() {
// with potentially retrying, including generating new route points, for now disabled
return 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ public interface RoutingTemplate {
* This method merges the returned paths appropriately e.g. all paths from the list into one
* PathWrapper of GHResponse or multiple (via / round trip).
*/
boolean isReady(PathMerger pathMerger, Translation tr);
void finish(PathMerger pathMerger, Translation tr);

/**
* This method returns the maximum number of full retries of these 3 steps
*/
int getMaxRetries();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,19 +216,13 @@ private int throwImpossibleCurbsideConstraint(int placeIndex) {
}

@Override
public boolean isReady(PathMerger pathMerger, Translation tr) {
public void finish(PathMerger pathMerger, Translation tr) {
if (ghRequest.getPoints().size() - 1 != pathList.size())
throw new RuntimeException("There should be exactly one more points than paths. points:" + ghRequest.getPoints().size() + ", paths:" + pathList.size());

altResponse.setWaypoints(getWaypoints());
ghResponse.add(altResponse);
pathMerger.doWork(altResponse, pathList, encodingManager, tr);
return true;
}

@Override
public int getMaxRetries() {
return 1;
}

}

0 comments on commit 4811ff3

Please sign in to comment.