Search in sources :

Example 56 with ShortestPathTree

use of org.opentripplanner.routing.spt.ShortestPathTree 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 57 with ShortestPathTree

use of org.opentripplanner.routing.spt.ShortestPathTree 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 58 with ShortestPathTree

use of org.opentripplanner.routing.spt.ShortestPathTree 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 59 with ShortestPathTree

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

the class TestPatternHopFactory method testPickupDropoff.

public void testPickupDropoff() throws Exception {
    Vertex stop_o = graph.getVertex(feedId + ":O_depart");
    Vertex stop_p = graph.getVertex(feedId + ":P");
    assertEquals(2, stop_o.getOutgoing().size());
    long startTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 19, 12, 0, 0);
    RoutingRequest options = new RoutingRequest();
    options.dateTime = startTime;
    options.setRoutingContext(graph, stop_o, stop_p);
    ShortestPathTree spt = aStar.getShortestPathTree(options);
    GraphPath path = spt.getPath(stop_p, false);
    assertNotNull(path);
    long endTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 19, 12, 10, 0);
    assertEquals(endTime, path.getEndTime());
    startTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 19, 12, 0, 1);
    options.dateTime = startTime;
    options.setRoutingContext(graph, stop_o, stop_p);
    spt = aStar.getShortestPathTree(options);
    path = spt.getPath(stop_p, false);
    assertNotNull(path);
    endTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 19, 15, 10, 0);
    assertEquals(endTime, path.getEndTime());
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) IntersectionVertex(org.opentripplanner.routing.vertextype.IntersectionVertex) ShortestPathTree(org.opentripplanner.routing.spt.ShortestPathTree) GraphPath(org.opentripplanner.routing.spt.GraphPath) RoutingRequest(org.opentripplanner.routing.core.RoutingRequest)

Example 60 with ShortestPathTree

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

the class TestPatternHopFactory method testFrequencies.

public void testFrequencies() {
    Vertex stop_u = graph.getVertex(feedId + ":U_depart");
    Vertex stop_v = graph.getVertex(feedId + ":V_arrive");
    ShortestPathTree spt;
    GraphPath path;
    RoutingRequest options = new RoutingRequest();
    options.setModes(new TraverseModeSet("TRANSIT"));
    options.dateTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 7, 0, 0, 0);
    options.setRoutingContext(graph, stop_u, stop_v);
    // U to V - original stop times - shouldn't be used
    spt = aStar.getShortestPathTree(options);
    path = spt.getPath(stop_v, false);
    assertNotNull(path);
    assertEquals(4, path.states.size());
    long endTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 7, 6, 40, 0);
    assertEquals(endTime, path.getEndTime());
    // U to V - first frequency
    options.dateTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 7, 7, 0, 0);
    options.setRoutingContext(graph, stop_u, stop_v);
    spt = aStar.getShortestPathTree(options);
    path = spt.getPath(stop_v, false);
    assertNotNull(path);
    assertEquals(4, path.states.size());
    endTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 7, 7, 40, 0);
    assertEquals(endTime, path.getEndTime());
    // U to V - second frequency
    options.dateTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 7, 14, 0, 0);
    options.setRoutingContext(graph, stop_u, stop_v);
    spt = aStar.getShortestPathTree(options);
    path = spt.getPath(stop_v, false);
    assertNotNull(path);
    assertEquals(4, path.states.size());
    endTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 7, 14, 40, 0);
    assertEquals(endTime, path.getEndTime());
// TODO more detailed testing of frequencies
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) IntersectionVertex(org.opentripplanner.routing.vertextype.IntersectionVertex) ShortestPathTree(org.opentripplanner.routing.spt.ShortestPathTree) GraphPath(org.opentripplanner.routing.spt.GraphPath) RoutingRequest(org.opentripplanner.routing.core.RoutingRequest) TraverseModeSet(org.opentripplanner.routing.core.TraverseModeSet)

Aggregations

ShortestPathTree (org.opentripplanner.routing.spt.ShortestPathTree)64 RoutingRequest (org.opentripplanner.routing.core.RoutingRequest)52 GraphPath (org.opentripplanner.routing.spt.GraphPath)47 Vertex (org.opentripplanner.routing.graph.Vertex)34 State (org.opentripplanner.routing.core.State)24 Edge (org.opentripplanner.routing.graph.Edge)17 Test (org.junit.Test)15 IntersectionVertex (org.opentripplanner.routing.vertextype.IntersectionVertex)15 Graph (org.opentripplanner.routing.graph.Graph)13 AStar (org.opentripplanner.routing.algorithm.AStar)10 HashSet (java.util.HashSet)9 TraverseModeSet (org.opentripplanner.routing.core.TraverseModeSet)8 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)7 StreetEdge (org.opentripplanner.routing.edgetype.StreetEdge)7 DominanceFunction (org.opentripplanner.routing.spt.DominanceFunction)7 TransitStop (org.opentripplanner.routing.vertextype.TransitStop)7 NonLocalizedString (org.opentripplanner.util.NonLocalizedString)7 Coordinate (com.vividsolutions.jts.geom.Coordinate)6 File (java.io.File)5 TemporaryStreetLocation (org.opentripplanner.routing.location.TemporaryStreetLocation)5