Search in sources :

Example 21 with TIntIntIterator

use of gnu.trove.iterator.TIntIntIterator in project OpenTripPlanner by opentripplanner.

the class Histogram method mean.

public int mean() {
    long sum = 0;
    for (TIntIntIterator it = bins.iterator(); it.hasNext(); ) {
        it.advance();
        sum += it.key() * it.value();
    }
    return (int) (sum / count);
}
Also used : TIntIntIterator(gnu.trove.iterator.TIntIntIterator)

Example 22 with TIntIntIterator

use of gnu.trove.iterator.TIntIntIterator in project OpenTripPlanner by opentripplanner.

the class RaptorWorker method runRaptor.

/**
 * @param accessTimes a map from transit stops to the time it takes to reach those stops
 * @param nonTransitTimes the time to reach all targets without transit. Targets can be vertices or points/samples.
 */
public PropagatedTimesStore runRaptor(Graph graph, TIntIntMap accessTimes, int[] nonTransitTimes, TaskStatistics ts) {
    long beginCalcTime = System.currentTimeMillis();
    TIntIntMap initialStops = new TIntIntHashMap();
    TIntIntIterator initialIterator = accessTimes.iterator();
    while (initialIterator.hasNext()) {
        initialIterator.advance();
        int stopIndex = initialIterator.key();
        int accessTime = initialIterator.value();
        initialStops.put(stopIndex, accessTime);
    }
    PropagatedTimesStore propagatedTimesStore = new PropagatedTimesStore(graph, this.req, data.nTargets);
    // optimization: if no schedules, only run Monte Carlo
    int fromTime = req.fromTime;
    int monteCarloDraws = MONTE_CARLO_COUNT_PER_MINUTE;
    if (!data.hasSchedules) {
        // only do one iteration
        fromTime = req.toTime - 60;
        monteCarloDraws = TOTAL_MONTE_CARLO_COUNT;
    }
    // if no frequencies, don't run Monte Carlo
    int iterations = (req.toTime - fromTime - 60) / 60 + 1;
    // if we multiply when we're not doing monte carlo, we'll end up with too many iterations.
    if (data.hasFrequencies)
        // we add 2 because we do two "fake" draws where we do min or max instead of a monte carlo draw
        iterations *= (monteCarloDraws + 2);
    ts.searchCount = iterations;
    // Iterate backward through minutes (range-raptor) taking a snapshot of router state after each call
    int[][] timesAtTargetsEachIteration = new int[iterations][data.nTargets];
    // for each iteration, whether it is the result of a schedule or Monte Carlo search, or whether it is an extrema.
    // extrema are not included in averages.
    boolean[] includeIterationInAverages = new boolean[iterations];
    Arrays.fill(includeIterationInAverages, true);
    // TODO don't hardwire timestep below
    ts.timeStep = 60;
    // times at targets from scheduled search
    int[] scheduledTimesAtTargets = new int[data.nTargets];
    Arrays.fill(scheduledTimesAtTargets, UNREACHED);
    // current iteration
    int iteration = 0;
    // FIXME this should be changed to tolerate a zero-width time range
    for (int departureTime = req.toTime - 60, n = 0; departureTime >= fromTime; departureTime -= 60, n++) {
        if (n % 15 == 0) {
            LOG.info("minute {}", n);
        }
        // run the scheduled search
        this.runRaptorScheduled(initialStops, departureTime);
        this.doPropagation(bestNonTransferTimes, scheduledTimesAtTargets, departureTime);
        // walking a block
        for (int i = 0; i < scheduledTimesAtTargets.length; i++) {
            if (nonTransitTimes[i] != UNREACHED && nonTransitTimes[i] + departureTime < scheduledTimesAtTargets[i])
                scheduledTimesAtTargets[i] = nonTransitTimes[i] + departureTime;
        }
        // run the frequency searches
        if (data.hasFrequencies) {
            for (int i = 0; i < monteCarloDraws + 2; i++) {
                // make copies for just this search. We need copies because we can't use dynamic
                // programming/range-raptor with randomized schedules
                int[] bestTimesCopy = Arrays.copyOf(bestTimes, bestTimes.length);
                int[] bestNonTransferTimesCopy = Arrays.copyOf(bestNonTransferTimes, bestNonTransferTimes.length);
                int[] previousPatternsCopy = Arrays.copyOf(previousPatterns, previousPatterns.length);
                // special cases: calculate the best and the worst cases as well
                // Note that this (intentionally) does not affect searches where the user has requested
                // an assumption other than RANDOM, or stops with transfer rules.
                RaptorWorkerTimetable.BoardingAssumption requestedBoardingAssumption = req.boardingAssumption;
                if (i == 0 && req.boardingAssumption == RaptorWorkerTimetable.BoardingAssumption.RANDOM) {
                    req.boardingAssumption = RaptorWorkerTimetable.BoardingAssumption.WORST_CASE;
                    // don't include extrema in averages
                    includeIterationInAverages[iteration] = false;
                } else if (i == 1 && req.boardingAssumption == RaptorWorkerTimetable.BoardingAssumption.RANDOM) {
                    req.boardingAssumption = RaptorWorkerTimetable.BoardingAssumption.BEST_CASE;
                    // don't include extrema in averages
                    includeIterationInAverages[iteration] = false;
                } else if (requestedBoardingAssumption == RaptorWorkerTimetable.BoardingAssumption.RANDOM)
                    // use a new Monte Carlo draw each time
                    // included in averages by default
                    offsets.randomize();
                this.runRaptorFrequency(departureTime, bestTimesCopy, bestNonTransferTimesCopy, previousPatternsCopy);
                req.boardingAssumption = requestedBoardingAssumption;
                // do propagation
                int[] frequencyTimesAtTargets = timesAtTargetsEachIteration[iteration++];
                System.arraycopy(scheduledTimesAtTargets, 0, frequencyTimesAtTargets, 0, scheduledTimesAtTargets.length);
                // updates timesAtTargetsEachIteration directly because it has a reference into the array.
                this.doPropagation(bestNonTransferTimesCopy, frequencyTimesAtTargets, departureTime);
                // convert to elapsed time
                for (int t = 0; t < frequencyTimesAtTargets.length; t++) {
                    if (frequencyTimesAtTargets[t] != UNREACHED)
                        frequencyTimesAtTargets[t] -= departureTime;
                }
            }
        } else {
            final int dt = departureTime;
            timesAtTargetsEachIteration[iteration++] = IntStream.of(scheduledTimesAtTargets).map(i -> i != UNREACHED ? i - dt : i).toArray();
        }
    }
    // iteration should be incremented past end of array by ++ in assignment above
    if (iteration != iterations)
        throw new IllegalStateException("Iterations did not completely fill output array");
    long calcTime = System.currentTimeMillis() - beginCalcTime;
    LOG.info("calc time {}sec", calcTime / 1000.0);
    LOG.info("  propagation {}sec", totalPropagationTime / 1000.0);
    LOG.info("  raptor {}sec", (calcTime - totalPropagationTime) / 1000.0);
    ts.propagation = (int) totalPropagationTime;
    ts.transitSearch = (int) (calcTime - totalPropagationTime);
    // dumpVariableByte(timesAtTargetsEachMinute);
    // we can use min_max here as we've also run it once with best case and worst case board,
    // so the best and worst cases are meaningful.
    propagatedTimesStore.setFromArray(timesAtTargetsEachIteration, includeIterationInAverages, PropagatedTimesStore.ConfidenceCalculationMethod.MIN_MAX);
    return propagatedTimesStore;
}
Also used : TIntIntIterator(gnu.trove.iterator.TIntIntIterator) TIntIntHashMap(gnu.trove.map.hash.TIntIntHashMap) TIntIntMap(gnu.trove.map.TIntIntMap)

