Skip to content

Commit

Permalink
Ignire negative travel-times in Raptor - skip trip alight
Browse files Browse the repository at this point in the history
  • Loading branch information
t2gran committed Oct 20, 2023
1 parent 3eb9909 commit b2f4bd7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/workflows/performance-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- dev-2.x
- otp2_raptor_ignore_neg_trip_times

jobs:
perf-test:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.opentripplanner.raptor.spi.RaptorBoardOrAlightEvent;
import org.opentripplanner.raptor.spi.RaptorConstrainedBoardingSearch;
import org.opentripplanner.raptor.spi.RaptorRoute;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The purpose of this class is to implement a routing strategy for finding the best arrival-time.
Expand All @@ -27,12 +29,15 @@
public final class ArrivalTimeRoutingStrategy<T extends RaptorTripSchedule>
implements RoutingStrategy<T> {

private static final Logger LOG = LoggerFactory.getLogger(ArrivalTimeRoutingStrategy.class);

private static final int NOT_SET = -1;

private final StdWorkerState<T> state;
private final TimeBasedBoardingSupport<T> boardingSupport;
private final TransitCalculator<T> calculator;

private int logCount = 0;
private int onTripIndex;
private int onTripBoardTime;
private int onTripBoardStop;
Expand Down Expand Up @@ -66,7 +71,14 @@ public void prepareForTransitWith(RaptorRoute<T> route) {
public void alightOnlyRegularTransferExist(int stopIndex, int stopPos, int alightSlack) {
if (onTripIndex != UNBOUNDED_TRIP_INDEX) {
final int stopArrivalTime = calculator.stopArrivalTime(onTrip, stopPos, alightSlack);
state.transitToStop(stopIndex, stopArrivalTime, onTripBoardStop, onTripBoardTime, onTrip);

// TODO: Make sure that the TimeTables can not have negative trip times, then
// this check can be removed.
if (calculator.isBefore(stopArrivalTime, onTripBoardTime)) {
logInvalidAlightTime(stopPos, stopArrivalTime);
} else {
state.transitToStop(stopIndex, stopArrivalTime, onTripBoardStop, onTripBoardTime, onTrip);
}
}
}

Expand Down Expand Up @@ -125,4 +137,17 @@ private int prevArrivalTime(int stopIndex) {
private TransitArrival<T> previousTransitArrival(int boardStopIndex) {
return state.previousTransit(boardStopIndex);
}

private void logInvalidAlightTime(int stopPos, int stopArrivalTime) {
if (logCount < 3) {
++logCount;
LOG.error(
"Traveling back in time is not allowed. Board stop pos: {}, alight stop pos: {}, stop arrival time: {}, trip: {}.",
onTrip.findDepartureStopPosition(onTripBoardTime, onTripBoardStop),
stopPos,
stopArrivalTime,
onTrip
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

import org.opentripplanner.raptor.api.model.RaptorAccessEgress;
import org.opentripplanner.raptor.api.model.RaptorTripSchedule;
import org.opentripplanner.raptor.api.model.TransitArrival;
import org.opentripplanner.raptor.rangeraptor.internalapi.RoutingStrategy;
import org.opentripplanner.raptor.rangeraptor.internalapi.WorkerLifeCycle;
import org.opentripplanner.raptor.rangeraptor.support.TimeBasedBoardingSupport;
import org.opentripplanner.raptor.rangeraptor.transit.TransitCalculator;
import org.opentripplanner.raptor.spi.RaptorBoardOrAlightEvent;
import org.opentripplanner.raptor.spi.RaptorConstrainedBoardingSearch;
import org.opentripplanner.raptor.spi.RaptorRoute;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The purpose of this class is to implement a routing strategy for finding the best minimum travel
Expand All @@ -29,12 +30,15 @@
public final class MinTravelDurationRoutingStrategy<T extends RaptorTripSchedule>
implements RoutingStrategy<T> {

private static final Logger LOG = LoggerFactory.getLogger(MinTravelDurationRoutingStrategy.class);

private static final int NOT_SET = -1;

private final StdWorkerState<T> state;
private final TimeBasedBoardingSupport<T> boardingSupport;
private final TransitCalculator<T> calculator;

private int logCount = 0;
private int onTripIndex;
private int onTripBoardTime;
private int onTripBoardStop;
Expand Down Expand Up @@ -113,10 +117,16 @@ private void alight(int stopIndex, int stopPos, int alightSlackApplied) {

// Remove the wait time from the arrival-time. We don´t need to use the transit
// calculator because of the way we compute the time-shift. It is positive in the case
// of a forward-search and negative int he case of a reverse-search.
// of a forward-search and negative in the case of a reverse-search.
final int stopArrivalTime = stopArrivalTime0 - onTripTimeShift;

state.transitToStop(stopIndex, stopArrivalTime, onTripBoardStop, onTripBoardTime, onTrip);
// TODO: Make sure that the TimeTables can not have negative trip times, then
// this check can be removed.
if (calculator.isBefore(stopArrivalTime, onTripBoardTime)) {
logInvalidAlightTime(stopPos, stopArrivalTime);
} else {
state.transitToStop(stopIndex, stopArrivalTime, onTripBoardStop, onTripBoardTime, onTrip);
}
}
}

Expand Down Expand Up @@ -168,7 +178,16 @@ private int prevArrivalTime(int stopIndex) {
return state.bestTimePreviousRound(stopIndex);
}

private TransitArrival<T> previousTransitArrival(int boardStopIndex) {
return state.previousTransit(boardStopIndex);
private void logInvalidAlightTime(int stopPos, int stopArrivalTime) {
if (logCount < 3) {
++logCount;
LOG.error(
"Traveling back in time is not allowed. Board stop pos: {}, alight stop pos: {}, stop arrival time: {}, trip: {}.",
onTrip.findDepartureStopPosition(onTripBoardTime, onTripBoardStop),
stopPos,
stopArrivalTime,
onTrip
);
}
}
}

0 comments on commit b2f4bd7

Please sign in to comment.