Skip to content

Edge-based bidirectional algorithms do not agree with Dijkstra when there is a loop and turn restrictions. #1592

Closed

Description

@easbar

Here is a graph involving a loop and some turn restrictions for which the edge-based bidirectional algorithms do not agree with Dijkstra / do not find the optimal path:

@Test
public void testLoop_issue1592() {
GraphHopperStorage g = createStorage(createEncodingManager(true));
// 0-6
// \ \
// 4-3
// |
// 1o
g.edge(0, 6, 10, true);
g.edge(6, 3, 10, true);
g.edge(0, 4, 1, true);
g.edge(4, 1, 1, true);
g.edge(4, 3, 1, true);
g.edge(1, 1, 10, true);
TurnCostExtension tcs = (TurnCostExtension) g.getExtension();
addTurnRestriction(g, tcs, 0, 4, 3);
Path p = createAlgo(g, AlgorithmOptions.start().
weighting(createWeighting(carEncoder, tcs, 40)).
traversalMode(TraversalMode.EDGE_BASED_2DIR).build()).
calcPath(0, 3);
assertEquals(14, p.getDistance(), 1.e-3);
assertEquals(IntArrayList.from(0, 4, 1, 1, 4, 3), p.calcNodes());
}

Activity

  1. easbar commented on Apr 11, 2019

    @easbar
    MemberAuthor

    The problem here is that the forward and backward seach uses different edge keys when they discover the loop edge 1-1. This is because of the reverse parameter here:

    /**
    * Creates unique positive number for specified edgeId taking into account the direction defined
    * by nodeA, nodeB and reverse.
    */
    public static int createEdgeKey(int nodeA, int nodeB, int edgeId, boolean reverse) {
    edgeId = edgeId << 1;
    if (reverse)
    return (nodeA > nodeB) ? edgeId : edgeId + 1;
    return (nodeA > nodeB) ? edgeId + 1 : edgeId;
    }

    The forward search uses createEdgeKey(1,1,5,false)=10 while the backward search uses createEdgeKey(1,1,5,true)=11. For non-loop edges it has to be this way to differentiate the two possible directions of an edge, but for a loop the reverse parameter changes the edge key simply depending on which search (fwd/bwd) found it. Because of the different edge keys the according SPTEntry is not found by the opposite search and the best path is not updated for the loop edge (here the traversalId is the edge key):

    protected void updateBestPath(EdgeIteratorState edgeState, SPTEntry entry, int traversalId, boolean reverse) {
    SPTEntry entryOther = bestWeightMapOther.get(traversalId);
    if (entryOther == null)
    return;

    In principle this would not be a problem because when the search continues one step further the best path will be updated for the next edge, but in the present case the search stops before this happens, because the (suboptimal) path 0-6-4-3 is found just before and the search ends.

    So the bug requires:

    • edge-based routing
    • a bidirectional algorithm
    • a loop edge (our OSM import prevents this Do not create loop-edges #1531)
    • a turn restriction or something like a forced start/target direction such that the loop has to be considered

    I wonder if we could make the result of createEdgeKey independent of the reverse parameter in case the two node ids are equal ?

  2. changed the title [-]Bidirectional algorithms do not agree with Dijkstra when there is a loop and turn restrictions.[/-] [+]Edge-based bidirectional algorithms do not agree with Dijkstra when there is a loop and turn restrictions.[/+] on Apr 11, 2019
  3. easbar commented on Apr 11, 2019

    @easbar
    MemberAuthor

    I wonder if we could make the result of createEdgeKey independent of the reverse parameter in case the two node ids are equal ?

    This would make both 'directions' of the loop be treated equal, but there is no way to differentiate the two directions anyway #1525 which is why we started to exclude the loop edges during the import already #1531.

  4. easbar commented on May 3, 2019

    @easbar
    MemberAuthor

    Ok adjusting createEdgeKey such that it returns the same value for loops regardless of the reverse flag is part one of the puzzle. The second problem is here:

    if (entryOther.adjNode != entry.adjNode) {
    // prevents the path to contain the edge at the meeting point twice and subtracts the weight (excluding turn weight => no previous edge)
    entry = entry.getParent();
    weight -= weighting.calcWeight(edgeState, reverse, EdgeIterator.NO_EDGE);
    } else if (!traversalMode.hasUTurnSupport())
    // we detected a u-turn at meeting point, skip if not supported
    return;

    If entry.adjNode == entryOther.adjNode the best path is not updated, because this situation is interpreted as being a u-turn. However this can happen in two different cases:

    1. there is in fact a u-turn and we have been using TraversalMode.EDGE_BASED_1DIR
    2. we have encountered a loop edge at the meeting point (the situation discussed here)

    The best path may only be rejected in the first case.

  5. karussell commented on May 3, 2019

    @karussell
    Member

    So we can simplify code just by throwing away 1DIR? If I understand this correctly, then let's remove this (#1520)

  6. easbar commented on May 3, 2019

    @easbar
    MemberAuthor

    Yes the first case is specific to 1DIR and if we remove this TraversalMode we no longer need special handling for it in AbstractBidirAlgo.

  7. easbar commented on May 3, 2019

    @easbar
    MemberAuthor

    Do you mean we should remove 1DIR in the PR for this issue already ? I do not think it requires many changes to remove it.

  8. easbar commented on May 3, 2019

    @easbar
    MemberAuthor

    Not so many changes needed at least in core repo, just tried it:
    easbar@81d14b5

  9. karussell commented on May 3, 2019

    @karussell
    Member

    This sounds good.

    Maybe we could even remove EDGE_BASED_2DIR_UTURN too, i.e. fix #1520 and then fix this here?

  10. easbar commented on May 3, 2019

    @easbar
    MemberAuthor

    This sounds good.

    Ok will add to the PR.

    Maybe we could even remove EDGE_BASED_2DIR_UTURN

    Hmm I think this requires a little more thought, I would prefer doing this separately.

  11. easbar commented on May 3, 2019

    @easbar
    MemberAuthor

    Or maybe to put it another way I think u-turns should be handled by using infinite turn costs instead of doing explicit u-turn checks at several different places. Simply removing EDGE_BASED_2DIR_UTURN might be possible, but I think it would be nicer to do this together with the above mentioned u-turn cleanup and also this is not related to the present issue.

  12. karussell commented on May 3, 2019

    @karussell
    Member

    Ok 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions