Repository navigation
Refactors bidirectional routing algorithms. - #1341
Conversation
* Fixes some typos and whitespace * Adds visited nodes sum to via routing template debug info.
Signed-off-by: ammagamma <[email protected]>
# Conflicts: # core/src/main/java/com/graphhopper/GraphHopper.java # core/src/main/java/com/graphhopper/routing/ch/PrepareContractionHierarchies.java # core/src/main/java/com/graphhopper/util/GHUtility.java # reader-gtfs/src/main/java/com/graphhopper/reader/gtfs/RealtimeFeed.java
Signed-off-by: ammagamma <[email protected]>
Signed-off-by: ammagamma <[email protected]>
Signed-off-by: ammagamma <[email protected]>
This makes AbstractRoutingAlgorithm independent from SPTEntry and allows introducing a generic subclass that can be used to share the bidirectional dijkstra logic between the different algorithms.
Signed-off-by: ammagamma <[email protected]>
…kstraBidirection. Signed-off-by: ammagamma <[email protected]>
Signed-off-by: ammagamma <[email protected]>
Signed-off-by: ammagamma <[email protected]>
Signed-off-by: ammagamma <[email protected]>
Signed-off-by: ammagamma <[email protected]>
Signed-off-by: ammagamma <[email protected]>
Signed-off-by: ammagamma <[email protected]>
Signed-off-by: ammagamma <[email protected]>
…idirection. Signed-off-by: ammagamma <[email protected]>
Signed-off-by: ammagamma <[email protected]>
Signed-off-by: ammagamma <[email protected]>
Signed-off-by: ammagamma <[email protected]>
karussell
left a comment
There was a problem hiding this comment.
Looks really good. Thanks! Some minor comments...
| // using 'weight' is important and correct here e.g. approximation can get negative and smaller than 'weightOfVisitedPath' | ||
| return currFrom.weight + currTo.weight >= bestPath.getWeight(); | ||
| protected AStarEntry getParent(AStarEntry entry) { | ||
| return entry.getParent(); |
There was a problem hiding this comment.
Why can't we call entry.getParent directly?
There was a problem hiding this comment.
This is needed to satisfy the type system. The abstract method this method implements has return type T extends SPTEntry and is used to assign to a variable with the same type via
`entry = getParent(entry)`
here. Calling
entry = entry.getParent()
would be a type error because we are assigning SPTEntry to T and we would need an unchecked cast like this:
`entry = (T) entry.getParent()`
This cast is avoided by implementing getParent for the specific subclasses of SPTEntry AStarEntry etc.
There was a problem hiding this comment.
Hmmh, this would be an extra call just for the broken generic handling in Java. Highly likely the JIT optimizes this away but the only reason we do this is that we want to avoid this cast:
((AStarEntry) entry).weightOfVisitedPath = weight;
in AStarBidirection. For this reason we create the separate GenericDijkstraBidirection class, add a generic argument to it and 3 getParent methods. I would say to simplify this (remove the 3 methods and the separate GenericDijkstraBidirection class) and avoid the extra getParent call we should accept this cast, which is also pretty save because the createEntry methods control object creation. If you agree I could also push my changes here.
There was a problem hiding this comment.
Sure if you think you have a better solution push it! Its also true that we need to cast at some point anyway the way it is now (the getParent() implementations all do a cast). Not sure which statment weightOfVisitePath = weight you mean but I guess it becomes clear to me when you push.
There was a problem hiding this comment.
Regarding JIT, yes thats what I would assume as well, but in the end you never know :) I ran the measurement class a few times for this PR and master and could not note a clear difference (but it is also hard to tell because each measurement run gives somewhat different results)
| return bestPath; | ||
| protected boolean finished() { | ||
| // using 'weight' is important and correct here e.g. approximation can get negative and smaller than 'weightOfVisitedPath' | ||
| return super.finished(); |
There was a problem hiding this comment.
What is the purpose of this? Also I do not understand what you mean with the comment.
There was a problem hiding this comment.
Ok, so actually I think you wrote this comment and the sole purpose of overriding this method here was to have a place where I can put the comment because I did not want to delete it :)
I think what you wanted to say with this comment is this: For AStar we store two weights in the shortest path entries: The first one weight is the one that determines the order in which the nodes are visited (the order in the priority queue) and includes the approximate remaining distance to the target. The second one weightOfVisitedPath stores the real weight a shortest path entry has (without approximation). The latter is also used for the weight in bestPath. When we check if the search has finished we do this
currFrom.weight + currTo.weight >= bestPath.getWeight()
So we actually compare the weight (with approximation) to the weight without approximation in bestPath and I think the comment is trying to make the reader aware that we use currFrom/To.weight not currFrom/To.weightOfVisitedPath. However, I do not think I fully understand what is meant with 'approximation can get negative and smaller than weight'.
There was a problem hiding this comment.
you wrote this comment
Haha, ok ;)
Very strange comment. I added it in this commit about a performance improvement: 37df1b7
Also this comment in the same commit is strange: 37df1b7#diff-4bdaac1f080eb5164041e7db4280d193L284
Probably this comment should be removed to avoid further confusion.
| protected boolean acceptTraversalId(int traversalId, boolean reverse) { | ||
| // todo: ignoreExplorationFrom/To always stays empty (?!) | ||
| IntHashSet ignoreExploration = reverse ? ignoreExplorationTo : ignoreExplorationFrom; | ||
| return !ignoreExploration.contains(traversalId); |
There was a problem hiding this comment.
Maybe we should remove this then. I think I used it previously to implement some performance improvement that didn't work out. Not sure.
There was a problem hiding this comment.
Ok, yes I agree. I wanted to hear your opinion about this.
| createAndInitPath(); | ||
| initFrom(to, 0); | ||
| initTo(from, 0); | ||
| init(from, 0, to, 0); |
There was a problem hiding this comment.
oh, wow...what was this. Thanks!
There was a problem hiding this comment.
The code was correct, but the variable names were misleading :)
Signed-off-by: ammagamma <[email protected]>
|
Please review and let me know if my my merge of the two classes and removal of getParent method good to go? |
|
Basically you moved the code from GenericDijkstra into AbstractBidirAlgo, right ? Now we do not have a type parameter anymore and subclasses need to cast SPTEntry into whatever entry-type they use (AStarEntry and for turn-cost CH there will be CHEntry and maybe AStarCHEntry) in case there needs to be done something specific (like updating weightOfVisitedPath) in AStarBidirection. The good thing is that the class hierarchy becomes shallower, so lets do it your way! |
| * | ||
| * GraphHopper GmbH licenses this file to you under the Apache License, | ||
| * Version 2.0 (the "License"); you may not use this file except in | ||
| * |
There was a problem hiding this comment.
Looks like you updated your IntelliJ version ? Maybe we can run the code formatting on the whole project to avoid having these diffs in every commit ?
There was a problem hiding this comment.
When you are at it maybe make use of Java 7 diamond operator as well ('Explicit type can be replaced with <>' inspection in IntelliJ) :)
Yes
Yes, it avoids the getParent call also. But if casting gets ugly, we will reconsider? |
Yes, thats nice. We can also use the polymorphism of the SPTEntry subclasses in a few places (for example we do |
|
Unless of course you fear that calling a getter method has worse performance than accessing the variable directly (?). This really should be handled by the machine ? Casting would be ugly for turn cost CH I think, because there we always need to get the |
It has ... but is very tiny and will be very fast optimized away. But sometimes you can help JIT a bit with final methods then. So you are ok with merging? |
Yes |
This is a follow up of #1336 and it should be less code to review after this is merged.
The goal of this PR is to reduce some code duplication between bidirectional Dijkstra and AStar algorithms and to make it possible to reuse the code for turn-cost CH as well (#1247).
Both DijkstraBidirectionRef and AStarBidirection now subclass a new class GenericDijkstraBidirection that has a generic parameter for the type of shortest path entries.
I tried to make the refactoring somewhat easy to follow in the single commits, no real logic has changed it mostly moving code around and some renaming, with one exception: In the initFrom/To methods there were some checks for null that were needed because a) it was not enforced in which order these methods were called and b) LandmarkExplorer used these methods to initialize a fwd/bwd-only search. I have disentangled the code in these methods and changed LandmarkExplorer accordingly such that these null-checks should be no longer necessary.