Search in sources :

Example 1 with InterleavedBidirectionalHeuristic

use of org.opentripplanner.routing.algorithm.strategies.InterleavedBidirectionalHeuristic 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;
}
Also used : RemainingWeightHeuristic(org.opentripplanner.routing.algorithm.strategies.RemainingWeightHeuristic) TrivialRemainingWeightHeuristic(org.opentripplanner.routing.algorithm.strategies.TrivialRemainingWeightHeuristic) EuclideanRemainingWeightHeuristic(org.opentripplanner.routing.algorithm.strategies.EuclideanRemainingWeightHeuristic) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) AStar(org.opentripplanner.routing.algorithm.AStar) GraphPath(org.opentripplanner.routing.spt.GraphPath) EuclideanRemainingWeightHeuristic(org.opentripplanner.routing.algorithm.strategies.EuclideanRemainingWeightHeuristic) InterleavedBidirectionalHeuristic(org.opentripplanner.routing.algorithm.strategies.InterleavedBidirectionalHeuristic) TrivialRemainingWeightHeuristic(org.opentripplanner.routing.algorithm.strategies.TrivialRemainingWeightHeuristic) RoutingRequest(org.opentripplanner.routing.core.RoutingRequest) DominanceFunction(org.opentripplanner.routing.spt.DominanceFunction)

Example 2 with InterleavedBidirectionalHeuristic

use of org.opentripplanner.routing.algorithm.strategies.InterleavedBidirectionalHeuristic 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("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("WALK,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("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("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("WALK,CAR_PARK,TRANSIT");
    // 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("WALK,CAR_PARK,TRANSIT");
    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("WALK,CAR_PARK,TRANSIT");
    options.setArriveBy(true);
    options.setRoutingContext(graph, A, C);
    options.rctx.remainingWeightHeuristic = new InterleavedBidirectionalHeuristic();
    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("WALK,CAR_PARK,TRANSIT");
    // options.arriveBy
    options.setRoutingContext(graph, A, C);
    options.rctx.remainingWeightHeuristic = new InterleavedBidirectionalHeuristic();
    tree = aStar.getShortestPathTree(options);
    path = tree.getPath(C, false);
    assertNotNull(path);
}
Also used : ParkAndRideVertex(org.opentripplanner.routing.vertextype.ParkAndRideVertex) ShortestPathTree(org.opentripplanner.routing.spt.ShortestPathTree) NonLocalizedString(org.opentripplanner.util.NonLocalizedString) GraphPath(org.opentripplanner.routing.spt.GraphPath) InterleavedBidirectionalHeuristic(org.opentripplanner.routing.algorithm.strategies.InterleavedBidirectionalHeuristic) RoutingRequest(org.opentripplanner.routing.core.RoutingRequest) TraverseModeSet(org.opentripplanner.routing.core.TraverseModeSet)

Aggregations

InterleavedBidirectionalHeuristic (org.opentripplanner.routing.algorithm.strategies.InterleavedBidirectionalHeuristic)2 RoutingRequest (org.opentripplanner.routing.core.RoutingRequest)2 GraphPath (org.opentripplanner.routing.spt.GraphPath)2 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)1 AStar (org.opentripplanner.routing.algorithm.AStar)1 EuclideanRemainingWeightHeuristic (org.opentripplanner.routing.algorithm.strategies.EuclideanRemainingWeightHeuristic)1 RemainingWeightHeuristic (org.opentripplanner.routing.algorithm.strategies.RemainingWeightHeuristic)1 TrivialRemainingWeightHeuristic (org.opentripplanner.routing.algorithm.strategies.TrivialRemainingWeightHeuristic)1 TraverseModeSet (org.opentripplanner.routing.core.TraverseModeSet)1 DominanceFunction (org.opentripplanner.routing.spt.DominanceFunction)1 ShortestPathTree (org.opentripplanner.routing.spt.ShortestPathTree)1 ParkAndRideVertex (org.opentripplanner.routing.vertextype.ParkAndRideVertex)1 NonLocalizedString (org.opentripplanner.util.NonLocalizedString)1