use of org.opentripplanner.routing.graph.Graph 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();
}
}
use of org.opentripplanner.routing.graph.Graph 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"));
}
use of org.opentripplanner.routing.graph.Graph in project OpenTripPlanner by opentripplanner.
the class TurnRestrictionTest method before.
@Before
public void before() {
_graph = new Graph();
// Graph for a fictional grid city with turn restrictions
StreetVertex maple1 = vertex("maple_1st", 2.0, 2.0);
StreetVertex maple2 = vertex("maple_2nd", 1.0, 2.0);
StreetVertex maple3 = vertex("maple_3rd", 0.0, 2.0);
StreetVertex main1 = vertex("main_1st", 2.0, 1.0);
StreetVertex main2 = vertex("main_2nd", 1.0, 1.0);
StreetVertex main3 = vertex("main_3rd", 0.0, 1.0);
StreetVertex broad1 = vertex("broad_1st", 2.0, 0.0);
StreetVertex broad2 = vertex("broad_2nd", 1.0, 0.0);
StreetVertex broad3 = vertex("broad_3rd", 0.0, 0.0);
// Each block along the main streets has unit length and is one-way
StreetEdge maple1_2 = edge(maple1, maple2, 100.0, false);
StreetEdge maple2_3 = edge(maple2, maple3, 100.0, false);
StreetEdge main1_2 = edge(main1, main2, 100.0, false);
StreetEdge main2_3 = edge(main2, main3, 100.0, false);
broad1_2 = edge(broad1, broad2, 100.0, false);
StreetEdge broad2_3 = edge(broad2, broad3, 100.0, false);
// Each cross-street connects
maple_main1 = edge(maple1, main1, 50.0, false);
StreetEdge main_broad1 = edge(main1, broad1, 100.0, false);
StreetEdge maple_main2 = edge(maple2, main2, 50.0, false);
StreetEdge main_broad2 = edge(main2, broad2, 50.0, false);
StreetEdge maple_main3 = edge(maple3, main3, 100.0, false);
StreetEdge main_broad3 = edge(main3, broad3, 100.0, false);
// Turn restrictions are only for driving modes.
// - can't turn from 1st onto Main.
// - can't turn from 2nd onto Main.
// - can't turn from 2nd onto Broad.
DisallowTurn(maple_main1, main1_2);
DisallowTurn(maple_main2, main2_3);
DisallowTurn(main_broad2, broad2_3);
// Hold onto some vertices for the tests
topRight = maple1;
bottomLeft = broad3;
}
use of org.opentripplanner.routing.graph.Graph in project OpenTripPlanner by opentripplanner.
the class PartialStreetEdgeTest method setUp.
@Before
public void setUp() throws Exception {
_graph = new Graph();
// Graph for a fictional grid city with turn restrictions
v1 = vertex("maple_1st", 2.0, 2.0);
v2 = vertex("maple_2nd", 1.0, 2.0);
v3 = vertex("maple_3rd", 0.0, 2.0);
v4 = vertex("maple_4th", -1.0, 2.0);
e1 = edge(v1, v2, 1.0, StreetTraversalPermission.ALL);
e1Reverse = edge(v2, v1, 1.0, StreetTraversalPermission.ALL);
e2 = edge(v2, v3, 1.0, StreetTraversalPermission.ALL);
e3 = edge(v3, v4, 1.0, StreetTraversalPermission.ALL);
}
use of org.opentripplanner.routing.graph.Graph in project OpenTripPlanner by opentripplanner.
the class TurnCostTest method before.
@Before
public void before() {
_graph = new Graph();
// Graph for a fictional grid city with turn restrictions
StreetVertex maple1 = vertex("maple_1st", 2.0, 2.0);
StreetVertex maple2 = vertex("maple_2nd", 1.0, 2.0);
StreetVertex maple3 = vertex("maple_3rd", 0.0, 2.0);
StreetVertex main1 = vertex("main_1st", 2.0, 1.0);
StreetVertex main2 = vertex("main_2nd", 1.0, 1.0);
StreetVertex main3 = vertex("main_3rd", 0.0, 1.0);
StreetVertex broad1 = vertex("broad_1st", 2.0, 0.0);
StreetVertex broad2 = vertex("broad_2nd", 1.0, 0.0);
StreetVertex broad3 = vertex("broad_3rd", 0.0, 0.0);
// Each block along the main streets has unit length and is one-way
StreetEdge maple1_2 = edge(maple1, maple2, 100.0, false);
StreetEdge maple2_3 = edge(maple2, maple3, 100.0, false);
StreetEdge main1_2 = edge(main1, main2, 100.0, false);
StreetEdge main2_3 = edge(main2, main3, 100.0, false);
broad1_2 = edge(broad1, broad2, 100.0, false);
StreetEdge broad2_3 = edge(broad2, broad3, 100.0, false);
// Each cross-street connects
maple_main1 = edge(maple1, main1, 50.0, false);
StreetEdge main_broad1 = edge(main1, broad1, 100.0, false);
StreetEdge maple_main2 = edge(maple2, main2, 50.0, false);
StreetEdge main_broad2 = edge(main2, broad2, 50.0, false);
StreetEdge maple_main3 = edge(maple3, main3, 100.0, false);
StreetEdge main_broad3 = edge(main3, broad3, 100.0, false);
// Turn restrictions are only for driving modes.
// - can't turn from 1st onto Main.
// - can't turn from 2nd onto Main.
// - can't turn from 2nd onto Broad.
DisallowTurn(maple_main1, main1_2);
DisallowTurn(maple_main2, main2_3);
DisallowTurn(main_broad2, broad2_3);
// Hold onto some vertices for the tests
topRight = maple1;
bottomLeft = broad3;
// Make a prototype routing request.
proto = new RoutingRequest();
proto.carSpeed = 1.0;
proto.walkSpeed = 1.0;
proto.bikeSpeed = 1.0;
proto.turnReluctance = (1.0);
proto.setWalkReluctance(1.0);
proto.stairsReluctance = (1.0);
// Turn costs are all 0 by default.
proto.traversalCostModel = (new ConstantIntersectionTraversalCostModel(0.0));
}
Aggregations