use of org.opentripplanner.routing.trippattern.TripTimes 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.trippattern.TripTimes in project OpenTripPlanner by opentripplanner.
the class Stats method create.
/**
* Scan through all trips on this pattern and summarize those that are running.
*/
public static Stats create(TripPattern pattern, int stop0, int stop1, TimeWindow window) {
Stats s = new Stats();
s.min = Integer.MAX_VALUE;
s.num = 0;
// the trips whose service is not running
for (TripTimes tripTimes : pattern.scheduledTimetable.tripTimes) {
int depart = tripTimes.getDepartureTime(stop0);
int arrive = tripTimes.getArrivalTime(stop1);
if (window.includes(depart) && window.includes(arrive) && window.servicesRunning.get(tripTimes.serviceCode)) {
int t = arrive - depart;
if (t < s.min)
s.min = t;
if (t > s.max)
s.max = t;
s.avg += t;
++s.num;
}
}
/* Do the same thing for any frequency-based trips. */
for (FrequencyEntry freq : pattern.scheduledTimetable.frequencyEntries) {
TripTimes tt = freq.tripTimes;
int overlap = window.overlap(freq.startTime, freq.endTime, tt.serviceCode);
if (overlap == 0)
continue;
// number of trip instances in the overlap. round up, avoid zeros.
int n = overlap / freq.headway + 1;
int depart = tt.getDepartureTime(stop0);
int arrive = tt.getArrivalTime(stop1);
int t = arrive - depart;
if (t < s.min)
s.min = t;
if (t > s.max)
s.max = t;
s.avg += (t * n);
s.num += n;
}
if (s.num > 0) {
s.avg /= s.num;
return s;
}
/* There are no running trips within the time range, on the given serviceIds. */
return null;
}
use of org.opentripplanner.routing.trippattern.TripTimes in project OpenTripPlanner by opentripplanner.
the class PatternDwell method traverse.
public State traverse(State state0) {
// int trip = state0.getTrip();
TripTimes tripTimes = state0.getTripTimes();
int dwellTime = tripTimes.getDwellTime(stopIndex);
StateEditor s1 = state0.edit(this);
s1.setBackMode(getMode());
s1.incrementTimeInSeconds(dwellTime);
s1.incrementWeight(dwellTime);
return s1.makeState();
}
use of org.opentripplanner.routing.trippattern.TripTimes in project OpenTripPlanner by opentripplanner.
the class TripTimeShort method fromTripTimes.
/**
* must pass in both table and trip, because tripTimes do not have stops.
*/
public static List<TripTimeShort> fromTripTimes(Timetable table, Trip trip) {
TripTimes times = table.getTripTimes(table.getTripIndex(trip.getId()));
List<TripTimeShort> out = Lists.newArrayList();
// one per stop, not one per hop, thus the <= operator
for (int i = 0; i < times.getNumStops(); ++i) {
out.add(new TripTimeShort(times, i, table.pattern.getStop(i)));
}
return out;
}
use of org.opentripplanner.routing.trippattern.TripTimes in project OpenTripPlanner by opentripplanner.
the class SkipStop method omitStops.
public TripTimes omitStops(TripTimes tt, int... stopsToSkip) {
TIntSet skipped = new TIntHashSet(stopsToSkip);
List<StopTime> newSts = Lists.newArrayList();
int cumulativeTime = -1;
for (int i = 0; i < tt.getNumStops(); i++) {
int hopTime = i != 0 ? tt.getArrivalTime(i) - tt.getDepartureTime(i - 1) : 0;
int dwellTime = tt.getDepartureTime(i) - tt.getArrivalTime(i);
// handle the first stop(s) being skipped
if (cumulativeTime != -1)
// note that we include hopTime before the check if the stop is included but dwell time after,
// the assumption being that there is no dwell at a skipped stop.
cumulativeTime += hopTime;
if (skipped.contains(i))
continue;
// if this stop is now the first stop, get the time
if (cumulativeTime == -1)
cumulativeTime = tt.getArrivalTime(i);
StopTime stopTime = new StopTime();
stopTime.setArrivalTime(cumulativeTime);
cumulativeTime += dwellTime;
stopTime.setDepartureTime(cumulativeTime);
stopTime.setStopSequence(tt.getStopSequence(i));
stopTime.setTimepoint(tt.isTimepoint(i) ? 1 : 0);
newSts.add(stopTime);
}
TripTimes newtt = new TripTimes(tt.trip, newSts, new Deduplicator());
newtt.serviceCode = tt.serviceCode;
return newtt;
}
Aggregations