From 7c6f2c62b6bb84903649ae49f771a0c1f5a6bb5f Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Sun, 3 Nov 2019 17:17:47 -0800 Subject: [PATCH] compare faster trips by baseline over time --- game/src/challenges.rs | 18 ++++++++++++++---- game/src/sandbox/gameplay.rs | 15 +++++++++++++-- game/src/sandbox/trip_stats.rs | 2 +- sim/src/analytics.rs | 9 +++++---- sim/src/events.rs | 3 ++- sim/src/trips.rs | 24 ++++++++++++++++++++---- 6 files changed, 55 insertions(+), 16 deletions(-) diff --git a/game/src/challenges.rs b/game/src/challenges.rs index 4110191a5d..639fec8971 100644 --- a/game/src/challenges.rs +++ b/game/src/challenges.rs @@ -167,10 +167,14 @@ pub struct PrebakedResults { } #[derive(Serialize, Deserialize)] -pub struct FasterTrips(pub BTreeMap); +pub struct FasterTrips(pub Vec<(Duration, Option, Duration)>); impl FasterTrips { pub fn from(sim: &Sim) -> FasterTrips { + FasterTrips(sim.get_analytics().finished_trips.clone()) + } + + pub fn to_stats(&self, now: Duration) -> BTreeMap { let mut distribs: BTreeMap = BTreeMap::new(); for m in vec![ TripMode::Walk, @@ -180,14 +184,20 @@ impl FasterTrips { ] { distribs.insert(m, Default::default()); } - for (_, m, dt) in sim.get_finished_trips().finished_trips { - distribs.get_mut(&m).unwrap().add(dt); + for (t, mode, dt) in &self.0 { + if *t > now { + break; + } + // Skip aborted trips + if let Some(m) = mode { + distribs.get_mut(&m).unwrap().add(*dt); + } } let mut results = BTreeMap::new(); for (m, distrib) in distribs { results.insert(m, distrib.to_stats()); } - FasterTrips(results) + results } } diff --git a/game/src/sandbox/gameplay.rs b/game/src/sandbox/gameplay.rs index a358301f84..26df2fe9f6 100644 --- a/game/src/sandbox/gameplay.rs +++ b/game/src/sandbox/gameplay.rs @@ -407,8 +407,15 @@ fn gridlock_panel(ui: &UI, prebaked: &PrebakedResults) -> Text { } fn faster_trips_panel(mode: TripMode, ui: &UI, prebaked: &PrebakedResults) -> Text { - let now = &FasterTrips::from(&ui.primary.sim).0[&mode]; - let baseline = &prebaked.faster_trips.0[&mode]; + let now = FasterTrips::from(&ui.primary.sim) + .to_stats(ui.primary.sim.time()) + .remove(&mode) + .unwrap(); + let baseline = prebaked + .faster_trips + .to_stats(ui.primary.sim.time()) + .remove(&mode) + .unwrap(); let mut txt = Text::new(); txt.add(Line(format!( @@ -417,6 +424,10 @@ fn faster_trips_panel(mode: TripMode, ui: &UI, prebaked: &PrebakedResults) -> Te mode, prettyprint_usize(baseline.count), ))); + if now.count == 0 || baseline.count == 0 { + return txt; + } + // TODO Which one? if false { for (stat, dt) in &now.stats { diff --git a/game/src/sandbox/trip_stats.rs b/game/src/sandbox/trip_stats.rs index 4ab2e65aed..18cbab13d8 100644 --- a/game/src/sandbox/trip_stats.rs +++ b/game/src/sandbox/trip_stats.rs @@ -51,7 +51,7 @@ impl ShowTripStats { let mut counts = Counter::new(); let mut pts_per_mode: BTreeMap, Vec<(Duration, usize)>> = lines.iter().map(|(_, _, m)| (*m, Vec::new())).collect(); - for (t, m) in &ui.primary.sim.get_analytics().finished_trips { + for (t, m, _) in &ui.primary.sim.get_analytics().finished_trips { counts.inc(*m); if *t > times[0] { times.remove(0); diff --git a/sim/src/analytics.rs b/sim/src/analytics.rs index f94747fc0d..b183a53ec2 100644 --- a/sim/src/analytics.rs +++ b/sim/src/analytics.rs @@ -12,7 +12,8 @@ pub struct Analytics { pub bus_arrivals: HashMap<(BusStopID, BusRouteID), Vec>, pub total_bus_passengers: Counter, // TODO Hack: No TripMode means aborted - pub finished_trips: Vec<(Duration, Option)>, + // Finish time, mode (or None as aborted), trip duration + pub finished_trips: Vec<(Duration, Option, Duration)>, } pub struct ThruputStats { @@ -65,10 +66,10 @@ impl Analytics { } // Finished trips - if let Event::TripFinished(_, mode) = ev { - self.finished_trips.push((time, Some(mode))); + if let Event::TripFinished(_, mode, dt) = ev { + self.finished_trips.push((time, Some(mode), dt)); } else if let Event::TripAborted(_) = ev { - self.finished_trips.push((time, None)); + self.finished_trips.push((time, None, Duration::ZERO)); } } } diff --git a/sim/src/events.rs b/sim/src/events.rs index 02dd0a95bf..52b0c8f6ac 100644 --- a/sim/src/events.rs +++ b/sim/src/events.rs @@ -1,4 +1,5 @@ use crate::{AgentID, CarID, ParkingSpot, PedestrianID, TripID, TripMode}; +use geom::Duration; use map_model::{BuildingID, BusRouteID, BusStopID, IntersectionID, LaneID, Traversable}; use serde_derive::{Deserialize, Serialize}; @@ -21,6 +22,6 @@ pub enum Event { AgentEntersTraversable(AgentID, Traversable), - TripFinished(TripID, TripMode), + TripFinished(TripID, TripMode, Duration), TripAborted(TripID), } diff --git a/sim/src/trips.rs b/sim/src/trips.rs index e42bf5d9e0..66ae7b8983 100644 --- a/sim/src/trips.rs +++ b/sim/src/trips.rs @@ -138,7 +138,11 @@ impl TripManager { assert!(!trip.finished_at.is_some()); trip.finished_at = Some(now); self.unfinished_trips -= 1; - self.events.push(Event::TripFinished(trip.id, trip.mode)); + self.events.push(Event::TripFinished( + trip.id, + trip.mode, + now - trip.spawned_at, + )); return; } _ => {} @@ -309,7 +313,11 @@ impl TripManager { assert!(!trip.finished_at.is_some()); trip.finished_at = Some(now); self.unfinished_trips -= 1; - self.events.push(Event::TripFinished(trip.id, trip.mode)); + self.events.push(Event::TripFinished( + trip.id, + trip.mode, + now - trip.spawned_at, + )); } // If no route is returned, the pedestrian boarded a bus immediately. @@ -389,7 +397,11 @@ impl TripManager { assert!(!trip.finished_at.is_some()); trip.finished_at = Some(now); self.unfinished_trips -= 1; - self.events.push(Event::TripFinished(trip.id, trip.mode)); + self.events.push(Event::TripFinished( + trip.id, + trip.mode, + now - trip.spawned_at, + )); } pub fn car_or_bike_reached_border(&mut self, now: Duration, car: CarID, i: IntersectionID) { @@ -403,7 +415,11 @@ impl TripManager { assert!(!trip.finished_at.is_some()); trip.finished_at = Some(now); self.unfinished_trips -= 1; - self.events.push(Event::TripFinished(trip.id, trip.mode)); + self.events.push(Event::TripFinished( + trip.id, + trip.mode, + now - trip.spawned_at, + )); } pub fn abort_trip_failed_start(&mut self, id: TripID) {