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());
}
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;
}
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);
}
}
}
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;
}
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;
}
Aggregations