Search in sources :

Example 81 with DoubleArrayList

use of cern.colt.list.DoubleArrayList in project onebusaway-application-modules by camsys.

the class RealTimeHistoryServiceImpl method sampleScheduleDeviationsForVehicle.

@Override
public ScheduleDeviationSamples sampleScheduleDeviationsForVehicle(BlockInstance instance, VehicleLocationRecord record, ScheduledBlockLocation scheduledBlockLocation) {
    if (scheduledBlockLocation == null)
        return null;
    BlockTripEntry blockTrip = scheduledBlockLocation.getActiveTrip();
    TripEntry trip = blockTrip.getTrip();
    ScheduleDeviationHistory history = _scheduleDeviationHistoryDao.getScheduleDeviationHistoryForTripId(trip.getId());
    if (history == null)
        return null;
    ScheduleDeviationHistory resampledHistory = resampleHistory(history, scheduledBlockLocation.getScheduledTime(), record.getScheduleDeviation());
    DoubleArrayList scheduleTimes = new DoubleArrayList();
    DoubleArrayList mus = new DoubleArrayList();
    DoubleArrayList sigmas = new DoubleArrayList();
    for (int t = 0; t <= _predictionLookahead; t += 5 * 60) {
        int scheduleTime = scheduledBlockLocation.getScheduledTime() + t;
        double[] deviations = getScheduleDeviationsForScheduleTime(resampledHistory, scheduleTime);
        deviations = noNans(deviations);
        DoubleArrayList values = new DoubleArrayList(deviations);
        double mu = Descriptive.mean(values);
        double var = Descriptive.sampleVariance(values, mu);
        double sigma = Descriptive.sampleStandardDeviation(values.size(), var);
        scheduleTimes.add(scheduleTime);
        mus.add(mu);
        sigmas.add(sigma);
    }
    scheduleTimes.trimToSize();
    mus.trimToSize();
    sigmas.trimToSize();
    return new ScheduleDeviationSamples(scheduleTimes.elements(), mus.elements(), sigmas.elements());
}
Also used : BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) ScheduleDeviationSamples(org.onebusaway.transit_data_federation.services.realtime.ScheduleDeviationSamples) TripEntry(org.onebusaway.transit_data_federation.services.transit_graph.TripEntry) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) DoubleArrayList(cern.colt.list.DoubleArrayList)

Example 82 with DoubleArrayList

use of cern.colt.list.DoubleArrayList in project onebusaway-application-modules by camsys.

the class CurrentVehicleEstimationServiceImpl method computeCumulativeProbabilityForScheduledBlockLocations.

private void computeCumulativeProbabilityForScheduledBlockLocations(List<Record> records, BlockInstance blockInstance, double minProbabilityForConsideration, List<CurrentVehicleEstimateBean> beans) {
    DoubleArrayList ps = new DoubleArrayList();
    List<ScheduledBlockLocation> blockLocations = new ArrayList<ScheduledBlockLocation>();
    Record firstRecord = records.get(0);
    ScheduledBlockLocation firstLocation = _blockGeospatialService.getBestScheduledBlockLocationForLocation(blockInstance, firstRecord.getLocation(), firstRecord.getTimestamp(), 0, Double.POSITIVE_INFINITY);
    blockLocations.add(firstLocation);
    ps.add(updateScheduledBlockLocationProbability(blockInstance, firstRecord, firstLocation));
    Record lastRecord = records.get(records.size() - 1);
    ScheduledBlockLocation lastLocation = _blockGeospatialService.getBestScheduledBlockLocationForLocation(blockInstance, lastRecord.getLocation(), lastRecord.getTimestamp(), 0, Double.POSITIVE_INFINITY);
    ps.add(updateScheduledBlockLocationProbability(blockInstance, lastRecord, lastLocation));
    if (Descriptive.mean(ps) < minProbabilityForConsideration)
        return;
    /**
     * If the vehicle is traveling backwards in time, we kill the prediction
     */
    int maxTravelBackwardsTime = computeMaxTravelBackwardsTime(lastRecord.getTimestamp() - firstRecord.getTimestamp());
    if (lastLocation.getScheduledTime() < firstLocation.getScheduledTime() - maxTravelBackwardsTime)
        return;
    double minDistanceAlongBlock = Math.min(firstLocation.getDistanceAlongBlock(), lastLocation.getDistanceAlongBlock()) - 500;
    double maxDistanceAlongBlock = Math.max(firstLocation.getDistanceAlongBlock(), lastLocation.getDistanceAlongBlock()) + 500;
    for (int i = 1; i < records.size() - 1; i++) {
        Record record = records.get(i);
        ScheduledBlockLocation location = _blockGeospatialService.getBestScheduledBlockLocationForLocation(blockInstance, record.getLocation(), record.getTimestamp(), minDistanceAlongBlock, maxDistanceAlongBlock);
        blockLocations.add(location);
        ps.add(updateScheduledBlockLocationProbability(blockInstance, record, location));
        if (Descriptive.mean(ps) < minProbabilityForConsideration)
            return;
    }
    blockLocations.add(lastLocation);
    updateProbabilitiesWithScheduleDeviations(records, blockLocations, ps);
    BlockLocation location = _blockLocationService.getLocationForBlockInstanceAndScheduledBlockLocation(blockInstance, lastLocation, lastRecord.getTimestamp());
    double mu = Descriptive.mean(ps);
    String debug = asString(ps);
    addResult(location, mu, debug, minProbabilityForConsideration, beans);
}
Also used : ScheduledBlockLocation(org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation) DoubleArrayList(cern.colt.list.DoubleArrayList) ArrayList(java.util.ArrayList) Record(org.onebusaway.transit_data.model.realtime.CurrentVehicleEstimateQueryBean.Record) DoubleArrayList(cern.colt.list.DoubleArrayList) ScheduledBlockLocation(org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation) BlockLocation(org.onebusaway.transit_data_federation.services.realtime.BlockLocation) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint)

Aggregations

DoubleArrayList (cern.colt.list.DoubleArrayList)82 RegressionResult (edu.cmu.tetrad.regression.RegressionResult)11 ArrayList (java.util.ArrayList)9 AndersonDarlingTest (edu.cmu.tetrad.data.AndersonDarlingTest)8 IntArrayList (cern.colt.list.IntArrayList)6 DenseDoubleMatrix2D (cern.colt.matrix.impl.DenseDoubleMatrix2D)5 TetradVector (edu.cmu.tetrad.util.TetradVector)5 Test (org.junit.Test)5 DoubleMatrix2D (cern.colt.matrix.DoubleMatrix2D)4 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)4 DoubleMatrix1D (cern.colt.matrix.DoubleMatrix1D)3 DenseDoubleMatrix1D (cern.colt.matrix.impl.DenseDoubleMatrix1D)3 Regression (edu.cmu.tetrad.regression.Regression)3 RegressionDataset (edu.cmu.tetrad.regression.RegressionDataset)3 StopWatch (org.apache.commons.lang3.time.StopWatch)2 CoordinatePoint (org.onebusaway.geospatial.model.CoordinatePoint)2 Record (org.onebusaway.transit_data.model.realtime.CurrentVehicleEstimateQueryBean.Record)2 ScheduledBlockLocation (org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation)2 BlockLocation (org.onebusaway.transit_data_federation.services.realtime.BlockLocation)2 ByteArrayConverter (ubic.basecode.io.ByteArrayConverter)2