-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Implement GTFS Flex safe duration spec draft #5796
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
Merged
leonardehrenfried
merged 22 commits into
opentripplanner:dev-2.x
from
ibi-group:flex-duration-factors
May 17, 2024
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
b1ed4ff
Add flex duration factors and offsets
leonardehrenfried 95527bb
Add initial implementation of flex duration factors
leonardehrenfried ea0960a
Remove stop-time-based factors
leonardehrenfried a0eb066
Move factors into calcultor
leonardehrenfried cc76012
Encapsulate factors, add parsing
leonardehrenfried dca331b
Make factors serializable
leonardehrenfried 721845e
Use safe instead of mean values
leonardehrenfried abcfa93
Use correct booking info instances
leonardehrenfried 5066052
Implement duration modifier for ScheduledDeviated trip
leonardehrenfried 90b15cd
Rename and test
leonardehrenfried 41b2e50
Extract class for building flex stop times
leonardehrenfried dec30cd
Remove durationModifier to ScheduledDeviatedTrip
leonardehrenfried 354c968
Add test and docs for DurationModifier
leonardehrenfried 991406e
Cleanup, tests and documentation
leonardehrenfried 6c7d953
Replace DurationModifier with TimePenalty
leonardehrenfried 165e8cf
Merge remote-tracking branch 'upstream/dev-2.x' into flex-duration-fa…
leonardehrenfried 355b287
Update documentation
leonardehrenfried 90148e0
Update docs about experimental fields
leonardehrenfried a4dc25b
Revert renaming
leonardehrenfried 42b36bf
Merge remote-tracking branch 'upstream/dev-2.x' into flex-duration-fa…
leonardehrenfried ea67975
Remove extra collection conversion
leonardehrenfried 695161a
Rename modifiers/factors to time penalty
leonardehrenfried File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/ext-test/java/org/opentripplanner/ext/flex/FlexStopTimesForTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package org.opentripplanner.ext.flex; | ||
|
|
||
| import static org.opentripplanner.model.StopTime.MISSING_VALUE; | ||
|
|
||
| import org.opentripplanner._support.geometry.Polygons; | ||
| import org.opentripplanner.framework.time.TimeUtils; | ||
| import org.opentripplanner.model.StopTime; | ||
| import org.opentripplanner.transit.model._data.TransitModelForTest; | ||
| import org.opentripplanner.transit.model.site.RegularStop; | ||
| import org.opentripplanner.transit.model.site.StopLocation; | ||
|
|
||
| public class FlexStopTimesForTest { | ||
|
|
||
| private static final TransitModelForTest TEST_MODEL = TransitModelForTest.of(); | ||
| private static final StopLocation AREA_STOP = TEST_MODEL.areaStopForTest("area", Polygons.BERLIN); | ||
| private static final RegularStop REGULAR_STOP = TEST_MODEL.stop("stop").build(); | ||
|
|
||
| public static StopTime area(String startTime, String endTime) { | ||
| return area(AREA_STOP, endTime, startTime); | ||
| } | ||
|
|
||
| public static StopTime area(StopLocation areaStop, String endTime, String startTime) { | ||
| var stopTime = new StopTime(); | ||
| stopTime.setStop(areaStop); | ||
| stopTime.setFlexWindowStart(TimeUtils.time(startTime)); | ||
| stopTime.setFlexWindowEnd(TimeUtils.time(endTime)); | ||
| return stopTime; | ||
| } | ||
|
|
||
| public static StopTime regularArrival(String arrivalTime) { | ||
| return regularStopTime(TimeUtils.time(arrivalTime), MISSING_VALUE); | ||
| } | ||
|
|
||
| public static StopTime regularStopTime(String arrivalTime, String departureTime) { | ||
| return regularStopTime(TimeUtils.time(arrivalTime), TimeUtils.time(departureTime)); | ||
| } | ||
|
|
||
| public static StopTime regularStopTime(int arrivalTime, int departureTime) { | ||
| var stopTime = new StopTime(); | ||
| stopTime.setStop(REGULAR_STOP); | ||
| stopTime.setArrivalTime(arrivalTime); | ||
| stopTime.setDepartureTime(departureTime); | ||
| return stopTime; | ||
| } | ||
|
|
||
| public static StopTime regularDeparture(String departureTime) { | ||
| return regularStopTime(MISSING_VALUE, TimeUtils.time(departureTime)); | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
src/ext-test/java/org/opentripplanner/ext/flex/flexpathcalculator/FlexPathTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package org.opentripplanner.ext.flex.flexpathcalculator; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.List; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.Arguments; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
| import org.opentripplanner._support.geometry.LineStrings; | ||
| import org.opentripplanner.routing.api.request.framework.TimePenalty; | ||
|
|
||
| class FlexPathTest { | ||
|
|
||
| private static final int THIRTY_MINS_IN_SECONDS = (int) Duration.ofMinutes(30).toSeconds(); | ||
| private static final FlexPath PATH = new FlexPath( | ||
| 10_000, | ||
| THIRTY_MINS_IN_SECONDS, | ||
| () -> LineStrings.SIMPLE | ||
| ); | ||
|
|
||
| static List<Arguments> cases() { | ||
| return List.of( | ||
| Arguments.of(TimePenalty.ZERO, THIRTY_MINS_IN_SECONDS), | ||
| Arguments.of(TimePenalty.of(Duration.ofMinutes(10), 1), 2400), | ||
| Arguments.of(TimePenalty.of(Duration.ofMinutes(10), 1.5f), 3300), | ||
| Arguments.of(TimePenalty.of(Duration.ZERO, 3), 5400) | ||
| ); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @MethodSource("cases") | ||
| void calculate(TimePenalty mod, int expectedSeconds) { | ||
| var modified = PATH.withDurationModifier(mod); | ||
| assertEquals(expectedSeconds, modified.durationSeconds); | ||
| assertEquals(LineStrings.SIMPLE, modified.getGeometry()); | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
...java/org/opentripplanner/ext/flex/flexpathcalculator/ScheduledFlexPathCalculatorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package org.opentripplanner.ext.flex.flexpathcalculator; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.opentripplanner.ext.flex.FlexStopTimesForTest.area; | ||
| import static org.opentripplanner.ext.flex.FlexStopTimesForTest.regularStopTime; | ||
| import static org.opentripplanner.street.model._data.StreetModelForTest.V1; | ||
| import static org.opentripplanner.street.model._data.StreetModelForTest.V2; | ||
| import static org.opentripplanner.transit.model._data.TransitModelForTest.id; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.List; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.opentripplanner._support.geometry.LineStrings; | ||
| import org.opentripplanner.ext.flex.trip.ScheduledDeviatedTrip; | ||
|
|
||
| class ScheduledFlexPathCalculatorTest { | ||
|
|
||
| private static final ScheduledDeviatedTrip TRIP = ScheduledDeviatedTrip | ||
| .of(id("123")) | ||
| .withStopTimes( | ||
| List.of( | ||
| regularStopTime("10:00", "10:01"), | ||
| area("10:10", "10:20"), | ||
| regularStopTime("10:25", "10:26"), | ||
| area("10:40", "10:50") | ||
| ) | ||
| ) | ||
| .build(); | ||
|
|
||
| @Test | ||
| void calculateTime() { | ||
| var c = (FlexPathCalculator) (fromv, tov, fromStopIndex, toStopIndex) -> | ||
| new FlexPath(10_000, (int) Duration.ofMinutes(10).toSeconds(), () -> LineStrings.SIMPLE); | ||
| var calc = new ScheduledFlexPathCalculator(c, TRIP); | ||
| var path = calc.calculateFlexPath(V1, V2, 0, 1); | ||
| assertEquals(Duration.ofMinutes(19), Duration.ofSeconds(path.durationSeconds)); | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
...-test/java/org/opentripplanner/ext/flex/flexpathcalculator/TimePenaltyCalculatorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package org.opentripplanner.ext.flex.flexpathcalculator; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
|
||
| import java.time.Duration; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.opentripplanner._support.geometry.LineStrings; | ||
| import org.opentripplanner.routing.api.request.framework.TimePenalty; | ||
| import org.opentripplanner.street.model._data.StreetModelForTest; | ||
|
|
||
| class TimePenaltyCalculatorTest { | ||
|
|
||
| private static final int THIRTY_MINS_IN_SECONDS = (int) Duration.ofMinutes(30).toSeconds(); | ||
|
|
||
| @Test | ||
| void calculate() { | ||
| FlexPathCalculator delegate = (fromv, tov, fromStopIndex, toStopIndex) -> | ||
| new FlexPath(10_000, THIRTY_MINS_IN_SECONDS, () -> LineStrings.SIMPLE); | ||
|
|
||
| var mod = TimePenalty.of(Duration.ofMinutes(10), 1.5f); | ||
| var calc = new TimePenaltyCalculator(delegate, mod); | ||
| var path = calc.calculateFlexPath(StreetModelForTest.V1, StreetModelForTest.V2, 0, 5); | ||
| assertEquals(3300, path.durationSeconds); | ||
| } | ||
|
|
||
| @Test | ||
| void nullValue() { | ||
| FlexPathCalculator delegate = (fromv, tov, fromStopIndex, toStopIndex) -> null; | ||
| var mod = TimePenalty.of(Duration.ofMinutes(10), 1.5f); | ||
| var calc = new TimePenaltyCalculator(delegate, mod); | ||
| var path = calc.calculateFlexPath(StreetModelForTest.V1, StreetModelForTest.V2, 0, 5); | ||
| assertNull(path); | ||
| } | ||
| } |
45 changes: 45 additions & 0 deletions
45
src/ext-test/java/org/opentripplanner/ext/flex/trip/UnscheduledDrivingDurationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package org.opentripplanner.ext.flex.trip; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.opentripplanner.street.model._data.StreetModelForTest.V1; | ||
| import static org.opentripplanner.street.model._data.StreetModelForTest.V2; | ||
| import static org.opentripplanner.transit.model._data.TransitModelForTest.id; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.List; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.opentripplanner._support.geometry.LineStrings; | ||
| import org.opentripplanner.ext.flex.FlexStopTimesForTest; | ||
| import org.opentripplanner.ext.flex.flexpathcalculator.FlexPath; | ||
| import org.opentripplanner.ext.flex.flexpathcalculator.FlexPathCalculator; | ||
| import org.opentripplanner.model.StopTime; | ||
| import org.opentripplanner.routing.api.request.framework.TimePenalty; | ||
|
|
||
| class UnscheduledDrivingDurationTest { | ||
|
|
||
| static final FlexPathCalculator STATIC_CALCULATOR = (fromv, tov, fromStopIndex, toStopIndex) -> | ||
| new FlexPath(10_000, (int) Duration.ofMinutes(10).toSeconds(), () -> LineStrings.SIMPLE); | ||
| private static final StopTime STOP_TIME = FlexStopTimesForTest.area("10:00", "18:00"); | ||
|
|
||
| @Test | ||
| void noPenalty() { | ||
| var trip = UnscheduledTrip.of(id("1")).withStopTimes(List.of(STOP_TIME)).build(); | ||
|
|
||
| var calculator = trip.flexPathCalculator(STATIC_CALCULATOR); | ||
| var path = calculator.calculateFlexPath(V1, V2, 0, 0); | ||
| assertEquals(600, path.durationSeconds); | ||
| } | ||
|
|
||
| @Test | ||
| void withPenalty() { | ||
| var trip = UnscheduledTrip | ||
| .of(id("1")) | ||
| .withStopTimes(List.of(STOP_TIME)) | ||
| .withTimePenalty(TimePenalty.of(Duration.ofMinutes(2), 1.5f)) | ||
| .build(); | ||
|
|
||
| var calculator = trip.flexPathCalculator(STATIC_CALCULATOR); | ||
| var path = calculator.calculateFlexPath(V1, V2, 0, 0); | ||
| assertEquals(1020, path.durationSeconds); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.