Skip to content

Setting different speeds for both directions can cause problems with loops #1631

Closed

Description

@easbar

Routing can fail if we use MotorCycleEncoder instead of CarFlagEncoder and there are loops (for OSM they are excluded during import though), because motorcycle allows setting different speeds for both directions. See this branch: motorcycle_edge_based_ch_bug

Related:

Activity

  1. easbar commented on Jun 1, 2019

    @easbar
    MemberAuthor

    The test only shows the problem for edge-based CH, but I am not sure if it could be a problem for other (edge-based ?) algorithms as well, not sure if this is fully covered by tests.

  2. easbar commented on Dec 13, 2020

    @easbar
    MemberAuthor

    The test only shows the problem for edge-based CH, but I am not sure if it could be a problem for other (edge-based ?) algorithms as well, not sure if this is fully covered by tests.

    Yes, this is a problem for all edge-based algorithms and its nothing specific to edge-based CH. We cannot reliably use different speeds for the two directions for loops. The current solution is still that loops are excluded during the import. Here is a recent commit where RandomizedRoutingTest fails sometimes (run it many times): eda2b53

    Interestingly the test is 'fixed' by bbb15df, but this is only because with this commit the speeds aren't properly set to different values due to a changed randomGraph method, see this comment: #2202 (comment)

  3. added a commit that references this issue on Dec 29, 2020
  4. easbar commented on Feb 28, 2021

    @easbar
    MemberAuthor

    In our current model loops with different properties for the two directions are not well defined. Every edge has an edge ID, a base node ID (A) and an adj node ID (B). A loop is an edge where A=B. The direction of an edge is the </> relation between A and B, but since A=B a loop has only one direction. Therefore setting different edge properties for the 'fwd' and 'bwd' direction of a loop is just wrong. Currently there are a few places (like DefaultEdgeFilter) where we do special handling for loop edges, but these would not be necessary if we did not use direction-dependent properties for loop edges in the first place. Can we somehow deny setting different values for fwd/bwd directions on loops? We probably need to enforce 'storeTwoDirections=false' for loops or something?

    This should not be confused with loop shortcuts in edge-based CH. The direction of these loop shortcuts is actually well defined, because of the first/last orig edges.

  5. easbar commented on Mar 1, 2021

    @easbar
    MemberAuthor

    Maybe we could replace, for example:

           @Override
            public EdgeIteratorState set(BooleanEncodedValue property, boolean value) {
                property.setBool(reverse, getFlags(), value);
                baseGraph.writeFlags(edgePointer, getFlags());
                return this;
            }
    
            @Override
            public EdgeIteratorState setReverse(BooleanEncodedValue property, boolean value) {
                property.setBool(!reverse, getFlags(), value);
                baseGraph.writeFlags(edgePointer, getFlags());
                return this;
            }
    }

    with

           @Override
            public EdgeIteratorState set(BooleanEncodedValue property, boolean value) {
                property.setBool(reverse, getFlags(), value);
                if (baseNode == adjNode) {
                    property.setBool(!reverse, getFlags(), value);
                }
                baseGraph.writeFlags(edgePointer, getFlags());
                return this;
            }
    
            @Override
            public EdgeIteratorState setReverse(BooleanEncodedValue property, boolean value) {
                property.setBool(!reverse, getFlags(), value);
                if (baseNode == adjNode) {
                    property.setBool(reverse, getFlags(), value);
                }
                baseGraph.writeFlags(edgePointer, getFlags());
                return this;
            }

    ? i.e. for loop edges we set the opposite direction always to the same value that is being set automatically?

  6. easbar commented on Mar 1, 2021

    @easbar
    MemberAuthor

    ... or we even reject setReverse entirely for loops? (but then the calling code always needs to check for loops). Right now we are already in the situation where we have to check 'storeTwoDirections' before calling setReverse

  7. easbar commented on Mar 15, 2021

    @easbar
    MemberAuthor

    We have a few options:

    1. Silently set the opposite direction's value in set/setReverse as proposed above. I do not like this so much because if we do
    loop.set(property, true);
    loop.setReverse(property, false);

    we get loop.get(property) == false (even though we just set it to true.

    1. Reject setReverse for loops altogether. This way we are forced to check if an edge is a loop before calling setReverse which seems reasonable to me. But what about getReverse? Again we could reject it for loops, but then we even have to check for loops when calling the getter. This seems cumbersome to me. We could forward loop.getReverse() to loop.get(), or set the reverse property also when we call loop.set(). The usage would be like this:
    loop.set(property, true); // ok
    loop.setReverse(property, true); // error
    loop.get(property); // -> true
    loop.getReverse(property); // -> true

    Any opinions about this? I have a small preference for the second option.

  8. karussell commented on Mar 15, 2021

    @karussell
    Member

    Yes, second options sounds reasonable. And you would then remove the special handling for loops on import?

    loop.setReverse(property, true); // error

    Probably only an error if the property has support for one value per direction?

  9. easbar commented on Mar 15, 2021

    @easbar
    MemberAuthor

    Probably only an error if the property has support for one value per direction?

    This is another good question. Currently we have this in set(property, boolean fwd, boolean bwd):

                if (!property.isStoreTwoDirections())
                    throw new IllegalArgumentException("EncodedValue " + property.getName() + " supports only one direction");
    

    But why is it not also an error to call setReverse when !property.isStoreTwoDirections. Isn't it like when we only have one direction there is no 'reverse' property so we can only use get/set (and not getReverse/setReverse)?

    Yes, second options sounds reasonable. And you would then remove the special handling for loops on import?

    Ok and yes hopefully we can remove the special handling then. However, we probably need another special handling then. In case there are actual loops in OSM with different speeds for the two directions or different geometries etc. (does this exist?) we actually have to add two loops (one for each direction). And right now I am wondering if this can cause other problems, like turning from one loop to the other etc.

  10. easbar commented on Mar 15, 2021

    @easbar
    MemberAuthor

    we actually have to add two loops (one for each direction).

    Probably we need to do this for all loops, because otherwise we won't be able to e.g. block one direction dynamically... But once we start adding two loop edges for all loop ways, maybe we can just as well split all loop ways into two edges like we currently do and stop using loops in BaseGraph altogether?!

  11. karussell commented on Mar 16, 2021

    @karussell
    Member

    But why is it not also an error to call setReverse when !property.isStoreTwoDirections.

    We can try this and see if test succeed. Maybe the problem was that our code is in general "direction-dependent" and calls setReverse explicitly even if not stored separately.

    maybe we can just as well split all loop ways into two edges like we currently do and stop using loops in BaseGraph altogether?!

    Ok. If it makes so many problems and we already avoid this in the import, why not :) ?

  12. easbar commented on Mar 16, 2021

    @easbar
    MemberAuthor

    Ok. If it makes so many problems and we already avoid this in the import, why not :) ?

    I put in a lot of effort to make e.g. edge-based CH work with base-graph loops (shortcut loops are always necessary). And for all I know it is working, so we solved a lot of problems related to such loops already and maybe we can handle loops / deal with a base graph structure that allows loops. But with my current knowledge I think directed loops are ill-defined so there is still some work to get this right. At the same time right now I do not know about a single 'real' use-case for loop edges and this somewhat suggests that we maybe should just not spend time dealing with loops. Loop ways in OSM can be split into two edges. Loop edges used to represent stations in PT are only used because LocationIndex can only find edges, but these can be replaced by small loops as well (according to @michaz). Can you think of other potential use-cases for loops?

  13. easbar commented on Mar 16, 2021

    @easbar
    MemberAuthor

    Just to be clear what the real issue is:

    1. We allow setting different speeds, access etc. for the two directions of each edge (including loops)
    2. When we traverse edges using EdgeExplorer we process one edge ID at a time. Each edge ID represents two directions (two edge keys). The routing algorithms typically only consider one of these directions (only outgoing edges for the forward search, only incoming edges for the backward search), i.e. they call get(speedEnc) or getReverse(speedEnc) but not both.
    3. For loops we need to consider both directions. This is why we already adjusted the DefaultEdgeFilter so it accepts loops if only one of the two directions is accessible:
      @Override
      public final boolean accept(EdgeIteratorState iter) {
      if (iter.getBaseNode() == iter.getAdjNode()) {
      // this is needed for edge-based CH, see #1525
      // background: we need to explicitly accept shortcut edges that are loops, because if we insert a loop
      // shortcut with the fwd flag a DefaultEdgeFilter with bwd=true and fwd=false does not find it, although
      // it is also an 'incoming' edge.
      return iter.get(accessEnc) || iter.getReverse(accessEnc);
      }
      return fwd && iter.get(accessEnc) || bwd && iter.getReverse(accessEnc);
      }
      However, given only an edgeID it is unclear which direction should be considered to calculate e.g. the weight.
    4. For the weight calculation we blindly call get/getReverse for the forward/backward search and since for loops we always get the same direction in some cases we get the wrong one. Actually there is not even a 'correct' direction and for loops we would have to consider both directions or take the minimum of the forward and backward weight.

    I think it is not worth it to mess around with loops any longer and we should just remove them from our graph model altogether.

    Removing loops seems rather simple, because we already exclude them in OSMReader and all the tests that fail when we reject loops are testing whether we can deal with loops if they exist. The real issue is the PT module where we use loops here:

    EdgeIteratorState edge = graph.edge(streetNode, streetNode);
    and in one test also this produces a loop:
    EdgeIteratorState edge = graph.edge(timeNodes.get(timeNodes.lastKey()), timeNodes.get(timeNodes.firstKey()));

  14. added a commit that references this issue on Aug 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions