maps: reject transfer-padded transit alternatives (#nav)
- Commit
a7ac3c20818454531689f9f7c23e8f9a8e56c8c6- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-16 21:10 UTC (19 days ago)
- Parents
e7e99f6
Strengthen the transit diversity gate in AlternativeRoutes: transfer count is now a cost, not a difference in itself. A candidate with more transfers (more ride legs) than an already-shown journey is rejected when: - it re-covers that journey's corridor or stop sequence — served-stop overlap >= 0.67 or shared-geometry ratio >= 0.70. This catches the early-alight + parallel-bus rejoin: same stops/geometry, service sequence dressed up with an added transfer. (0.67, not 0.5, because a shared origin/destination alone already reach 0.5 against a two-stop primary.) and otherwise is shown only when it pays for the extra transfer in at least one of two documented ways: - it arrives meaningfully earlier: at least EXTRA_TRANSFER_MIN_SAVING_MINUTES = 5 whole minutes of journey duration (feed headways on covered corridors run 6-15 minutes, so a smaller saving is timetable noise), or - it uses a genuinely different corridor: served-stop overlap and shared-geometry ratio both below 0.5. Equal-transfer candidates are unaffected (the existing signature/service-sequence and walk-share gates still apply), the primary and ETA ordering are unchanged. Tests (AlternativeRoutesTest, 8 existing kept green + 3 new synthetic cases): (a) ride-through vs alight-early + parallel bus over the same stops/geometry — rejected; (b) one extra transfer arriving ~the same time (1 minute saving) — rejected; (c) one extra transfer 9 minutes faster on different services and geometry — accepted. Verified: AlternativeRoutesTest (11 tests) passes; :maps:compileDevKotlin green; full suite run follows.
Diff
maps/src/main/java/com/vayunmathur/maps/util/AlternativeRoutes.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/util/AlternativeRoutes.kt b/maps/src/main/java/com/vayunmathur/maps/util/AlternativeRoutes.ktindex ba5d162cf5..70a39fb889 100644--- a/maps/src/main/java/com/vayunmathur/maps/util/AlternativeRoutes.kt+++ b/maps/src/main/java/com/vayunmathur/maps/util/AlternativeRoutes.kt@@ -31,6 +31,12 @@ import kotlin.math.sqrt * sequence equals, or is a subset/truncation of, an already-shown * journey's is the same ride taken less far (early alight + longer * walk), however it is re-padded.+ * - Extra transfers are a cost: a transit candidate with more+ * transfers than a shown journey is rejected when it re-covers+ * that journey's corridor/stops (served-stop overlap >= 0.67 or+ * shared geometry >= 0.70 — the early-alight + parallel-bus+ * rejoin), and otherwise must arrive at least 5 minutes earlier+ * or use a genuinely different corridor (both overlaps < 0.5). * * The primary is always kept, and candidates keep their input order * (fastest first as generated), so ETA ordering among shown routes@@ -44,6 +50,24 @@ object AlternativeRoutes { /** Above this walking share a transit candidate is not a transit route. */ const val MAX_WALK_SHARE = 0.45 + /**+ * Minimum arrival saving that can pay for an extra transfer: 5 whole+ * minutes of journey duration. Feed headways on the corridors the pack+ * covers are typically 6-15 minutes, so a saving smaller than one+ * headway step is timetable noise, not a reason to change vehicles.+ */+ const val EXTRA_TRANSFER_MIN_SAVING_MINUTES = 5++ /**+ * At/above this served-stop overlap an added transfer merely re-covers+ * the corridor (shared origin/destination alone reach only 0.5 against+ * a two-stop primary, so the bar sits at two-thirds).+ */+ const val RE_COVER_STOP_OVERLAP = 0.67++ /** Below both overlaps a corridor counts as genuinely different. */+ const val DIFFERENT_CORRIDOR_MAX_OVERLAP = 0.5+ /** Geometry match tolerance (m) for the shared-length measurement. */ private const val MATCH_TOLERANCE_M = 30.0 @@ -134,11 +158,45 @@ object AlternativeRoutes { // different board/alight stops. val lines = signature.map { it.line } val otherLines = otherSignature.map { it.line }- otherSignature == signature || isSubsetOrTruncation(signature, otherSignature) ||+ if (otherSignature == signature || isSubsetOrTruncation(signature, otherSignature) || lines == otherLines || lines.toSet().all { it in otherLines.toSet() }+ ) {+ return@none true+ }+ // Transfer count is a cost, not a difference. A candidate with+ // more transfers than an accepted journey must either arrive+ // meaningfully earlier (>= EXTRA_TRANSFER_MIN_SAVING_MINUTES of+ // duration) or be a genuinely different corridor — and one that+ // re-covers the accepted corridor/stop sequence via an added+ // transfer (early alight, parallel bus, rejoin) is neither,+ // however its service sequence is dressed up.+ val extraTransfers = signature.size > otherSignature.size+ if (!extraTransfers) return@none false+ val stopOverlap = servedStopOverlap(signature, otherSignature)+ val geometryOverlap = sharedGeometryRatio(other, candidate)+ val reCovers = stopOverlap >= RE_COVER_STOP_OVERLAP ||+ geometryOverlap >= MAX_SHARED_GEOMETRY_RATIO+ if (reCovers) return@none true+ val savingMinutes = (other.duration - candidate.duration).inWholeMinutes+ val corridorDifferent = stopOverlap < DIFFERENT_CORRIDOR_MAX_OVERLAP &&+ geometryOverlap < DIFFERENT_CORRIDOR_MAX_OVERLAP+ savingMinutes < EXTRA_TRANSFER_MIN_SAVING_MINUTES && !corridorDifferent } } + /** Board+alight stops a signature serves, as a set. */+ private fun servedStops(signature: List<TransitLegKey>): Set<String> =+ signature.flatMapTo(mutableSetOf()) { listOf(it.board, it.alight) }++ /** Shared served stops over the smaller journey's stop count. */+ private fun servedStopOverlap(a: List<TransitLegKey>, b: List<TransitLegKey>): Double {+ val aStops = servedStops(a)+ val bStops = servedStops(b)+ val smaller = minOf(aStops.size, bStops.size)+ if (smaller == 0) return 0.0+ return aStops.intersect(bStops).size.toDouble() / smaller+ }+ /** True when [a]'s leg set is contained in [b]'s, or [a] is a prefix of [b]. */ private fun isSubsetOrTruncation(a: List<TransitLegKey>, b: List<TransitLegKey>): Boolean { if (a.size <= b.size && b.subList(0, a.size) == a) return true
maps/src/test/java/com/vayunmathur/maps/util/AlternativeRoutesTest.kt
diff --git a/maps/src/test/java/com/vayunmathur/maps/util/AlternativeRoutesTest.kt b/maps/src/test/java/com/vayunmathur/maps/util/AlternativeRoutesTest.ktindex 3b0ecbcc74..2847e3eab2 100644--- a/maps/src/test/java/com/vayunmathur/maps/util/AlternativeRoutesTest.kt+++ b/maps/src/test/java/com/vayunmathur/maps/util/AlternativeRoutesTest.kt@@ -59,6 +59,8 @@ class AlternativeRoutesTest { legs: List<Triple<String, String, String>>, walkMeters: Double = 200.0, rideMeters: Double = 5_000.0,+ durationMinutes: Int = 20,+ polyline: List<GeoPoint> = listOf(pt(0.0, 0.0), pt(0.02, 0.0)), ): RouteService.Route { val steps = mutableListOf<RouteService.Step>() if (walkMeters > 0) {@@ -87,9 +89,9 @@ class AlternativeRoutesTest { ) } return RouteService.Route(- duration = 20.minutes,+ duration = durationMinutes.minutes, distanceMeters = walkMeters + rideMeters * legs.size,- polyline = listOf(pt(0.0, 0.0), pt(0.02, 0.0)),+ polyline = polyline, step = steps, ) }@@ -125,6 +127,45 @@ class AlternativeRoutesTest { assertEquals(emptyList(), AlternativeRoutes.diverseAlternatives(full, listOf(walkHeavy), transit = true)) } + @Test+ fun `alight early and transfer to a parallel bus on the same corridor is rejected`() {+ // (a) Ride-through on Red vs alight at B and continue on the+ // parallel Blue over the same stops and geometry: the transfer+ // re-covers the primary's corridor, so it is not an alternative.+ val primary = transitRoute(listOf(Triple("Red", "A", "C")))+ val parallel = transitRoute(+ listOf(Triple("Red", "A", "B"), Triple("Blue", "B", "C")),+ )+ assertEquals(emptyList(), AlternativeRoutes.diverseAlternatives(primary, listOf(parallel), transit = true))+ }++ @Test+ fun `one extra transfer arriving about the same time is rejected`() {+ // (b) A different second service on a different corridor, but the+ // extra transfer buys no time: transfer count is a cost, and+ // ~0 minutes of saving does not pay for it.+ val primary = transitRoute(listOf(Triple("Red", "A", "C")), durationMinutes = 20)+ val sameTime = transitRoute(+ listOf(Triple("Blue", "A", "D"), Triple("Green", "D", "E")),+ durationMinutes = 19,+ polyline = listOf(pt(0.0, 0.0), pt(0.01, 0.01), pt(0.02, 0.01), pt(0.03, 0.0)),+ )+ assertEquals(emptyList(), AlternativeRoutes.diverseAlternatives(primary, listOf(sameTime), transit = true))+ }++ @Test+ fun `extra transfer substantially faster on a different line is accepted`() {+ // (c) One more transfer, but 9 minutes faster on genuinely+ // different services and geometry: the saving pays for it.+ val primary = transitRoute(listOf(Triple("Red", "A", "C")), durationMinutes = 25)+ val faster = transitRoute(+ listOf(Triple("Blue", "A", "D"), Triple("Green", "D", "E")),+ durationMinutes = 16,+ polyline = listOf(pt(0.0, 0.0), pt(0.01, 0.01), pt(0.02, 0.01), pt(0.03, 0.0)),+ )+ assertEquals(listOf(faster), AlternativeRoutes.diverseAlternatives(primary, listOf(faster), transit = true))+ }+ @Test fun `eta order of accepted alternatives is preserved`() { val slower = roadRoute(listOf(pt(0.0, 0.0), pt(0.01, 0.012), pt(0.02, 0.012), pt(0.03, 0.0)), minutes = 18)