Repository navigation
Extracts new class NodeContractor from PrepareContractionHierarchies. - #1225
Conversation
* some simple restructuring to prepare NodeContractor extraction * some readability improvements
* reordering is supposed to make diffs easier to read when NodeContractor is extracted * besides the reordering no code is changed
* because findShortcuts shall be attributed to NodeContractor in the future it needs to be decoupled from meanDegree (meanDegree is used by PrepareContractionHierarchies to determine the node ordering). to achieve this another variable maxVisitedNodes is introduced. * the changes made to findShortcuts are replaced by corresponding code that is inserted before / after the few places where findShortcuts is called, if this is needed
…tractionHierarchies * creates the methods (getters etc. that will belong to NodeContractor)
…chies. * NodeContractor is responsible for searching for witness paths and adding required shortcut edges for a given node that shall be contracted * PrepareContractionHierarchies is responsible for determining the order in which nodes are contracted and provides the interface to start the contraction of a graph
* adjusts some access modifiers so NodeContractor can be moved out of PrepareContractionHierarchies * adds some documentation * otherwise does not change the code
* some minor clean-up
* makes NodeContractor (and thereby PrepareContractionHierarchies) throw an exception when it is called with edge-based traversal mode, because it is not supported yet
…ies. * meanDegree / tmpDegreeCounter variables are a bit confusing as they do not represent the degree of a node (number of adjacent edges)
* fixes todo in NodeContractor#setOrigEdgeCount, the previous implementation returned the number of original edges where most likely the number of original edges + shortcuts was meant.
…ContractionHierarchies. * so far maxVisitedNodes was always set to zero in prepareNodes() / calculatePriority() because meanDegree was initialized only afterwards in contractNodes() this means so far the priorities were not calculated right because no witness paths could be found. * this fix requires adjusting three tests that depend on the contraction order. adjusted createExampleGraph instead of testAddShortcut, because testAddShortcut does no longer find a shortcut with the example graph. the other tests using createExampleGraph are not be affected by this change.
…archies. * previously maxVisitedNodes was 0 if meanDegree < 1. * this change affects the node contraction order and one test had to be adjusted.
* postpones creation of NodeContractor in PrepareContractionHierarchies to initFromGraph(), gets rid of NodeContractor#setMaxEdgesCount * makes levelFilter in PCH a local variable
* moves testShortcutMergeBug to NodeContractorTest * removes PCH#testAddShortcuts
…ctorTest and cleans up. * these tests basically 'misuse' PCH only to set the node priorities to the maximum level * makes PCH#initFromGraph,prepareNodes,contractNodes private * removes PCH#testFindShortcuts * some clean-up
…rom 0 to max. * so far NodeContractor used a default maxVisitedNodes of 0, which did not make much sense
…actorTest. * for some reason AllCHEdgesIterator returns a shortcut where both skipped1/2edges are 0
|
Thanks, this looks nice, will try what this means for bigger areas.
Yeah, this is really time intense but might be worth trying different periodicUpdatesPercentage, lastNodesLazyUpdatePercentage and neighborUpdatePercentage
Hmmh, this might be a bug where we need to skip invalid edges (maybe edges from CH disconnection) |
Also it might be worth trying different linear combinations in calculatePriority
It looks like |
hmmh, I need to refresh my memories here :) but yes, this might be a bug.
When we call prepareGraph.disconnect in the CH preparation there will be edges afterwards that are no longer used from the graph, but are still present in the edge storage. And we skip them (maybe here is a bug). BTW: In graph.optimize we remove unused nodes coming from PrepareRoutingSubnetworks but we do not remove unused edges yet, as removing nodes is already a too complicated thing. |
* adds another test, adds some infrastructure now that AllCHEdgeIterator is fixed. 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]>
|
I ran a few more performance tests with the OSM data for all of Germany. Its not easy to draw clear conclusions (without doing many many runs), but it looks like the 'fixed' node ordering slows down the prepare CH time ( |
|
Thanks, this makes sense. Have added a note in #240. |
| algo.setEdgeFilter(levelFilter); | ||
| return algo; | ||
| private int getMaxVisitedNodesEstimate() { | ||
| // todo: we return 0 here if meanDegree is < 0, which is not really what we want, but changing this changes |
There was a problem hiding this comment.
did you mean "we return 0 here if meanDegree is < 1" instead of <0?
Signed-off-by: ammagamma <[email protected]>
|
Sorry I forgot about this. Do you think I can squash & merge it or do you want to preserve some commits separated? |
|
You can squash & merge, I will keep this branch in my fork. Not sure about these new code climate issues though, for example maximum number of parameters=4, maximum method length=25 lines, maximum file length=250 lines. I think they will be rather difficult to realize especially in test code and will for sure require many changes in existing code. I am nowhere against strong separation of concerns and short methods, but I don't think such strict rules are the best way to achieve this. I think it is better to let the programmer(s) decide (via code reviews for example ;). |
|
Yeah, the codeclimate stuff is rather restrictive and for some things I find it valuable but for the most things not really. Probably we should remove it for now. |
This PR extracts a new class
NodeContractorfromPrepareContractionHierarchies.The idea is this:
NodeContractoris responsible for searching for witness paths and adding requiredshortcut edges for a single given node that shall be contracted
PrepareContractionHierarchiesis responsible for determining the orderin which nodes are contracted and provides the interface to start the
contraction of a graph
This separation of concerns should make it easier to extend contraction hierarchies for
new features and applications (like turncosts, dynamic ch or transit node routing).
It also makes it easier to test the contraction of a single node and hopefully increases
readability.
I tried to make the code changes easy to follow in the single commits. In the first commits
basically no real code changes are made except some reordering, renaming and delegation. I also
stumbled over a few minor issues in the existing
PrepareContractionHierarchieswhich should nowbe fixed:
In
setOrigEdgeCount()anIllegalStateExceptionwas thrown that gives information about 'graph.max'I think this was supposed to return the number of all edges in the graph (original + shortcuts),
but
ghStorage.getAllEdges().getMaxId()returns only the number of original edges, which is redundantto 'max' in the message.
The
tmpDegreeCountervariable incontractNodesdoes not really count the degree of the node,because it is only increased for each incoming (but not outgoing) edge. This is a bit confusing
when reading the code, but otherwise is not very crucial, because the degree calculation is
only used to estimate the
meanDegreeof the graph, which in turn is used to estimate a good upperbound of the
maxVisitedNodesfor the local witness path searches. I tried to document this a bit,but maybe there is a better name than 'degree' ?
When calling
doWork()inPrepareContractionHierarchiesthe priority for all nodes is calculatedin
prepareNodes(). However, at this pointmeanDegreeis zero, and so infindShortcutsa
maxVisitedNodesof 0 is set onprepareAlgo. This means that no witness paths can be foundand all shortcuts are counted (but not added).
For example this can be seen in
PrepareContractionHierarchiesTest#testAddShortcuts.When
contractNodes()is calledmeanDegreeis finally set to a non-zero value and when actuallyinserting the shortcuts the witness path searches work as expected.
This was an easy fix, but required adjusting a few tests, because
PrepareContractionHierarchiesnow finds a different (potentially better) node ordering.
when calculating
maxVisitedNodesfrommeanDegreeit was cast like this:(int) meanDegree * 100, somaxVisitedNodeswas 0 ifmeanDegree< 1I changed this to
(int) (meanDegree * 100), which yields a different (probably better) nodecontraction order.
The last two fixes affect the node ordering when contracting the graph.
I ran the measurement class on the latest OSM data for Hessen and the performance
seems to be similar before and after the refactoring. This version introduces a few (~1-2%) less shortcuts, takes longer to prepare the graph (~10%) and leads to faster queries (for example routingCH.sum was reduced by 5%). All this seems plausible given that in the prior version
maxVisitedNodeswas sometimes zero, i.e. the witness path searches were cancelled quickly and therefore more shortcuts were introduced. I did not make exhaustive measurements (different maps, multiple runs etc.) though.In the new version the node ordering should be closer to what was intended, but maybe the parameter
tuning in
PrepareContractionHierarchiesneeds to be adjusted.One thing I am not sure of is if we need
NodeContractor#initFromGraph, because so far it is alwayscalled directly after the constructor. However, if we call the constructor and change the graph
afterwards we need to call
initFromGraph()to makeNodeContractoraware of the changes. Alternatively,we could call
initFromGraph()insideNodeContractoras needed (for example incontractNode()etc.).Finally, I stumbled over a weird behavior of
AllCHEdgesIteratorwhere the skippedEdge values are equalalthough they should be different, see
NodeContractorTest#testContractNode_directed_shortcutRequired.I have not looked into this, any idea what the reason for this could be ?