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

Use millisecond precision in A* traversal #6436

Open
wants to merge 3 commits into
base: dev-2.x
Choose a base branch
from

Conversation

tkalvas
Copy link
Contributor

@tkalvas tkalvas commented Feb 6, 2025

Summary

The existing State handling code in searches updates the time (integer, seconds) by calculating the time used by each step, rounding it up to the nearest integer, and then updating the total. In well-mapped indoor areas, the steps are small, and there are many of them. In our example problem spot, there are 42 edges in 284 metres. This ends up making the total 35 seconds too long. Our transfers try to be well designed, and we have a 7 minute window for that transfer. It takes 287 seconds to walk that distance, and a 90 second transferSlack. We cannot afford inaccuracies of the order of half a minute.

This PR changes the unit of calculation to milliseconds in the actual traversal, and still uses whole numbers. The granularity of time elsewhere in OTP still mostly stays as one second, to avoid a massive change.

The symmetry of backward and forward changes is preserved. When the same trip is searched backward and forward, the duration is exactly the same both in milliseconds and when rounded to seconds. When a forward search is made, the rounded end time is taken as a start time of a reverse search, the rounded end time of the reverse search is the same as the start time of the forward search, given that that had a millisecond part of zero.

Unit tests

In a large number of tests trip durations have become slightly shorter. This accounts for the large amount of changes to tests.

Changelog

This will be a significant change, because the durations have been incorrect for a long time, and people will notice.

Bumping the serialization version id

Unnecessary.

@tkalvas tkalvas requested a review from a team as a code owner February 6, 2025 10:17
@tkalvas tkalvas marked this pull request as draft February 6, 2025 10:17
… backwards are always the same length, to the millisecond, and also when rounded to seconds
Copy link

codecov bot commented Feb 10, 2025

Codecov Report

Attention: Patch coverage is 86.00000% with 7 lines in your changes missing coverage. Please review.

Project coverage is 70.31%. Comparing base (7f5c12c) to head (f9a8e90).
Report is 163 commits behind head on dev-2.x.

Files with missing lines Patch % Lines
...entripplanner/street/search/state/StateEditor.java 55.55% 1 Missing and 3 partials ⚠️
...tripplanner/street/model/edge/ElevatorHopEdge.java 50.00% 1 Missing and 1 partial ⚠️
...org/opentripplanner/street/search/state/State.java 92.85% 1 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##             dev-2.x    #6436      +/-   ##
=============================================
+ Coverage      69.84%   70.31%   +0.47%     
- Complexity     18124    18165      +41     
=============================================
  Files           2069     2058      -11     
  Lines          77268    76952     -316     
  Branches        7855     7772      -83     
=============================================
+ Hits           53965    54107     +142     
+ Misses         20546    20091     -455     
+ Partials        2757     2754       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@tkalvas tkalvas marked this pull request as ready for review February 10, 2025 08:41
@leonardehrenfried leonardehrenfried changed the title change time calculations in State to whole milliseconds to control rounding error Use millisecond precision in A* traversal Feb 10, 2025
@@ -1169,7 +1169,7 @@ private StateEditor doTraverse(State s0, TraverseMode traverseMode, boolean walk
default -> otherTraversalCosts(preferences, traverseMode, walkingBike, speed);
};

int time = (int) Math.ceil(traversalCosts.time());
long time_ms = (long) Math.ceil(1000.0 * traversalCosts.time());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Snake case is only allowed in static values.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's discuss this in relation to StreetEdge.length_mm in the dev meeting. I can see at least three different ways of writing a unit in our code base.

@@ -111,19 +111,19 @@ public State[] traverse(State s0) {
RoutingPreferences preferences = s0.getPreferences();

/* TODO: Consider mode, so that passing through multiple fare gates is not possible */
int time = traversalTime;
long time_ms = 1000L * traversalTime;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No snake case.

// the current time at this state, in seconds since UNIX epoch
protected long time;
// the current time at this state, in milliseconds since UNIX epoch
protected long time_ms;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

@@ -49,7 +49,7 @@ public State[] traverse(State s0) {
var streetPreferences = s0.getPreferences().street();

s1.incrementWeight(streetPreferences.elevator().boardCost());
s1.incrementTimeInSeconds(streetPreferences.elevator().boardTime());
s1.incrementTimeInMilliseconds(1000L * streetPreferences.elevator().boardTime());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the seconds method here.

int seconds = this.travelTime > 0
? this.travelTime
: (int) (preferences.street().elevator().hopTime() * this.levels);
s1.incrementTimeInMilliseconds(1000L * seconds);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the seconds method here.

@leonardehrenfried
Copy link
Member

I think this PR will be a bit easier to review if you use the seconds method wherever possible and only millis where required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants