Search in sources :

Example 31 with State

use of org.opentripplanner.routing.core.State 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 32 with State

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

the class TurnCostTest method checkForwardRouteDuration.

private GraphPath checkForwardRouteDuration(RoutingRequest options, int expectedDuration) {
    ShortestPathTree tree = new AStar().getShortestPathTree(options);
    GraphPath path = tree.getPath(bottomLeft, false);
    assertNotNull(path);
    // Without turn costs, this path costs 2x100 + 2x50 = 300.
    assertEquals(expectedDuration, path.getDuration());
    // Weight == duration when reluctances == 0.
    assertEquals(expectedDuration, (int) path.getWeight());
    for (State s : path.states) {
        assertEquals(s.getElapsedTimeSeconds(), (int) s.getWeight());
    }
    return path;
}
Also used : ShortestPathTree(org.opentripplanner.routing.spt.ShortestPathTree) State(org.opentripplanner.routing.core.State) GraphPath(org.opentripplanner.routing.spt.GraphPath)

Example 33 with State

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

the class WalkableAreaBuilder method pruneAreaEdges.

/**
 * Do an all-pairs shortest path search from a list of vertices over a specified set of edges,
 * and retain only those edges which are actually used in some shortest path.
 *
 * @param startingVertices
 * @param edges
 */
private void pruneAreaEdges(Collection<Vertex> startingVertices, Set<Edge> edges) {
    if (edges.size() == 0)
        return;
    TraverseMode mode;
    StreetEdge firstEdge = (StreetEdge) edges.iterator().next();
    if (firstEdge.getPermission().allows(StreetTraversalPermission.PEDESTRIAN)) {
        mode = TraverseMode.WALK;
    } else if (firstEdge.getPermission().allows(StreetTraversalPermission.BICYCLE)) {
        mode = TraverseMode.BICYCLE;
    } else {
        mode = TraverseMode.CAR;
    }
    RoutingRequest options = new RoutingRequest(mode);
    options.setDummyRoutingContext(graph);
    options.dominanceFunction = new DominanceFunction.EarliestArrival();
    GenericDijkstra search = new GenericDijkstra(options);
    search.setSkipEdgeStrategy(new ListedEdgesOnly(edges));
    Set<Edge> usedEdges = new HashSet<Edge>();
    for (Vertex vertex : startingVertices) {
        State state = new State(vertex, options);
        ShortestPathTree spt = search.getShortestPathTree(state);
        for (Vertex endVertex : startingVertices) {
            GraphPath path = spt.getPath(endVertex, false);
            if (path != null) {
                for (Edge edge : path.edges) {
                    usedEdges.add(edge);
                }
            }
        }
    }
    for (Edge edge : edges) {
        if (!usedEdges.contains(edge)) {
            graph.removeEdge(edge);
        }
    }
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) IntersectionVertex(org.opentripplanner.routing.vertextype.IntersectionVertex) GenericDijkstra(org.opentripplanner.routing.algorithm.GenericDijkstra) GraphPath(org.opentripplanner.routing.spt.GraphPath) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) ShortestPathTree(org.opentripplanner.routing.spt.ShortestPathTree) State(org.opentripplanner.routing.core.State) TraverseMode(org.opentripplanner.routing.core.TraverseMode) RoutingRequest(org.opentripplanner.routing.core.RoutingRequest) EarliestArrival(org.opentripplanner.routing.spt.DominanceFunction.EarliestArrival) DominanceFunction(org.opentripplanner.routing.spt.DominanceFunction) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) AreaEdge(org.opentripplanner.routing.edgetype.AreaEdge) Edge(org.opentripplanner.routing.graph.Edge) HashSet(java.util.HashSet)

Example 34 with State

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

the class EarliestArrivalSearch method getShortestPathTree.

public ShortestPathTree getShortestPathTree(RoutingRequest options, double relTimeout, SearchTerminationStrategy terminationStrategy) {
    // clone options before modifying, otherwise disabling resource limiting will cause
    // SPT cache misses for subsequent requests.
    options = options.clone();
    // disable any resource limiting, which is algorithmically invalid here
    options.maxTransfers = Integer.MAX_VALUE;
    options.setMaxWalkDistance(Double.MAX_VALUE);
    if (options.clampInitialWait < 0)
        options.clampInitialWait = (60 * 30);
    // impose search cutoff
    final long maxt = maxDuration + options.clampInitialWait;
    options.worstTime = options.dateTime + (options.arriveBy ? -maxt : maxt);
    // SPT cache does not look at routing request in SPT to perform lookup,
    // so it's OK to construct with the local cloned one
    ShortestPathTree spt = new DominanceFunction.EarliestArrival().getNewShortestPathTree(options);
    State initialState = new State(options);
    spt.add(initialState);
    BinHeap<State> pq = new BinHeap<State>();
    pq.insert(initialState, 0);
    while (!pq.empty()) {
        State u = pq.extract_min();
        Vertex u_vertex = u.getVertex();
        if (!spt.visit(u))
            continue;
        Collection<Edge> edges = options.arriveBy ? u_vertex.getIncoming() : u_vertex.getOutgoing();
        for (Edge edge : edges) {
            for (State v = edge.traverse(u); v != null; v = v.getNextResult()) {
                if (isWorstTimeExceeded(v, options)) {
                    continue;
                }
                if (spt.add(v)) {
                    // activeTime?
                    pq.insert(v, v.getActiveTime());
                }
            }
        }
    }
    return spt;
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) ShortestPathTree(org.opentripplanner.routing.spt.ShortestPathTree) State(org.opentripplanner.routing.core.State) BinHeap(org.opentripplanner.common.pqueue.BinHeap) DominanceFunction(org.opentripplanner.routing.spt.DominanceFunction) Edge(org.opentripplanner.routing.graph.Edge)

Example 35 with State

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

the class InterleavedBidirectionalHeuristic method streetSearch.

/**
 * Explore the streets around the origin or target, recording the minimum weight of a path to each street vertex.
 * When searching around the target, also retain the states that reach transit stops since we'll want to
 * explore the transit network backward, in order to guide the main forward search.
 *
 * The main search always proceeds from the "origin" to the "target" (names remain unchanged in arriveBy mode).
 * The reverse heuristic search always proceeds outward from the target (name remains unchanged in arriveBy).
 *
 * When the main search is departAfter:
 * it gets outgoing edges and traverses them with arriveBy=false,
 * the heuristic search gets incoming edges and traverses them with arriveBy=true,
 * the heuristic destination street search also gets incoming edges and traverses them with arriveBy=true,
 * the heuristic origin street search gets outgoing edges and traverses them with arriveBy=false.
 *
 * When main search is arriveBy:
 * it gets incoming edges and traverses them with arriveBy=true,
 * the heuristic search gets outgoing edges and traverses them with arriveBy=false,
 * the heuristic destination street search also gets outgoing edges and traverses them with arriveBy=false,
 * the heuristic origin street search gets incoming edges and traverses them with arriveBy=true.
 * The streetSearch method traverses using the real traverse method rather than the lower bound traverse method
 * because this allows us to keep track of the distance walked.
 * Perhaps rather than tracking walk distance, we should just check the straight-line radius and
 * only walk within that distance. This would avoid needing to call the main traversal functions.
 *
 * TODO what if the egress segment is by bicycle or car mode? This is no longer admissible.
 */
