Search in sources :

Example 61 with GraphPath

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

the class TestBanning method doTestBannedTrips.

/**
 * Test trip banning. We compute a set of shortest routes between two random stops in the Portland graph. We then ban, for each route, up to a
 * certain amount of trips used in this route, one by one, and recompute the path. The banned trips must not appear in the new computed route.
 *
 * This is using a seeded random generator to easily make a reproducible and arbitrary list
 * of start/end points and trip to ban. It allow for a (bit) more coverage than doing a
 * single hand-picked test only.
 *
 * @param partial True to test partial trip banning, false for complete trip
 * @param seed Value to use for random generator seed -- Keep the same value for consistency.
 */
public void doTestBannedTrips(boolean partial, int seed) {
    Graph graph = ConstantsForTests.getInstance().getPortlandGraph();
    String feedId = graph.getFeedIds().iterator().next();
    Random rand = new Random(seed);
    for (int i = 0; i < 20; i++) {
        RoutingRequest options = new RoutingRequest();
        options.dateTime = TestUtils.dateInSeconds("America/Los_Angeles", 2009, 11, 1, 12, 34, 25);
        // Pick two random locations
        Vertex start = null;
        Vertex end = null;
        while (start == null) start = graph.getVertex(feedId + ":" + rand.nextInt(10000));
        while (end == null) end = graph.getVertex(feedId + ":" + rand.nextInt(10000));
        options.setRoutingContext(graph, start, end);
        ShortestPathTree spt = null;
        int n = rand.nextInt(5) + 3;
        for (int j = 0; j < n; j++) {
            spt = aStar.getShortestPathTree(options);
            GraphPath path = spt.getPath(end, true);
            if (path == null || spt == null)
                // No path found
                break;
            // List of used [trip,stop index] in the path
            Set<T2<AgencyAndId, BannedStopSet>> usedTripDefs = new HashSet<T2<AgencyAndId, BannedStopSet>>();
            for (State s : path.states) {
                if (s.getBackEdge() instanceof TransitBoardAlight) {
                    TransitBoardAlight tbae = (TransitBoardAlight) s.getBackEdge();
                    int boardingStopIndex = tbae.getStopIndex();
                    AgencyAndId tripId = s.getTripId();
                    BannedStopSet stopSet;
                    if (partial) {
                        stopSet = new BannedStopSet();
                        stopSet.add(boardingStopIndex);
                    } else {
                        stopSet = BannedStopSet.ALL;
                    }
                    if (tripId != null)
                        usedTripDefs.add(new T2<AgencyAndId, BannedStopSet>(tripId, stopSet));
                }
            }
            // Used trips should not contains a banned trip
            for (T2<AgencyAndId, BannedStopSet> usedTripDef : usedTripDefs) {
                BannedStopSet bannedStopSet = options.bannedTrips.get(usedTripDef.first);
                if (bannedStopSet != null) {
                    for (Integer stopIndex : usedTripDef.second) {
                        assertFalse(bannedStopSet.contains(stopIndex));
                    }
                }
            }
            if (usedTripDefs.size() == 0)
                // Not a transit trip, no sense to ban trip any longer
                break;
            // Pick a random used trip + stop set to ban
            List<T2<AgencyAndId, BannedStopSet>> usedTripDefsList = new ArrayList<T2<AgencyAndId, BannedStopSet>>(usedTripDefs);
            T2<AgencyAndId, BannedStopSet> tripDefToBan = usedTripDefsList.get(rand.nextInt(usedTripDefs.size()));
            options.bannedTrips.put(tripDefToBan.first, tripDefToBan.second);
        }
        options.bannedTrips.clear();
    }
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) BannedStopSet(org.opentripplanner.routing.request.BannedStopSet) TransitBoardAlight(org.opentripplanner.routing.edgetype.TransitBoardAlight) GraphPath(org.opentripplanner.routing.spt.GraphPath) Graph(org.opentripplanner.routing.graph.Graph) ShortestPathTree(org.opentripplanner.routing.spt.ShortestPathTree) State(org.opentripplanner.routing.core.State) RoutingRequest(org.opentripplanner.routing.core.RoutingRequest) T2(org.opentripplanner.common.model.T2)

Example 62 with GraphPath

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

the class TestFares method testFareComponent.

public void testFareComponent() throws Exception {
    Graph gg = new Graph();
    GtfsContext context = GtfsLibrary.readGtfs(new File(ConstantsForTests.FARE_COMPONENT_GTFS));
    GTFSPatternHopFactory factory = new GTFSPatternHopFactory(context);
    factory.run(gg);
    gg.putService(CalendarServiceData.class, GtfsLibrary.createCalendarServiceData(context.getDao()));
    String feedId = gg.getFeedIds().iterator().next();
    RoutingRequest options = new RoutingRequest();
    long startTime = TestUtils.dateInSeconds("America/Los_Angeles", 2009, 8, 7, 12, 0, 0);
    options.dateTime = startTime;
    ShortestPathTree spt;
    GraphPath path = null;
    Fare fare = null;
    List<FareComponent> fareComponents = null;
    FareService fareService = gg.getService(FareService.class);
    Money tenUSD = new Money(new WrappedCurrency("USD"), 1000);
    // A -> B, base case
    options.setRoutingContext(gg, feedId + ":A", feedId + ":B");
    spt = aStar.getShortestPathTree(options);
    path = spt.getPath(gg.getVertex(feedId + ":B"), true);
    fare = fareService.getCost(path);
    fareComponents = fare.getDetails(FareType.regular);
    assertEquals(fareComponents.size(), 1);
    assertEquals(fareComponents.get(0).price, tenUSD);
    assertEquals(fareComponents.get(0).fareId, new AgencyAndId(feedId, "AB"));
    assertEquals(fareComponents.get(0).routes.get(0), new AgencyAndId("agency", "1"));
    // D -> E, null case
    options.setRoutingContext(gg, feedId + ":D", feedId + ":E");
    spt = aStar.getShortestPathTree(options);
    path = spt.getPath(gg.getVertex(feedId + ":E"), true);
    fare = fareService.getCost(path);
    assertNull(fare);
    // A -> C, 2 components in a path
    options.setRoutingContext(gg, feedId + ":A", feedId + ":C");
    spt = aStar.getShortestPathTree(options);
    path = spt.getPath(gg.getVertex(feedId + ":C"), true);
    fare = fareService.getCost(path);
    fareComponents = fare.getDetails(FareType.regular);
    assertEquals(fareComponents.size(), 2);
    assertEquals(fareComponents.get(0).price, tenUSD);
    assertEquals(fareComponents.get(0).fareId, new AgencyAndId(feedId, "AB"));
    assertEquals(fareComponents.get(0).routes.get(0), new AgencyAndId("agency", "1"));
    assertEquals(fareComponents.get(1).price, tenUSD);
    assertEquals(fareComponents.get(1).fareId, new AgencyAndId(feedId, "BC"));
    assertEquals(fareComponents.get(1).routes.get(0), new AgencyAndId("agency", "2"));
    // B -> D, 2 fully connected components
    options.setRoutingContext(gg, feedId + ":B", feedId + ":D");
    spt = aStar.getShortestPathTree(options);
    path = spt.getPath(gg.getVertex(feedId + ":D"), true);
    fare = fareService.getCost(path);
    fareComponents = fare.getDetails(FareType.regular);
    assertEquals(fareComponents.size(), 1);
    assertEquals(fareComponents.get(0).price, tenUSD);
    assertEquals(fareComponents.get(0).fareId, new AgencyAndId(feedId, "BD"));
    assertEquals(fareComponents.get(0).routes.get(0), new AgencyAndId("agency", "2"));
    assertEquals(fareComponents.get(0).routes.get(1), new AgencyAndId("agency", "3"));
    // E -> G, missing in between fare
    options.setRoutingContext(gg, feedId + ":E", feedId + ":G");
    spt = aStar.getShortestPathTree(options);
    path = spt.getPath(gg.getVertex(feedId + ":G"), true);
    fare = fareService.getCost(path);
    fareComponents = fare.getDetails(FareType.regular);
    assertEquals(fareComponents.size(), 1);
    assertEquals(fareComponents.get(0).price, tenUSD);
    assertEquals(fareComponents.get(0).fareId, new AgencyAndId(feedId, "EG"));
    assertEquals(fareComponents.get(0).routes.get(0), new AgencyAndId("agency", "5"));
    assertEquals(fareComponents.get(0).routes.get(1), new AgencyAndId("agency", "6"));
    // C -> E, missing fare after
    options.setRoutingContext(gg, feedId + ":C", feedId + ":E");
    spt = aStar.getShortestPathTree(options);
    path = spt.getPath(gg.getVertex(feedId + ":E"), true);
    fare = fareService.getCost(path);
    fareComponents = fare.getDetails(FareType.regular);
    assertEquals(fareComponents.size(), 1);
    assertEquals(fareComponents.get(0).price, tenUSD);
    assertEquals(fareComponents.get(0).fareId, new AgencyAndId(feedId, "CD"));
    assertEquals(fareComponents.get(0).routes.get(0), new AgencyAndId("agency", "3"));
    // D -> G, missing fare before
    options.setRoutingContext(gg, feedId + ":D", feedId + ":G");
    spt = aStar.getShortestPathTree(options);
    path = spt.getPath(gg.getVertex(feedId + ":G"), true);
    fare = fareService.getCost(path);
    fareComponents = fare.getDetails(FareType.regular);
    assertEquals(fareComponents.size(), 1);
    assertEquals(fareComponents.get(0).price, tenUSD);
    assertEquals(fareComponents.get(0).fareId, new AgencyAndId(feedId, "EG"));
    assertEquals(fareComponents.get(0).routes.get(0), new AgencyAndId("agency", "5"));
    assertEquals(fareComponents.get(0).routes.get(1), new AgencyAndId("agency", "6"));
    // A -> D, use individual component parts
    options.setRoutingContext(gg, feedId + ":A", feedId + ":D");
    spt = aStar.getShortestPathTree(options);
    path = spt.getPath(gg.getVertex(feedId + ":D"), true);
    fare = fareService.getCost(path);
    fareComponents = fare.getDetails(FareType.regular);
    assertEquals(fareComponents.size(), 2);
    assertEquals(fareComponents.get(0).price, tenUSD);
    assertEquals(fareComponents.get(0).fareId, new AgencyAndId(feedId, "AB"));
    assertEquals(fareComponents.get(0).routes.get(0), new AgencyAndId("agency", "1"));
    assertEquals(fareComponents.get(1).price, tenUSD);
    assertEquals(fareComponents.get(1).fareId, new AgencyAndId(feedId, "BD"));
    assertEquals(fareComponents.get(1).routes.get(0), new AgencyAndId("agency", "2"));
    assertEquals(fareComponents.get(1).routes.get(1), new AgencyAndId("agency", "3"));
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) GtfsContext(org.opentripplanner.gtfs.GtfsContext) GraphPath(org.opentripplanner.routing.spt.GraphPath) WrappedCurrency(org.opentripplanner.routing.core.WrappedCurrency) GTFSPatternHopFactory(org.opentripplanner.routing.edgetype.factory.GTFSPatternHopFactory) FareService(org.opentripplanner.routing.services.FareService) Fare(org.opentripplanner.routing.core.Fare) Money(org.opentripplanner.routing.core.Money) Graph(org.opentripplanner.routing.graph.Graph) ShortestPathTree(org.opentripplanner.routing.spt.ShortestPathTree) RoutingRequest(org.opentripplanner.routing.core.RoutingRequest) FareComponent(org.opentripplanner.routing.core.FareComponent) File(java.io.File)

