Search in sources :

Example 6 with FrequencyEntry

use of org.opentripplanner.routing.trippattern.FrequencyEntry in project OpenTripPlanner by opentripplanner.

the class Ride method calcStatsForFreqs.

/**
 * Calculate the wait time stats for boarding all (non-exact) frequency entries in this Ride.
 */
private Stats calcStatsForFreqs(TimeWindow window) {
    // all stats fields are initialized to zero
    Stats stats = new Stats();
    // the total number of seconds that headway boarding is possible
    stats.num = 0;
    for (PatternRide patternRide : patternRides) {
        for (FrequencyEntry freq : patternRide.pattern.scheduledTimetable.frequencyEntries) {
            if (freq.exactTimes) {
                LOG.error("Exact times not yet supported in profile routing.");
                return null;
            }
            int overlap = window.overlap(freq.startTime, freq.endTime, freq.tripTimes.serviceCode);
            if (overlap > 0) {
                if (freq.headway > stats.max)
                    stats.max = freq.headway;
                // weight the average of each headway by the number of seconds it is valid
                stats.avg += (freq.headway / 2) * overlap;
                stats.num += overlap;
            }
        }
    }
    if (stats.num == 0)
        return null;
    /* Some frequency entries were added to the stats. */
    stats.avg /= stats.num;
    return stats;
}
Also used : FrequencyEntry(org.opentripplanner.routing.trippattern.FrequencyEntry)

Example 7 with FrequencyEntry

use of org.opentripplanner.routing.trippattern.FrequencyEntry 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;
}
Also used : TripTimes(org.opentripplanner.routing.trippattern.TripTimes) FrequencyEntry(org.opentripplanner.routing.trippattern.FrequencyEntry)

Example 8 with FrequencyEntry

use of org.opentripplanner.routing.trippattern.FrequencyEntry in project OpenTripPlanner by opentripplanner.

the class RaptorWorkerTimetable method forPattern.

/**
 * This is a factory function rather than a constructor to avoid calling the super constructor for rejected patterns.
 * BannedRoutes is formatted as agencyid_routeid.
 */
