Skip to content

Fix transit egress costs and destination geometry in detailed itineraries - #607

Merged
rafapereirabr merged 17 commits into
ipea:masterfrom
e-kotov:fix/detailed-itinerary-egress
Oct 26, 2026
Merged

rafapereirabr merged 17 commits into
ipea:masterfrom
e-kotov:fix/detailed-itinerary-egress

Conversation

@e-kotov

@e-kotov e-kotov commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Additional walking-cost regression check

Test-only follow-up 22a55b5c adds a comparison of each known walking egress leg with a separate forward walk from its reported starting coordinate, plus the destination's off-street walking connection. Both calls explicitly use walk_speed = 3.6 (1 m/s); snap/route success is asserted and tolerance is 0.15 minutes. This is an approximate check for the two known POA fixtures, not a general equality rule across modes or cost models. It adds no production, bridge, helper, fixture or workflow changes.

Fresh Actions run 40311691843 passed all eight jobs at 22a55b5c2f8997b2a84681857a29ed765a047f54. All six package checks report Status: OK; Java build and coverage also passed. The new 22 assertions passed across the full platform tests: Linux/macOS report FAIL 0 | WARN 39 | SKIP 0 | PASS 2338, Windows FAIL 0 | WARN 39 | SKIP 1 | PASS 2337. The existing 39 warnings and Windows Unix-only skip are unchanged.

The bridge JAR is identical to the preceding implementation (a4cf9a7560a57faa31ef89fc0f77376bc4dfd6def7b408600c21dcd63527cf7b); rebuilding produced the same hash. The separate mutation review motivated this addition; its mutation runs were not repeated in this Actions run. The validation below records the preceding implementation snapshot and retired historical audit.

Summary

