Skip to content

Implements a simple version of stall-on-demand for CH queries. - #1179

Merged
karussell merged 15 commits into
graphhopper:masterfrom
easbar:stall_on_demand_ch
Oct 23, 2017
Merged

karussell merged 15 commits into
graphhopper:masterfrom
easbar:stall_on_demand_ch

Conversation

@easbar

@easbar easbar commented Oct 6, 2017

Copy link
Copy Markdown
Member

This is a simple version of stall-on-demand that leads to a 1.5x speed up of CH queries on the osm map for Germany. Also see discussion in: #240.
I put the stall-on-demand version of the bidirectional ch dijkstra into a separate class, because PrepareContractionHierarchies already seems very large (?). I thought it would be better to leave it up to configuration whether stall-on-demand is used or not. I also added some measurement tests for stall on demand in the measurement class. Not sure if this is really needed, but for testing it is helpful at the moment. I might test some of the more aggressive versions of stall on demand if I find the time.

@easbar easbar mentioned this pull request Oct 6, 2017
3 of 10 tasks
// if using none-bidirectional algorithm make sure you exclude CH routing
final String algoStr = ch ? Algorithms.DIJKSTRA_BI : Algorithms.ASTAR_BI;
put(prefix + ".guessed_algorithm", algoStr);
String guessed_algorithm = ch ? ("".equals(algoStr) ? DIJKSTRA_BI : algoStr) : Algorithms.ASTAR_BI;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting the guessed algorithm string seems rather messy (?) ;)

@karussell

Copy link
Copy Markdown
Member

Wow, cool - will try!

@karussell

Copy link
Copy Markdown
Member

Wow, really nice & thanks a lot! I can confirm this is a huge improvement from 3.9ms to 2.7ms in my case (measurement of Germany & car)

private PriorityQueue<SPTEntry> pqOpenSetFrom;
private PriorityQueue<SPTEntry> pqOpenSetTo;
protected PriorityQueue<SPTEntry> pqOpenSetFrom;
protected PriorityQueue<SPTEntry> pqOpenSetTo;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make it package protected instead? As it is not final this is a bit unsafe to allow subclasses access to it.

public static final String DIJKSTRA_BI = "dijkstrabi";
/**
* Bidirectional Dijkstra making use of stall-on-demand optimization
*/

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A new internal parameter is ok for tests and comparison etc, but I wouldn't make this official in the Parameters class as I think we have to make SOD the default :)

+ " time:" + Helper.round2(sodRsp.getBest().getTime() / 1000) + ", original: " + Helper.round2(stdRsp.getBest().getTime() / 1000)
+ " points:" + sodRsp.getBest().getPoints().size() + ", original: " + stdRsp.getBest().getPoints().size();

if (Math.abs(1 - sodRsp.getBest().getRouteWeight() / stdRsp.getBest().getRouteWeight()) > 0.000001)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, really nice that you did this test too!


void fillEdges(SPTEntry currEdge, PriorityQueue<SPTEntry> prioQueue,
IntObjectMap<SPTEntry> bestWeightMap, EdgeExplorer explorer, boolean reverse) {
protected void fillEdges(SPTEntry currEdge, PriorityQueue<SPTEntry> prioQueue,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be necessary IMO

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to use package private and still use this method (and pqOpenSetFrom/To) we need to move DijkstraBidirectionCHWithSOD to the same package as DijkstraBidirectionRef. In this case I would also move DijkstraBidirectionCH and AStarBidirectionCH from PrepareContractionHierarchies to com.graphhopper.routing. I would find this cleaner anyway ;)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please ;)

@karussell

karussell commented Oct 8, 2017 •

Copy link
Copy Markdown
Member

BTW: for disabled instructions the improvement is even bigger: 2.9ms vs. 1.5ms!

@devemux86

Copy link
Copy Markdown
Contributor

Seem impressive results!

@boldtrn

boldtrn commented Oct 9, 2017 •

Copy link
Copy Markdown
Member

Wow this sounds very impressive! If I understand this correctly we only stall a node when we are sure that the path via this node is worse than a previously found path. We do this by checking the current weight + the weight to one of the next edges is higher than the previously calculated best weight.

Could it happen that we due to this incorrectly stall a node and might not find the best path anymore?

Assume this is a part of a graph. We are calculating the shortest path from some node X that is to the left to node D.
The weight to Node C is 5
The weight to Node A is 4
The weight for the edge between A and C is 2.
If we now check if A is stallable, we check 4+2 > 5 => Ok, A is stallable
The weight of the edge A-B and B-C might be 1, but the weight of the edge C-D might be 5, but we stalled A, and cannot find the best path anymore.
------  C
     /      \
--  A - B - D 

Probably this situation is impossible because of the hierarchy and due to this it is guaranteed that we always find the best path?

@easbar

easbar commented Oct 16, 2017

Copy link
Copy Markdown
Member Author

