Skip to content

Implementing turn costs - #238

Merged
karussell merged 14 commits into
masterfrom
turn_costs
Oct 22, 2014
Merged

karussell merged 14 commits into
masterfrom
turn_costs

Conversation

@karussell

Copy link
Copy Markdown
Member

I've now partially integrated turn restriction and turn cost. @khuebner already did most of the work - thanks a lot! You can enabled turn costs via osmreader.acceptyWay=car|turnCosts=true or in java via new CarFlagEncoder(5, 5, /*maxTurnCost see below*/3).

In order to do so I applied some changed which we should discuss:

  • to provide context for turn costs I added a new parameter prevOrNextEdgeId into Weighting.calcWeight. This avoids separate instanceof checks in all algorithms
  • I introduced "piping" of Weighting so the TurnWeighting now just holds another Weighting like for bike or car.
  • removed dijkstraNativebi as no performance advantage but maintenance disadvantage and similar to oneToManyDijkstra
  • removed TRAVERSAL_MODE.EDGE_BASED as I saw no benefit, so now either an algorithm is edgeBased or not.
  • changed the definition for costs vs. restriction: infinity == 'max cost' == restricted
  • currently turn restrictions for cars are disabled by default. We should probably change this.
  • fixed bug in OSMTurnRelation.getRestrictionAsEntries
  • defined weight to be in seconds not arbitrary for FastestWeighting to make adding of turn costs more meaningful (seconds!)
  • default u-turn cost is at 40 seconds but maybe we should completely avoid uturns as it increases search space? Also the only two applications for a u-turn I can think of are: 'tricking' turn restrictions and when via-points come into the game. Another minor problem is that we have to disable the performance tweak for edgeBased algorithms (if (!edgeBased && iter.getEdge() == prevOrNextEdgeId) return false;)
  • I implemented turn costs as well but only 2 bits (can be configured). I thought about the following sense: 0 (no costs), 1 (right turn), 2 (left turn?), 3 (forbidden). Also the bike and car encoder should make use of it (currently they don't, only restriction)

The following things are on my to do list before we can merge this branch:

  • fixing Exploring edges differs in QueryGraph #163
  • make one failing test pass in bidir algos (testCalcIfEmptyWay)
  • compare performance with node based algos: nodebased still unchanged perf but horrible slow if enabled!
  • make faster!

New issues: #269 and #270

@brunesto

Copy link
Copy Markdown

Great. thanks!

@karussell karussell added this to the 0.4 milestone Jul 18, 2014
@karussell karussell self-assigned this Jul 18, 2014
@karussell

Copy link
Copy Markdown
Member Author

We need to reintroduce TRAVERSAL_MODE as there are at least 4 options with significant differences in speed and features. E.g. node_based, edge_based_1dir (supports turn restrictions, as fast as node-based), edge_based_2dir (two times slower but supports p-turns) and edge_based_2dir_uturn (~5 times slower with u-turn support).

I'm still unsure whether we need u-turns in practice as they either produce ugly routes ('tricking' the turn restrictions) or when we put a penalty on them they won't occur. Also they make routing a lot slower as we have to disable the performance tweak (see above). Still they'd be useful on via points but that is a separate issue.

@karussell karussell modified the milestones: 0.4, 0.5 Aug 18, 2014
@brunesto

Copy link
Copy Markdown

Hi Peter, I added a OneToManyDijkstraRef class that simply extends Dijkstra and it passes the DijkstraOneToManyTest with all traversal modes (not sure about performance though). The branch is here: https://github.com/brunesto/graphhopper/tree/turn_costs_1tomanyref . I am still fighting with git/github so I am not able yet to make a pull request though :(

@karussell

Copy link
Copy Markdown
Member Author

Sounds interesting. The finish condition is not entirely correct though. But yes, I thought about this same solution to make it easier for future changes and working with the QueryGraph. Still performance is crucial and has to be tested and possibly improved.

@brunesto

Copy link
Copy Markdown

I blindly implemented the missing methods for using OneToManyDijktraRef instead of OneToManyDijktra with CH and all current tests pass but it does not actually work. I encounter 2 problems (so far):

  1. The node with turn restriction is having all its edges disconnected. I don't understand fully CH but it seems to me very strange. Here is a test case (http://pastebin.com/cFKy5mq1). Please could check it and let me know if it is a bug or the expected behavior?

This problem can be hacked around by uncommenting tmpIter.isShortcut() on line 248 in LevelGraphStorage, which lets me move to the next problem.

  1. u turns involving shortcuts might not be identified as such since the edgeIds differ.
    Is there something already implemented that I can use to retrieve the original edgeIds of the extremities of a shortcut or do I have to add fields fo this purpose to the levelGraphStorage? or do you have another suggestion?
    thanks!

@karussell

Copy link
Copy Markdown
Member Author

Regarding 1), not sure about the exact reason, but maybe the branch issue_116 can help where we disabled the optimization and use the LevelFilter instead (currently 20% slower)

Re 2) Yes, the edgeIds will be different. I've not thought about it yet. A related improvement could be done when the turn info is stored at the 'from edge' and not at the 'via node'. This way we can point to such the same turn information from an edge and all of its shortcuts (which is also an edge), no need for separate turn informations.

@brunesto

brunesto commented Sep 1, 2014

Copy link
Copy Markdown

I managed to get the CH working with turn restrictions but it is extremely slow.
The change that causes the slow down is adding the isShortut condition in LevelGraph.disconnectEdge(). It slows down by more than 50 times on our real life examples :(
This condition change is necessary as otherwise some nodes get completely disconnected, (see https://github.com/brunesto/graphhopper/blob/turn_costs_1tomanyref/core/src/test/java/com/graphhopper/routing/ch/NodesShouldHaveStillAtLeastOneEdgeTest.java)
and then graphhopper fails to find a route

I have tried with the issue_116 which indeed fixes the disconnection of nodes, but it also slows down a lot.

This simple test shows the speed difference:

@Test
public void testMe() throws IOException{

String osmFile="../../maps/OLD/prague-latest.osm.pbf";

GraphHopper graphHopper=new GraphHopper().setInMemory(true)
            .setGraphHopperLocation("/tmp/ghLoc-"+this.getClass().getSimpleName())
            .setOSMFile(osmFile)
.setEncodingManager(new EncodingManager("car"));
graphHopper.importOrLoad();

long start=System.currentTimeMillis();
for(int i=0;i<100;i++){
    GHRequest ghRequest=new GHRequest(
            new GHPoint( 50.0982604, 14.4000974),
            new GHPoint(50.0787133, 14.4744632)
            );
    ghRequest.setAlgorithm("astarbi");
    GHResponse ghResponse=graphHopper.route(ghRequest);
    System.err.println(ghResponse.isFound());
}
long end=System.currentTimeMillis();
System.err.println("100 routes in "+(end-start)+" millis");
}

100 routes in 1029 millis for master
100 routes in 4665 millis for master (with the isSortcut condition added in LevelGraph.disconnectEdge() )
100 routes in 4713 millis for issue-116 branch

Please could you give me some assitance? I see 2 ways to go further, either to speed up the routing in issue_116 branch, or to confirm if the removal of the nodes is indeed a bug or is an expected behavior.

Thanks

@karussell

Copy link
Copy Markdown
Member Author

The 'disconnect' is not a bug and expected behaviour. It is done to speed up things. We probably can implement turn costs with this limitation but it will be more complex so I have decided to avoid this optimization and accept the 25% slow down for now. There are some possible performance improvements possible as stated in #116 - see #240, some of them are simple like fixing the DFS bug, some not so.

This simple test shows the speed difference

Hmmh, I'll have to try this but for me I only see about 25% slower not >400%

@karussell

Copy link
Copy Markdown
Member Author

with the isSortcut condition added in LevelGraph.disconnectEdge()

that is not sufficient you'll need to add the LevelEdgeFilter enabled as done in 116, as well as some other minor fixes.

@brunesto

brunesto commented Sep 4, 2014

Copy link
Copy Markdown

Hi Peter, I am still struggling to run CH with turn restrictions. I am running out of time, so I would like to ask you if you would consider merging the turn restrictions in the master branch even if it is not supported with CH? I am asking because our plan was to make a pull request for
some of the functionalities we added (mainly vehicle weight/dimensions) but only once turn restrictions are inside the master branch.

FYI here are the main steps which I have done so far:

To handle TraversalMode.Edge2Dir:
One2ManyDijkstraRef

CH prepare does not disconnect edges anymore, instead they are flagged as non CH navigable.
The reason is that DijkstraBidirRef evaluates twice the joing edge, but with CH the joining edge (or its shortcut(s)) is sometimes only evaluated in one direction.
When a non CH navigable edge is evaluated its adjacent edges won't be inserted into the queue.

To handle turn restrictions:
The LevelGraph stores the original edgeId on both ends of an edge. (ie on a plain edge it is the same edgeId, on a shortcut of plain edges, the original edgeids == the skipped edges.)
The iterationKey uses the originalEdgeId instead of edgeId, and so does the turn cost weighting function.

@karussell

Copy link
Copy Markdown
Member Author

Why not use a separate branch where turn_costs is merged? Unfortunately I'll not have much time in the next weeks for this.

some of the functionalities we added (mainly vehicle weight/dimensions)
but only once turn restrictions are inside the master branch.

Is turn costs required for those features or why would it be necessary to wait?

@karussell

Copy link
Copy Markdown
Member Author

on a shortcut of plain edges

It is also possible to have a shortcut of shortcuts or mixed so the turn costs info should not use the skippedEdge. Instead I would make it possible to use one turn cost information for multiple edges.

@brunesto

brunesto commented Sep 5, 2014

Copy link
Copy Markdown

Why not use a separate branch where turn_costs is merged?

So I will prepare a branch to merge Turn restrictions into the master and then add DijkstraOneToManyRef to it.

Is turn costs required for those features or why would it be necessary to wait?

Well it makes more sense for us to push for the turn restrictions first in the master, because
we might use it as soon as it will be there, while putting first the vehicle limitations might have the opposite effect of slowing down the turn restriction integration.

@karussell

Copy link
Copy Markdown
Member Author

For the open work I've created: #269

For CH there is also a new issue: #270

@karussell karussell closed this Oct 21, 2014
@karussell karussell reopened this Oct 21, 2014
@karussell karussell modified the milestones: 0.4, 0.5 Oct 21, 2014
@karussell
karussell merged commit 6d8e7e7 into master Oct 22, 2014
@karussell
karussell deleted the turn_costs branch January 20, 2015 14:42
dabiih pushed a commit to dabiih/graphhopper that referenced this pull request Jan 7, 2025
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