Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / maps / src / main / rust / examples / transit_parity.rs
transit_parity.rs186 lines · 7.7 KiBRaw
| 1 | //! `transit_parity` — planet5 transit check: the same plans and departure boards from a TRIX v7 |
| 2 | //! pack and its v8 (ZSEC, thinned shapes, no ROUTE_TRIPS) rewrite. |
| 3 | //! |
| 4 | //! ```text |
| 5 | //! cargo run --release --example transit_parity -- V7_DIR V8_DIR FEED [N] |
| 6 | //! ``` |
| 7 | //! |
| 8 | //! Loads `<dir>/<FEED>.transit` from both directories and runs N origin/destination plans around |
| 9 | //! 24 transit-heavy cities at three departure times, plus a departure board at each origin. |
| 10 | //! Times, stops, routes, headsigns and stop counts must match exactly; ride distances and |
| 11 | //! polylines may differ only by the shape thinning (reported). Also times both, since v8 reads |
| 12 | //! go through the block cache. |
| 13 | //! |
| 14 | //! Experiment tooling (examples/): never in the cdylib. |
| 15 | |
| 16 | use offlinerouter::transit::{plan, stop_departures, LegKind, QueryDay, Schedule, TransitIndex}; |
| 17 | use std::time::Instant; |
| 18 | |
| 19 | const CITIES: [(&str, f64, f64); 24] = [ |
| 20 | ("SF", 37.7749, -122.4194), |
| 21 | ("LA", 34.0522, -118.2437), |
| 22 | ("Seattle", 47.6062, -122.3321), |
| 23 | ("NYC", 40.7580, -73.9855), |
| 24 | ("Chicago", 41.8781, -87.6298), |
| 25 | ("Toronto", 43.6532, -79.3832), |
| 26 | ("Mexico City", 19.4326, -99.1332), |
| 27 | ("Sao Paulo", -23.5505, -46.6333), |
| 28 | ("London", 51.5074, -0.1278), |
| 29 | ("Paris", 48.8566, 2.3522), |
| 30 | ("Berlin", 52.52, 13.405), |
| 31 | ("Madrid", 40.4168, -3.7038), |
| 32 | ("Rome", 41.9028, 12.4964), |
| 33 | ("Amsterdam", 52.3676, 4.9041), |
| 34 | ("Zurich", 47.3769, 8.5417), |
| 35 | ("Vienna", 48.2082, 16.3738), |
| 36 | ("Warsaw", 52.2297, 21.0122), |
| 37 | ("Stockholm", 59.3293, 18.0686), |
| 38 | ("Tokyo", 35.6812, 139.7671), |
| 39 | ("Seoul", 37.5665, 126.978), |
| 40 | ("Sydney", -33.8688, 151.2093), |
| 41 | ("Melbourne", -37.8136, 144.9631), |
| 42 | ("Auckland", -36.8485, 174.7633), |
| 43 | ("Singapore", 1.3521, 103.8198), |
| 44 | ]; |
| 45 | |
| 46 | fn main() -> std::process::ExitCode { |
| 47 | let a: Vec<String> = std::env::args().skip(1).collect(); |
| 48 | if a.len() < 3 { |
| 49 | eprintln!("usage: transit_parity V7_DIR V8_DIR FEED [N]"); |
| 50 | return std::process::ExitCode::from(2); |
| 51 | } |
| 52 | let n: usize = a.get(3).and_then(|v| v.parse().ok()).unwrap_or(480); |
| 53 | let t = Instant::now(); |
| 54 | let v7 = TransitIndex::load(&a[0], &a[2]).expect("v7 pack loads"); |
| 55 | let v8 = TransitIndex::load(&a[1], &a[2]).expect("v8 pack loads"); |
| 56 | println!("loaded both in {:.2}s", t.elapsed().as_secs_f64()); |
| 57 | // Tuesday 2026-11-17 (weekday 1 = Tue), previous day Monday. |
| 58 | let day = QueryDay { weekday: 1, date: 20261117, prev_weekday: 0, prev_date: 20261116 }; |
| 59 | let sched = Schedule { day, overlay: None }; |
| 60 | let mut seed = 0x9E37_79B9_7F4A_7C15u64; |
| 61 | let mut rnd = || { |
| 62 | seed ^= seed << 13; |
| 63 | seed ^= seed >> 7; |
| 64 | seed ^= seed << 17; |
| 65 | (seed >> 11) as f64 / (1u64 << 53) as f64 |
| 66 | }; |
| 67 | let (mut same, mut diff, mut none_both, mut routed) = (0usize, 0usize, 0usize, 0usize); |
| 68 | // Same final arrival through a different itinerary: an equally optimal journey (tie-breaks |
| 69 | // read ride distances, which shape thinning moves by centimetres). Counted apart, not failed. |
| 70 | let mut alt = 0usize; |
| 71 | let (mut boards_same, mut boards) = (0usize, 0usize); |
| 72 | let (mut t7, mut t8) = (0.0f64, 0.0f64); |
| 73 | let mut max_dist_diff = 0.0f64; |
| 74 | let mut coords7 = 0usize; |
| 75 | let mut coords8 = 0usize; |
| 76 | for q in 0..n { |
| 77 | let (_, la, lo) = CITIES[q % CITIES.len()]; |
| 78 | let off = |r: f64| (r - 0.5) * 0.12; |
| 79 | let (fla, flo) = (la + off(rnd()), lo + off(rnd())); |
| 80 | let (tla, tlo) = (la + off(rnd()), lo + off(rnd())); |
| 81 | let dep = [7 * 3600 + 30 * 60, 12 * 3600, 18 * 3600 + 15 * 60][q % 3]; |
| 82 | let s = Instant::now(); |
| 83 | let p7 = plan(&v7, fla, flo, tla, tlo, dep, sched); |
| 84 | t7 += s.elapsed().as_secs_f64(); |
| 85 | let s = Instant::now(); |
| 86 | let p8 = plan(&v8, fla, flo, tla, tlo, dep, sched); |
| 87 | t8 += s.elapsed().as_secs_f64(); |
| 88 | match (&p7, &p8) { |
| 89 | (None, None) => none_both += 1, |
| 90 | (Some(x), Some(y)) => { |
| 91 | routed += 1; |
| 92 | let key = |l: &Vec<offlinerouter::transit::TransitLeg>| { |
| 93 | l.iter() |
| 94 | .map(|g| { |
| 95 | ( |
| 96 | g.kind == LegKind::Ride, |
| 97 | g.name.clone(), |
| 98 | g.from_stop.clone(), |
| 99 | g.to_stop.clone(), |
| 100 | g.headsign.clone(), |
| 101 | g.dep_secs, |
| 102 | g.arr_secs, |
| 103 | g.stop_count, |
| 104 | g.board_stop_motis_id.clone(), |
| 105 | g.alight_stop_motis_id.clone(), |
| 106 | ) |
| 107 | }) |
| 108 | .collect::<Vec<_>>() |
| 109 | }; |
| 110 | if key(x) == key(y) { |
| 111 | same += 1; |
| 112 | for (g7, g8) in x.iter().zip(y.iter()) { |
| 113 | max_dist_diff = max_dist_diff.max((g7.dist_m - g8.dist_m).abs()); |
| 114 | coords7 += g7.coords.len() / 2; |
| 115 | coords8 += g8.coords.len() / 2; |
| 116 | } |
| 117 | } else if x.last().map(|g| g.arr_secs) == y.last().map(|g| g.arr_secs) |
| 118 | && x.first().map(|g| g.dep_secs) == y.first().map(|g| g.dep_secs) |
| 119 | { |
| 120 | alt += 1; |
| 121 | } else { |
| 122 | diff += 1; |
| 123 | if diff <= 5 { |
| 124 | println!("query {q}: plans differ"); |
| 125 | for (tag, l) in [("v7", x), ("v8", y)] { |
| 126 | for g in l.iter() { |
| 127 | println!( |
| 128 | " {tag} ride={} {:?} {:?}->{:?} hs={:?} {}..{} n={} d={:.0}", |
| 129 | g.kind == LegKind::Ride, |
| 130 | g.name, |
| 131 | g.from_stop, |
| 132 | g.to_stop, |
| 133 | g.headsign, |
| 134 | g.dep_secs, |
| 135 | g.arr_secs, |
| 136 | g.stop_count, |
| 137 | g.dist_m |
| 138 | ); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | _ => { |
| 145 | diff += 1; |
| 146 | println!("query {q}: routable in one pack only"); |
| 147 | } |
| 148 | } |
| 149 | eprintln!( |
| 150 | "q{q}: v7 {} legs, v8 {} legs, {:.0} / {:.0} ms so far", |
| 151 | p7.as_ref().map_or(0, |l| l.len()), |
| 152 | p8.as_ref().map_or(0, |l| l.len()), |
| 153 | t7 * 1e3, |
| 154 | t8 * 1e3 |
| 155 | ); |
| 156 | let b7 = stop_departures(&v7, fla, flo, dep, sched, 20); |
| 157 | let b8 = stop_departures(&v8, fla, flo, dep, sched, 20); |
| 158 | let bk = |b: &Vec<offlinerouter::transit::StopDeparture>| { |
| 159 | b.iter() |
| 160 | .map(|d| { |
| 161 | (d.route_name.clone(), d.headsign.clone(), d.stop_code.clone(), d.dep_secs) |
| 162 | }) |
| 163 | .collect::<Vec<_>>() |
| 164 | }; |
| 165 | boards += 1; |
| 166 | if bk(&b7) == bk(&b8) { |
| 167 | boards_same += 1; |
| 168 | } |
| 169 | } |
| 170 | println!( |
| 171 | "plans: {routed} routed, {none_both} unroutable in both; identical legs {same}, same arrival via another \ |
| 172 | itinerary {alt}, different {diff}\n\ |
| 173 | departure boards identical {boards_same}/{boards}\n\ |
| 174 | ride polylines: {coords7} -> {coords8} points; max leg distance change {max_dist_diff:.2} m\n\ |
| 175 | plan time: v7 {:.1} ms/query, v8 {:.1} ms/query", |
| 176 | t7 * 1e3 / n as f64, |
| 177 | t8 * 1e3 / n as f64 |
| 178 | ); |
| 179 | if diff == 0 && boards_same == boards { |
| 180 | println!("TRANSIT PARITY OK"); |
| 181 | std::process::ExitCode::SUCCESS |
| 182 | } else { |
| 183 | println!("TRANSIT PARITY FAILED"); |
| 184 | std::process::ExitCode::FAILURE |
| 185 | } |
| 186 | } |