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;
}
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;
}
}
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());
}
}
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);
}
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);
}
Aggregations