Skip to content

Extracts new class NodeContractor from PrepareContractionHierarchies. - #1225

Merged
karussell merged 29 commits into
graphhopper:masterfrom
easbar:prepare_ch_refactoring
Dec 18, 2017
Merged

karussell merged 29 commits into
graphhopper:masterfrom
easbar:prepare_ch_refactoring

Conversation

@easbar

@easbar easbar commented Nov 19, 2017

Copy link
Copy Markdown
Member

This PR extracts a new class NodeContractor from PrepareContractionHierarchies.
The idea is this:

  • NodeContractor is responsible for searching for witness paths and adding required
    shortcut edges for a single 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

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 PrepareContractionHierarchies which should now
be fixed:

  1. In setOrigEdgeCount() an IllegalStateException was 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 redundant
    to 'max' in the message.

  2. The tmpDegreeCounter variable in contractNodes does 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 meanDegree of the graph, which in turn is used to estimate a good upper
    bound of the maxVisitedNodes for the local witness path searches. I tried to document this a bit,
    but maybe there is a better name than 'degree' ?

  3. When calling doWork() in PrepareContractionHierarchies the priority for all nodes is calculated
    in prepareNodes(). However, at this point meanDegree is zero, and so in findShortcuts
    a maxVisitedNodes of 0 is set on prepareAlgo. This means that no witness paths can be found
    and all shortcuts are counted (but not added).
    For example this can be seen in PrepareContractionHierarchiesTest#testAddShortcuts.
    When contractNodes() is called meanDegree is finally set to a non-zero value and when actually
    inserting the shortcuts the witness path searches work as expected.
    This was an easy fix, but required adjusting a few tests, because PrepareContractionHierarchies
    now finds a different (potentially better) node ordering.

  4. when calculating maxVisitedNodes from meanDegree it was cast like this:
    (int) meanDegree * 100, so maxVisitedNodes was 0 if meanDegree < 1
    I changed this to (int) (meanDegree * 100), which yields a different (probably better) node
    contraction 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 maxVisitedNodes was 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 PrepareContractionHierarchies needs to be adjusted.

One thing I am not sure of is if we need NodeContractor#initFromGraph, because so far it is always
called directly after the constructor. However, if we call the constructor and change the graph
afterwards we need to call initFromGraph() to make NodeContractor aware of the changes. Alternatively,
we could call initFromGraph() inside NodeContractor as needed (for example in contractNode() etc.).

Finally, I stumbled over a weird behavior of AllCHEdgesIterator where the skippedEdge values are equal
although they should be different, see NodeContractorTest#testContractNode_directed_shortcutRequired.
I have not looked into this, any idea what the reason for this could be ?

* 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
* 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
@karussell
karussell self-requested a review November 19, 2017 19:59
@karussell karussell added this to the 0.10 milestone Nov 19, 2017
@karussell

Copy link
Copy Markdown
Member

Thanks, this looks nice, will try what this means for bigger areas.

In the new version the node ordering should be closer to what was intended, but maybe the parameter tuning in PrepareContractionHierarchies needs to be adjusted.

Yeah, this is really time intense but might be worth trying different periodicUpdatesPercentage, lastNodesLazyUpdatePercentage and neighborUpdatePercentage

Finally, I stumbled over a weird behavior of AllCHEdgesIterator where the skippedEdge values are equal although they should be different

Hmmh, this might be a bug where we need to skip invalid edges (maybe edges from CH disconnection)

@easbar

easbar commented Nov 19, 2017

Copy link
Copy Markdown
Member Author

Yeah, this is really time intense but might be worth trying different periodicUpdatesPercentage, lastNodesLazyUpdatePercentage and neighborUpdatePercentage

Also it might be worth trying different linear combinations in calculatePriority

Hmmh, this might be a bug where we need to skip invalid edges (maybe edges from CH disconnection)

It looks like AllEdgesCHIterator.getSkippedEdge1/2 always returns 0. What do you mean 'we need to skip invalid edges' ? You mean this behavor is intentional or something needs to be changed ? In the unit tests I would have liked to get all shortcut edges directly from the graph after the contraction to be able if the shortcuts I am expecting have been added (with the right weight, distance and skippedEdge1/2).

@karussell

karussell commented Nov 19, 2017 •

Copy link
Copy Markdown
Member

It looks like AllEdgesCHIterator.getSkippedEdge1/2 always returns 0

hmmh, I need to refresh my memories here :) but yes, this might be a bug.

What do you mean 'we need to skip invalid edges' ?

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]>
@easbar

easbar commented Nov 23, 2017

Copy link
Copy Markdown
Member Author

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 (prepare.ch.time in the Measurement class) by a few (5-10) percent. The query time does not seem to be improved significantly, although about 1.5% less shortcuts are introduced.
Therefore I have reverted the fixes to the node ordering, and simply left a comment about the issues for now. I would postpone fixing them until somebody takes the time to re-optimize the parameters in PrepareContractionHierarchies.

@karussell karussell mentioned this pull request Nov 23, 2017
3 of 10 tasks
@karussell

Copy link
Copy Markdown
Member

Thanks, this makes sense. Have added a note in #240.

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

LGTM

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

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.

did you mean "we return 0 here if meanDegree is < 1" instead of <0?

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

@karussell

Copy link
Copy Markdown
Member

Sorry I forgot about this. Do you think I can squash & merge it or do you want to preserve some commits separated?

@easbar

easbar commented Dec 17, 2017

Copy link
Copy Markdown
Member Author

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

@karussell

Copy link
Copy Markdown
Member

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.

@karussell
karussell merged commit 2409d0d into graphhopper:master Dec 18, 2017
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