Aggregations

TIntIntIterator (gnu.trove.iterator.TIntIntIterator)22 TIntIntMap (gnu.trove.map.TIntIntMap)3 TIntIntHashMap (gnu.trove.map.hash.TIntIntHashMap)2 LocalDate (org.joda.time.LocalDate)2 Test (org.junit.Test)2 TaskStatistics (org.opentripplanner.analyst.cluster.TaskStatistics)2 QualifiedModeSet (org.opentripplanner.api.parameter.QualifiedModeSet)2 FakeGraph (org.opentripplanner.graph_builder.module.FakeGraph)2 ProfileRequest (org.opentripplanner.profile.ProfileRequest)2 RaptorWorkerData (org.opentripplanner.profile.RaptorWorkerData)2 RepeatedRaptorProfileRouter (org.opentripplanner.profile.RepeatedRaptorProfileRouter)2 TraverseModeSet (org.opentripplanner.routing.core.TraverseModeSet)2 Graph (org.opentripplanner.routing.graph.Graph)2 DefaultStreetVertexIndexFactory (org.opentripplanner.routing.impl.DefaultStreetVertexIndexFactory)2 CUtils (cc.redberry.pipe.CUtils)1 OutputPort (cc.redberry.pipe.OutputPort)1 Range (com.milaboratory.core.Range)1 com.milaboratory.core.alignment (com.milaboratory.core.alignment)1 MutationsBuilder (com.milaboratory.core.mutations.MutationsBuilder)1 com.milaboratory.core.sequence (com.milaboratory.core.sequence)1