Search in sources :

Example 1 with ConstantIntersectionTraversalCostModel

use of org.opentripplanner.routing.core.ConstantIntersectionTraversalCostModel in project OpenTripPlanner by opentripplanner.

the class TriangleInequalityTest method checkTriangleInequality.

private void checkTriangleInequality(TraverseModeSet traverseModes) {
    assertNotNull(start);
    assertNotNull(end);
    RoutingRequest prototypeOptions = new RoutingRequest();
    // All reluctance terms are 1.0 so that duration is monotonically increasing in weight.
    prototypeOptions.stairsReluctance = (1.0);
    prototypeOptions.setWalkReluctance(1.0);
    prototypeOptions.turnReluctance = (1.0);
    prototypeOptions.carSpeed = 1.0;
    prototypeOptions.walkSpeed = 1.0;
    prototypeOptions.bikeSpeed = 1.0;
    prototypeOptions.traversalCostModel = (new ConstantIntersectionTraversalCostModel(10.0));
    prototypeOptions.dominanceFunction = new DominanceFunction.EarliestArrival();
    if (traverseModes != null) {
        prototypeOptions.setModes(traverseModes);
    }
    RoutingRequest options = prototypeOptions.clone();
    options.setRoutingContext(_graph, start, end);
    AStar aStar = new AStar();
    ShortestPathTree tree = aStar.getShortestPathTree(options);
    GraphPath path = tree.getPath(end, false);
    options.cleanup();
    assertNotNull(path);
    double startEndWeight = path.getWeight();
    int startEndDuration = path.getDuration();
    assertTrue(startEndWeight > 0);
    assertEquals(startEndWeight, (double) startEndDuration, 1.0 * path.edges.size());
    // Try every vertex in the graph as an intermediate.
    boolean violated = false;
    for (Vertex intermediate : _graph.getVertices()) {
        if (intermediate == start || intermediate == end) {
            continue;
        }
        GraphPath startIntermediatePath = getPath(aStar, prototypeOptions, null, start, intermediate);
        if (startIntermediatePath == null) {
            continue;
        }
        Edge back = startIntermediatePath.states.getLast().getBackEdge();
        GraphPath intermediateEndPath = getPath(aStar, prototypeOptions, back, intermediate, end);
        if (intermediateEndPath == null) {
            continue;
        }
        double startIntermediateWeight = startIntermediatePath.getWeight();
        int startIntermediateDuration = startIntermediatePath.getDuration();
        double intermediateEndWeight = intermediateEndPath.getWeight();
        int intermediateEndDuration = intermediateEndPath.getDuration();
        // TODO(flamholz): fix traversal so that there's no rounding at the second resolution.
        assertEquals(startIntermediateWeight, (double) startIntermediateDuration, 1.0 * startIntermediatePath.edges.size());
        assertEquals(intermediateEndWeight, (double) intermediateEndDuration, 1.0 * intermediateEndPath.edges.size());
        double diff = startIntermediateWeight + intermediateEndWeight - startEndWeight;
        if (diff < -0.01) {
            System.out.println("Triangle inequality violated - diff = " + diff);
            violated = true;
        }
    // assertTrue(startIntermediateDuration + intermediateEndDuration >=
    // startEndDuration);
    }
    assertFalse(violated);
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) ShortestPathTree(org.opentripplanner.routing.spt.ShortestPathTree) AStar(org.opentripplanner.routing.algorithm.AStar) GraphPath(org.opentripplanner.routing.spt.GraphPath) RoutingRequest(org.opentripplanner.routing.core.RoutingRequest) ConstantIntersectionTraversalCostModel(org.opentripplanner.routing.core.ConstantIntersectionTraversalCostModel) DominanceFunction(org.opentripplanner.routing.spt.DominanceFunction) Edge(org.opentripplanner.routing.graph.Edge)

Example 2 with ConstantIntersectionTraversalCostModel

use of org.opentripplanner.routing.core.ConstantIntersectionTraversalCostModel in project OpenTripPlanner by opentripplanner.

the class TurnCostTest method testForwardCarConstTurnCosts.

@Test
public void testForwardCarConstTurnCosts() {
    RoutingRequest options = proto.clone();
    options.traversalCostModel = (new ConstantIntersectionTraversalCostModel(10.0));
    options.setMode(TraverseMode.CAR);
    options.setRoutingContext(_graph, topRight, bottomLeft);
    // Without turn costs, this path costs 3x100 + 1x50 = 350.
    // Since there are 3 turns, the total cost should be 380.
    GraphPath path = checkForwardRouteDuration(options, 380);
    List<State> states = path.states;
    assertEquals(5, states.size());
    assertEquals("maple_1st", states.get(0).getVertex().getLabel());
    assertEquals("main_1st", states.get(1).getVertex().getLabel());
    assertEquals("broad_1st", states.get(2).getVertex().getLabel());
    assertEquals("broad_2nd", states.get(3).getVertex().getLabel());
    assertEquals("broad_3rd", states.get(4).getVertex().getLabel());
    assertEquals(0, states.get(0).getElapsedTimeSeconds());
    // maple_main1 = 50
    assertEquals(50, states.get(1).getElapsedTimeSeconds());
    // main1_2 = 100
    assertEquals(160, states.get(2).getElapsedTimeSeconds());
    // broad1_2 = 100
    assertEquals(270, states.get(3).getElapsedTimeSeconds());
    // broad2_3 = 100
    assertEquals(380, states.get(4).getElapsedTimeSeconds());
}
Also used : State(org.opentripplanner.routing.core.State) GraphPath(org.opentripplanner.routing.spt.GraphPath) RoutingRequest(org.opentripplanner.routing.core.RoutingRequest) ConstantIntersectionTraversalCostModel(org.opentripplanner.routing.core.ConstantIntersectionTraversalCostModel) Test(org.junit.Test)

Example 3 with ConstantIntersectionTraversalCostModel

use of org.opentripplanner.routing.core.ConstantIntersectionTraversalCostModel in project OpenTripPlanner by opentripplanner.

the class TurnCostTest method before.

@Before
public void before() {
    _graph = new Graph();
    // Graph for a fictional grid city with turn restrictions
    StreetVertex maple1 = vertex("maple_1st", 2.0, 2.0);
    StreetVertex maple2 = vertex("maple_2nd", 1.0, 2.0);
    StreetVertex maple3 = vertex("maple_3rd", 0.0, 2.0);
    StreetVertex main1 = vertex("main_1st", 2.0, 1.0);
    StreetVertex main2 = vertex("main_2nd", 1.0, 1.0);
    StreetVertex main3 = vertex("main_3rd", 0.0, 1.0);
    StreetVertex broad1 = vertex("broad_1st", 2.0, 0.0);
    StreetVertex broad2 = vertex("broad_2nd", 1.0, 0.0);
    StreetVertex broad3 = vertex("broad_3rd", 0.0, 0.0);
    // Each block along the main streets has unit length and is one-way
    StreetEdge maple1_2 = edge(maple1, maple2, 100.0, false);
    StreetEdge maple2_3 = edge(maple2, maple3, 100.0, false);
    StreetEdge main1_2 = edge(main1, main2, 100.0, false);
    StreetEdge main2_3 = edge(main2, main3, 100.0, false);
    broad1_2 = edge(broad1, broad2, 100.0, false);
    StreetEdge broad2_3 = edge(broad2, broad3, 100.0, false);
    // Each cross-street connects
    maple_main1 = edge(maple1, main1, 50.0, false);
    StreetEdge main_broad1 = edge(main1, broad1, 100.0, false);
    StreetEdge maple_main2 = edge(maple2, main2, 50.0, false);
    StreetEdge main_broad2 = edge(main2, broad2, 50.0, false);
    StreetEdge maple_main3 = edge(maple3, main3, 100.0, false);
    StreetEdge main_broad3 = edge(main3, broad3, 100.0, false);
    // Turn restrictions are only for driving modes.
    // - can't turn from 1st onto Main.
    // - can't turn from 2nd onto Main.
    // - can't turn from 2nd onto Broad.
    DisallowTurn(maple_main1, main1_2);
    DisallowTurn(maple_main2, main2_3);
    DisallowTurn(main_broad2, broad2_3);
    // Hold onto some vertices for the tests
    topRight = maple1;
    bottomLeft = broad3;
    // Make a prototype routing request.
    proto = new RoutingRequest();
    proto.carSpeed = 1.0;
    proto.walkSpeed = 1.0;
    proto.bikeSpeed = 1.0;
    proto.turnReluctance = (1.0);
    proto.setWalkReluctance(1.0);
    proto.stairsReluctance = (1.0);
    // Turn costs are all 0 by default.
    proto.traversalCostModel = (new ConstantIntersectionTraversalCostModel(0.0));
}
Also used : Graph(org.opentripplanner.routing.graph.Graph) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) RoutingRequest(org.opentripplanner.routing.core.RoutingRequest) StreetVertex(org.opentripplanner.routing.vertextype.StreetVertex) ConstantIntersectionTraversalCostModel(org.opentripplanner.routing.core.ConstantIntersectionTraversalCostModel) Before(org.junit.Before)

