Skip to content

Refactors bidirectional routing algorithms. - #1341

Merged
karussell merged 25 commits into
graphhopper:masterfrom
easbar:algorithm_refactoring
Apr 18, 2018
Merged

karussell merged 25 commits into
graphhopper:masterfrom
easbar:algorithm_refactoring

Conversation

@easbar

@easbar easbar commented Apr 14, 2018

Copy link
Copy Markdown
Member

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.

easbar added 23 commits April 11, 2018 18:45
* Fixes some typos and whitespace
* Adds visited nodes sum to via routing template debug info.
# 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
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]>
Signed-off-by: ammagamma <[email protected]>
Signed-off-by: ammagamma <[email protected]>
Signed-off-by: ammagamma <[email protected]>

@karussell karussell left a comment

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.

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();

@karussell karussell Apr 14, 2018 •

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.

Why can't we call entry.getParent directly?

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.

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.

@karussell karussell Apr 15, 2018 •

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.

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.

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 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.

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.

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();

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.

What is the purpose of this? Also I do not understand what you mean with the comment.

@easbar easbar Apr 15, 2018 •

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.

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'.

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.

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);

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.

Maybe we should remove this then. I think I used it previously to implement some performance improvement that didn't work out. Not sure.

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.

Ok, yes I agree. I wanted to hear your opinion about this.

createAndInitPath();
initFrom(to, 0);
initTo(from, 0);
init(from, 0, to, 0);

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.

oh, wow...what was this. Thanks!

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.

The code was correct, but the variable names were misleading :)

@karussell karussell added this to the 0.11 milestone Apr 15, 2018
@karussell

Copy link
Copy Markdown
Member

Please review and let me know if my my merge of the two classes and removal of getParent method good to go?

@easbar

easbar commented Apr 18, 2018

Copy link
Copy Markdown
Member Author

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
*

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.

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 ?

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, will do.

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.

When you are at it maybe make use of Java 7 diamond operator as well ('Explicit type can be replaced with <>' inspection in IntelliJ) :)

@karussell

Copy link
Copy Markdown
Member

Basically you moved the code from GenericDijkstra into AbstractBidirAlgo, right ?

Yes

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)

Yes, it avoids the getParent call also. But if casting gets ugly, we will reconsider?

@easbar

easbar commented Apr 18, 2018

Copy link
Copy Markdown
Member Author

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 .getWeightOfVisitedPath() instead of .weight in a few places and AStarEntry will return a different weight here than SPTEntry. So the cases where we really have to cast should be rather limited and I agree that at the moment this does not necessarily justify an extra class and the type parameter.

@easbar

easbar commented Apr 18, 2018 •

Copy link
Copy Markdown
Member Author

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 incEdge (the last original edge of a shortcut edge) whenever we expand an SPTEntry.

@karussell

Copy link
Copy Markdown
Member

Unless of course you fear that calling a getter method has worse performance than accessing the variable directly

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?

@easbar

easbar commented Apr 18, 2018 •

Copy link
Copy Markdown
Member Author

So you are ok with merging?

Yes

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.

2 participants