Example 63 with GraphPath

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

the class TurnRestrictionTest method testForwardAsCar.

@Test
public void testForwardAsCar() {
    RoutingRequest options = new RoutingRequest(TraverseMode.CAR);
    options.carSpeed = 1.0;
    options.setRoutingContext(_graph, topRight, bottomLeft);
    ShortestPathTree tree = new AStar().getShortestPathTree(options);
    GraphPath path = tree.getPath(bottomLeft, false);
    assertNotNull(path);
    // If not for turn restrictions, the shortest path would be to take 1st to Main,
    // Main to 2nd, 2nd to Broad and Broad until the corner of Broad and 3rd.
    // However, most of these turns are not allowed. Instead, the shortest allowed
    // path is 1st to Broad, Broad to 3rd.
    List<State> states = path.states;
    assertEquals(5, states.size());
    assertEquals("maple_1st", states.get(0).getVertex().getLabel());
    assertEquals("main_1st", states.get(1).getVertex().getLabel());
    assertEquals("broad_1st", states.get(2).getVertex().getLabel());
    assertEquals("broad_2nd", states.get(3).getVertex().getLabel());
    assertEquals("broad_3rd", states.get(4).getVertex().getLabel());
}
Also used : ShortestPathTree(org.opentripplanner.routing.spt.ShortestPathTree) State(org.opentripplanner.routing.core.State) GraphPath(org.opentripplanner.routing.spt.GraphPath) RoutingRequest(org.opentripplanner.routing.core.RoutingRequest) Test(org.junit.Test)

Example 64 with GraphPath

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

the class TestTransfers method testForbiddenStopToStopTransferWithFrequencyInReverse.

