Search in sources :

Example 1 with RemainingWeightHeuristic

use of org.opentripplanner.routing.algorithm.astar.strategies.RemainingWeightHeuristic in project OpenTripPlanner by opentripplanner.

the class GraphPathFinder method getPaths.

/**
 * This no longer does "trip banning" to find multiple itineraries.
 * It just searches once trying to find a non-transit path.
 */
public List<GraphPath> getPaths(RoutingRequest options) {
    if (options == null) {
        LOG.error("PathService was passed a null routing request.");
        return null;
    }
    if (options.streetSubRequestModes.isTransit()) {
        throw new UnsupportedOperationException("Transit search not supported");
    }
    // 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
    }
    // 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;
    if (options.disableRemainingWeightHeuristic || options.oneToMany) {
        heuristic = new TrivialRemainingWeightHeuristic();
    } else {
        heuristic = new EuclideanRemainingWeightHeuristic();
    }
    options.rctx.remainingWeightHeuristic = heuristic;
    /* 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. An unlimited value would cause the bidi heuristic to do unbounded street
         * searches and consider the whole graph walkable.
         *
         * After the limited areas of the street network around the origin and destination are explored, the
         * options.maxWalkDistance will be set to unlimited for similar reasons to maxTransfers above. That happens
         * in method org.opentripplanner.routing.algorithm.astar.strategies.InterleavedBidirectionalHeuristic.initialize
         */
    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");
    double timeout = searchBeginTime + router.streetRoutingTimeoutSeconds() * 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;
        return null;
    }
    // Don't dig through the SPT object, just ask the A star algorithm for the states that reached the target.
    aStar.getShortestPathTree(options, timeout);
    List<GraphPath> paths = aStar.getPathsToTarget().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, options.getPathComparator(options.arriveBy));
    return paths;
}
Also used : Logger(org.slf4j.Logger) Iterator(java.util.Iterator) PathNotFoundException(org.opentripplanner.routing.error.PathNotFoundException) LoggerFactory(org.slf4j.LoggerFactory) GraphPath(org.opentripplanner.routing.spt.GraphPath) TrivialRemainingWeightHeuristic(org.opentripplanner.routing.algorithm.astar.strategies.TrivialRemainingWeightHeuristic) Collectors(java.util.stream.Collectors) RoutingValidationException(org.opentripplanner.routing.error.RoutingValidationException) DominanceFunction(org.opentripplanner.routing.spt.DominanceFunction) List(java.util.List) EuclideanRemainingWeightHeuristic(org.opentripplanner.routing.algorithm.astar.strategies.EuclideanRemainingWeightHeuristic) Router(org.opentripplanner.standalone.server.Router) RemainingWeightHeuristic(org.opentripplanner.routing.algorithm.astar.strategies.RemainingWeightHeuristic) RoutingErrorCode(org.opentripplanner.routing.api.response.RoutingErrorCode) AStar(org.opentripplanner.routing.algorithm.astar.AStar) RoutingRequest(org.opentripplanner.routing.api.request.RoutingRequest) Collections(java.util.Collections) TrivialRemainingWeightHeuristic(org.opentripplanner.routing.algorithm.astar.strategies.TrivialRemainingWeightHeuristic) EuclideanRemainingWeightHeuristic(org.opentripplanner.routing.algorithm.astar.strategies.EuclideanRemainingWeightHeuristic) RemainingWeightHeuristic(org.opentripplanner.routing.algorithm.astar.strategies.RemainingWeightHeuristic) AStar(org.opentripplanner.routing.algorithm.astar.AStar) GraphPath(org.opentripplanner.routing.spt.GraphPath) TrivialRemainingWeightHeuristic(org.opentripplanner.routing.algorithm.astar.strategies.TrivialRemainingWeightHeuristic) EuclideanRemainingWeightHeuristic(org.opentripplanner.routing.algorithm.astar.strategies.EuclideanRemainingWeightHeuristic) DominanceFunction(org.opentripplanner.routing.spt.DominanceFunction)

Aggregations

Collections (java.util.Collections)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 AStar (org.opentripplanner.routing.algorithm.astar.AStar)1 EuclideanRemainingWeightHeuristic (org.opentripplanner.routing.algorithm.astar.strategies.EuclideanRemainingWeightHeuristic)1 RemainingWeightHeuristic (org.opentripplanner.routing.algorithm.astar.strategies.RemainingWeightHeuristic)1 TrivialRemainingWeightHeuristic (org.opentripplanner.routing.algorithm.astar.strategies.TrivialRemainingWeightHeuristic)1 RoutingRequest (org.opentripplanner.routing.api.request.RoutingRequest)1 RoutingErrorCode (org.opentripplanner.routing.api.response.RoutingErrorCode)1 PathNotFoundException (org.opentripplanner.routing.error.PathNotFoundException)1 RoutingValidationException (org.opentripplanner.routing.error.RoutingValidationException)1 DominanceFunction (org.opentripplanner.routing.spt.DominanceFunction)1 GraphPath (org.opentripplanner.routing.spt.GraphPath)1 Router (org.opentripplanner.standalone.server.Router)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1