Thanks for testing everyone!
@boldtrn I am not sure if I understand your example completely. Checking if A is stallable happens when settling A during the bidirectional CH Dijkstra. A can be stalled if there is an incoming edge coming from a node such that the tentative distance of the neighboring node plus the edge weight is smaller than the tentative distance of A (5+2 would have to be smaller than 4 in your example).
But you are right, the stalling optimization is specific to the query algorithm in contraction hierarchies (and some other hierarchical approaches). It is guaranteed that the shortest path is always found. For example if a node that lies on a shortest path gets stalled it will be discovered by the backward search. It gets more complicated when one tries to stall not only the current node, but also other ones that are reached from the stalled node (aggressive stalling). In this case one might have to un-stall nodes later, but this does not apply for the simple version that is used here.

@boldtrn

boldtrn commented Oct 16, 2017

Copy link
Copy Markdown
Member

But you are right, the stalling optimization is specific to the query algorithm in contraction hierarchies (and some other hierarchical approaches). It is guaranteed that the shortest path is always found.

Thanks for the answer. I thought that this would work due to the hierarchical properties of CH, but I couldn't proof it myself :).

For example if a node that lies on a shortest path gets stalled it will be discovered by the backward search.

Ah that's interesting, so the stalling only happens for one direction. That makes sense.


@Override
public String getName() {
return "dijkstrabinosod|ch";

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the version with stall on demand becomes the default, maybe change the class names as well ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use a more readable name dijkstrabi_no_sod?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or even dijkstrabi|ch|no_sod?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure


@Override
public String getName() {
return "dijkstrabinosod|ch";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use a more readable name dijkstrabi_no_sod?


@Override
public String getName() {
return "dijkstrabinosod|ch";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or even dijkstrabi|ch|no_sod?

printTimeOfRouteQuery(hopper, isCH, isLM, count, "routingCH_no_instr", vehicleStr, false, -1, "");
printTimeOfRouteQuery(hopper, isCH, isLM, count, "routingCH_sod_no_instr", vehicleStr, false, -1, DIJKSTRA_BI_SOD);
printTimeOfRouteQuery(hopper, isCH, isLM, count, "routingCH", vehicleStr, true, -1);
printTimeOfRouteQuery(hopper, isCH, isLM, count, "routingCH_no_instr", vehicleStr, false, -1);

@karussell karussell Oct 17, 2017 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also here I would test 1. with SOD 2. without SOD and 3. with SOD and no instructions instead of running two queries per printTimeOfRouteQuery call

@karussell karussell added this to the 0.10 milestone Oct 17, 2017
@easbar

easbar commented Oct 17, 2017

Copy link
Copy Markdown
Member Author

some integration tests are failing now that sod is used by default, will have a look ;)

easbar and others added 7 commits October 17, 2017 23:41
Stall on demand can prevent finding a shortest path if the weight of an
edge depends on the driving direction as with Motorcycle or Bike2 weightings.
When checking the stall on demand condition so far the wrong edge
direction was used to calculate the weight of the incoming edge.
* Uses a very simple version of stall-on-demand (SOD) for CH queries to prevent exploring nodes that can not be part
* of a shortest path. When a node that is about to be settled is stallable it is not expanded, but no further search
* for neighboring stallable nodes is performed.
*/

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to add an @author tag here.

* @author Peter Karich
*/
public class PrepareContractionHierarchies extends AbstractAlgoPreparation implements RoutingAlgorithmFactory {
public static final String STALL_ON_DEMAND = "stall_on_demand";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this better go into the Parameters class?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or should we probably hide this config and do not even make it public? (At least it needs the ch. prefix)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was in Parameters.Algorithms before :) Maybe Parameters.CH would be a good place for it ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is useful to be able to deactivate it for example in the Measurement class

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:D ... oh ... so yes, lets keep it here but reduce visibility? I currently do not see a case where it would make sense to disable stalling except for debugging and performance measurements. And then we could use the pure string without introducing some public usable constants.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually prefer constants so I can search for usages, but you probably want to keep the public API as small as possible to reduce compatibility problems in future versions I guess. In this case yes I would make the constant private or remove it and just use the string value where it is needed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly. The only solution would be package protected but would not help much here.

@easbar

easbar commented Oct 22, 2017

Copy link
Copy Markdown
Member Author

Looks like I am too used to IntelliJ's organize imports feature ...

@karussell
karussell merged commit e601eb8 into graphhopper:master Oct 23, 2017
@boldtrn

boldtrn commented Oct 23, 2017

Copy link
Copy Markdown
Member

Great PR, thanks for the awesome work. Just to make sure, this does not change the graph generation in any way, meaning, if I imported a graph before that PR, this graph is still perfectly valid and functional? From looking at the code, I would assume that this should be the case, just wanted to make sure.

@easbar

easbar commented Oct 25, 2017

Copy link
Copy Markdown
Member Author

Yes a previous graph should still work, the graph pre-processing is the same and only the query algorithm is changed.

@easbar
easbar deleted the stall_on_demand_ch branch November 22, 2017 06:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants