Search in sources :

Example 16 with TIntList

use of gnu.trove.list.TIntList in project OpenTripPlanner by opentripplanner.

the class TNPropagatedTimesStore method setFromArray.

/**
 * @param times for search (varying departure time), an array of travel times to each transit stop.
 */
public void setFromArray(int[][] times, ConfidenceCalculationMethod confidenceCalculationMethod) {
    if (times.length == 0)
        // nothing to do
        return;
    // assume array is rectangular
    int stops = times[0].length;
    // cache random numbers. This should be fine as we're mixing it with the number of minutes
    // at which each destination is accessible, which is sometimes not 120, as well as the stop
    // position in the list (note that we have cleverly chosen a number which is a prime
    // so is not divisible by the number of iterations on the bootstrap). Finally recall that
    // the maximum number of times we're sampling from is generally 120 and we modulo this,
    // so the pigeonhole principle applies.
    // this is effectively a "random number generator" with phase 10007
    int[] randomNumbers = random.ints().limit(10007).map(Math::abs).toArray();
    int nextRandom = 0;
    // loop over stops on the outside so we can bootstrap
    STOPS: for (int stop = 0; stop < stops; stop++) {
        // compute the average
        int sum = 0;
        int count = 0;
        TIntList timeList = new TIntArrayList();
        ITERATIONS: for (int i = 0; i < times.length; i++) {
            if (times[i][stop] == RaptorWorker.UNREACHED)
                continue ITERATIONS;
            sum += times[i][stop];
            count++;
            timeList.add(times[i][stop]);
        }
        if (count == 0)
            continue STOPS;
        avgs[stop] = sum / count;
        switch(confidenceCalculationMethod) {
            case BOOTSTRAP:
                // now bootstrap out a 95% confidence interval on the time
                int[] bootMeans = new int[N_BOOTSTRAPS];
                for (int boot = 0; boot < N_BOOTSTRAPS; boot++) {
                    int bsum = 0;
                    // sample from the Monte Carlo distribution with replacement
                    for (int iter = 0; iter < count; iter++) {
                        bsum += timeList.get(randomNumbers[nextRandom++ % randomNumbers.length] % count);
                    // bsum += timeList.get(random.nextInt(count));
                    }
                    bootMeans[boot] = bsum / count;
                }
                Arrays.sort(bootMeans);
                // 2.5 percentile of distribution of means
                mins[stop] = bootMeans[N_BOOTSTRAPS / 40];
                // 97.5 percentile of distribution of means
                maxs[stop] = bootMeans[N_BOOTSTRAPS - N_BOOTSTRAPS / 40];
                break;
            case PERCENTILE:
                timeList.sort();
                mins[stop] = timeList.get(timeList.size() / 40);
                maxs[stop] = timeList.get(39 * timeList.size() / 40);
                break;
            case NONE:
                mins[stop] = maxs[stop] = avgs[stop];
                break;
            case MIN_MAX:
            default:
                mins[stop] = timeList.min();
                maxs[stop] = timeList.max();
                break;
        }
    }
}
Also used : TIntList(gnu.trove.list.TIntList) TIntArrayList(gnu.trove.list.array.TIntArrayList)

Example 17 with TIntList

use of gnu.trove.list.TIntList in project OpenTripPlanner by opentripplanner.

the class TimeTracker method add.

/**
 * Add a variable number of int arguments or an array of ints to the bag for a given stopcluster.
 * Optionally sort all the entries after a bulk add.
 */
public void add(StopCluster stopCluster, boolean sort, int... time) {
    TIntList times = timesForCluster.get(stopCluster);
    if (times == null) {
        times = new TIntArrayList();
        timesForCluster.put(stopCluster, times);
    }
    times.add(time);
    if (sort) {
        times.sort();
    }
}
Also used : TIntList(gnu.trove.list.TIntList) TIntArrayList(gnu.trove.list.array.TIntArrayList)

Example 18 with TIntList

use of gnu.trove.list.TIntList in project OpenTripPlanner by opentripplanner.

the class TimeTracker method dump.

public void dump() {
    for (StopCluster stopCluster : timesForCluster.keySet()) {
        TIntList ints = timesForCluster.get(stopCluster);
        System.out.printf("%s --> %s \n", stopCluster.toString(), ints.toString());
    }
}
Also used : TIntList(gnu.trove.list.TIntList)

