Search in sources :

Example 1 with EuclideanRemainingWeightHeuristic

use of org.opentripplanner.routing.algorithm.astar.strategies.EuclideanRemainingWeightHeuristic 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)

Example 2 with EuclideanRemainingWeightHeuristic

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

the class TestParkAndRide method testCar.

public void testCar() throws Exception {
    AStar aStar = new AStar();
    // It is impossible to get from A to C in WALK mode,
    RoutingRequest options = new RoutingRequest(new TraverseModeSet(TraverseMode.WALK));
    options.setRoutingContext(graph, A, C);
    ShortestPathTree tree = aStar.getShortestPathTree(options);
    GraphPath path = tree.getPath(C, false);
    assertNull(path);
    // or CAR+WALK (no P+R).
    options = new RoutingRequest(new TraverseModeSet(TraverseMode.WALK, TraverseMode.CAR));
    options.freezeTraverseMode();
    options.setRoutingContext(graph, A, C);
    tree = aStar.getShortestPathTree(options);
    path = tree.getPath(C, false);
    assertNull(path);
    // So we Add a P+R at B.
    ParkAndRideVertex PRB = new ParkAndRideVertex(graph, "P+R", "P+R.B", 0.001, 45.00001, new NonLocalizedString("P+R B"));
    new ParkAndRideEdge(PRB);
    new ParkAndRideLinkEdge(PRB, B);
    new ParkAndRideLinkEdge(B, PRB);
    // But it is still impossible to get from A to C by WALK only
    // (AB is CAR only).
    options = new RoutingRequest(new TraverseModeSet(TraverseMode.WALK));
    options.freezeTraverseMode();
    options.setRoutingContext(graph, A, C);
    tree = aStar.getShortestPathTree(options);
    path = tree.getPath(C, false);
    assertNull(path);
    // Or CAR only (BC is WALK only).
    options = new RoutingRequest(new TraverseModeSet(TraverseMode.CAR));
    options.freezeTraverseMode();
    options.setRoutingContext(graph, A, C);
    tree = aStar.getShortestPathTree(options);
    path = tree.getPath(C, false);
    assertNull(path);
    // But we can go from A to C with CAR+WALK mode using P+R. arriveBy false
    options = new RoutingRequest(new TraverseModeSet(TraverseMode.WALK, TraverseMode.CAR, TraverseMode.TRANSIT));
    options.parkAndRide = true;
    // options.arriveBy
    options.setRoutingContext(graph, A, C);
    tree = aStar.getShortestPathTree(options);
    path = tree.getPath(C, false);
    assertNotNull(path);
    // But we can go from A to C with CAR+WALK mode using P+R. arriveBy true
    options = new RoutingRequest(new TraverseModeSet(TraverseMode.WALK, TraverseMode.CAR, TraverseMode.TRANSIT));
    options.parkAndRide = true;
    options.setArriveBy(true);
    options.setRoutingContext(graph, A, C);
    tree = aStar.getShortestPathTree(options);
    path = tree.getPath(A, false);
    assertNotNull(path);
    // But we can go from A to C with CAR+WALK mode using P+R. arriveBy true interleavedBidiHeuristic
    options = new RoutingRequest(new TraverseModeSet(TraverseMode.WALK, TraverseMode.CAR, TraverseMode.TRANSIT));
    options.parkAndRide = true;
    options.setArriveBy(true);
    options.setRoutingContext(graph, A, C);
    options.rctx.remainingWeightHeuristic = new EuclideanRemainingWeightHeuristic();
    tree = aStar.getShortestPathTree(options);
    path = tree.getPath(A, false);
    assertNotNull(path);
    // But we can go from A to C with CAR+WALK mode using P+R. arriveBy false interleavedBidiHeuristic
    options = new RoutingRequest(new TraverseModeSet(TraverseMode.WALK, TraverseMode.CAR, TraverseMode.TRANSIT));
    options.parkAndRide = true;
    // options.arriveBy
    options.setRoutingContext(graph, A, C);
    options.rctx.remainingWeightHeuristic = new EuclideanRemainingWeightHeuristic();
    tree = aStar.getShortestPathTree(options);
    path = tree.getPath(C, false);
    assertNotNull(path);
}
Also used : ParkAndRideEdge(org.opentripplanner.routing.edgetype.ParkAndRideEdge) ParkAndRideVertex(org.opentripplanner.routing.vertextype.ParkAndRideVertex) ShortestPathTree(org.opentripplanner.routing.spt.ShortestPathTree) ParkAndRideLinkEdge(org.opentripplanner.routing.edgetype.ParkAndRideLinkEdge) NonLocalizedString(org.opentripplanner.util.NonLocalizedString) AStar(org.opentripplanner.routing.algorithm.astar.AStar) GraphPath(org.opentripplanner.routing.spt.GraphPath) RoutingRequest(org.opentripplanner.routing.api.request.RoutingRequest) TraverseModeSet(org.opentripplanner.routing.core.TraverseModeSet) EuclideanRemainingWeightHeuristic(org.opentripplanner.routing.algorithm.astar.strategies.EuclideanRemainingWeightHeuristic)

Aggregations

AStar (org.opentripplanner.routing.algorithm.astar.AStar)2 EuclideanRemainingWeightHeuristic (org.opentripplanner.routing.algorithm.astar.strategies.EuclideanRemainingWeightHeuristic)2 RoutingRequest (org.opentripplanner.routing.api.request.RoutingRequest)2 GraphPath (org.opentripplanner.routing.spt.GraphPath)2 Collections (java.util.Collections)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 RemainingWeightHeuristic (org.opentripplanner.routing.algorithm.astar.strategies.RemainingWeightHeuristic)1 TrivialRemainingWeightHeuristic (org.opentripplanner.routing.algorithm.astar.strategies.TrivialRemainingWeightHeuristic)1 RoutingErrorCode (org.opentripplanner.routing.api.response.RoutingErrorCode)1 TraverseModeSet (org.opentripplanner.routing.core.TraverseModeSet)1 ParkAndRideEdge (org.opentripplanner.routing.edgetype.ParkAndRideEdge)1 ParkAndRideLinkEdge (org.opentripplanner.routing.edgetype.ParkAndRideLinkEdge)1 PathNotFoundException (org.opentripplanner.routing.error.PathNotFoundException)1 RoutingValidationException (org.opentripplanner.routing.error.RoutingValidationException)1 DominanceFunction (org.opentripplanner.routing.spt.DominanceFunction)1 ShortestPathTree (org.opentripplanner.routing.spt.ShortestPathTree)1 ParkAndRideVertex (org.opentripplanner.routing.vertextype.ParkAndRideVertex)1 Router (org.opentripplanner.standalone.server.Router)1