maps(routing): fix A* heuristic consistency — driving edges tagged >105km/h (e.g. 70mph freeways) made the heuristic inconsistent, corrupting the monotonic radix heap and causing wrong-direction detours / no-route. Scale heuristic to MAX_DRIVING_KMH(130) and clamp each edge's effective speed to it; bump transit heuristic to 150km/h for CA rail
- Commit
2e308a2aef5281bf735ba67d2d6a8baf64b9029e- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-18 22:22 UTC (3 months ago)
- Parents
ee13f06
Diff
maps/src/main/rust/src/geometry.rs
diff --git a/maps/src/main/rust/src/geometry.rs b/maps/src/main/rust/src/geometry.rsindex 11371ba042..546b746ba8 100644--- a/maps/src/main/rust/src/geometry.rs+++ b/maps/src/main/rust/src/geometry.rs@@ -7,8 +7,8 @@ use std::collections::HashMap; use crate::graph::{- Edge, Graph, DEG_TO_RAD, DRIVING, INVALID_EDGE, LIVING_STREET, MOTORWAY, PUBLIC_TRANSIT, STEPS,- TRANSIT_FLAG,+ Edge, Graph, DEG_TO_RAD, DRIVING, INVALID_EDGE, LIVING_STREET, MAX_DRIVING_KMH, MOTORWAY,+ PUBLIC_TRANSIT, STEPS, TRANSIT_FLAG, }; /// Live traffic snapshot: global edge id -> speed (km/h). 0 means "unknown".@@ -89,6 +89,10 @@ pub fn get_edge_time_10ms( 0 }; let effective_limit = if traffic_speed > 0 { traffic_speed } else { limit };+ // Clamp to the heuristic's max speed so no edge is ever faster than the+ // heuristic assumes — required to keep the A* heuristic consistent for the+ // monotonic radix heap (see MAX_DRIVING_KMH).+ let effective_limit = effective_limit.min(MAX_DRIVING_KMH as u8); if effective_limit > 0 { let speed_m_s = effective_limit as f64 / 3.6; return (dist_mm as f64 / (speed_m_s * 10.0)) as u32;
maps/src/main/rust/src/graph.rs
diff --git a/maps/src/main/rust/src/graph.rs b/maps/src/main/rust/src/graph.rsindex 65d068fd4a..bb9333e47f 100644--- a/maps/src/main/rust/src/graph.rs+++ b/maps/src/main/rust/src/graph.rs@@ -46,6 +46,18 @@ pub const INVALID_EDGE: u64 = 0xFFFF_FFFF_FFFF_FFFF; pub const WALK_SPEED_M_S: f64 = 4.5 / 3.6; pub const BICYCLE_SPEED_M_S: f64 = 16.0 / 3.6;+/// Upper bound on any driving edge's effective speed (km/h). The A* heuristic is+/// scaled to this speed AND every edge's effective speed is clamped to it in+/// [`crate::geometry::get_edge_time_10ms`], which together keep the heuristic+/// CONSISTENT (never overestimates a single edge). This is required for the+/// monotonic radix heap: if an edge could be faster than the heuristic assumes,+/// a relaxed node's f-value can fall below the last popped key, the heap+/// mis-buckets it, and A* terminates on a suboptimal path (the "detours the+/// wrong way / no route" bug). 130 km/h ≈ 80 mph covers every real US limit.+pub const MAX_DRIVING_KMH: f64 = 130.0;+/// Upper bound on any transit in-vehicle speed (km/h) for the same consistency+/// reason — CA rail (BART/Caltrain/Amtrak) all exceed the old 80 km/h estimate.+pub const MAX_TRANSIT_KMH: f64 = 150.0; pub const DEG_TO_RAD: f64 = std::f64::consts::PI / 180.0; // --- On-disk packed structs ---@@ -292,9 +304,12 @@ impl Graph { let mut time_scale_fixed = [0u64; 4]; time_scale_fixed[WALK as usize] = calc_scale(WALK_SPEED_M_S); time_scale_fixed[BICYCLE as usize] = calc_scale(BICYCLE_SPEED_M_S);- time_scale_fixed[DRIVING as usize] = calc_scale(105.0 / 3.6);- // Optimistic 80 km/h transit velocity keeps the heuristic admissible.- time_scale_fixed[PUBLIC_TRANSIT as usize] = calc_scale(80.0 / 3.6);+ // Heuristic speed MUST be >= the fastest achievable edge speed (which is+ // clamped to MAX_DRIVING_KMH in get_edge_time_10ms) so the heuristic stays+ // consistent for the monotonic radix heap. See MAX_DRIVING_KMH.+ time_scale_fixed[DRIVING as usize] = calc_scale(MAX_DRIVING_KMH / 3.6);+ // Same consistency requirement for transit in-vehicle speed.+ time_scale_fixed[PUBLIC_TRANSIT as usize] = calc_scale(MAX_TRANSIT_KMH / 3.6); let mut edge_time_multipliers = [[0u64; 16]; 4]; for (m, row) in edge_time_multipliers.iter_mut().enumerate() {