public void testForbiddenStopToStopTransferWithFrequencyInReverse() throws Exception {
    // Replace the transfer table with an empty table
    TransferTable table = new TransferTable();
    when(graph.getTransferTable()).thenReturn(table);
    // Compute a normal path between two stops
    Vertex origin = graph.getVertex(feedId + ":U");
    Vertex destination = graph.getVertex(feedId + ":J");
    // Set options like time and routing context
    RoutingRequest options = new RoutingRequest();
    options.setArriveBy(true);
    options.dateTime = TestUtils.dateInSeconds("America/New_York", 2009, 7, 11, 11, 11, 0);
    options.setRoutingContext(graph, origin, destination);
    // Plan journey
    GraphPath path;
    List<Trip> trips;
    path = planJourney(options);
    trips = extractTrips(path);
    // Validate result
    assertEquals("15.1", trips.get(0).getId().getId());
    assertEquals("5.1", trips.get(1).getId().getId());
    // Add forbidden transfer to table
    Stop stopV = new Stop();
    stopV.setId(new AgencyAndId(feedId, "V"));
    Stop stopI = new Stop();
    stopI.setId(new AgencyAndId(feedId, "I"));
    table.addTransferTime(stopV, stopI, null, null, null, null, StopTransfer.FORBIDDEN_TRANSFER);
    // Plan journey
    path = planJourney(options);
    trips = extractTrips(path);
    // Check that no trip was returned
    assertEquals(0, trips.size());
    // Revert the graph, thus using the original transfer table again
    reset(graph);
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) Trip(org.onebusaway.gtfs.model.Trip) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) TransitStop(org.opentripplanner.routing.vertextype.TransitStop) Stop(org.onebusaway.gtfs.model.Stop) GraphPath(org.opentripplanner.routing.spt.GraphPath)

Example 65 with GraphPath

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

the class TestTransfers method testStopToStopTransferWithFrequency.

public void testStopToStopTransferWithFrequency() throws Exception {
    // Replace the transfer table with an empty table
    TransferTable table = new TransferTable();
    when(graph.getTransferTable()).thenReturn(table);
    // Compute a normal path between two stops
    Vertex origin = graph.getVertex(feedId + ":O");
    Vertex destination = graph.getVertex(feedId + ":V");
    // Set options like time and routing context
    RoutingRequest options = new RoutingRequest();
    options.dateTime = TestUtils.dateInSeconds("America/New_York", 2009, 7, 11, 13, 11, 0);
    options.setRoutingContext(graph, origin, destination);
    // Plan journey
    GraphPath path;
    List<Trip> trips;
    path = planJourney(options);
    trips = extractTrips(path);
    // Validate result
    assertEquals("10.5", trips.get(0).getId().getId());
    assertEquals("15.1", trips.get(1).getId().getId());
    // Find state with FrequencyBoard back edge and save time of that state
    long time = -1;
    for (State s : path.states) {
        if (s.getBackEdge() instanceof TransitBoardAlight && ((TransitBoardAlight) s.getBackEdge()).boarding) {
            // find the final board edge, don't break
            time = s.getTimeSeconds();
        }
    }
    assertTrue(time >= 0);
    // Add transfer to table such that the next trip will be chosen
    // (there are 3600 seconds between trips), transfer time was 75 seconds
    Stop stopP = new Stop();
    stopP.setId(new AgencyAndId(feedId, "P"));
    Stop stopU = new Stop();
    stopU.setId(new AgencyAndId(feedId, "U"));
    table.addTransferTime(stopP, stopU, null, null, null, null, 3675);
    // Plan journey
    path = planJourney(options);
    trips = extractTrips(path);
    // Check whether a later second trip was taken
    assertEquals("10.5", trips.get(0).getId().getId());
    assertEquals("15.1", trips.get(1).getId().getId());
    // Find state with FrequencyBoard back edge and save time of that state
    long newTime = -1;
    for (State s : path.states) {
        if (s.getBackEdge() instanceof TransitBoardAlight && ((TransitBoardAlight) s.getBackEdge()).boarding) {
            // find the final board edge, don't break
            newTime = s.getTimeSeconds();
        }
    }
    assertTrue(newTime >= 0);
    assertTrue(newTime > time);
    assertEquals(3600, newTime - time);
    // Revert the graph, thus using the original transfer table again
    reset(graph);
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) Trip(org.onebusaway.gtfs.model.Trip) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) TransitBoardAlight(org.opentripplanner.routing.edgetype.TransitBoardAlight) TransitStop(org.opentripplanner.routing.vertextype.TransitStop) Stop(org.onebusaway.gtfs.model.Stop) GraphPath(org.opentripplanner.routing.spt.GraphPath)

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