private TObjectDoubleMap<Vertex> streetSearch(RoutingRequest rr, boolean fromTarget, long abortTime) {
    LOG.debug("Heuristic street search around the {}.", fromTarget ? "target" : "origin");
    rr = rr.clone();
    if (fromTarget) {
        rr.setArriveBy(!rr.arriveBy);
    }
    // Create a map that returns Infinity when it does not contain a vertex.
    TObjectDoubleMap<Vertex> vertices = new TObjectDoubleHashMap<>(100, 0.5f, Double.POSITIVE_INFINITY);
    ShortestPathTree spt = new DominanceFunction.MinimumWeight().getNewShortestPathTree(rr);
    // TODO use normal OTP search for this.
    BinHeap<State> pq = new BinHeap<State>();
    Vertex initVertex = fromTarget ? rr.rctx.target : rr.rctx.origin;
    State initState = new State(initVertex, rr);
    pq.insert(initState, 0);
    while (!pq.empty()) {
        if (abortTime < Long.MAX_VALUE && System.currentTimeMillis() > abortTime) {
            return null;
        }
        State s = pq.extract_min();
        Vertex v = s.getVertex();
        // This is the lowest cost we will ever see for this vertex. We can record the cost to reach it.
        if (v instanceof TransitStop) {
            // place vertices on the transit queue so we can explore the transit network backward later.
            if (fromTarget) {
                double weight = s.getWeight();
                transitQueue.insert(v, weight);
                if (weight > maxWeightSeen) {
                    maxWeightSeen = weight;
                }
            }
            continue;
        }
        // Record the cost to reach this vertex.
        if (!vertices.containsKey(v)) {
            // FIXME time or weight? is RR using right mode?
            vertices.put(v, (int) s.getWeight());
        }
        for (Edge e : rr.arriveBy ? v.getIncoming() : v.getOutgoing()) {
            // arriveBy has been set to match actual directional behavior in this subsearch.
            // Walk cutoff will happen in the street edge traversal method.
            State s1 = e.traverse(s);
            if (s1 == null) {
                continue;
            }
            if (spt.add(s1)) {
                pq.insert(s1, s1.getWeight());
            }
        }
    }
    LOG.debug("Heuristric street search hit {} vertices.", vertices.size());
    LOG.debug("Heuristric street search hit {} transit stops.", transitQueue.size());
    return vertices;
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) StreetVertex(org.opentripplanner.routing.vertextype.StreetVertex) TObjectDoubleHashMap(gnu.trove.map.hash.TObjectDoubleHashMap) ShortestPathTree(org.opentripplanner.routing.spt.ShortestPathTree) TransitStop(org.opentripplanner.routing.vertextype.TransitStop) State(org.opentripplanner.routing.core.State) BinHeap(org.opentripplanner.common.pqueue.BinHeap) DominanceFunction(org.opentripplanner.routing.spt.DominanceFunction) Edge(org.opentripplanner.routing.graph.Edge)

Aggregations

State (org.opentripplanner.routing.core.State)78 RoutingRequest (org.opentripplanner.routing.core.RoutingRequest)42 GraphPath (org.opentripplanner.routing.spt.GraphPath)25 ShortestPathTree (org.opentripplanner.routing.spt.ShortestPathTree)24 Test (org.junit.Test)23 Edge (org.opentripplanner.routing.graph.Edge)22 Vertex (org.opentripplanner.routing.graph.Vertex)20 StreetEdge (org.opentripplanner.routing.edgetype.StreetEdge)13 Coordinate (com.vividsolutions.jts.geom.Coordinate)11 TransitStop (org.opentripplanner.routing.vertextype.TransitStop)8 Graph (org.opentripplanner.routing.graph.Graph)7 DominanceFunction (org.opentripplanner.routing.spt.DominanceFunction)7 NonLocalizedString (org.opentripplanner.util.NonLocalizedString)6 LineString (com.vividsolutions.jts.geom.LineString)5 HashSet (java.util.HashSet)5 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)5 Trip (org.onebusaway.gtfs.model.Trip)5 StateEditor (org.opentripplanner.routing.core.StateEditor)5 TemporaryStreetLocation (org.opentripplanner.routing.location.TemporaryStreetLocation)5 IntersectionVertex (org.opentripplanner.routing.vertextype.IntersectionVertex)5