Search in sources :

Example 36 with GraphPath

use of org.opentripplanner.routing.spt.GraphPath in project OpenTripPlanner by opentripplanner.

the class TurnCostTest method checkForwardRouteDuration.

private GraphPath checkForwardRouteDuration(RoutingRequest options, int expectedDuration) {
    ShortestPathTree tree = new AStar().getShortestPathTree(options);
    GraphPath path = tree.getPath(bottomLeft, false);
    assertNotNull(path);
    // Without turn costs, this path costs 2x100 + 2x50 = 300.
    assertEquals(expectedDuration, path.getDuration());
    // Weight == duration when reluctances == 0.
    assertEquals(expectedDuration, (int) path.getWeight());
    for (State s : path.states) {
        assertEquals(s.getElapsedTimeSeconds(), (int) s.getWeight());
    }
    return path;
}
Also used : ShortestPathTree(org.opentripplanner.routing.spt.ShortestPathTree) State(org.opentripplanner.routing.core.State) GraphPath(org.opentripplanner.routing.spt.GraphPath)

Example 37 with GraphPath

use of org.opentripplanner.routing.spt.GraphPath in project OpenTripPlanner by opentripplanner.

the class TestOnBoardRouting method testOnBoardRouting.

/**
 * Compute a set of path between two random stop locations in a test GTFS.
 *
 * For each departure/arrival location, compute a normal path (depart alighted). Then re-run the
 * same itinerary but with departure while on-board at a randomly-picked up trip alongside the
 * path.
 *
 * We assert that the two itineraries will arrive at the same time, at the same place, with at
 * least one less boarding, and take a less or equals amount of time.
 */
@SuppressWarnings("deprecation")
public void testOnBoardRouting() throws Exception {
    String feedId = graph.getFeedIds().iterator().next();
    // Seed the random generator to make consistent set of tests
    Random rand = new Random(42);
    // Number of tests to run
    final int NTESTS = 100;
    int n = 0;
    while (true) {
        /* Compute a normal path between two random stops... */
        Vertex origin, destination;
        do {
            /* See FAKE_GTFS for available locations */
            origin = graph.getVertex(feedId + ":" + (char) (65 + rand.nextInt(20)));
            destination = graph.getVertex(feedId + ":" + (char) (65 + rand.nextInt(20)));
        } while (origin.equals(destination));
        /* ...at a random date/time */
        RoutingRequest options = new RoutingRequest();
        options.dateTime = TestUtils.dateInSeconds("America/New_York", 2009, 5 + rand.nextInt(4), 1 + rand.nextInt(20), 4 + rand.nextInt(10), rand.nextInt(60), 0);
        ShortestPathTree spt;
        GraphPath path;
        options.setRoutingContext(graph, origin, destination);
        spt = aStar.getShortestPathTree(options);
        path = spt.getPath(destination, false);
        if (path == null)
            continue;
        System.out.println("Testing path between " + origin.getLabel() + " and " + destination.getLabel() + " at " + new Date(options.dateTime * 1000));
        long arrivalTime1 = 0L;
        long elapsedTime1 = 0L;
        int numBoardings1 = 0;
        Vertex arrivalVertex1 = null;
        if (verbose)
            System.out.println("PATH 1 ---------------------");
        for (State s : path.states) {
            if (verbose)
                System.out.println(s + " [" + s.getVertex().getClass().getName() + "]");
            arrivalTime1 = s.getTimeSeconds();
            arrivalVertex1 = s.getVertex();
            elapsedTime1 = s.getElapsedTimeSeconds();
            numBoardings1 = s.getNumBoardings();
        }
        /* Get a random transit hop from the computed path */
        Stop end = null;
        PatternStopVertex nextV = null;
        TripTimes tripTimes = null;
        int stopIndex = 0;
        long newStart = 0L;
        int nhop = 0;
        for (State s : path.states) {
            if (s.getVertex() instanceof PatternArriveVertex && s.getBackEdge() instanceof PatternHop)
                nhop++;
        }
        int hop = rand.nextInt(nhop);
        nhop = 0;
        float k = rand.nextFloat();
        for (State s : path.states) {
            Vertex v = s.getVertex();
            if (v instanceof PatternArriveVertex && s.getBackEdge() instanceof PatternHop) {
                if (hop == nhop) {
                    PatternArriveVertex pav = (PatternArriveVertex) v;
                    end = pav.getStop();
                    nextV = pav;
                    PatternHop phe = (PatternHop) s.getBackEdge();
                    stopIndex = phe.getStopIndex();
                    tripTimes = s.getTripTimes();
                    int hopDuration = tripTimes.getRunningTime(stopIndex);
                    /*
                         * New start time at k% of hop. Note: do not try to make: round(time +
                         * k.hop) as it will be off few seconds due to floating-point rounding
                         * errors.
                         */
                    newStart = s.getBackState().getTimeSeconds() + Math.round(hopDuration * k);
                    break;
                }
                nhop++;
            }
        }
        System.out.println("Boarded depart: trip=" + tripTimes.trip + ", nextStop=" + nextV.getStop() + " stopIndex=" + stopIndex + " startTime=" + new Date(newStart * 1000L));
        /* And use it for onboard departure */
        double lat = end.getLat();
        // Mock location, not really important here.
        double lon = end.getLon();
        OnboardDepartVertex onboardOrigin = new OnboardDepartVertex("OnBoard_Origin", lat, lon);
        @SuppressWarnings("unused") OnBoardDepartPatternHop currentHop = new OnBoardDepartPatternHop(onboardOrigin, nextV, tripTimes, options.rctx.serviceDays.get(1), stopIndex, k);
        options.dateTime = newStart;
        options.setRoutingContext(graph, onboardOrigin, destination);
        spt = aStar.getShortestPathTree(options);
        /* Re-compute a new path starting boarded */
        GraphPath path2 = spt.getPath(destination, false);
        assertNotNull(path2);
        if (verbose)
            System.out.println("PATH 2 ---------------------");
        long arrivalTime2 = 0L;
        long elapsedTime2 = 0L;
        int numBoardings2 = 0;
        Vertex arrivalVertex2 = null;
        for (State s : path2.states) {
            if (verbose)
                System.out.println(s + " [" + s.getVertex().getClass().getName() + "]");
            arrivalTime2 = s.getTimeSeconds();
            arrivalVertex2 = s.getVertex();
            elapsedTime2 = s.getElapsedTimeSeconds();
            numBoardings2 = s.getNumBoardings();
        }
        /* Arrival time and vertex *must* match */
        assertEquals(arrivalTime1, arrivalTime2);
        assertEquals(arrivalVertex1, destination);
        assertEquals(arrivalVertex2, destination);
        /* On-board *must* be shorter in time */
        assertTrue(elapsedTime2 <= elapsedTime1);
        /* On-board *must* have less boardings */
        assertTrue(numBoardings2 < numBoardings1);
        /* Cleanup edges */
        for (Edge edge : onboardOrigin.getOutgoing()) {
            graph.removeEdge(edge);
        }
        n++;
        if (n > NTESTS)
            break;
    }
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) OnboardDepartVertex(org.opentripplanner.routing.vertextype.OnboardDepartVertex) PatternArriveVertex(org.opentripplanner.routing.vertextype.PatternArriveVertex) PatternStopVertex(org.opentripplanner.routing.vertextype.PatternStopVertex) Stop(org.onebusaway.gtfs.model.Stop) GraphPath(org.opentripplanner.routing.spt.GraphPath) OnboardDepartVertex(org.opentripplanner.routing.vertextype.OnboardDepartVertex) Date(java.util.Date) Random(java.util.Random) ShortestPathTree(org.opentripplanner.routing.spt.ShortestPathTree) PatternHop(org.opentripplanner.routing.edgetype.PatternHop) OnBoardDepartPatternHop(org.opentripplanner.routing.edgetype.OnBoardDepartPatternHop) TripTimes(org.opentripplanner.routing.trippattern.TripTimes) PatternStopVertex(org.opentripplanner.routing.vertextype.PatternStopVertex) PatternArriveVertex(org.opentripplanner.routing.vertextype.PatternArriveVertex) OnBoardDepartPatternHop(org.opentripplanner.routing.edgetype.OnBoardDepartPatternHop) Edge(org.opentripplanner.routing.graph.Edge)

