Skip to content

calling "addTurnInfo()" twice won't update turn costs #1107

Closed

Description

@knowname

When using the function addTurnInfo(int fromEdge, int viaNode, int toEdge, long turnFlags) a second time on the same edges, it doesn't update the turn costs.
When trying to receive them by calling getTurnCostFlags(int edgeFrom, int nodeVia, int edgeTo) you'll get the first cost entry - not the 'updated' one.

Add the following test to EdgeBasedRoutingAlgorithmTest shows that effect.

@Test
public void testTurnCostsUpdateBug() {
    final GraphHopperStorage g = createStorage(createEncodingManager(false));
    initGraph(g);
    TurnCostExtension tcs = (TurnCostExtension) g.getExtension();
    long tflags = carEncoder.getTurnFlags(false, 1);
    tcs.addTurnInfo(getEdge(g, 5, 2).getEdge(), 2, getEdge(g, 2, 3).getEdge(), tflags);

    tflags = carEncoder.getTurnFlags(false, 2);
    tcs.addTurnInfo(getEdge(g, 5, 2).getEdge(), 2, getEdge(g, 2, 3).getEdge(), tflags);

    assertEquals(tflags, tcs.getTurnCostFlags(getEdge(g, 5, 2).getEdge(), 2, getEdge(g, 2, 3).getEdge()));
}

i know this problem could be solved by simply using the correct turnCosts in the first place, but in some cases this might be the easier way.

This hack is fixing the problem by iterating all turnCostEntries and using the maximum.

     private long nextCostFlags(int edgeFrom, int nodeVia, int edgeTo) {
         int turnCostIndex = nodeAccess.getAdditionalNodeField(nodeVia);
+        int cost = -1;
         int i = 0;
         for (; i < 1000; i++) {
             if (turnCostIndex == NO_TURN_ENTRY)
@@ -168,7 +168,8 @@ public class TurnCostExtension implements GraphExtension {
             long turnCostPtr = (long) turnCostIndex * turnCostsEntryBytes;
             if (edgeFrom == turnCosts.getInt(turnCostPtr + TC_FROM)) {
                 if (edgeTo == turnCosts.getInt(turnCostPtr + TC_TO))
-                    return turnCosts.getInt(turnCostPtr + TC_FLAGS);
+                    if (cost < turnCosts.getInt(turnCostPtr + TC_FLAGS))
+                       cost = turnCosts.getInt(turnCostPtr + TC_FLAGS);
             }
 
             int nextTurnCostIndex = turnCosts.getInt(turnCostPtr + TC_NEXT);
@@ -177,6 +178,9 @@ public class TurnCostExtension implements GraphExtension {
 
             turnCostIndex = nextTurnCostIndex;
         }
+               if (cost >= 0)
+                       return cost;
+        
         // so many turn restrictions on one node? here is something wrong
         if (i >= 1000)
             throw new IllegalStateException("something went wrong: there seems to be no end of the turn cost-list!?");
        return EMPTY_FLAGS;
    }

I guess the clean solution would be an update mechanism when adding the turnCosts.

Activity

  1. Nakaner commented on Jan 22, 2018

    @Nakaner
    Contributor

    I observed a problem which is also caused by this "bug".

    If you want to have an EncodingManager with multiple FlagEncoders which use turn costs, it will only work with the FlagEncoder whose turn costs you wrote first to the TurnCostExtension because these costs will be returned later for all FlagEncoders.

    This test will pass:

        @Test
        public void testMultipleFlagEncoders() {
            FlagEncoder carEncoder1 = new CarFlagEncoder(5, 5, 3);
            FlagEncoder carEncoder2 = new BikeFlagEncoder(5, 5, 3);
            EncodingManager mangager = new EncodingManager(carEncoder1, carEncoder2);
            GraphHopperStorage g = createStorage(mangager);
            initGraph(g);
            TurnCostExtension tcs = (TurnCostExtension) g.getExtension();
    
            // introduce some turn costs
            long tflags = carEncoder1.getTurnFlags(true, 0);
            long tflags2 = carEncoder2.getTurnFlags(true, 0);
            tcs.addTurnInfo(getEdge(g, 5, 2).getEdge(), 2, getEdge(g, 2, 3).getEdge(), tflags | tflags2);
    
            long flags = tcs.getTurnCostFlags(getEdge(g, 5, 2).getEdge(), 2, getEdge(g, 2, 3).getEdge());
            assertEquals(12L, flags & 12L);
            assertEquals(3L, flags & 3L);
            assertEquals(Double.POSITIVE_INFINITY, carEncoder1.getTurnCost(flags), 0);
            assertEquals(Double.POSITIVE_INFINITY, carEncoder2.getTurnCost(flags), 0);
        }

    But it will fail if I try to write the turn costs of both encoders separately:

        @Test
        public void testMultipleFlagEncoders() {
            FlagEncoder carEncoder1 = new CarFlagEncoder(5, 5, 3);
            FlagEncoder carEncoder2 = new BikeFlagEncoder(5, 5, 3);
            EncodingManager mangager = new EncodingManager(carEncoder1, carEncoder2);
            GraphHopperStorage g = createStorage(mangager);
            initGraph(g);
            TurnCostExtension tcs = (TurnCostExtension) g.getExtension();
    
            // introduce some turn costs
            long tflags = carEncoder1.getTurnFlags(true, 0);
            long tflags2 = carEncoder2.getTurnFlags(true, 0);
            tcs.addTurnInfo(getEdge(g, 5, 2).getEdge(), 2, getEdge(g, 2, 3).getEdge(), tflags);
            tcs.addTurnInfo(getEdge(g, 5, 2).getEdge(), 2, getEdge(g, 2, 3).getEdge(), tflags2);
    
            long flags = tcs.getTurnCostFlags(getEdge(g, 5, 2).getEdge(), 2, getEdge(g, 2, 3).getEdge());
            assertEquals(12L, flags & 12L);
            assertEquals(3L, flags & 3L);
            assertEquals(Double.POSITIVE_INFINITY, carEncoder1.getTurnCost(flags), 0);
            assertEquals(Double.POSITIVE_INFINITY, carEncoder2.getTurnCost(flags), 0);
        }

    A user can circumvent this "bug" by writing all turn costs information with only one single call of TurnCostExtension.addTurnInfo(int, int, long) and doing a bitwise OR on all flags to be written.

    I am currently thinking how to fix this bug nicely without just adding a warning in the documenation of TurnCostExtension.addTurnInfo(int, int, long).

  2. karussell commented on Jan 22, 2018

    @karussell
    Member

    Thanks for pointing this out. Before we add a new turn info we must somehow check existing stuff and overwrite them (or due to multiple FlagEncoders we have to properly merge them). The cool thing would be if we could rewrite this turn cost part with #1112 in mind or afterwards or even fix this with #1112.

  3. added a commit that references this issue on Feb 16, 2018
    bc91215
  4. added this to the 0.10 milestone on Feb 16, 2018
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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions