Repository navigation
Description
So far there are two todos in NodeBasedNodeContractor, that refer to odd/false calculations regarding the CH preparation heuristics (also pointed out here: #1225 (comment)):
graphhopper/core/src/main/java/com/graphhopper/routing/ch/NodeBasedNodeContractor.java
Lines 81 to 86 in 0a967b3
| // todo: initializing meanDegree here instead of in initFromGraph() means that in the first round of calculating | |
| // node priorities all shortcut searches are cancelled immediately and all possible shortcuts are counted because | |
| // no witness path can be found. this is not really what we want, but changing it requires re-optimizing the | |
| // graph contraction parameters, because it affects the node contraction order. | |
| // when this is done there should be no need for this method any longer. | |
| meanDegree = prepareGraph.getEdges() / prepareGraph.getNodes(); |
graphhopper/core/src/main/java/com/graphhopper/routing/ch/NodeBasedNodeContractor.java
Lines 312 to 314 in 0a967b3
| // todo: we return 0 here if meanDegree is < 1, which is not really what we want, but changing this changes | |
| // the node contraction order and requires re-optimizing the parameters of the graph contraction | |
| return (int) meanDegree * 100; |
These are easy to fix, but fixing them probably requires re-optimizing the CH preparation parameters.
I think there is another one: One of the CH preparation heuristics counts the number of contracted neighbor nodes. Currently this is done like this:
graphhopper/core/src/main/java/com/graphhopper/routing/ch/NodeBasedNodeContractor.java
Lines 116 to 123 in 0a967b3
| int contractedNeighbors = 0; | |
| int degree = 0; | |
| CHEdgeIterator iter = remainingEdgeExplorer.setBaseNode(node); | |
| while (iter.next()) { | |
| degree++; | |
| if (iter.isShortcut()) | |
| contractedNeighbors++; | |
| } |
with:
graphhopper/core/src/main/java/com/graphhopper/routing/ch/NodeBasedNodeContractor.java
Lines 69 to 75 in 0a967b3
| final EdgeFilter remainingNodesFilter = new LevelEdgeFilter(prepareGraph) { | |
| @Override | |
| public final boolean accept(EdgeIteratorState edgeState) { | |
| return super.accept(edgeState) && allFilter.accept(edgeState); | |
| } | |
| }; | |
| remainingEdgeExplorer = prepareGraph.createEdgeExplorer(remainingNodesFilter); |
and:
graphhopper/core/src/main/java/com/graphhopper/routing/util/LevelEdgeFilter.java
Lines 40 to 52 in 5c980b4
| public boolean accept(EdgeIteratorState edgeIterState) { | |
| int base = edgeIterState.getBaseNode(); | |
| int adj = edgeIterState.getAdjNode(); | |
| // always accept virtual edges, see #288 | |
| if (base >= maxNodes || adj >= maxNodes) | |
| return true; | |
| // minor performance improvement: shortcuts in wrong direction are disconnected, so no need to exclude them | |
| if (((CHEdgeIteratorState) edgeIterState).isShortcut()) | |
| return true; | |
| return graph.getLevel(base) <= graph.getLevel(adj); | |
| } |
A 'contracted neighbor' should be a node that is contracted already and connected to the current node via an edge or a shortcut. The super.accept(edgeState) in the remainingNodesFilter accepts all shortcuts and all edges that go to equal or higher CH level (we can ignore the maxNodes condition because there are no virtual edges during CH preparation). We increase the contractedNeighbors counter for every shortcut and do not increase it for non-shortcut edges. This seems wrong to me: there can be shortcuts leading to un-contracted nodes and there can be non-shortcuts leading to contracted nodes.
So it should be something like this:
// just an edge explorer using `DefaultEdgeFilter.allEdges`
EdgeIterator iter = allEdgeExplorer.setBaseNode(node);
while (iter.next()) {
// we only calculate priorities for not yet contracted nodes
assert chGraph.getLevel(iter.getBaseNode()) == maxLevel;
if (chGraph.getLevel(iter.getAdjNode()) < maxLevel) {
contractedNeighbors++;
}
}Another (probably minor/not important) thing to consider here is that there can be multiple edges/shortcuts going to the same node in which case we should not increase contractedNeighbors multiple times.
Activity
[-]Counting contracted neighbors in node-based CH preparation seems odd[/-][+]Bugs in node-based CH preparation heuristics[/+]