Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Record values informing error for dwell and travel times separately. #226

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -506,11 +506,13 @@ protected void storeInDbAndLog(ArrivalDeparture arrivalDeparture) {

/* add event to vehicle state. Will increment tripCounter if the last arrival in a trip */
VehicleState vehicleState = VehicleStateManager.getInstance().getVehicleState(arrivalDeparture.getVehicleId());

vehicleState.setEvent(arrivalDeparture);

vehicleState.incrementTripCounter(arrivalDeparture);

// Generate prediction accuracy info as appropriate
PredictionAccuracyModule.handleArrivalDeparture(arrivalDeparture);
PredictionAccuracyModule.handleArrivalDeparture(arrivalDeparture, vehicleState);


}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,17 @@ public static int getMaxPredictionsTimeSecs() {
* Indicates that vehicle is late and now generating predictions
* for a subsequent trip. Use to indicate that the predictions
* are less certain.
* @param predictedDwellTimeTotal
* @param predictedTravelTimeTotal
* @param predictedDwellTimeTotal2
* @param predictedTravelTimeTotal2
* @return The generated Prediction
*/
protected IpcPrediction generatePredictionForStop(AvlReport avlReport,
Indices indices, long predictionTime, boolean useArrivalTimes,
boolean affectedByWaitStop, boolean isDelayed,

boolean lateSoMarkAsUncertain, int tripCounter, Integer scheduleDeviation) {
boolean lateSoMarkAsUncertain, int tripCounter, Integer scheduleDeviation, long predictedTravelTime, long predictedTravelTimeTotal, long predictedDwellTimeTotal) {

// Determine additional parameters for the prediction to be generated

Expand Down Expand Up @@ -188,7 +192,7 @@ protected IpcPrediction generatePredictionForStop(AvlReport avlReport,
return new IpcPrediction(avlReport, stopId, gtfsStopSeq, trip,
predictionTime, predictionTime, indices.atEndOfTrip(),
affectedByWaitStop, isDelayed, lateSoMarkAsUncertain,
ArrivalOrDeparture.ARRIVAL, scheduleDeviation, freqStartTime, tripCounter,vehicleState.isCanceled());
ArrivalOrDeparture.ARRIVAL, scheduleDeviation, freqStartTime, tripCounter,vehicleState.isCanceled(), 0L, predictedTravelTime, predictedDwellTimeTotal, predictedTravelTimeTotal+predictedTravelTime);

} else {

Expand Down Expand Up @@ -305,7 +309,7 @@ protected IpcPrediction generatePredictionForStop(AvlReport avlReport,
indices.atEndOfTrip(), affectedByWaitStop,
isDelayed, lateSoMarkAsUncertain,

ArrivalOrDeparture.DEPARTURE, scheduleDeviation, freqStartTime, tripCounter,vehicleState.isCanceled());
ArrivalOrDeparture.DEPARTURE, scheduleDeviation, freqStartTime, tripCounter,vehicleState.isCanceled(), predictionForUser-predictionTime, predictedTravelTime, predictedDwellTimeTotal+(predictionForUser-predictionTime), predictedTravelTimeTotal+predictedTravelTime);

} else {
// Use the expected departure times, possibly adjusted for
Expand All @@ -315,7 +319,7 @@ protected IpcPrediction generatePredictionForStop(AvlReport avlReport,
indices.atEndOfTrip(), affectedByWaitStop,
isDelayed, lateSoMarkAsUncertain,

ArrivalOrDeparture.DEPARTURE, scheduleDeviation, freqStartTime, tripCounter,vehicleState.isCanceled());
ArrivalOrDeparture.DEPARTURE, scheduleDeviation, freqStartTime, tripCounter,vehicleState.isCanceled(), expectedDepartureTime-predictionTime,predictedTravelTime,predictedDwellTimeTotal+(expectedDepartureTime-predictionTime), predictedTravelTimeTotal+predictedTravelTime);

}
} else {
Expand All @@ -326,7 +330,7 @@ protected IpcPrediction generatePredictionForStop(AvlReport avlReport,
predictionTime + expectedStopTimeMsec,
indices.atEndOfTrip(),
affectedByWaitStop, isDelayed, lateSoMarkAsUncertain,
ArrivalOrDeparture.DEPARTURE, scheduleDeviation, freqStartTime, tripCounter,vehicleState.isCanceled());
ArrivalOrDeparture.DEPARTURE, scheduleDeviation, freqStartTime, tripCounter,vehicleState.isCanceled(), expectedStopTimeMsec, predictedTravelTime, predictedDwellTimeTotal+(expectedStopTimeMsec), predictedTravelTimeTotal+predictedTravelTime);


}
Expand Down Expand Up @@ -373,8 +377,15 @@ public List<IpcPrediction> generate(VehicleState vehicleState) {

// Get time to end of first path and thereby determine prediction for
// first stop.
long predictedTravelTime= expectedTravelTimeFromMatchToEndOfStopPath(avlReport, match);

long predictionTime = avlTime + expectedTravelTimeFromMatchToEndOfStopPath(avlReport, match);
long predictionTime = avlTime + predictedTravelTime;

long predictedTravelTimeTotal=0L;

long predictedDwellTimeTotal=0L;

long predictedDwellTime=0L;

// Determine if vehicle is so late that predictions for subsequent
// trips should be marked as uncertain given that another vehicle
Expand All @@ -401,6 +412,8 @@ public List<IpcPrediction> generate(VehicleState vehicleState) {

// Continue through block until end of block or limit on how far
// into the future should generate predictions reached.
IpcPrediction previousPrediction=null;

while (schedBasedPreds
|| predictionTime <
avlTime + maxPredictionsTimeSecs.getValue() * Time.MS_PER_SEC) {
Expand All @@ -424,10 +437,13 @@ public List<IpcPrediction> generate(VehicleState vehicleState) {

// Determine the new prediction
IpcPrediction predictionForStop = generatePredictionForStop(avlReport,
indices, predictionTime,
indices, predictionTime,
useArrivalPreds, affectedByWaitStop,
vehicleState.isDelayed(), lateSoMarkAsUncertain, tripCounter, delay);
vehicleState.isDelayed(), lateSoMarkAsUncertain, tripCounter, delay, predictedTravelTime, predictedTravelTimeTotal, predictedDwellTimeTotal);

predictedDwellTimeTotal=predictionForStop.getTotalPredictedDwellTime();
predictedTravelTimeTotal=predictionForStop.getTotalPredictedTravelTime();


if((predictionForStop.getPredictionTime()-Core.getInstance().getSystemTime())<generateHoldingTimeWhenPredictionWithin.getValue() &&
(predictionForStop.getPredictionTime()-Core.getInstance().getSystemTime())>0)
Expand Down Expand Up @@ -518,10 +534,18 @@ public List<IpcPrediction> generate(VehicleState vehicleState) {
// getActualPredictionTime() instead of getPredictionTime() to
// handle situations where want to display to the user for wait
// stops schedule times instead of the calculated prediction time.
predictionTime = predictionForStop.getActualPredictionTime();
long actualPredictionTime = predictionForStop.getActualPredictionTime();

if(predictedDwellTimeTotal<0&&predictedDwellTime<0)
logger.error("Total dwell is less than zero.");

predictionTime = actualPredictionTime;

if (predictionForStop.isArrival())
{
predictionTime += getStopTimeForPath(indices, avlReport, vehicleState);
{

predictionTime += getStopTimeForPath(indices, avlReport, vehicleState);;

/* TODO this is where we should take account of holding time */
if(useHoldingTimeInPrediction.getValue() && HoldingTimeGeneratorFactory.getInstance()!=null)
{
Expand All @@ -538,6 +562,10 @@ public List<IpcPrediction> generate(VehicleState vehicleState) {

}
}

if(predictedDwellTime>0)
logger.info("These should be mostly over 0");

indices.incrementStopPath(predictionTime);
// If reached end of block then done
if (indices.pastEndOfBlock(predictionTime)) {
Expand All @@ -549,9 +577,18 @@ public List<IpcPrediction> generate(VehicleState vehicleState) {
boolean isCircuitRoute=true;
// Add in travel time for the next path to get to predicted
// arrival time of this stop
if (!lastStopOfNonSchedBasedTrip && isCircuitRoute) {
predictionTime += getTravelTimeForPath(indices, avlReport, vehicleState);
}
if (!lastStopOfNonSchedBasedTrip && isCircuitRoute) {
predictedTravelTime= getTravelTimeForPath(indices, avlReport, vehicleState);

predictionTime += predictedTravelTime;
}

//predictedTravelTimeTotal=predictedTravelTimeTotal+predictedTravelTime;

if(predictedTravelTime>0)
logger.info("These should be mostly over 0");


}

for(IpcPrediction prediction : filteredPredictions.values()){
Expand All @@ -565,6 +602,8 @@ public List<IpcPrediction> generate(VehicleState vehicleState) {
}




public long getTravelTimeForPath(Indices indices, AvlReport avlReport, VehicleState vehicleState)
{
//logger.debug("Using transiTime default algorithm for travel time prediction : " + indices + " Value: "+indices.getTravelTimeForPath());
Expand Down
42 changes: 40 additions & 2 deletions transitclock/src/main/java/org/transitclock/core/VehicleState.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public class VehicleState {
//Used for schedPred AVL. Identify if trip is canceled.
private boolean isCanceled;

private LinkedList<ArrivalDeparture> eventHistory= new LinkedList<ArrivalDeparture> ();

public Headway getHeadway() {
return headway;
Expand Down Expand Up @@ -245,7 +246,38 @@ public void setMatch(TemporalMatch match) {
temporalMatchHistory.removeLast();
}
}

/**
* Return the last event.
* @return
*/
public ArrivalDeparture getEvent()
{
try {
return eventHistory.getFirst();
} catch (NoSuchElementException e) {
return null;
}
}
/**
*
* @param event The event you want to find the preceding event for.
* Return event for this vehicle that happened just before the one passed in.
* @return
*/
public ArrivalDeparture getPreviousEvent(ArrivalDeparture event)
{
for(ArrivalDeparture currentEvent:eventHistory)
{
// Events are store in order with the the latest first.
if(!currentEvent.equals(event) && currentEvent.getTime()<event.getTime())
{
return currentEvent;
}
}
return null;
}


/**
* Returns the last temporal match. Returns null if there isn't one.
* @return
Expand Down Expand Up @@ -352,7 +384,13 @@ public int numberOfBadMatches() {
public boolean lastMatchIsValid() {
return numberOfBadMatches == 0;
}

public void setEvent(ArrivalDeparture event)
{
eventHistory.addFirst(event);
while (eventHistory.size() > CoreConfig.getEventHistoryMaxSize()) {
eventHistory.removeLast();
}
}
/**
* Stores the specified avlReport into the history for the vehicle.
* Makes sure that the AVL history doesn't exceed maximum size.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ private void processExternalPredictionsForRoute(
// Store in memory the prediction based on absolute time
PredAccuracyPrediction pred = new PredAccuracyPrediction(
routeId, directionId, stopId, tripId, vehicleId,
predictedTime, predictionsReadTime, isArrival,
affectedByWaitStop, "NextBus",null,null);
predictedTime, predictionsReadTime, null,isArrival,
affectedByWaitStop, "NextBus",null,null, null, null);
storePrediction(pred);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ public class PredAccuracyPrediction {
// The time the prediction was read. This allows us to determine
// how far out into the future the prediction is for.
private final Date predictionReadTime;
private final Date avlTime;
private final String scheduledTime;
private final boolean isArrival;
// affectedByWaitStop is a Boolean so that null can represent "don't know"
private final Boolean affectedByWaitStop;
private final String source;
private final String algorithm;
private final Long totalPredictedDwellTime;
private final Long totalPredictedTravelTime;


/********************** Member Functions **************************/

Expand Down Expand Up @@ -72,9 +76,9 @@ public class PredAccuracyPrediction {
* This is the algorithm used to generate prediction.
*/
public PredAccuracyPrediction(String routeId, String directionId,
String stopId, String tripId, String vehicleId, Date predictedTime,
Date predictionReadTime, boolean isArrival,
Boolean affectedByWaitStop, String source, String algorithm, String scheduledTime) {
String stopId, String tripId, String vehicleId, Date predictedTime,
Date predictionReadTime, Date avlTime, boolean isArrival,
Boolean affectedByWaitStop, String source, String algorithm, String scheduledTime, Long totalPredictedDwellTime, Long totalPredictedTravelTime) {
super();
this.routeId = routeId;
this.directionId = directionId;
Expand All @@ -88,6 +92,9 @@ public PredAccuracyPrediction(String routeId, String directionId,
this.source = source;
this.scheduledTime = scheduledTime;
this.algorithm = algorithm;
this.totalPredictedDwellTime=totalPredictedDwellTime;
this.totalPredictedTravelTime=totalPredictedTravelTime;
this.avlTime=avlTime;
}

public String getAlgorithm() {
Expand Down Expand Up @@ -139,24 +146,27 @@ public Boolean isAffectedByWaitStop() {
return affectedByWaitStop;
}


public Long getTotalPredictedDwellTime() {
return totalPredictedDwellTime;
}

public Long getTotalPredictedTravelTime() {
return totalPredictedTravelTime;
}

public Date getAvlTime() {
return avlTime;
}

@Override
public String toString() {
return "PredAccuracyPrediction ["
+ "routeId=" + routeId
+ ", directionId=" + directionId
+ ", stopId=" + stopId
+ ", tripId=" + tripId
+ ", vehicleId=" + vehicleId
+ ", predictedTime=" + predictedTime
+ ", predictionReadTime=" + predictionReadTime
+ ", predictionLengthMsec=" +
(predictedTime.getTime() - predictionReadTime.getTime())
+ ", isArrival=" + isArrival
+ ", affectedByWaitStop=" + affectedByWaitStop
+ ", source=" + source
+ ", algorithm=" + algorithm
+ ", scheduleTime=" + scheduledTime
+ "]";
return "PredAccuracyPrediction [routeId=" + routeId + ", directionId=" + directionId + ", stopId=" + stopId
+ ", tripId=" + tripId + ", vehicleId=" + vehicleId + ", predictedTime=" + predictedTime
+ ", predictionReadTime=" + predictionReadTime + ", avlTime=" + avlTime + ", scheduledTime="
+ scheduledTime + ", isArrival=" + isArrival + ", affectedByWaitStop=" + affectedByWaitStop
+ ", source=" + source + ", algorithm=" + algorithm + ", totalPredictedDwellTime="
+ totalPredictedDwellTime + ", totalPredictedTravelTime=" + totalPredictedTravelTime + "]";
}

public String getScheduledTime() {
Expand Down
Loading