Repository navigation
Edge-based Contraction Hierarchies and Turncost support - #1247
Conversation
|
Looks like |
|
Regarding u-turn based traversal mode: For edge based CH we have to remember in quite a few places to exclude u-turns, which makes it hard sometimes. Would it not be much easier to set the default u-turn cost in TurnWeighting to infinity instead of explicitly checking if we make a u-turn (something like |
|
Great PR! Thanks for this.
I just recently worked on recognizing u-turns. Unfortunately it is not as easy as checking if edgeId == previousId, this should only happen when you place a via point, but each path between two points is calculated separately so this case should not really occur for route calculation itself. More frequently you would face situations that are for example double left or double right turns resulting on the opposite lane of a lane separated street. These are different edges in GH and different ways in OSM, so the id would not be equal. For example this U-turn won't be recognized by this technique. The recognizing of these u-turns currently only happens in the instruction calculation, see here. |
|
Cool, thanks a lot for this!
The idea was to allow u-turns at certain places to avoid large detours because of some turn restrictions, but as they are implemented they are too slow and also the routes feel often wrong as there might be u-turns at any junction. We could limit those junctions to e.g. dead ends but this could be done with the EDGE_BASED_2DIR traversal too IMO but still the u-turn costs would not be applied to situations mentioned from @boldtrn. |
| * | ||
| * @author Peter Karich | ||
| */ | ||
| public abstract class GenericDijkstraBidirection<T extends SPTEntry> extends AbstractBidirAlgo { |
There was a problem hiding this comment.
Nice, probably we could merge with AbstractBidirAlgo and use GenericDijkstraBidirection for bidirectional A* too then.
There was a problem hiding this comment.
Yes I thought about this too. There should be quite a bit of code in Astar that can be reused here, but I have not looked close yet.
| } | ||
| // we can not use in/outEdgeExplorer here because this method will be called within a loop using the | ||
| // in/outEdgeExplorer already | ||
| EdgeExplorer edgeExplorer = graph.createEdgeExplorer(new DefaultEdgeFilter(flagEncoder, reverse, !reverse)); |
There was a problem hiding this comment.
As creating EdgeExplorers is very expensive we should probably create two additional explorers in the constructor.
Yes. Another possibiility where this also fails is if two edges with fwd flags in different directions are used instead of one edge with fwd&bwd, but the osm import does not do this so far I think. In this PR the goal will first be to get the same results as Dijkstra with EDGE_BASED_2DIR and when this works we could try to do the same with EDGE_BASED_2DIR_UTURN. Other kind of u-turns (for example lane switches like you mentioned) that are not detected by the current Dijkstra implementation should probably be first fixed with Dijkstra before we implement this for CH. What do you think ?
Could we not use a default u-turn cost of infinity and use some finite value at the junctions where it should be allowed ? In the algorithms we would then stop expanding nodes/edges when the weight is infinity. In this case the u-turn behavior could be decided via the turn cost tables and the turn weightings and the algorithms would stay agnostic of the desired u-turn behavior. Just an idea, not sure... |
yes, this is for another issue
Yes, sounds good. Then we could reduce the traversal modes to just the two node vs. edge based. |
|
Have not looked deeply in the sources but tried it and works nicely! (I am using this map https://ahorn.lima-city.de/tr/ to easily see OSM turn restrictions) I can confirm that more shortcuts are created (for my berlin case it was just a factor of 8) but the preparation took much longer: 40min vs. 8 seconds
The config graph.flag_encoders=car|turn_costs=true is already usable for master too, but it is ignored for CH, so no need to set weightings to Most important message: it is really nice that now we have a working implementation that 'just' needs a bit tuning - this was a long way, thanks a lot :) ! |
Ah, strange. Are you including brandenburg? For just Berlin I get ~500K shortcuts compared to 84K for master. The edge count is 112K and 79K nodes. So the goal for Berlin should be under 100K I think :) |
|
Thanks for testing!
Yes, so far I mostly focused on 'working' and 'tested', 'fast' and 'efficient' should be next ;)
Ah thats nice, do you use it just to find junctions with turn restrictions or can we also use it as map layer and show our routes ?
Yes, your numbers are correct, I was using the same map, but accidentally was looking at the number of dijkstras not shortcuts in the log. So for Berlin there are about six (not fifty, like I wrote) times more shortcuts (good news ;)
Should we be able to switch on/off turn costs at query time provided that the preparation has been done for node & edge based cases ? See |
Here I was just thinking about how early we merge this PR. If we merge it early to avoid too many update work we need the old "node based way" for CH even if there is turn_costs=true specified (which would be then only used for the non-CH stuff like it is done at the moment). If we merge when query & preparation performance is close or just 2x away no backward compatibility config is necessary. |
Ok, right now I do not see a big problem having this on a separate branch and I would do the / some performance improvements before merging. I could also pick some changes that are not only related to turn costs from this branch and put them in a separate PR. |
|
Update: I have made some improvements. Besides some minor improvements local searches are now cancelled entirely as soon as it is clear that the target edge can not be reached via the node to be contracted, it is checked if a shortcut that is about to be added exists already and I implemented the node contraction priority calculation as given in the paper. For Berlin the contraction now takes about 4 minutes on my machine (about 15min previously) and I measured an average query time of about 13ms. For Bremen) the contraction takes about 25s contraction time and the queries run about 3ms. However, the number of shortcuts is still very high and I am struggling to find a way to reduce it. For Berlin I am getting about 430k and for Bremen about 110k. The high density of shortcuts edges seems to limit the performance of both the graph contraction and the routing queries. Especially towards the end of the contraction there seem to be just too many shortcut edges that get introduced. Right now I am still hoping to find an error that is responsible for the large number of shortcuts, but having checked many things I am slowly running out of ideas what could be wrong. Here is a list of things I have already looked into so far (but did not help much at least when I tried them):
It could be that the rather poor performance can only be improved by a combination of all of the above things, but I am still hoping there is some kind of bottleneck that can be fixed or I am simply forgetting something important. @karussell (or anybody else interested) maybe you could have a look and notice something ? Not sure how familiar you are with the turncost version of CH, but maybe there is something that was crucial for node-based CH that I am missing ? For testing I am currently using the CHMeasurement class, which performs the contraction for given OSM data and compares some random queries with normal Dijkstra. It usually also logs some errors, where the calculated weights are different, but they should be very small (I think some rounding issue that I did not find the reason for yet). Afterwards the query time is also measured. During the contraction some statistics are logged (for example we see that a lot of time is spent on neighbor updates and the number of shortcuts that are checked rises dramatically at the end). |
|
Thanks! I have no idea for a solution but I will have a look and here are my 2 cents:
Maybe we can at some point do a brainstorming call :) ? |
This is interesting and might be important to understand. Especially in this case - maybe the heuristic for node-based-CH needs other things than the edge-based variant. |
Yes, this is my fear also and the reason why I postponed it so far. However, I think sooner or later it will be required.
Just knowing that this took some time already helps and makes me feel less stupid ;)
Yes, this would be nice! I will do a bit more analysis, but then I think this would be a very good idea.
This duplicate neighbor check did not exist in the CH algorithm so far, I introduced it in hope of making the contraction faster because I did not see any reason to update a neighbor node multiple times just because there are multiple edges leading to it (which can happen quickly in edge-based CH). It definitely slows down the queries so I removed it / commented it out for now. The most surprising thing for me is that the number of shortcuts is not really affected, but the query time changes so much. I agree that we should check carefully how the neighbor updates affect the performance, and I have not done this systematically yet. |
|
As it is often with 'very interesting' behavior it turns out the slow down of the routing queries when the duplicate neighbor update check was enabled was simply due to a stupid error: The check not only prevented duplicate neighbor updates but also important edge disconnects. Sorry for the confusion. |
Really no problem and good that you sorted it out (this smaller issue :)) |
Yes, I think so. This way you can still set isForward/isBackward(encoder) to false in a profile to filter out the edge. Sorry, I was not aware this would make sense for any profile. I thought you would rather set the distance to infinity to block an edge ? |
…DefaultEdgeFilter. Signed-off-by: easbar <[email protected]>
|
Okay, cool. It still seems kind of dangerous / special-case-ish. PHILOSOPHICAL on Which means that I can, for the sake of an algorithm, turn the undirected graph into a directed graph for mode M. Which is what the edge filter is used for, but it is not the only thing it is used for. Now with loops, the problem is that they don't have two distinct "directions" in the mathematical sense, (because (a,b) == (b,a) given a==b) but the word "direction" above is not meant in the mathematical sense: In the data structure, even the loop has two directions, hence two sets of attributes. Now when I filter for edges that go in "one direction", I will still get loops that go in the "other direction". Meaning that if I use the filter to do the above transformation into a directed graph, I may get the loop with the wrong of the two sets of attributes. |
|
(Talking to myself now) Also, this seems to be an argument to forbid loops that have "both directions" accessible. Because that would mean that the transformation to a directed graph would duplicate loops (and only loops), which would make it a multigraph and not a graph. |
|
Thanks for your explanations!
I just thought the opposite. I would forbid loops that only have one direction accessible, because there is no way to differentiate between the two (besides edge-based shortcuts, see above...). Or maybe I would just forbid any kind of loops... ;) But I do get your point about the transformation to a directed graph, this is also true :) |
|
To give an example, have a look here: https://github.com/easbar/graphhopper/blob/2375b34b211b379c319ba3bb14966462cb7d0498/core/src/test/java/com/graphhopper/routing/ch/EdgeBasedNodeContractorTest.java#L1139 There is a (fwd-only) loop at node 1 and we need to determine the edges incoming to this node. |
The geometry is one thing which can be used to differentiate between the two. Assume a one-way loop for cars that allows both directions for foot and is accessible only in the reverse direction for bike - such a complicated case should be possible already from the routing and storing side. I remember having issues with one-way loops in the early days, see #162 (also for one more real world one-way loop example) @easbar in the (mergable) edge flags branch we have the flags always in the same direction (i.e. there is no longer a need to reverse flags when accessing the edge making a few things easier). Would this somehow help? |
|
Likely the Isochrone test needs a fix as it needs access to the one-way loop too regardless of the edge filter, just like in your edge-based case. The node-based routing is different: either, when there is no waypoint on a loop, we do not care about the loop or we create a virtual node via QueryGraph to e.g. start routing there which avoids this uncertainty. |
Ok, yes. This also fits with the way of thinking @michaz explained. We cannot differentiate between the two the way the routing is done at the moment, but this is for another issue. So we need a way to have loops that are only forward or backward. |
Signed-off-by: easbar <[email protected]>
Signed-off-by: easbar <[email protected]> # Conflicts: # core/src/main/java/com/graphhopper/routing/ch/CHAlgoFactoryDecorator.java # core/src/main/java/com/graphhopper/routing/ch/PrepareContractionHierarchies.java # core/src/main/java/com/graphhopper/storage/CHGraph.java # reader-osm/src/test/java/com/graphhopper/reader/osm/GraphHopperOSMTest.java # tools/src/main/java/com/graphhopper/tools/Measurement.java
…separate issue; no need for special EdgeFilter anymore; fixes graphhopper#1525
Signed-off-by: easbar <[email protected]>
Signed-off-by: easbar <[email protected]>
Signed-off-by: easbar <[email protected]>
# Conflicts: # core/src/test/java/com/graphhopper/routing/util/NameSimilarityEdgeFilterTest.java
… shortcuts in backward search. Signed-off-by: easbar <[email protected]>
There was a problem hiding this comment.
Ah, this is already green now - hurray!
From my side we can merge it.
@easbar I would not really like to squash this huge work as there is likely a huge learning work hidden which is important to keep IMO. Would be merging 297 commits ok? Or do you know some git magic to reduce the number a bit :) ?
Also you need to store the Email you used in the commits into your github account (I think you can easily add it even if you do no longer use actively it), otherwise those many commits do not belong to you.
Signed-off-by: easbar <[email protected]>
yay!
Hmm the only thing would be interactive rebase with some squashes ? There are quite a few 'fixes/minor/less important' commits so maybe it would be reduced to about 150 ? Then again sometimes when there are merge commits in between one has to resolve the conflicts again, which could be annoying. In any case this way we would still lose the git hashes (probably sometimes used in the discussion here). Either way I will keep this branch and I can also add it to the core repo. I think I would have squashed everything into one commit, but whatever you like better.
Ok, thanks for pointing this out, I added my mail&pgp key. I added some |