Example 38 with GraphPath

use of org.opentripplanner.routing.spt.GraphPath in project OpenTripPlanner by opentripplanner.

the class TestOpenStreetMapGraphBuilder method testBuildingAreas.

/**
 * This reads test file with area
 * and tests if it can be routed if visibility is used and if it isn't
 *
 * Routing needs to be successful in both options since without visibility calculation
 * area rings are used.
 * @param skipVisibility if true visibility calculations are skipped
 * @throws UnsupportedEncodingException
 */
private void testBuildingAreas(boolean skipVisibility) throws UnsupportedEncodingException {
    Graph gg = new Graph();
    OpenStreetMapModule loader = new OpenStreetMapModule();
    loader.skipVisibility = skipVisibility;
    loader.setDefaultWayPropertySetSource(new DefaultWayPropertySetSource());
    FileBasedOpenStreetMapProviderImpl provider = new FileBasedOpenStreetMapProviderImpl();
    File file = new File(URLDecoder.decode(getClass().getResource("usf_area.osm.gz").getFile(), "UTF-8"));
    provider.setPath(file);
    loader.setProvider(provider);
    loader.buildGraph(gg, extra);
    new StreetVertexIndexServiceImpl(gg);
    OTPServer otpServer = new OTPServer(new CommandLineParameters(), new GraphService());
    otpServer.getGraphService().registerGraph("A", new MemoryGraphSource("A", gg));
    Router a = otpServer.getGraphService().getRouter("A");
    RoutingRequest request = new RoutingRequest("WALK");
    // This are vertices that can be connected only over edges on area (with correct permissions)
    // It tests if it is possible to route over area without visibility calculations
    Vertex bottomV = gg.getVertex("osm:node:580290955");
    Vertex topV = gg.getVertex("osm:node:559271124");
    request.setRoutingContext(a.graph, bottomV, topV);
    GraphPathFinder graphPathFinder = new GraphPathFinder(a);
    List<GraphPath> pathList = graphPathFinder.graphPathFinderEntryPoint(request);
    assertNotNull(pathList);
    assertFalse(pathList.isEmpty());
    for (GraphPath path : pathList) {
        assertFalse(path.states.isEmpty());
    }
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) IntersectionVertex(org.opentripplanner.routing.vertextype.IntersectionVertex) CommandLineParameters(org.opentripplanner.standalone.CommandLineParameters) FileBasedOpenStreetMapProviderImpl(org.opentripplanner.openstreetmap.impl.FileBasedOpenStreetMapProviderImpl) GraphPath(org.opentripplanner.routing.spt.GraphPath) Router(org.opentripplanner.standalone.Router) GraphService(org.opentripplanner.routing.services.GraphService) Graph(org.opentripplanner.routing.graph.Graph) OTPServer(org.opentripplanner.standalone.OTPServer) MemoryGraphSource(org.opentripplanner.routing.impl.MemoryGraphSource) RoutingRequest(org.opentripplanner.routing.core.RoutingRequest) File(java.io.File) GraphPathFinder(org.opentripplanner.routing.impl.GraphPathFinder) StreetVertexIndexServiceImpl(org.opentripplanner.routing.impl.StreetVertexIndexServiceImpl)

Example 39 with GraphPath

use of org.opentripplanner.routing.spt.GraphPath in project OpenTripPlanner by opentripplanner.

the class AlertPatchTest method testStopAlertPatch.

public void testStopAlertPatch() {
    AlertPatch snp1 = new AlertPatch();
    snp1.setFeedId(feedId);
    snp1.setTimePeriods(Collections.singletonList(new TimePeriod(0, // until ~1/1/2011
    1000L * 60 * 60 * 24 * 365 * 40)));
    Alert note1 = Alert.createSimpleAlerts("The first note");
    snp1.setAlert(note1);
    snp1.setId("id1");
    snp1.setStop(new AgencyAndId(feedId, "A"));
    snp1.apply(graph);
    Vertex stop_a = graph.getVertex(feedId + ":A");
    Vertex stop_e = graph.getVertex(feedId + ":E_arrive");
    ShortestPathTree spt;
    GraphPath optimizedPath, unoptimizedPath;
    options.dateTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 7, 0, 0, 0);
    options.setRoutingContext(graph, stop_a, stop_e);
    spt = aStar.getShortestPathTree(options);
    optimizedPath = spt.getPath(stop_e, true);
    unoptimizedPath = spt.getPath(stop_e, false);
    assertNotNull(optimizedPath);
    HashSet<Alert> expectedAlerts = new HashSet<Alert>();
    expectedAlerts.add(note1);
    Edge optimizedEdge = optimizedPath.states.get(1).getBackEdge();
    HashSet<Alert> optimizedAlerts = new HashSet<Alert>();
    for (AlertPatch alertPatch : graph.getAlertPatches(optimizedEdge)) {
        optimizedAlerts.add(alertPatch.getAlert());
    }
    assertEquals(expectedAlerts, optimizedAlerts);
    Edge unoptimizedEdge = unoptimizedPath.states.get(1).getBackEdge();
    HashSet<Alert> unoptimizedAlerts = new HashSet<Alert>();
    for (AlertPatch alertPatch : graph.getAlertPatches(unoptimizedEdge)) {
        unoptimizedAlerts.add(alertPatch.getAlert());
    }
    assertEquals(expectedAlerts, unoptimizedAlerts);
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ShortestPathTree(org.opentripplanner.routing.spt.ShortestPathTree) GraphPath(org.opentripplanner.routing.spt.GraphPath) Edge(org.opentripplanner.routing.graph.Edge) HashSet(java.util.HashSet)

Example 40 with GraphPath

use of org.opentripplanner.routing.spt.GraphPath in project OpenTripPlanner by opentripplanner.

the class TestAStar method testMaxTime.

public void testMaxTime() {
    Graph graph = ConstantsForTests.getInstance().getPortlandGraph();
    String feedId = graph.getFeedIds().iterator().next();
    Vertex start = graph.getVertex(feedId + ":8371");
    Vertex end = graph.getVertex(feedId + ":8374");
    RoutingRequest options = new RoutingRequest();
    long startTime = TestUtils.dateInSeconds("America/Los_Angeles", 2009, 11, 1, 12, 34, 25);
    options.dateTime = startTime;
    // one hour is more than enough time
    options.worstTime = startTime + 60 * 60;
    options.setRoutingContext(graph, start, end);
    ShortestPathTree spt = aStar.getShortestPathTree(options);
    GraphPath path = spt.getPath(end, true);
    assertNotNull(path);
    // but one minute is not enough
    options.worstTime = startTime + 60;
    spt = aStar.getShortestPathTree(options);
    path = spt.getPath(end, true);
    assertNull(path);
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) Graph(org.opentripplanner.routing.graph.Graph) ShortestPathTree(org.opentripplanner.routing.spt.ShortestPathTree) GraphPath(org.opentripplanner.routing.spt.GraphPath) RoutingRequest(org.opentripplanner.routing.core.RoutingRequest)

Aggregations

GraphPath (org.opentripplanner.routing.spt.GraphPath)76 RoutingRequest (org.opentripplanner.routing.core.RoutingRequest)56 ShortestPathTree (org.opentripplanner.routing.spt.ShortestPathTree)47 Vertex (org.opentripplanner.routing.graph.Vertex)39 State (org.opentripplanner.routing.core.State)25 Test (org.junit.Test)18 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)18 IntersectionVertex (org.opentripplanner.routing.vertextype.IntersectionVertex)17 Edge (org.opentripplanner.routing.graph.Edge)15 Graph (org.opentripplanner.routing.graph.Graph)13 TransitStop (org.opentripplanner.routing.vertextype.TransitStop)11 Stop (org.onebusaway.gtfs.model.Stop)10 HashSet (java.util.HashSet)9 Trip (org.onebusaway.gtfs.model.Trip)9 StreetEdge (org.opentripplanner.routing.edgetype.StreetEdge)9 NonLocalizedString (org.opentripplanner.util.NonLocalizedString)8 TraverseModeSet (org.opentripplanner.routing.core.TraverseModeSet)6 File (java.io.File)5 TransitBoardAlight (org.opentripplanner.routing.edgetype.TransitBoardAlight)5 TemporaryStreetLocation (org.opentripplanner.routing.location.TemporaryStreetLocation)5