Skip to content

Remove TurnWeighting and handle turn weights within Weighting instead - #1863

Merged
easbar merged 25 commits into
masterfrom
remove_turn_weighting
Jan 28, 2020
Merged

easbar merged 25 commits into
masterfrom
remove_turn_weighting

Conversation

@easbar

@easbar easbar commented Jan 23, 2020 •

Copy link
Copy Markdown
Member

Fixes #1820

This is (more or less) what happens here:

  • Weighting has two new methods calcTurnWeight/Millis(inEdge, viaNode, outEdge) so besides edge-weights it now also calculates turn weights

  • The turn weight is calculated by implementations of a new interface TurnCostProvider, there is NoTurnCostProvider in case the turn costs should be zero. DefaultTurnCostProvider handles u-turn costs as we did so far and applies turn restrictions (same as TurnWeighting did before)

  • AbstractWeighting has a TurnCostProvider, which is passed via the constructor -> It must be known at the time the Weighting is created (in contrast to the old TurnWeighting which wrapped an existing Weighting after it was created).

  • Since AbstractWeighting provides default implementations for calcTurnWeight/Millis, the sub-classes usually do not need to be concerned with turn weightings. They only need to override calc*Edge*Weight/Millis. If you want to change the turn weights in a subclass you can just pass another TurnCostProvider to the AbstractWeighting` constructor.

  • Some Weightings do not inherit from AbstractWeighting, but rather wrap another weighting (e.g. avoid edges and block area). For this reason its ugly to have Weighting#calcWeight(edgeState, reverse, prevNext) which is now just a combination of edge and turn weights anyway. For example the AvoidEdgesWeighting adjusts the edge weights, but is unaware of the turn cost implementation of the wrapped weighting, so its not clear how to implement calcWeight based on the wrapped weighting. Therefore I removed Weighting#calcWeight. For the (quite common) case where we combine edge- with turn-weights there is a static helper method (currently in AbstractWeighting).

  • There is one more (bigger) complication: Turn costs at virtual nodes and edges. So far we had to use the (special) TurnCostStorage from QueryGraph and pass it into TurnWeighting. But now that TurnWeighting is gone we have to wrap the Weighting into QueryGraphWeighting when we use it with QueryGraph instead (at least to calculate turn costs). This is a bit ugly to do, so I added Graph#wrapWeighting (which is only really implemented by QueryGraph for now).

easbar added 18 commits January 19, 2020 21:38
# Conflicts:
#	core/src/test/java/com/graphhopper/routing/weighting/RandomizedRoutingTest.java
# Conflicts:
#	core/src/main/java/com/graphhopper/storage/GraphBuilder.java
#	core/src/test/java/com/graphhopper/routing/CHQueryWithTurnCostsTest.java
#	core/src/test/java/com/graphhopper/routing/RandomCHRoutingTest.java
#	core/src/test/java/com/graphhopper/routing/ch/EdgeBasedNodeContractorTest.java
#	core/src/test/java/com/graphhopper/routing/ch/EdgeBasedWitnessPathSearcherTest.java
#	core/src/test/java/com/graphhopper/storage/ShortcutUnpackerTest.java
# Conflicts:
#	core/src/main/java/com/graphhopper/storage/GraphBuilder.java
#	core/src/test/java/com/graphhopper/routing/CHQueryWithTurnCostsTest.java
#	core/src/test/java/com/graphhopper/routing/RandomCHRoutingTest.java
#	core/src/test/java/com/graphhopper/routing/ch/EdgeBasedNodeContractorTest.java
#	core/src/test/java/com/graphhopper/routing/ch/EdgeBasedWitnessPathSearcherTest.java
#	core/src/test/java/com/graphhopper/storage/ShortcutUnpackerTest.java
@easbar easbar added this to the 1.0 milestone Jan 23, 2020

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

Thanks a lot! This LGTM!

The only bigger problem I have is with the static AbstractWeighting.calc* methods - it just looks ugly :) ... should we make them non-static and include them in the Weighting interface? If you prefer to keep this interface lean then should we move the static methods to the interface? IMO it is applicable to all Weighting implementations not only the ones that extend AbstractWeighting (?)

Is it still a WIP? ... looks very polished already :)

@easbar

easbar commented Jan 26, 2020

Copy link
Copy Markdown
Member Author

Thanks a lot for taking a look!

The only bigger problem I have is with the static AbstractWeighting.calc* methods - it just looks ugly :) ... should we make them non-static and include them in the Weighting interface?

Yes this is not ideal, but having them non-static isn't better IMO. The reason I did not include it in Weighting is that its not clear how to implement when a weighting wraps another, for example BlockAreaWeighting (like I said in initial comment).

If you prefer to keep this interface lean then should we move the static methods to the interface? IMO it is applicable to all Weighting implementations not only the ones that extend AbstractWeighting (?)

Its certainly misleading that the static method is in AbstractWeighting. I would certainly like to move it into Weighting, but unfortunately that is not possible in Java 7. We could move it into GHUtilities? Or if you like it better we can of course also put it into the interface and somehow document the issue with wrapping Weightings.

Is it still a WIP?

I don't think there is much missing, should be possible to merge soon.

@devemux86

Copy link
Copy Markdown
Contributor

but unfortunately that is not possible in Java 7

Indeed that would break Java 7 compatibility, as seems needing Java 8.

@easbar easbar changed the title WIP: Remove TurnWeighting and handle turn weights within Weighting instead Remove TurnWeighting and handle turn weights within Weighting instead Jan 26, 2020
@karussell

Copy link
Copy Markdown
Member

I would certainly like to move it into Weighting, but unfortunately that is not possible in Java 7

Ah, this is ugly. Thought this was only regarding the default methods.

# Conflicts:
#	core/src/main/java/com/graphhopper/GraphHopper.java
#	core/src/main/java/com/graphhopper/storage/CHProfile.java
@easbar
easbar merged commit 4582ee0 into master Jan 28, 2020
@easbar
easbar deleted the remove_turn_weighting branch January 29, 2020 10:35
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.

Remove TurnWeighting

3 participants