Example 19 with TIntList

use of gnu.trove.list.TIntList in project OpenTripPlanner by opentripplanner.

the class Ride method calcStatsForTransfer.

/**
 * Calculates Stats for the transfer to the given ride from the previous ride.
 * This should only be called after all PatternRides have been added to the ride.
 * Distances can be stored in rides, including the first and last distance. But waits must be
 * calculated from full sets of patterns, which are not known until a round is over.
 */
public Stats calcStatsForTransfer(TimeWindow window, double walkSpeed) {
    TIntList arrivals = previous.getSortedStoptimes(window, true);
    TIntList departures = this.getSortedStoptimes(window, false);
    List<Integer> waits = Lists.newArrayList();
    TIntIterator departureIterator = departures.iterator();
    int departure = departureIterator.next();
    ARRIVAL: for (TIntIterator arrivalsIterator = arrivals.iterator(); arrivalsIterator.hasNext(); ) {
        int arrival = arrivalsIterator.next();
        // On transfers the access stats should have max=min=avg
        // We use the min, which would be best if min != max since it should only relax the bounds somewhat.
        int boardTime = arrival + accessStats.min + ProfileRouter.SLACK;
        while (departure <= boardTime) {
            if (!departureIterator.hasNext())
                break ARRIVAL;
            departure = departureIterator.next();
        }
        waits.add(departure - boardTime);
    }
    // Impossible to make this transfer.
    if (waits.isEmpty())
        return null;
    return new Stats(waits);
}
Also used : TIntIterator(gnu.trove.iterator.TIntIterator) TIntList(gnu.trove.list.TIntList)

Example 20 with TIntList

use of gnu.trove.list.TIntList in project OpenTripPlanner by opentripplanner.

the class Ride method calcStatsForBoarding.

/**
 * Produce stats about boarding an initial Ride, which has no previous ride.
 * This assumes arrival times are uniformly distributed during the window.
 * The Ride must contain some trips, and the window must have a positive duration.
 */
public Stats calcStatsForBoarding(TimeWindow window) {
    Stats stats = new Stats();
    // You can always arrive just before a train departs.
    stats.min = 0;
    TIntList departures = getSortedStoptimes(window, false);
    int last = window.from;
    double avgAccumulated = 0.0;
    /* All departures in the list are known to be running and within the window. */
    for (TIntIterator it = departures.iterator(); it.hasNext(); ) {
        int dep = it.next();
        int maxWait = dep - last;
        if (maxWait > stats.max)
            stats.max = maxWait;
        /* Weight the average of each interval by the number of seconds it contains. */
        avgAccumulated += (maxWait / 2.0) * maxWait;
        stats.num += maxWait;
        last = dep;
    }
    if (stats.num > 0) {
        stats.avg = (int) (avgAccumulated / stats.num);
    }
    return stats;
}
Also used : TIntIterator(gnu.trove.iterator.TIntIterator) TIntList(gnu.trove.list.TIntList)

Aggregations

TIntList (gnu.trove.list.TIntList)35 TIntArrayList (gnu.trove.list.array.TIntArrayList)21 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 List (java.util.List)5 TFloatList (gnu.trove.list.TFloatList)4 ItemStack (net.minecraft.item.ItemStack)4 BlockPos (net.minecraft.util.math.BlockPos)4 Vector2f (org.terasology.math.geom.Vector2f)4 Vector3i (org.terasology.math.geom.Vector3i)4 TIntIterator (gnu.trove.iterator.TIntIterator)3 TShortObjectHashMap (gnu.trove.map.hash.TShortObjectHashMap)3 InputStream (java.io.InputStream)3 Arrays (java.util.Arrays)3 Vector3f (org.terasology.math.geom.Vector3f)3 Lists (com.google.common.collect.Lists)2 TFloatArrayList (gnu.trove.list.array.TFloatArrayList)2 IvMutableBlockPos (ivorius.ivtoolkit.blocks.IvMutableBlockPos)2 IntegerRange (ivorius.ivtoolkit.gui.IntegerRange)2 Consumer (java.util.function.Consumer)2