use of org.opentripplanner.routing.spt.ShortestPathTree in project OpenTripPlanner by opentripplanner.
the class NearbyStopFinder method findNearbyStopsViaStreets.
/**
* Return all stops within a certain radius of the given vertex, using network distance along streets.
* If the origin vertex is a TransitStop, the result will include it.
*/
public List<StopAtDistance> findNearbyStopsViaStreets(Vertex originVertex) {
RoutingRequest routingRequest = new RoutingRequest(TraverseMode.WALK);
routingRequest.clampInitialWait = (0L);
routingRequest.setRoutingContext(graph, originVertex, null);
ShortestPathTree spt = earliestArrivalSearch.getShortestPathTree(routingRequest);
List<StopAtDistance> stopsFound = Lists.newArrayList();
if (spt != null) {
// TODO use GenericAStar and a traverseVisitor? Add an earliestArrival switch to genericAStar?
for (State state : spt.getAllStates()) {
Vertex targetVertex = state.getVertex();
if (targetVertex == originVertex)
continue;
if (targetVertex instanceof TransitStop) {
stopsFound.add(stopAtDistanceForState(state));
}
}
}
/* Add the origin vertex if needed. The SPT does not include the initial state. FIXME shouldn't it? */
if (originVertex instanceof TransitStop) {
stopsFound.add(new StopAtDistance((TransitStop) originVertex, 0));
}
routingRequest.cleanup();
return stopsFound;
}
use of org.opentripplanner.routing.spt.ShortestPathTree in project OpenTripPlanner by opentripplanner.
the class ProfileRouter method findDirectOption.
/**
* Look for an option connecting origin to destination without using transit.
*/
private void findDirectOption(QualifiedMode qmode) {
// Make a normal OTP routing request so we can traverse edges and use GenericAStar
RoutingRequest rr = new RoutingRequest(new TraverseModeSet());
// false because we never use transit in direct options
qmode.applyToRoutingRequest(rr, false);
if (qmode.mode == TraverseMode.BICYCLE) {
// TRIANGLE should only affect bicycle searches, but we wrap this in a conditional just to be clear.
rr.optimize = OptimizeType.TRIANGLE;
rr.setTriangleNormalized(request.bikeSafe, request.bikeSlope, request.bikeTime);
}
rr.from = (new GenericLocation(request.fromLat, request.fromLon));
rr.to = new GenericLocation(request.toLat, request.toLon);
rr.setArriveBy(false);
rr.setRoutingContext(graph);
rr.dominanceFunction = new DominanceFunction.MinimumWeight();
// This is not a batch search, it is a point-to-point search with goal direction.
// Impose a max time to protect against very slow searches.
int worstElapsedTime = request.streetTime * 60;
rr.worstTime = (rr.dateTime + worstElapsedTime);
rr.walkSpeed = request.walkSpeed;
rr.bikeSpeed = request.bikeSpeed;
AStar astar = new AStar();
rr.setNumItineraries(1);
ShortestPathTree spt = astar.getShortestPathTree(rr, 5);
State state = spt.getState(rr.rctx.target);
if (state != null) {
LOG.info("Found non-transit option for {}", qmode);
directPaths.add(new StopAtDistance(state, qmode));
}
// save context for later cleanup so temp edges remain available
routingContexts.add(rr.rctx);
}
use of org.opentripplanner.routing.spt.ShortestPathTree in project OpenTripPlanner by opentripplanner.
the class RoundBasedProfileRouter method findInitialStops.
/**
* find the boarding stops
*/
private Collection<ProfileState> findInitialStops(boolean dest) {
double lat = dest ? request.toLat : request.fromLat;
double lon = dest ? request.toLon : request.fromLon;
QualifiedModeSet modes = dest ? request.accessModes : request.egressModes;
List<ProfileState> stops = Lists.newArrayList();
RoutingRequest rr = new RoutingRequest(TraverseMode.WALK);
rr.dominanceFunction = new DominanceFunction.EarliestArrival();
rr.batch = true;
rr.from = new GenericLocation(lat, lon);
rr.walkSpeed = request.walkSpeed;
rr.to = rr.from;
rr.setRoutingContext(graph);
// RoutingRequest dateTime defaults to currentTime.
// If elapsed time is not capped, searches are very slow.
rr.worstTime = (rr.dateTime + request.maxWalkTime * 60);
AStar astar = new AStar();
rr.longDistance = true;
rr.setNumItineraries(1);
// timeout in seconds
ShortestPathTree spt = astar.getShortestPathTree(rr, 5);
for (TransitStop tstop : graph.index.stopVertexForStop.values()) {
State s = spt.getState(tstop);
if (s != null) {
ProfileState ps = new ProfileState();
ps.lowerBound = ps.upperBound = (int) s.getElapsedTimeSeconds();
ps.stop = tstop;
ps.accessType = Type.STREET;
stops.add(ps);
}
}
Map<TripPattern, ProfileState> optimalBoardingLocation = Maps.newHashMap();
TObjectIntMap<TripPattern> minBoardTime = new TObjectIntHashMap<TripPattern>(100, 0.75f, Integer.MAX_VALUE);
// Only board patterns at the closest possible stop
for (ProfileState ps : stops) {
for (TripPattern pattern : graph.index.patternsForStop.get(ps.stop.getStop())) {
if (ps.lowerBound < minBoardTime.get(pattern)) {
optimalBoardingLocation.put(pattern, ps);
minBoardTime.put(pattern, ps.lowerBound);
}
}
ps.targetPatterns = Sets.newHashSet();
}
LOG.info("Found {} reachable stops, filtering to only board at closest stops", stops.size());
for (Entry<TripPattern, ProfileState> e : optimalBoardingLocation.entrySet()) {
e.getValue().targetPatterns.add(e.getKey());
}
for (Iterator<ProfileState> it = stops.iterator(); it.hasNext(); ) {
if (it.next().targetPatterns.isEmpty())
it.remove();
}
rr.cleanup();
return stops;
}
use of org.opentripplanner.routing.spt.ShortestPathTree in project OpenTripPlanner by opentripplanner.
the class GenericDijkstra method getShortestPathTree.
public ShortestPathTree getShortestPathTree(State initialState) {
Vertex target = null;
if (options.rctx != null) {
target = initialState.getOptions().rctx.target;
}
ShortestPathTree spt = new DominanceFunction.MinimumWeight().getNewShortestPathTree(options);
BinHeap<State> queue = new BinHeap<State>(1000);
spt.add(initialState);
queue.insert(initialState, initialState.getWeight());
while (!queue.empty()) {
// Until the priority queue is empty:
State u = queue.extract_min();
Vertex u_vertex = u.getVertex();
if (traverseVisitor != null) {
traverseVisitor.visitVertex(u);
}
if (!spt.getStates(u_vertex).contains(u)) {
continue;
}
if (verbose) {
System.out.println("min," + u.getWeight());
System.out.println(u_vertex);
}
if (searchTerminationStrategy != null && searchTerminationStrategy.shouldSearchTerminate(initialState.getVertex(), null, u, spt, options)) {
break;
}
for (Edge edge : options.arriveBy ? u_vertex.getIncoming() : u_vertex.getOutgoing()) {
if (skipEdgeStrategy != null && skipEdgeStrategy.shouldSkipEdge(initialState.getVertex(), null, u, edge, spt, options)) {
continue;
}
// returning NULL), the iteration is over.
for (State v = edge.traverse(u); v != null; v = v.getNextResult()) {
if (skipTraverseResultStrategy != null && skipTraverseResultStrategy.shouldSkipTraversalResult(initialState.getVertex(), null, u, v, spt, options)) {
continue;
}
if (traverseVisitor != null) {
traverseVisitor.visitEdge(edge, v);
}
if (verbose) {
System.out.printf(" w = %f + %f = %f %s", u.getWeight(), v.getWeightDelta(), v.getWeight(), v.getVertex());
}
if (v.exceedsWeightLimit(options.maxWeight))
continue;
if (spt.add(v)) {
double estimate = heuristic.estimateRemainingWeight(v);
queue.insert(v, v.getWeight() + estimate);
if (traverseVisitor != null)
traverseVisitor.visitEnqueue(v);
}
}
}
}
return spt;
}
use of org.opentripplanner.routing.spt.ShortestPathTree in project OpenTripPlanner by opentripplanner.
the class EuclideanRemainingWeightHeuristic method determineRequiredWalkDistance.
/**
* Figure out the minimum amount of walking to reach the destination from transit.
* This is done by doing a Dijkstra search for the first reachable transit stop.
*/
private double determineRequiredWalkDistance(final RoutingRequest req) {
// required walk distance will be unused.
if (!transit)
return 0;
RoutingRequest options = req.clone();
options.setArriveBy(!req.arriveBy);
options.setRoutingContext(req.rctx.graph, req.rctx.fromVertex, req.rctx.toVertex);
GenericDijkstra gd = new GenericDijkstra(options);
State s = new State(options);
gd.setHeuristic(new TrivialRemainingWeightHeuristic());
final ClosestStopTraverseVisitor visitor = new ClosestStopTraverseVisitor();
gd.traverseVisitor = visitor;
gd.searchTerminationStrategy = new SearchTerminationStrategy() {
@Override
public boolean shouldSearchTerminate(Vertex origin, Vertex target, State current, ShortestPathTree spt, RoutingRequest traverseOptions) {
return visitor.distanceToClosestStop != Double.POSITIVE_INFINITY;
}
};
gd.getShortestPathTree(s);
return visitor.distanceToClosestStop;
}
Aggregations