From a0956995c9ff79446e52a8315b736431d534a0e7 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Mon, 2 Dec 2019 14:44:03 -0800 Subject: [PATCH] start to display more detailed breakdown of active agents --- game/src/abtest/mod.rs | 15 +++++++++++---- game/src/sandbox/mod.rs | 15 +++++++++++---- sim/src/sim.rs | 15 +++++++++------ sim/src/trips.rs | 30 ++++++++++++++++++++++++------ 4 files changed, 55 insertions(+), 20 deletions(-) diff --git a/game/src/abtest/mod.rs b/game/src/abtest/mod.rs index 70d9b37b71..7eddce5f0b 100644 --- a/game/src/abtest/mod.rs +++ b/game/src/abtest/mod.rs @@ -14,7 +14,7 @@ use ezgui::{ use geom::{Circle, Distance, Line, PolyLine}; use map_model::{Map, LANE_THICKNESS}; use serde_derive::{Deserialize, Serialize}; -use sim::{Sim, SimOptions, TripID}; +use sim::{Sim, SimOptions, TripID, TripMode}; // TODO I took out speed controls pub struct ABTestMode { @@ -100,9 +100,16 @@ impl State for ABTestMode { diff.lines.len() ))); } - let (active, unfinished, buses) = ui.primary.sim.num_trips(); - txt.add(Line(format!("{} active (+{} buses)", active, buses))); - txt.add(Line(format!("{} unfinished", unfinished))); + let (active, unfinished, by_mode) = ui.primary.sim.num_trips(); + txt.add(Line(format!("Active trips: {}", active))); + txt.add(Line(format!("Unfinished trips: {}", unfinished))); + txt.add(Line(format!( + "Peds {}, Bikes {}, Cars {}, Buses {}", + by_mode[&TripMode::Walk], + by_mode[&TripMode::Bike], + by_mode[&TripMode::Drive], + by_mode[&TripMode::Transit] + ))); self.menu.set_info(ctx, txt); } self.menu.event(ctx); diff --git a/game/src/sandbox/mod.rs b/game/src/sandbox/mod.rs index a5739a9581..625c1ba27b 100644 --- a/game/src/sandbox/mod.rs +++ b/game/src/sandbox/mod.rs @@ -22,7 +22,7 @@ pub use gameplay::spawner::spawn_agents_around; pub use gameplay::GameplayMode; use geom::Duration; use map_model::MapEdits; -use sim::Sim; +use sim::{Sim, TripMode}; pub struct SandboxMode { speed: speed::SpeedControls, @@ -95,9 +95,16 @@ impl State for SandboxMode { fn event(&mut self, ctx: &mut EventCtx, ui: &mut UI) -> Transition { { let mut txt = Text::new(); - let (active, unfinished, buses) = ui.primary.sim.num_trips(); - txt.add(Line(format!("{} active (+{} buses)", active, buses))); - txt.add(Line(format!("{} unfinished", unfinished))); + let (active, unfinished, by_mode) = ui.primary.sim.num_trips(); + txt.add(Line(format!("Active trips: {}", active))); + txt.add(Line(format!("Unfinished trips: {}", unfinished))); + txt.add(Line(format!( + "Peds {}, Bikes {}, Cars {}, Buses {}", + by_mode[&TripMode::Walk], + by_mode[&TripMode::Bike], + by_mode[&TripMode::Drive], + by_mode[&TripMode::Transit] + ))); txt.add(Line("")); { let edits = ui.primary.map.get_edits(); diff --git a/sim/src/sim.rs b/sim/src/sim.rs index b4c933dfc0..c55652d560 100644 --- a/sim/src/sim.rs +++ b/sim/src/sim.rs @@ -2,7 +2,7 @@ use crate::{ AgentID, AgentMetadata, Analytics, CarID, Command, CreateCar, DrawCarInput, DrawPedCrowdInput, DrawPedestrianInput, DrivingGoal, DrivingSimState, Event, FinishedTrips, GetDrawAgents, IntersectionSimState, ParkedCar, ParkingSimState, ParkingSpot, PedestrianID, Router, Scheduler, - SidewalkPOI, SidewalkSpot, TransitSimState, TripCount, TripID, TripLeg, TripManager, + SidewalkPOI, SidewalkSpot, TransitSimState, TripCount, TripID, TripLeg, TripManager, TripMode, TripPositions, TripResult, TripSpawner, TripSpec, TripStart, TripStatus, UnzoomedAgent, VehicleSpec, VehicleType, WalkingSimState, BUS_LENGTH, }; @@ -14,7 +14,7 @@ use map_model::{ PathRequest, PathStep, Traversable, }; use serde_derive::{Deserialize, Serialize}; -use std::collections::HashSet; +use std::collections::{BTreeMap, HashSet}; use std::panic; use std::time::Instant; @@ -755,13 +755,16 @@ impl Sim { self.time == Time::START_OF_DAY && self.is_done() } - // (active, unfinished, buses) prettyprinted - pub fn num_trips(&self) -> (String, String, String) { - let (active, unfinished, buses) = self.trips.num_trips(); + // (active, unfinished, by mode) prettyprinted + pub fn num_trips(&self) -> (String, String, BTreeMap) { + let (active, unfinished, by_mode) = self.trips.num_trips(); ( abstutil::prettyprint_usize(active), abstutil::prettyprint_usize(unfinished), - abstutil::prettyprint_usize(buses), + by_mode + .into_iter() + .map(|(k, v)| (k, abstutil::prettyprint_usize(v))) + .collect(), ) } diff --git a/sim/src/trips.rs b/sim/src/trips.rs index 3a1d66f97e..92b9973388 100644 --- a/sim/src/trips.rs +++ b/sim/src/trips.rs @@ -1,9 +1,9 @@ use crate::{ AgentID, CarID, Command, CreateCar, CreatePedestrian, DrivingGoal, Event, ParkingSimState, ParkingSpot, PedestrianID, Scheduler, SidewalkPOI, SidewalkSpot, TransitSimState, TripID, - Vehicle, WalkingSimState, + Vehicle, VehicleType, WalkingSimState, }; -use abstutil::{deserialize_btreemap, serialize_btreemap}; +use abstutil::{deserialize_btreemap, serialize_btreemap, Counter}; use geom::{Duration, Speed, Time}; use map_model::{ BuildingID, BusRouteID, BusStopID, IntersectionID, Map, PathConstraints, PathRequest, Position, @@ -490,12 +490,19 @@ impl TripManager { )] } - // (active not including buses, unfinished, buses) - pub fn num_trips(&self) -> (usize, usize, usize) { + // (total active trips, unfinished trips, active trips by the trip's current mode) + pub fn num_trips(&self) -> (usize, usize, BTreeMap) { + let mut cnt = Counter::new(); + for a in self.active_trip_mode.keys() { + cnt.inc(TripMode::from_agent(*a)); + } ( - self.active_trip_mode.len() - self.num_bus_trips, + self.active_trip_mode.len(), self.unfinished_trips, - self.num_bus_trips, + TripMode::all() + .into_iter() + .map(|k| (k, cnt.get(k))) + .collect(), ) } @@ -711,6 +718,17 @@ impl TripMode { TripMode::Drive, ] } + + fn from_agent(id: AgentID) -> TripMode { + match id { + AgentID::Pedestrian(_) => TripMode::Walk, + AgentID::Car(id) => match id.1 { + VehicleType::Car => TripMode::Drive, + VehicleType::Bike => TripMode::Bike, + VehicleType::Bus => TripMode::Transit, + }, + } + } } impl std::fmt::Display for TripMode {