Example 4 with ConstantIntersectionTraversalCostModel

use of org.opentripplanner.routing.core.ConstantIntersectionTraversalCostModel in project OpenTripPlanner by opentripplanner.

the class TurnCostTest method testForwardDefaultConstTurnCosts.

@Test
public void testForwardDefaultConstTurnCosts() {
    RoutingRequest options = proto.clone();
    options.traversalCostModel = (new ConstantIntersectionTraversalCostModel(10.0));
    options.setRoutingContext(_graph, topRight, bottomLeft);
    // Without turn costs, this path costs 2x100 + 2x50 = 300.
    // Since we traverse 3 intersections, the total cost should be 330.
    GraphPath path = checkForwardRouteDuration(options, 330);
    // The intersection traversal cost should be applied to the state *after*
    // the intersection itself.
    List<State> states = path.states;
    assertEquals(5, states.size());
    assertEquals("maple_1st", states.get(0).getVertex().getLabel());
    assertEquals("main_1st", states.get(1).getVertex().getLabel());
    assertEquals("main_2nd", states.get(2).getVertex().getLabel());
    assertEquals("broad_2nd", states.get(3).getVertex().getLabel());
    assertEquals("broad_3rd", states.get(4).getVertex().getLabel());
    assertEquals(0, states.get(0).getElapsedTimeSeconds());
    // maple_main1 = 50
    assertEquals(50, states.get(1).getElapsedTimeSeconds());
    // main1_2 = 100
    assertEquals(160, states.get(2).getElapsedTimeSeconds());
    // main_broad2 = 50
    assertEquals(220, states.get(3).getElapsedTimeSeconds());
    // broad2_3 = 100
    assertEquals(330, states.get(4).getElapsedTimeSeconds());
}
Also used : State(org.opentripplanner.routing.core.State) GraphPath(org.opentripplanner.routing.spt.GraphPath) RoutingRequest(org.opentripplanner.routing.core.RoutingRequest) ConstantIntersectionTraversalCostModel(org.opentripplanner.routing.core.ConstantIntersectionTraversalCostModel) Test(org.junit.Test)

Aggregations

ConstantIntersectionTraversalCostModel (org.opentripplanner.routing.core.ConstantIntersectionTraversalCostModel)4 RoutingRequest (org.opentripplanner.routing.core.RoutingRequest)4 GraphPath (org.opentripplanner.routing.spt.GraphPath)3 Test (org.junit.Test)2 State (org.opentripplanner.routing.core.State)2 Before (org.junit.Before)1 AStar (org.opentripplanner.routing.algorithm.AStar)1 StreetEdge (org.opentripplanner.routing.edgetype.StreetEdge)1 Edge (org.opentripplanner.routing.graph.Edge)1 Graph (org.opentripplanner.routing.graph.Graph)1 Vertex (org.opentripplanner.routing.graph.Vertex)1 DominanceFunction (org.opentripplanner.routing.spt.DominanceFunction)1 ShortestPathTree (org.opentripplanner.routing.spt.ShortestPathTree)1 StreetVertex (org.opentripplanner.routing.vertextype.StreetVertex)1