public static RaptorWorkerTimetable forPattern(Graph graph, TripPattern pattern, TimeWindow window, Scenario scenario, TaskStatistics ts) {
    // Filter down the trips to only those running during the window
    // This filtering can reduce number of trips and run time by 80 percent
    BitSet servicesRunning = window.servicesRunning;
    List<TripTimes> tripTimes = Lists.newArrayList();
    TT: for (TripTimes tt : pattern.scheduledTimetable.tripTimes) {
        if (servicesRunning.get(tt.serviceCode) && tt.getArrivalTime(0) < window.to && tt.getDepartureTime(tt.getNumStops() - 1) >= window.from) {
            // TODO: need to do this before filtering based on window!
            if (scenario != null && scenario.modifications != null) {
                for (TripFilter filter : Iterables.filter(scenario.modifications, TripFilter.class)) {
                    tt = filter.apply(tt.trip, pattern, tt);
                    if (tt == null)
                        continue TT;
                }
            }
            tripTimes.add(tt);
        }
    }
    // find frequency trips
    List<FrequencyEntry> freqs = Lists.newArrayList();
    FREQUENCIES: for (FrequencyEntry fe : pattern.scheduledTimetable.frequencyEntries) {
        if (servicesRunning.get(fe.tripTimes.serviceCode) && fe.getMinDeparture() < window.to && fe.getMaxArrival() > window.from) {
            if (fe.exactTimes) {
                LOG.warn("Exact-times frequency trips not yet supported");
                continue;
            }
            if (scenario != null && scenario.modifications != null) {
                for (TripFilter filter : Iterables.filter(scenario.modifications, TripFilter.class)) {
                    fe = filter.apply(fe.tripTimes.trip, pattern, fe);
                    if (fe == null)
                        continue FREQUENCIES;
                }
            }
            freqs.add(fe);
        }
    }
    if (tripTimes.isEmpty() && freqs.isEmpty()) {
        // no trips active, don't bother storing a timetable
        return null;
    }
    // Sort the trip times by their first arrival time
    Collections.sort(tripTimes, new Comparator<TripTimes>() {

        @Override
        public int compare(TripTimes tt1, TripTimes tt2) {
            return (tt1.getArrivalTime(0) - tt2.getArrivalTime(0));
        }
    });
    // Copy the times into the compacted table
    RaptorWorkerTimetable rwtt = new RaptorWorkerTimetable(tripTimes.size(), pattern.getStops().size());
    int t = 0;
    for (TripTimes tt : tripTimes) {
        int[] times = new int[rwtt.nStops * 2];
        for (int s = 0; s < pattern.getStops().size(); s++) {
            int arrival = tt.getArrivalTime(s);
            int departure = tt.getDepartureTime(s);
            times[s * 2] = arrival;
            times[s * 2 + 1] = departure;
        }
        rwtt.timesPerTrip[t++] = times;
    }
    ts.scheduledTripCount += rwtt.timesPerTrip.length;
    // save frequency times
    rwtt.frequencyTrips = new int[freqs.size()][pattern.getStops().size() * 2];
    rwtt.endTimes = new int[freqs.size()];
    rwtt.startTimes = new int[freqs.size()];
    rwtt.headwaySecs = new int[freqs.size()];
    {
        int i = 0;
        for (FrequencyEntry fe : freqs) {
            rwtt.headwaySecs[i] = fe.headway;
            rwtt.startTimes[i] = fe.startTime;
            rwtt.endTimes[i] = fe.endTime;
            ts.frequencyTripCount += fe.numTrips();
            int[] times = rwtt.frequencyTrips[i];
            // It's generally considered good practice to have frequency trips start at midnight, however that is
            // not always the case, and we need to preserve the original times so that we can update them in
            // real time.
            int startTime = fe.tripTimes.getArrivalTime(0);
            for (int s = 0; s < fe.tripTimes.getNumStops(); s++) {
                times[s * 2] = fe.tripTimes.getArrivalTime(s) - startTime;
                times[s * 2 + 1] = fe.tripTimes.getDepartureTime(s) - startTime;
            }
            i++;
        }
    }
    ts.frequencyEntryCount += rwtt.getFrequencyTripCount();
    rwtt.mode = pattern.route.getType();
    return rwtt;
}
Also used : BitSet(java.util.BitSet) TripFilter(org.opentripplanner.analyst.scenario.TripFilter) TripTimes(org.opentripplanner.routing.trippattern.TripTimes) FrequencyEntry(org.opentripplanner.routing.trippattern.FrequencyEntry)

Example 9 with FrequencyEntry

use of org.opentripplanner.routing.trippattern.FrequencyEntry in project OpenTripPlanner by opentripplanner.

the class TimetableFilterTest method testAdjustHeadway.

/**
 * test modifying frequencies
 */
@Test
public void testAdjustHeadway() {
    AdjustHeadway ah = new AdjustHeadway();
    ah.agencyId = agency.getId();
    ah.routeId = Arrays.asList(route.getId().getId());
    ah.headway = 120;
    // should have no effect on scheduled trips
    assertEquals(times, ah.apply(trip, pattern, times));
    FrequencyEntry fe2 = ah.apply(trip, pattern, frequencyEntry);
    assertNotNull(fe2);
    assertEquals(120, fe2.headway);
    // make sure we didn't accidentally modify the entry in the graph
    assertEquals(600, frequencyEntry.headway);
}
Also used : FrequencyEntry(org.opentripplanner.routing.trippattern.FrequencyEntry) Test(org.junit.Test)

Example 10 with FrequencyEntry

use of org.opentripplanner.routing.trippattern.FrequencyEntry in project OpenTripPlanner by opentripplanner.

the class TimetableFilterTest method setUp.

