Skip to content

Commit

Permalink
simplify stuff in analytics, removing a major bottleneck in the every…
Browse files Browse the repository at this point in the history
…one_weekday scenario
  • Loading branch information
dabreegster committed Apr 26, 2020
1 parent 1ecea0a commit 3f48b4b
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 53 deletions.
10 changes: 5 additions & 5 deletions data/MANIFEST.txt
Expand Up @@ -237,8 +237,8 @@ aad8ff309a1b38dc85a5d8a4b37f3c87 data/system/scenarios/downtown/weekday.bin
112a8e8a1f8e41f566a03ab83d14fe22 data/system/scenarios/caphill/weekday.bin
be38f2352b6ac6071707361472888c4c data/system/scenarios/montlake/everyone_weekday.bin
fa8391c0e45db61fabdd466ad81b83fa data/system/scenarios/montlake/weekday.bin
80cb748e090072b559b08bdd4bc8c30c data/system/prebaked_results/signal_single/tutorial lvl1.bin
ec841305a2dbc37649045f31afce09bd data/system/prebaked_results/signal_single/tutorial lvl2.bin
2da75e16bd2620e8ed1f8db58e852538 data/system/prebaked_results/montlake/car vs bike contention.bin
06eb9683f2decdcb2ee11b00d4fc2720 data/system/prebaked_results/montlake/weekday.bin
02ae8e1b35219ab4934969c07187842b data/system/prebaked_results/montlake/car vs bus contention.bin
f9c2c0d6b2beb8260ee35e0e3ec23a3e data/system/prebaked_results/signal_single/tutorial lvl1.bin
ff80c3565b243cdbf7a0f0c3fa0f559a data/system/prebaked_results/signal_single/tutorial lvl2.bin
9020e5c837e3b41279bd1d0e91838bcd data/system/prebaked_results/montlake/car vs bike contention.bin
764a4782af23dfbdf6f4dc15623ade3a data/system/prebaked_results/montlake/weekday.bin
a800719d76a09b1a257fc9d831a336e6 data/system/prebaked_results/montlake/car vs bus contention.bin
29 changes: 9 additions & 20 deletions sim/src/analytics.rs
Expand Up @@ -16,8 +16,7 @@ pub struct Analytics {
pub(crate) test_expectations: VecDeque<Event>,
pub bus_arrivals: Vec<(Time, CarID, BusRouteID, BusStopID)>,
pub bus_passengers_waiting: Vec<(Time, BusStopID, BusRouteID)>,
// TODO Scraping TripMode from TripPhaseStarting is frustrating.
pub started_trips: BTreeMap<TripID, (Time, TripMode)>,
pub started_trips: BTreeMap<TripID, Time>,
// TODO Hack: No TripMode means aborted
// Finish time, ID, mode (or None as aborted), trip duration
pub finished_trips: Vec<(Time, TripID, Option<TripMode>, Duration)>,
Expand Down Expand Up @@ -117,23 +116,15 @@ impl Analytics {
}

// Bus passengers
if let Event::TripPhaseStarting(_, _, _, _, ref tpt) = ev {
if let Event::TripPhaseStarting(_, _, _, ref tpt) = ev {
if let TripPhaseType::WaitingForBus(route, stop) = tpt {
self.bus_passengers_waiting.push((time, *stop, *route));
}
}

// Started trips
if let Event::TripPhaseStarting(id, _, mode, _, _) = ev {
// TODO More efficiently
if !self.started_trips.contains_key(&id)
&& !self
.finished_trips
.iter()
.any(|(_, trip, _, _)| *trip == id)
{
self.started_trips.insert(id, (time, mode));
}
if let Event::TripPhaseStarting(id, _, _, _) = ev {
self.started_trips.entry(id).or_insert(time);
}

// Finished trips
Expand All @@ -146,11 +137,9 @@ impl Analytics {
{
self.finished_trips
.push((time, trip, Some(mode), total_time));
} else if let Event::TripAborted(id, mode) = ev {
} else if let Event::TripAborted(id) = ev {
self.started_trips.entry(id).or_insert(time);
self.finished_trips.push((time, id, None, Duration::ZERO));
if !self.started_trips.contains_key(&id) {
self.started_trips.insert(id, (time, mode));
}
}

// Intersection delays
Expand Down Expand Up @@ -181,10 +170,10 @@ impl Analytics {

// TODO Kinda hacky, but these all consume the event, so kinda bundle em.
match ev {
Event::TripPhaseStarting(id, _, _, maybe_req, phase_type) => {
Event::TripPhaseStarting(id, _, maybe_req, phase_type) => {
self.trip_log.push((time, id, maybe_req, phase_type));
}
Event::TripAborted(id, _) => {
Event::TripAborted(id) => {
self.trip_log.push((time, id, None, TripPhaseType::Aborted));
}
Event::TripFinished { trip, .. } => {
Expand Down Expand Up @@ -578,7 +567,7 @@ impl Analytics {

pub fn active_agents(&self, now: Time) -> Vec<(Time, usize)> {
let mut starts_stops: Vec<(Time, bool)> = Vec::new();
for (_, (t, _)) in &self.started_trips {
for t in self.started_trips.values() {
if *t <= now {
starts_stops.push((*t, false));
}
Expand Down
10 changes: 2 additions & 8 deletions sim/src/events.rs
Expand Up @@ -38,14 +38,8 @@ pub enum Event {
total_time: Duration,
blocked_time: Duration,
},
TripAborted(TripID, TripMode),
TripPhaseStarting(
TripID,
PersonID,
TripMode,
Option<PathRequest>,
TripPhaseType,
),
TripAborted(TripID),
TripPhaseStarting(TripID, PersonID, Option<PathRequest>, TripPhaseType),

// Just use for parking replanning. Not happy about copying the full path in here, but the way
// to plumb info into Analytics is Event.
Expand Down
2 changes: 1 addition & 1 deletion sim/src/pandemic/pandemic.rs
Expand Up @@ -181,7 +181,7 @@ impl PandemicModel {
panic!("{} left {:?}, but they weren't inside", person, loc);
}
}
Event::TripPhaseStarting(_, p, _, _, tpt) => {
Event::TripPhaseStarting(_, p, _, tpt) => {
let person = *p;
match tpt {
TripPhaseType::WaitingForBus(_, stop) => {
Expand Down
5 changes: 1 addition & 4 deletions sim/src/router.rs
@@ -1,7 +1,6 @@
use crate::mechanics::Queue;
use crate::{
Event, ParkingSimState, ParkingSpot, PersonID, SidewalkSpot, TripID, TripMode, TripPhaseType,
Vehicle,
Event, ParkingSimState, ParkingSpot, PersonID, SidewalkSpot, TripID, TripPhaseType, Vehicle,
};
use geom::Distance;
use map_model::{
Expand Down Expand Up @@ -216,7 +215,6 @@ impl Router {
events.push(Event::TripPhaseStarting(
t,
p,
TripMode::Drive,
Some(PathRequest {
start: Position::new(current_lane, front),
end: new_pos,
Expand All @@ -240,7 +238,6 @@ impl Router {
events.push(Event::TripPhaseStarting(
t,
p,
TripMode::Drive,
Some(PathRequest {
start: Position::new(current_lane, front),
end: new_pos,
Expand Down
7 changes: 0 additions & 7 deletions sim/src/sim.rs
Expand Up @@ -449,12 +449,6 @@ impl Sim {
events.push(Event::TripPhaseStarting(
trip,
person,
// TODO sketchy...
if create_car.vehicle.id.1 == VehicleType::Car {
TripMode::Drive
} else {
TripMode::Bike
},
Some(create_car.req.clone()),
if create_car.vehicle.id.1 == VehicleType::Car {
TripPhaseType::Driving
Expand Down Expand Up @@ -496,7 +490,6 @@ impl Sim {
events.push(Event::TripPhaseStarting(
create_ped.trip,
create_ped.person,
TripMode::Walk,
Some(create_ped.req.clone()),
TripPhaseType::Walking,
));
Expand Down
6 changes: 2 additions & 4 deletions sim/src/transit.rs
@@ -1,6 +1,6 @@
use crate::{
CarID, Event, PedestrianID, PersonID, Router, Scheduler, TripID, TripManager, TripMode,
TripPhaseType, WalkingSimState,
CarID, Event, PedestrianID, PersonID, Router, Scheduler, TripID, TripManager, TripPhaseType,
WalkingSimState,
};
use abstutil::{deserialize_btreemap, serialize_btreemap};
use geom::{Distance, Time};
Expand Down Expand Up @@ -187,7 +187,6 @@ impl TransitSimState {
self.events.push(Event::TripPhaseStarting(
trip,
person,
TripMode::Transit,
Some(PathRequest {
start: map.get_bs(stop1).driving_pos,
end: map.get_bs(stop2).driving_pos,
Expand Down Expand Up @@ -249,7 +248,6 @@ impl TransitSimState {
self.events.push(Event::TripPhaseStarting(
trip,
person,
TripMode::Transit,
Some(PathRequest {
start: map.get_bs(stop1).driving_pos,
end: map.get_bs(stop2).driving_pos,
Expand Down
5 changes: 1 addition & 4 deletions sim/src/trips.rs
Expand Up @@ -447,7 +447,6 @@ impl TripManager {
self.events.push(Event::TripPhaseStarting(
trip.id,
trip.person,
trip.mode,
None,
TripPhaseType::WaitingForBus(route, stop),
));
Expand Down Expand Up @@ -642,7 +641,7 @@ impl TripManager {
let trip = &mut self.trips[id.0];
self.unfinished_trips -= 1;
trip.aborted = true;
self.events.push(Event::TripAborted(trip.id, trip.mode));
self.events.push(Event::TripAborted(trip.id));
let person = trip.person;

// Maintain consistentency for anyone listening to events
Expand Down Expand Up @@ -893,7 +892,6 @@ impl TripManager {
self.events.push(Event::TripPhaseStarting(
trip,
person.id,
self.trips[trip.0].mode,
None,
TripPhaseType::DelayedStart,
));
Expand Down Expand Up @@ -1174,7 +1172,6 @@ impl TripManager {
self.events.push(Event::TripPhaseStarting(
trip,
person.id,
self.trips[trip.0].mode,
None,
TripPhaseType::Remote,
));
Expand Down

0 comments on commit 3f48b4b

Please sign in to comment.