use of org.opentripplanner.routing.algorithm.strategies.EuclideanRemainingWeightHeuristic in project OpenTripPlanner by opentripplanner.
the class GraphPathFinder method createReversedTransitRequest.
private RoutingRequest createReversedTransitRequest(RoutingRequest originalReq, RoutingRequest options, Vertex fromVertex, Vertex toVertex, long arrDepTime, RemainingWeightHeuristic remainingWeightHeuristic) {
RoutingRequest request = createReversedRequest(originalReq, options, fromVertex, toVertex, arrDepTime, new EuclideanRemainingWeightHeuristic());
if ((originalReq.parkAndRide || originalReq.kissAndRide) && !originalReq.arriveBy) {
request.parkAndRide = false;
request.kissAndRide = false;
request.modes.setCar(false);
}
request.maxWalkDistance = CLAMP_MAX_WALK;
return request;
}
use of org.opentripplanner.routing.algorithm.strategies.EuclideanRemainingWeightHeuristic in project OpenTripPlanner by opentripplanner.
the class GraphPathFinder method getPaths.
/**
* Repeatedly build shortest path trees, retaining the best path to the destination after each try.
* For search N, all trips used in itineraries retained from trips 0..(N-1) are "banned" to create variety.
* The goal direction heuristic is reused between tries, which means the later tries have more information to
* work with (in the case of the more sophisticated bidirectional heuristic, which improves over time).
*/
public List<GraphPath> getPaths(RoutingRequest options) {
RoutingRequest originalReq = options.clone();
if (options == null) {
LOG.error("PathService was passed a null routing request.");
return null;
}
// Reuse one instance of AStar for all N requests, which are carried out sequentially
AStar aStar = new AStar();
if (options.rctx == null) {
options.setRoutingContext(router.graph);
// The special long-distance heuristic should be sufficient to constrain the search to the right area.
}
// If this Router has a GraphVisualizer attached to it, set it as a callback for the AStar search
if (router.graphVisualizer != null) {
aStar.setTraverseVisitor(router.graphVisualizer.traverseVisitor);
// options.disableRemainingWeightHeuristic = true; // DEBUG
}
// Without transit, we'd just just return multiple copies of the same on-street itinerary.
if (!options.modes.isTransit()) {
options.numItineraries = 1;
}
// FORCING the dominance function to weight only
options.dominanceFunction = new DominanceFunction.MinimumWeight();
LOG.debug("rreq={}", options);
// Choose an appropriate heuristic for goal direction.
RemainingWeightHeuristic heuristic;
RemainingWeightHeuristic reversedSearchHeuristic;
if (options.disableRemainingWeightHeuristic) {
heuristic = new TrivialRemainingWeightHeuristic();
reversedSearchHeuristic = new TrivialRemainingWeightHeuristic();
} else if (options.modes.isTransit()) {
// Only use the BiDi heuristic for transit. It is not very useful for on-street modes.
// heuristic = new InterleavedBidirectionalHeuristic(options.rctx.graph);
// Use a simplistic heuristic until BiDi heuristic is improved, see #2153
heuristic = new InterleavedBidirectionalHeuristic();
reversedSearchHeuristic = new InterleavedBidirectionalHeuristic();
} else {
heuristic = new EuclideanRemainingWeightHeuristic();
reversedSearchHeuristic = new EuclideanRemainingWeightHeuristic();
}
options.rctx.remainingWeightHeuristic = heuristic;
/* In RoutingRequest, maxTransfers defaults to 2. Over long distances, we may see
* itineraries with far more transfers. We do not expect transfer limiting to improve
* search times on the LongDistancePathService, so we set it to the maximum we ever expect
* to see. Because people may use either the traditional path services or the
* LongDistancePathService, we do not change the global default but override it here. */
options.maxTransfers = 4;
// Now we always use what used to be called longDistance mode. Non-longDistance mode is no longer supported.
options.longDistance = true;
/* In long distance mode, maxWalk has a different meaning than it used to.
* It's the radius around the origin or destination within which you can walk on the streets.
* If no value is provided, max walk defaults to the largest double-precision float.
* This would cause long distance mode to do unbounded street searches and consider the whole graph walkable. */
if (options.maxWalkDistance == Double.MAX_VALUE)
options.maxWalkDistance = DEFAULT_MAX_WALK;
if (options.maxWalkDistance > CLAMP_MAX_WALK)
options.maxWalkDistance = CLAMP_MAX_WALK;
long searchBeginTime = System.currentTimeMillis();
LOG.debug("BEGIN SEARCH");
List<GraphPath> paths = Lists.newArrayList();
while (paths.size() < options.numItineraries) {
// TODO pull all this timeout logic into a function near org.opentripplanner.util.DateUtils.absoluteTimeout()
int timeoutIndex = paths.size();
if (timeoutIndex >= router.timeouts.length) {
timeoutIndex = router.timeouts.length - 1;
}
double timeout = searchBeginTime + (router.timeouts[timeoutIndex] * 1000);
// Convert from absolute to relative time
timeout -= System.currentTimeMillis();
// Convert milliseconds to seconds
timeout /= 1000;
if (timeout <= 0) {
// Catch the case where advancing to the next (lower) timeout value means the search is timed out
// before it even begins. Passing a negative relative timeout in the SPT call would mean "no timeout".
options.rctx.aborted = true;
break;
}
// Don't dig through the SPT object, just ask the A star algorithm for the states that reached the target.
aStar.getShortestPathTree(options, timeout);
if (options.rctx.aborted) {
// Search timed out or was gracefully aborted for some other reason.
break;
}
List<GraphPath> newPaths = aStar.getPathsToTarget();
if (newPaths.isEmpty()) {
break;
}
// Do a full reversed search to compact the legs
if (options.compactLegsByReversedSearch) {
newPaths = compactLegsByReversedSearch(aStar, originalReq, options, newPaths, timeout, reversedSearchHeuristic);
}
// Find all trips used in this path and ban them for the remaining searches
for (GraphPath path : newPaths) {
// path.dump();
List<AgencyAndId> tripIds = path.getTrips();
for (AgencyAndId tripId : tripIds) {
options.banTrip(tripId);
}
if (tripIds.isEmpty()) {
// This path does not use transit (is entirely on-street). Do not repeatedly find the same one.
options.onlyTransitTrips = true;
}
}
paths.addAll(newPaths.stream().filter(path -> {
double duration = options.useRequestedDateTimeInMaxHours ? options.arriveBy ? options.dateTime - path.getStartTime() : path.getEndTime() - options.dateTime : path.getDuration();
return duration < options.maxHours * 60 * 60;
}).collect(Collectors.toList()));
LOG.debug("we have {} paths", paths.size());
}
LOG.debug("END SEARCH ({} msec)", System.currentTimeMillis() - searchBeginTime);
Collections.sort(paths, new PathComparator(options.arriveBy));
return paths;
}
use of org.opentripplanner.routing.algorithm.strategies.EuclideanRemainingWeightHeuristic in project OpenTripPlanner by opentripplanner.
the class GraphPathFinder method compactLegsByReversedSearch.
/**
* Do a full reversed search to compact the legs of the path.
*
* By doing a reversed search we are looking for later departures that will still be in time for transfer
* to the next trip, shortening the transfer wait time. Also considering other routes than the ones found
* in the original search.
*
* For arrive-by searches, we are looking to shorten transfer wait time and rather arrive earlier.
*/
private List<GraphPath> compactLegsByReversedSearch(AStar aStar, RoutingRequest originalReq, RoutingRequest options, List<GraphPath> newPaths, double timeout, RemainingWeightHeuristic remainingWeightHeuristic) {
List<GraphPath> reversedPaths = new ArrayList<>();
for (GraphPath newPath : newPaths) {
State targetAcceptedState = options.arriveBy ? newPath.states.getLast().reverse() : newPath.states.getLast();
if (targetAcceptedState.stateData.getNumBooardings() < 2) {
reversedPaths.add(newPath);
continue;
}
final long arrDepTime = targetAcceptedState.getTimeSeconds();
LOG.debug("Dep time: " + new Date(newPath.getStartTime() * 1000));
LOG.debug("Arr time: " + new Date(newPath.getEndTime() * 1000));
// find first/last transit stop
Vertex transitStop = null;
long transitStopTime = arrDepTime;
while (transitStop == null) {
if (targetAcceptedState.backEdge instanceof TransitBoardAlight) {
if (options.arriveBy) {
transitStop = targetAcceptedState.backEdge.getFromVertex();
} else {
transitStop = targetAcceptedState.backEdge.getToVertex();
}
transitStopTime = targetAcceptedState.getTimeSeconds();
}
targetAcceptedState = targetAcceptedState.getBackState();
}
// find the path from transitStop to origin/destination
Vertex fromVertex = options.arriveBy ? options.rctx.fromVertex : transitStop;
Vertex toVertex = options.arriveBy ? transitStop : options.rctx.toVertex;
RoutingRequest reversedTransitRequest = createReversedTransitRequest(originalReq, options, fromVertex, toVertex, arrDepTime, new EuclideanRemainingWeightHeuristic());
aStar.getShortestPathTree(reversedTransitRequest, timeout);
List<GraphPath> pathsToTarget = aStar.getPathsToTarget();
if (pathsToTarget.isEmpty()) {
reversedPaths.add(newPath);
continue;
}
GraphPath walkPath = pathsToTarget.get(0);
// do the reversed search to/from transitStop
Vertex fromTransVertex = options.arriveBy ? transitStop : options.rctx.fromVertex;
Vertex toTransVertex = options.arriveBy ? options.rctx.toVertex : transitStop;
RoutingRequest reversedMainRequest = createReversedMainRequest(originalReq, options, fromTransVertex, toTransVertex, transitStopTime, remainingWeightHeuristic);
aStar.getShortestPathTree(reversedMainRequest, timeout);
List<GraphPath> newRevPaths = aStar.getPathsToTarget();
if (newRevPaths.isEmpty()) {
reversedPaths.add(newPath);
} else {
List<GraphPath> joinedPaths = new ArrayList<>();
for (GraphPath newRevPath : newRevPaths) {
LOG.debug("REV Dep time: " + new Date(newRevPath.getStartTime() * 1000));
LOG.debug("REV Arr time: " + new Date(newRevPath.getEndTime() * 1000));
List<GraphPath> concatenatedPaths = Arrays.asList(newRevPath, walkPath);
if (options.arriveBy) {
Collections.reverse(concatenatedPaths);
}
GraphPath joinedPath = joinPaths(concatenatedPaths);
if ((!options.arriveBy && joinedPath.states.getFirst().getTimeInMillis() > options.dateTime * 1000) || (options.arriveBy && joinedPath.states.getLast().getTimeInMillis() < options.dateTime * 1000)) {
joinedPaths.add(joinedPath);
if (newPaths.size() > 1) {
for (AgencyAndId tripId : joinedPath.getTrips()) {
options.banTrip(tripId);
}
}
}
}
reversedPaths.addAll(joinedPaths);
}
}
return reversedPaths.isEmpty() ? newPaths : reversedPaths;
}
Aggregations