@Override
protected void setUp() {
    agency = new Agency();
    agency.setId("AGENCY");
    route = new Route();
    route.setType(com.conveyal.gtfs.model.Route.BUS);
    route.setShortName("T");
    route.setLongName("TEST");
    route.setAgency(agency);
    route.setId(new AgencyAndId(agency.getId(), "TEST"));
    metro = new Route();
    metro.setType(com.conveyal.gtfs.model.Route.SUBWAY);
    metro.setShortName("M");
    metro.setLongName("METRO");
    metro.setAgency(agency);
    metro.setId(new AgencyAndId(agency.getId(), "METRO"));
    trip = new Trip();
    trip.setRoute(route);
    trip.setId(new AgencyAndId(agency.getId(), "TRIP"));
    trip2 = new Trip();
    trip2.setRoute(route);
    trip2.setId(new AgencyAndId(agency.getId(), "TRIP2"));
    stops = new Stop[4];
    for (int i = 0; i < stops.length; i++) {
        Stop s = new Stop();
        s.setLat(-122.123);
        s.setLon(37.363 + i * 0.001);
        s.setId(new AgencyAndId(agency.getId(), "" + i));
        stops[i] = s;
    }
    List<StopTime> stopTimes = makeStopTimes(trip);
    StopPattern sp = new StopPattern(stopTimes);
    pattern = new TripPattern(route, sp);
    // make a triptimes
    times = makeTripTimes(trip, stopTimes);
    pattern.scheduledTimetable.addTripTimes(times);
    pattern.scheduledTimetable.addTripTimes(makeTripTimes(trip2, makeStopTimes(trip2)));
    // ten-minute frequency
    frequencyEntry = new FrequencyEntry(7 * 3600, 12 * 3600, 600, false, makeTripTimes(trip, makeStopTimes(trip)));
    pattern.scheduledTimetable.addFrequencyEntry(frequencyEntry);
    pattern.scheduledTimetable.addFrequencyEntry(new FrequencyEntry(7 * 3600, 12 * 3600, 600, false, makeTripTimes(trip2, makeStopTimes(trip2))));
    metroTrip = new Trip();
    metroTrip.setRoute(metro);
    metroTrip.setId(new AgencyAndId(agency.getId(), "TRIP"));
    stopTimes = makeStopTimes(metroTrip);
    sp = new StopPattern(stopTimes);
    metroPattern = new TripPattern(metro, sp);
    metroTimes = makeTripTimes(metroTrip, stopTimes);
    metroPattern.scheduledTimetable.addTripTimes(metroTimes);
}
Also used : StopPattern(org.opentripplanner.model.StopPattern) Trip(org.onebusaway.gtfs.model.Trip) Agency(org.onebusaway.gtfs.model.Agency) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Stop(org.onebusaway.gtfs.model.Stop) FrequencyEntry(org.opentripplanner.routing.trippattern.FrequencyEntry) Route(org.onebusaway.gtfs.model.Route) TripPattern(org.opentripplanner.routing.edgetype.TripPattern) StopTime(org.onebusaway.gtfs.model.StopTime)

Aggregations

FrequencyEntry (org.opentripplanner.routing.trippattern.FrequencyEntry)15 TripTimes (org.opentripplanner.routing.trippattern.TripTimes)12 TripPattern (org.opentripplanner.routing.edgetype.TripPattern)7 Stop (org.onebusaway.gtfs.model.Stop)6 TransitStop (org.opentripplanner.routing.vertextype.TransitStop)4 ArrayList (java.util.ArrayList)3 StopTime (org.onebusaway.gtfs.model.StopTime)3 StopPattern (org.opentripplanner.model.StopPattern)3 TIntList (gnu.trove.list.TIntList)2 TIntArrayList (gnu.trove.list.array.TIntArrayList)2 TObjectIntMap (gnu.trove.map.TObjectIntMap)2 TObjectIntHashMap (gnu.trove.map.hash.TObjectIntHashMap)2 List (java.util.List)2 Test (org.junit.Test)2 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)2 Trip (org.onebusaway.gtfs.model.Trip)2 HashMultimap (com.google.common.collect.HashMultimap)1 Multimap (com.google.common.collect.Multimap)1 Ints (com.google.common.primitives.Ints)1 LineString (com.vividsolutions.jts.geom.LineString)1