use of org.opentripplanner.routing.spt.ShortestPathTree 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;
}
use of org.opentripplanner.routing.spt.ShortestPathTree 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;
}
}
use of org.opentripplanner.routing.spt.ShortestPathTree in project OpenTripPlanner by opentripplanner.
the class TestShapefileStreetGraphBuilderImpl method testBasic.
@Test
public void testBasic() throws Exception {
Graph gg = new Graph();
URL resource = getClass().getResource("nyc_streets/streets.shp");
File file = null;
if (resource != null) {
file = new File(resource.getFile());
}
if (file == null || !file.exists()) {
System.out.println("No New York City basemap; skipping; see comment here for details");
/*
* This test requires the New York City base map. Place it among the source
* resources and Eclipse should automatically copy it over to the target directory.
* Once you have prepared these files, you may need to 'refresh' in Eclipse's package
* explorer to force Eclipse to notice the new resources.
*
* Recent versions of this map are available only in Arcview Geodatabase format.
* For conversion to a Shapefile, you will need the archived MapInfo version at:
* http://www.nyc.gov/html/dcp/html/bytes/bytesarchive.shtml#lion
* Download the MapInfo file of Lion version 10B.
*
* This must then be converted to a ShapeFile as follows:
* cd opentripplanner-graph-builder/src/test/resources/org/opentripplanner/graph_builder/module/shapefile
* mkdir nyc_streets (this is where we will store the shapefile)
* unzip nyc_lion10ami.zip (this should place zipfile contents in a ./lion directory)
* ogr2ogr -f 'ESRI Shapefile' nyc_streets/streets.shp lion/MNLION1.tab
* ogr2ogr -update -append -f 'ESRI Shapefile' nyc_streets lion/SILION1.tab -nln streets
* ogr2ogr -update -append -f 'ESRI Shapefile' nyc_streets lion/QNLION1.tab -nln streets
* ogr2ogr -update -append -f 'ESRI Shapefile' nyc_streets lion/BKLION1.tab -nln streets
* ogr2ogr -update -append -f 'ESRI Shapefile' nyc_streets lion/BXLION1.tab -nln streets
*
* Testing also requires NYC Subway data in GTFS in the same location:
* wget http://data.topplabs.org/data/mta_nyct_subway/subway.zip
*/
return;
}
ShapefileFeatureSourceFactoryImpl factory = new ShapefileFeatureSourceFactoryImpl(file);
ShapefileStreetSchema schema = new ShapefileStreetSchema();
schema.setIdAttribute("SegmentID");
schema.setNameAttribute("Street");
/* only featuretyp=0 are streets */
CaseBasedBooleanConverter selector = new CaseBasedBooleanConverter("FeatureTyp", false);
HashMap<String, Boolean> streets = new HashMap<String, Boolean>();
streets.put("0", true);
selector.setValues(streets);
schema.setFeatureSelector(selector);
/* street directions */
CaseBasedTraversalPermissionConverter perms = new CaseBasedTraversalPermissionConverter("TrafDir", StreetTraversalPermission.PEDESTRIAN_AND_BICYCLE);
perms.addPermission("W", StreetTraversalPermission.ALL, StreetTraversalPermission.PEDESTRIAN);
perms.addPermission("A", StreetTraversalPermission.PEDESTRIAN, StreetTraversalPermission.ALL);
perms.addPermission("T", StreetTraversalPermission.ALL, StreetTraversalPermission.ALL);
schema.setPermissionConverter(perms);
ShapefileStreetModule loader = new ShapefileStreetModule();
loader.setFeatureSourceFactory(factory);
loader.setSchema(schema);
loader.buildGraph(gg, new HashMap<Class<?>, Object>());
// find start and end vertices
Vertex start = null;
Vertex end = null;
Vertex carlton = null;
Coordinate vanderbiltAtPark = new Coordinate(-73.969178, 40.676785);
Coordinate grandAtLafayette = new Coordinate(-73.999095, 40.720005);
Coordinate carltonAtPark = new Coordinate(-73.972347, 40.677447);
for (Vertex v : gg.getVertices()) {
if (v.getCoordinate().distance(vanderbiltAtPark) < 0.00005) {
/* we need the correct vanderbilt at park. In this case,
* that's the one facing west on vanderbilt.
*/
int numParks = 0;
int numCarltons = 0;
for (Edge e : v.getOutgoing()) {
if (e.getToVertex().getName().contains("PARK")) {
numParks++;
}
if (e.getToVertex().getName().contains("CARLTON")) {
numCarltons++;
}
}
if (numCarltons != 2 || numParks != 1) {
continue;
}
start = v;
} else if (v.getCoordinate().distance(grandAtLafayette) < 0.0001) {
end = v;
} else if (v.getCoordinate().distance(carltonAtPark) < 0.00005) {
/* we need the correct carlton at park. In this case,
* that's the one facing west.
*/
int numFlatbushes = 0;
int numParks = 0;
for (Edge e : v.getOutgoing()) {
if (e.getToVertex().getName().contains("FLATBUSH")) {
numFlatbushes++;
}
if (e.getToVertex().getName().contains("PARK")) {
numParks++;
}
}
if (numFlatbushes != 2 || numParks != 1) {
continue;
}
carlton = v;
}
}
assertNotNull(start);
assertNotNull(end);
assertNotNull(carlton);
assertEquals(3, start.getDegreeOut());
assertEquals(3, start.getDegreeIn());
AStar aStar = new AStar();
RoutingRequest opt = new RoutingRequest();
opt.setRoutingContext(gg, start, end);
ShortestPathTree spt = aStar.getShortestPathTree(opt);
assertNotNull(spt);
// test that the option to walk bikes on the first or last segment works
opt = new RoutingRequest(new TraverseModeSet(TraverseMode.BICYCLE));
// Real live cyclists tell me that they would prefer to ride around the long way than to
// walk their bikes the short way. If we slow down the default biking speed, that will
// force a change in preferences.
opt.bikeSpeed = 2;
opt.setRoutingContext(gg, start, carlton);
spt = aStar.getShortestPathTree(opt);
assertNotNull(spt);
/* commented out as bike walking is not supported */
/*
GraphPath path = spt.getPath(carlton.vertex);
assertNotNull(path);
assertTrue(path.edges.size() <= 3);
wo.setArriveBy(true);
spt = AStar.getShortestPathTreeBack(gg, start.vertex, carlton.vertex, new State(0), wo);
assertNotNull(spt);
path = spt.getPath(carlton.vertex);
assertTrue(path.edges.size() <= 3);
*/
}
use of org.opentripplanner.routing.spt.ShortestPathTree 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);
}
use of org.opentripplanner.routing.spt.ShortestPathTree 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);
}
Aggregations