This PR shall enable turn costs for contraction hierarchies as described in this paper by R. Geisberger and C.Vetter.
This PR fixes #270 and has the same goal as #912, but follows a rather different approach and is much closer to what is described in the paper. Several of the problems described in #912 are addressed here like multiple loops/p-turns, u-turn detection and fixed size turn cost tables. Fundamentally this is realized by storing two additional integers per shortcut edge, which are set to the
IDs of the first and last original edges that are skipped by the shortcut. Doing this requires
many adjustments of the CH query algorithm (when expanding nodes and when checking bridge nodes where forward and backward searches meet) as well as the node contraction / shortcut searches.
What is working so far:
Next steps:
Integration with previous (node-based) CH code (
NodeContractorandPrepareContractionHierarchies) and support for automatic node ordering. So far the nodes can only be ordered in random order or ordered by ID, but this makes no sense for larger graphs. So farEdgeBasedNodeContractordoes not return any metrics that can be used for node ordering heuristics.Integration with Graphhopper API and integration tests, see what the performance is like.
Performance optimizations: There are many obvious things that can be improved:
Probably the most important thing is reusing data structures between the different witness path searches, something like
DijkstraOneToMany. For the turn replacement difference algorithm that is being used we need a way to initialize the search with edges adjacent to the start node and it must be possible to initialize them with arbitrary weights.Tuning of the node contraction parameters. The above mentioned paper describes a different metric than the one that is currently used for example. We can start with something simple (edge difference / quotient only for example) and see what the performance is like.
Minor stuff: See some of the todos in the code, there is definitely room for improvements.