Fix the destination street leg in detailed_itineraries() for both RAPTOR (fastest trip without fares, #603) and McRAPTOR (alternatives/fares), including FastRaptor searches shared across destinations.

Reverse point-origin seeding in pinned R5 v7.6 pairs partial costs with the wrong endpoint, and State.reverse() drops the initial partial-edge/off-street cost. This patch corrects the seeds and reconstructs the clipped terminal street plus the connection to the requested destination. Selection, limits and output use the same complete directed cost.

Changes after maintainer review

Responding to Rafael's review:

  • Bicycle split seeds first use ordinary traversal, including walk-the-bike fallback. If a restricted-LTS bicycle probe fails, retry that partial with a cloned request at LTS4. This preserves R5's permissive point connection without changing the request's LTS limit for subsequent street traversal; normal permissions and deleted-edge checks still apply.
  • Truncate proportional partial and off-street costs to whole seconds using native setOrigin's convention. Partial cost still uses R5's average directed-edge adjustment; this is not the pointset base-speed model or exact integration of a clipped elevation profile.
  • Report the actual destination latitude/longitude when no permitted connection exists, including the shared FastRaptor path.
  • A dedicated invalid-origin exception is logged and only that itinerary is omitted. Other exceptions remain visible.
  • Removed the manual baseline/300-pair workflow, audit script/CSV, custom Gradle source set/check dependency and synthetic Java fixture. Replaced the R helper and tests with one compact POA testthat block: both algorithms, walking examples and bicycle destination 89a901291b7ffff at LTS2/4; actual transit reachability, destination endpoint, summed legs/waits and an adaptive tighter cutoff.

The existing geometry reconstruction and its explicit directed/self-loop root validation remain. Reusing StreetPath would still require restoring root cost and appending the directed terminal partial; the reviewed explicit helper was retained instead of taking that optional rewrite. State.reverse() sums existing increments; it does not re-time edges.

Earlier implementation validation

Fresh Actions run 40237179437 passed all eight current jobs at source commit 87e7e694b37a415c01980de183c4c5b00e6c3a74. Actions generated the bridge in 193945b24e045f9ea23e0e6ee418a0175204a9e1, now pulled into the branch.

  • Five platform package checks (Windows, macOS and Linux release/devel/oldrel) and the CRAN-mode check each report Status: OK. Java build and coverage passed.
  • The compact POA block ran with zero failures, including actual transit reachability at bicycle LTS2/4 for both algorithms, complete destination endpoints, leg sums and tighter-cutoff minimum equality.
  • Full test logs: Linux/macOS FAIL 0 | WARN 39 | SKIP 0 | PASS 2316; Windows FAIL 0 | WARN 39 | SKIP 1 | PASS 2315 (one Unix-only test skipped). The warnings remain in unchanged scenario/progress/verbosity tests; they are not package-check warnings.
  • CI-built bridge SHA256: a4cf9a7560a57faa31ef89fc0f77376bc4dfd6def7b408600c21dcd63527cf7b, matching the pulled artifact.

The subsequent cleanup commit 0aeb6d5c only restores build.gradle byte-for-byte to master, removing its trailing-newline-only diff. Production sources, tests and the CI-built JAR are unchanged from the validated snapshot, so that cleanup skips duplicate CI.

All builds and R/Java execution take place in Actions. This new run validates the revised implementation; the retired audit below is historical evidence.

Historical evidence (before this revision)

Run 40035176855 passed all nine then-present jobs against upstream 3dd6edaf, source 5a76d1bf, CI bridge acdd9c91. The native original behavior failed 14/16 physical assertions; the correction passed 51/51. Loaded bridge hashes and actual 4 GiB heaps were checked in isolated original/corrected installs.

Both algorithms passed the same 300 paired POA ODs at 53/60/90/120 minutes: zero missing or worse lower-cutoff minima across six comparisons per algorithm, when the higher-cutoff minimum was at least 0.1 minute below the lower cutoff. Reachable pairs: McRAPTOR 162/195/274/292; RAPTOR 165/195/274/292. A threaded 2x2 grid had identical shared and paired FastRaptor output. The retired audit remains in the git history at 7ff878eb and in that run's logs; it is no longer shipped as maintenance infrastructure.

Minimum reported minutes at a 90-minute limit before the truncation change:

POA example Original McRAPTOR Fixed McRAPTOR Original RAPTOR Fixed RAPTOR
Original example 52.7 53.2 52.4 52.9
Stronger example 46.0 70.9 45.0 69.9

Original geometry missed the destination in all 7 checked egress legs; corrected directed partials/endpoints passed on 9 legs. The stronger corrected example was excluded at 60 minutes for both algorithms. Rafael independently benchmarked 625 pairs with walking/bicycle egress and fares in the linked review, confirming the root causes, destination geometry correction and approximately neutral performance, while identifying the LTS blocker addressed here.

The former upward rounding added at most 2 seconds per chosen seed relative to truncating the same cost components. That alone cannot explain a mean difference measured in minutes versus TTM. The partial-edge elevation/base-speed model remains different; exact decomposition of the reported offset is unmeasured.

Scope and remaining follow-ups

Only point-seeded transit egress in detailed_itineraries() changes. The access/direct origin-geometry issue and other routing APIs are unchanged. The same-package adapter depends on R5 internals and needs review on engine upgrades; no package-version or R5-pin change is included.

An upstream conveyal/r5 issue link is still needed. No matching issue was found in searches; the TODO currently links this review, where Rafael offered to file it. This revision does not file a new engine issue.

Car egress, scenarios, other real networks and exact clipped-elevation error remain unmeasured here. The historical 300-pair audit measures cutoff consistency on one POA date, not independent physical correctness of every path or prevalence across networks.

@rafapereirabr

Copy link
Copy Markdown
Contributor

thanks, @e-kotov . I'll have a look into this !

@rafapereirabr

Copy link
Copy Markdown
Contributor

Thanks a lot for this, @e-kotov — careful work, and the diagnosis is right. We traced both root causes in R5 (the reverse branch of StreetRouter.setOrigin swaps the seed vertices but not their costs — R5's own FIXME — and State.reverse() drops the root partial/off-street cost). The McRAPTOR side has been there since the planner was ported from R5's PointToPointQuery in 2022, and you're right that the FastRaptor ranking from #603 made it worse: state.reverse() does not re-time the leg in the direction of travel, it only subtracts the destination connection. So we'd like to merge the approach, with a few changes below.

Benchmark on our side

Master (3dd6eda) vs this PR (7ff878e), installed in separate libraries and run in separate R sessions (loaded JAR hashes match the ones you report). POA sample data, 25 × 25 hexgrid points (set.seed(1)), all_to_all = TRUE, 2019-05-13 14:00, time_window = 10, n_threads = 4; shortest_path TRUE/FALSE × mode_egress WALK/BICYCLE × max_trip_duration 60/120, plus one McRAPTOR run with setup_fare_structure(net, 5).

  • Geometry: on master the last point of every transit itinerary misses the destination (median gap ~90–150 m, max 1.27 km). With the PR the gap is 0 m in every configuration.
  • Durations: fastest itinerary per pair, PR − master: median +1.5 to +1.7 min, p90 +3.7 to +5.6 min, max +24.8 min (walk egress). 5–9% of transit pairs change route; 13–50 pairs per configuration get faster.
  • Agreement with travel_time_matrix() (percentiles = 1, same inputs; walk egress, shortest_path = TRUE, 120 min): mean (itinerary − TTM) goes from −1.84 to +1.28 min, mean absolute gap from 2.61 to 2.06 min. For the worst destination (89a90e924b3ffff) master is 24.3 min below TTM on average and the PR is within 0.5 min, so the large changes are real corrections.
  • Cutoffs: at 60 min, 39 (RAPTOR) and 11 (McRAPTOR) walk-egress pairs drop out; they were listed at 39–60 min and really take longer. Nothing is lost at 120 min with walk egress.
  • Performance: neutral. Median of 3 warm runs, 625 pairs: McRAPTOR 70.8 s → 71.4 s, RAPTOR 1.86 s → 1.95 s; fares 58.2 s → 52.8 s. travel_time_matrix() output is identical.

Blocking: bicycle egress at the default max_lts = 2

With mode_egress = "BICYCLE" and the default max_lts = 2, destination 89a901291b7ffff (POA hexgrid) loses every transit itinerary on the PR; the log repeats MODE:BICYCLE, Edge near the destination coordinate ... wasn't found. Routing didn't start! (once with coordinate null on the per-origin FastRaptor path). Master and travel_time_matrix() reach it from all 25 origins; the PR does too with max_lts = 4.

d <- poa_hexgrid[id == "89a901291b7ffff"]
detailed_itineraries(net, origins, d, mode = c("WALK", "TRANSIT"),
                     mode_egress = "BICYCLE", all_to_all = TRUE,
                     departure_datetime = as.POSIXct("2019-05-13 14:00:00"),
                     max_trip_duration = 120)          # PR: no transit trips; master: 24

It looks like the traverse probe in R5REgressRouter applies the LTS limit to the edge the destination snaps to and rejects both seeds, whereas R5 seeds the split edge regardless (that's what the TTM uses). Could you make the seeding behave like R5 here — e.g. fall back to the unrestricted/walk-the-bike cost for the split edge rather than rejecting it — and add this destination as a test case? When both seeds are rejected, the "edge wasn't found" message is also misleading; something like "no permitted street at the destination for MODE" would help.

Please trim the scaffolding

The production change is close to minimal, but ~900 of the ~1,080 added lines are test/CI infrastructure, which is more than we can maintain across R5 upgrades. Could you:

  • drop the workflow_dispatch baseline job, .github/fixtures/walking-pairs.csv and .github/scripts/check-walking-egress.R (the audit is great evidence — please keep it in the PR description or a gist);
  • drop the egressRegression Gradle source set, its check dependency and EgressRegression.java — ./gradlew build in the build-jar job would otherwise run it on every JAR build, and the fake StreetLayer is tied to R5 internals;
  • replace test-egress-endpoint.R + helper-egress.R with one compact testthat block on the POA data, run for shortest_path = TRUE and FALSE, asserting that (1) the last coordinate of each transit itinerary equals the destination, (2) leg durations plus waits add up to total_duration within rounding, and (3) a tighter max_trip_duration just above the minimum returns the same minimum. Plus the bicycle/LTS case above.

Smaller points (non-blocking)

  • R5REgressRouter: please add a TODO linking an upstream conveyal/r5 issue for setOrigin/State.reverse() (happy to file it, or you can), so the adapter can be removed once a fixed R5 is pinned.
  • Partial and off-street costs are rounded up while R5 truncates; that seems to account for part of the +1.3 min mean offset vs TTM. Was rounding up deliberate? Matching R5's convention would keep borderline trips consistent with travel_time_matrix().
  • EgressPath.build throws IllegalArgumentException inside Trip.augment, which would abort the whole call; logging and skipping that itinerary would be safer.
  • Optional: EgressPath could probably shrink by building the leg with new StreetSegment(new StreetPath(state, network, true), ...) and only appending the clipped terminal edge and destination point.

Thanks again — once the bicycle case is fixed and the tests are slimmed down, we're happy to merge.

@e-kotov

e-kotov commented Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

Hi @rafapereirabr, thanks for the review and for running the benchmarks!

I've pushed the changes:

  • Bicycle/LTS: if the destination's street is rejected at the user's LTS, it now tries walking the bike, then retries that one street at LTS 4. The rest of the route still respects the user's LTS. Destination 89a901291b7ffff is now a test case at LTS 2 and 4. One small difference from R5: on a one-way street, only the end you're allowed to travel from is used.
  • Tests: I removed the baseline workflow, the audit script and CSV, the Gradle regression task and the Java fixture. They're replaced by one short test block on the POA data, run for both algorithms. It checks that itineraries end at the destination, that legs plus waits add up to the total, and that a tighter max_trip_duration gives the same fastest trip. I also added a check that the final walk takes the right time: it should equal a normal walking route from the last stop plus the walk from the street to the destination point. Without that check, the tests could still pass if those costs were wrong.
  • Smaller fixes: clearer log message when the destination can't be reached, and a bad egress now skips only that itinerary instead of failing the whole call.

CI is green on all eight jobs: https://github.com/e-kotov/r5r/actions/runs/40311691843

Rounding: I rounded up on purpose, so the walk to the destination would never be underestimated. I've switched to truncation, as R5 does. One difference remains: R5 truncates the distance to whole metres first, and I don't. I haven't looked into how much of the remaining gap with travel_time_matrix() this explains.

EgressPath: I kept it as is. Using StreetPath would still mean adding back the missing cost and the last bit of street by hand, and the current code already handles one-way streets and loops.

Audit: the results and old CI links are in the PR description, and the full scripts are in the history at 7ff878e. I've marked those results as historical, since the rounding has changed since.

Happy to leave the upstream R5 issue to you. Once you share the link, I'll update the TODO in the code.

@rafapereirabr
rafapereirabr marked this pull request as ready for review October 26, 2026 19:25
@rafapereirabr

Copy link
Copy Markdown
Contributor

thanks, @e-kotov ! this looks great !

@rafapereirabr

Copy link
Copy Markdown
Contributor

PR to fix the issue upstream in live here conveyal/r5#1027

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.

2 participants