From 41d23d2e54a4ff4ee541c0cdad198cf1805e1fa3 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Tue, 26 Feb 2019 16:55:54 -0800 Subject: [PATCH] properly implementing stuff in Sim so far... removing some half-baked summary stuff --- editor/Cargo.toml | 1 + .../plugins/sim/new_des_model/make/spawn.rs | 4 +- .../sim/new_des_model/mechanics/car.rs | 4 +- .../sim/new_des_model/mechanics/driving.rs | 2 +- .../new_des_model/mechanics/intersection.rs | 4 +- .../sim/new_des_model/mechanics/queue.rs | 2 +- .../sim/new_des_model/mechanics/walking.rs | 6 +- editor/src/plugins/sim/new_des_model/mod.rs | 4 +- editor/src/plugins/sim/new_des_model/query.rs | 2 + editor/src/plugins/sim/new_des_model/sim.rs | 67 +++++++++---------- editor/src/plugins/sim/new_des_model/trips.rs | 11 ++- geom/src/line.rs | 2 +- 12 files changed, 54 insertions(+), 55 deletions(-) diff --git a/editor/Cargo.toml b/editor/Cargo.toml index 89a4b8cc0f..e9268b1ae3 100644 --- a/editor/Cargo.toml +++ b/editor/Cargo.toml @@ -8,6 +8,7 @@ edition = "2018" aabb-quadtree = "0.1.0" abstutil = { path = "../abstutil" } counter = "0.4.3" +derivative = "1.0.0" derive-new = "0.5.6" downcast = "0.9.2" ezgui = { path = "../ezgui" } diff --git a/editor/src/plugins/sim/new_des_model/make/spawn.rs b/editor/src/plugins/sim/new_des_model/make/spawn.rs index ce4f418c52..ea590b4a17 100644 --- a/editor/src/plugins/sim/new_des_model/make/spawn.rs +++ b/editor/src/plugins/sim/new_des_model/make/spawn.rs @@ -9,7 +9,7 @@ use serde_derive::{Deserialize, Serialize}; use sim::{CarID, PedestrianID, VehicleType}; use std::collections::BTreeSet; -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, PartialEq)] pub enum TripSpec { // Can be used to spawn from a border or anywhere for interactive debugging. CarAppearing(Position, VehicleSpec, DrivingGoal), @@ -19,7 +19,7 @@ pub enum TripSpec { UsingTransit(SidewalkSpot, BusRouteID, BusStopID, BusStopID, SidewalkSpot), } -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, PartialEq)] pub struct TripSpawner { // TODO tmp pub pub(crate) car_id_counter: usize, diff --git a/editor/src/plugins/sim/new_des_model/mechanics/car.rs b/editor/src/plugins/sim/new_des_model/mechanics/car.rs index 94f0b3e120..ba4246a05d 100644 --- a/editor/src/plugins/sim/new_des_model/mechanics/car.rs +++ b/editor/src/plugins/sim/new_des_model/mechanics/car.rs @@ -7,7 +7,7 @@ use serde_derive::{Deserialize, Serialize}; use sim::{CarStatus, DrawCarInput}; use std::collections::VecDeque; -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, PartialEq)] pub struct Car { pub vehicle: Vehicle, pub state: CarState, @@ -129,7 +129,7 @@ impl Car { } } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, PartialEq)] pub enum CarState { // TODO These two should perhaps be collapsed to (TimeInterval, DistanceInterval, Traversable). Crossing(TimeInterval, DistanceInterval), diff --git a/editor/src/plugins/sim/new_des_model/mechanics/driving.rs b/editor/src/plugins/sim/new_des_model/mechanics/driving.rs index 32e37db1e7..eaf4ba3b8f 100644 --- a/editor/src/plugins/sim/new_des_model/mechanics/driving.rs +++ b/editor/src/plugins/sim/new_des_model/mechanics/driving.rs @@ -17,7 +17,7 @@ const WAITING: Color = Color::RED; const TIME_TO_UNPARK: Duration = Duration::const_seconds(10.0); const TIME_TO_PARK: Duration = Duration::const_seconds(15.0); -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, PartialEq)] pub struct DrivingSimState { queues: BTreeMap, } diff --git a/editor/src/plugins/sim/new_des_model/mechanics/intersection.rs b/editor/src/plugins/sim/new_des_model/mechanics/intersection.rs index 8075542eef..935531f7c3 100644 --- a/editor/src/plugins/sim/new_des_model/mechanics/intersection.rs +++ b/editor/src/plugins/sim/new_des_model/mechanics/intersection.rs @@ -4,7 +4,7 @@ use serde_derive::{Deserialize, Serialize}; use sim::AgentID; use std::collections::{BTreeMap, BTreeSet}; -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, PartialEq)] pub struct IntersectionSimState { controllers: BTreeMap, } @@ -47,7 +47,7 @@ impl IntersectionSimState { } } -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, PartialEq)] struct IntersectionController { id: IntersectionID, accepted: BTreeSet, diff --git a/editor/src/plugins/sim/new_des_model/mechanics/queue.rs b/editor/src/plugins/sim/new_des_model/mechanics/queue.rs index 4620dba3d7..591163519e 100644 --- a/editor/src/plugins/sim/new_des_model/mechanics/queue.rs +++ b/editor/src/plugins/sim/new_des_model/mechanics/queue.rs @@ -5,7 +5,7 @@ use map_model::{Map, Traversable}; use serde_derive::{Deserialize, Serialize}; use std::collections::VecDeque; -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, PartialEq)] pub struct Queue { pub id: Traversable, pub cars: VecDeque, diff --git a/editor/src/plugins/sim/new_des_model/mechanics/walking.rs b/editor/src/plugins/sim/new_des_model/mechanics/walking.rs index d8bd9bb59e..4fe799eaca 100644 --- a/editor/src/plugins/sim/new_des_model/mechanics/walking.rs +++ b/editor/src/plugins/sim/new_des_model/mechanics/walking.rs @@ -15,7 +15,7 @@ const SPEED: Speed = Speed::const_meters_per_second(3.9); const TIME_TO_START_BIKING: Duration = Duration::const_seconds(30.0); const TIME_TO_FINISH_BIKING: Duration = Duration::const_seconds(45.0); -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, PartialEq)] pub struct WalkingSimState { // BTreeMap not for deterministic simulation, but to make serialized things easier to compare. peds: BTreeMap, @@ -226,7 +226,7 @@ fn delete_ped_from_current_step(map: &mut MultiMap, p .retain(|&p| p != ped.id); } -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, PartialEq)] struct Pedestrian { id: PedestrianID, state: PedState, @@ -295,7 +295,7 @@ impl Pedestrian { } // crossing front path, bike parking, waiting at bus stop, etc -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, PartialEq)] enum PedState { // If we're past the TimeInterval, then blocked on a turn. // The bool is true when we've marked the turn finished. We might experience two turn sequences diff --git a/editor/src/plugins/sim/new_des_model/mod.rs b/editor/src/plugins/sim/new_des_model/mod.rs index f24629cf89..834509d6ad 100644 --- a/editor/src/plugins/sim/new_des_model/mod.rs +++ b/editor/src/plugins/sim/new_des_model/mod.rs @@ -200,7 +200,7 @@ pub enum SidewalkPOI { BikeRack(Position), } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, PartialEq)] pub struct TimeInterval { // TODO Private fields pub start: Duration, @@ -226,7 +226,7 @@ impl TimeInterval { } } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, PartialEq)] pub struct DistanceInterval { // TODO Private fields pub start: Distance, diff --git a/editor/src/plugins/sim/new_des_model/query.rs b/editor/src/plugins/sim/new_des_model/query.rs index e8cbed5e6f..1f3d71d115 100644 --- a/editor/src/plugins/sim/new_des_model/query.rs +++ b/editor/src/plugins/sim/new_des_model/query.rs @@ -30,6 +30,7 @@ impl Benchmark { } } +// TODO This is totally unused, needs to be re-thought // TODO moving vs stuck shouldn't be an instantaneous judgment -- stuck is if there's an agent // directly in front limiting speed significantly, or if an intersection isn't allowing movement // yet @@ -45,6 +46,7 @@ pub struct Summary { pub trips_with_ab_test_divergence: usize, } +// TODO This is totally unused, needs to be re-thought // As of a moment in time, not necessarily the end of the simulation #[derive(Serialize, Deserialize, Debug)] pub struct ScoreSummary { diff --git a/editor/src/plugins/sim/new_des_model/sim.rs b/editor/src/plugins/sim/new_des_model/sim.rs index 3337b86dc2..1f72643ef0 100644 --- a/editor/src/plugins/sim/new_des_model/sim.rs +++ b/editor/src/plugins/sim/new_des_model/sim.rs @@ -4,6 +4,7 @@ use crate::plugins::sim::new_des_model::{ WalkingSimState, }; use abstutil::Timer; +use derivative::Derivative; use ezgui::GfxCtx; use geom::Duration; use map_model::{BuildingID, LaneID, Map, Traversable}; @@ -13,8 +14,8 @@ use std::collections::{HashSet, VecDeque}; use std::panic; use std::time::Instant; -#[derive(Serialize, Deserialize)] -//#[derivative(PartialEq)] +#[derive(Serialize, Deserialize, Derivative)] +#[derivative(PartialEq)] pub struct Sim { driving: DrivingSimState, parking: ParkingSimState, @@ -29,9 +30,9 @@ pub struct Sim { pub(crate) map_name: String, pub(crate) edits_name: String, // Some tests deliberately set different scenario names for comparisons. - //#[derivative(PartialEq = "ignore")] + #[derivative(PartialEq = "ignore")] run_name: String, - //#[derivative(PartialEq = "ignore")] + #[derivative(PartialEq = "ignore")] savestate_every: Option, } @@ -173,6 +174,14 @@ impl Sim { &mut self.trips, ); + // Savestate? Do this AFTER incrementing the timestep. Otherwise we could repeatedly load a + // savestate, run a step, and invalidly save over it. + if let Some(t) = self.savestate_every { + if self.time.is_multiple_of(t) { + self.save(); + } + } + Vec::new() } } @@ -323,50 +332,38 @@ impl Sim { } impl Sim { + // TODO Rethink this pub fn summarize(&self, lanes: &HashSet) -> Summary { - /*let (cars_parked, open_parking_spots) = self.parking_state.count(lanes); - let (moving_cars, stuck_cars, buses) = self.driving_state.count(lanes); - let (moving_peds, stuck_peds) = self.walking_state.count(lanes);*/ - let (cars_parked, open_parking_spots) = (0, 0); - let (moving_cars, stuck_cars, buses) = (0, 0, 0); - let (moving_peds, stuck_peds) = (0, 0); - Summary { - cars_parked, - open_parking_spots, - moving_cars, - stuck_cars, - buses, - moving_peds, - stuck_peds, - // Something else has to calculate this + cars_parked: 0, + open_parking_spots: 0, + moving_cars: 0, + stuck_cars: 0, + buses: 0, + moving_peds: 0, + stuck_peds: 0, trips_with_ab_test_divergence: 0, } } - // TODO deprecate this, use the new Summary pub fn summary(&self) -> String { - /*let (waiting_cars, active_cars) = self.driving_state.get_active_and_waiting_count(); - let (waiting_peds, active_peds) = self.walking_state.get_active_and_waiting_count();*/ - let (waiting_cars, active_cars) = (0, 0); - let (waiting_peds, active_peds) = (0, 0); format!( - "Time: {0}, {1} / {2} active cars waiting, {3} cars parked, {4} / {5} pedestrians waiting", + "{}, {} active agents", self.time, - waiting_cars, - active_cars, - 0, - //self.parking_state.total_count(), - waiting_peds, active_peds, + self.trips.active_agents().len() ) } + // TODO Rethink this pub fn get_score(&self) -> ScoreSummary { - panic!("TODO"); - /*let mut s = self.trips_state.get_score(self.time); - if self.is_done() { - s.completion_time = Some(self.time); + ScoreSummary { + pending_walking_trips: 0, + total_walking_trips: 0, + total_walking_trip_time: Duration::ZERO, + pending_driving_trips: 0, + total_driving_trips: 0, + total_driving_trip_time: Duration::ZERO, + completion_time: None, } - s*/ } } diff --git a/editor/src/plugins/sim/new_des_model/trips.rs b/editor/src/plugins/sim/new_des_model/trips.rs index 120e5c52dd..6b9723e2fe 100644 --- a/editor/src/plugins/sim/new_des_model/trips.rs +++ b/editor/src/plugins/sim/new_des_model/trips.rs @@ -353,11 +353,14 @@ impl TripManager { id } - /* pub fn active_agents(&self) -> Vec { self.active_trip_mode.keys().cloned().collect() } + pub fn get_active_trips(&self) -> Vec { + self.active_trip_mode.values().cloned().collect() + } + pub fn trip_to_agent(&self, id: TripID) -> Option { let trip = self.trips.get(id.0)?; match trip.legs.get(0)? { @@ -375,10 +378,6 @@ impl TripManager { self.active_trip_mode.get(&id).cloned() } - pub fn get_active_trips(&self) -> Vec { - self.active_trip_mode.values().cloned().collect() - } - pub fn tooltip_lines(&self, id: AgentID) -> Vec { // Only called for agents that _should_ have trips let trip = &self.trips[self.active_trip_mode[&id].0]; @@ -387,7 +386,7 @@ impl TripManager { trip.id, trip.legs.back().unwrap() )] - }*/ + } pub fn is_done(&self) -> bool { // TODO Buses? diff --git a/geom/src/line.rs b/geom/src/line.rs index 74a14fbdd1..ee3d47cf7b 100644 --- a/geom/src/line.rs +++ b/geom/src/line.rs @@ -4,7 +4,7 @@ use serde_derive::{Deserialize, Serialize}; use std::fmt; // Segment, technically. Should rename. -#[derive(Clone, Serialize, Deserialize, Debug)] +#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)] pub struct Line(Pt2D, Pt2D); impl Line {