do ID allocation more up-front

This commit is contained in:
Dustin Carlino 2019-02-27 14:15:05 -08:00
parent 84c1a94f49
commit e6ff4481e7
5 changed files with 81 additions and 63 deletions

View File

@ -66,12 +66,6 @@ impl fmt::Display for CarID {
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct PedestrianID(pub usize); pub struct PedestrianID(pub usize);
impl PedestrianID {
pub fn tmp_new(idx: usize) -> PedestrianID {
PedestrianID(idx)
}
}
impl fmt::Display for PedestrianID { impl fmt::Display for PedestrianID {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "PedestrianID({0})", self.0) write!(f, "PedestrianID({0})", self.0)

View File

@ -1,11 +1,11 @@
mod a_b_test; mod a_b_test;
mod load; mod load;
mod scenario; mod scenario;
mod spawn; mod spawner;
pub use self::a_b_test::{ABTest, ABTestResults}; pub use self::a_b_test::{ABTest, ABTestResults};
pub use self::load::SimFlags; pub use self::load::SimFlags;
pub use self::scenario::{ pub use self::scenario::{
BorderSpawnOverTime, OriginDestination, Scenario, SeedParkedCars, SpawnOverTime, BorderSpawnOverTime, OriginDestination, Scenario, SeedParkedCars, SpawnOverTime,
}; };
pub use self::spawn::{TripSpawner, TripSpec}; pub use self::spawner::{TripSpawner, TripSpec};

View File

@ -21,18 +21,13 @@ pub enum TripSpec {
#[derive(Serialize, Deserialize, PartialEq)] #[derive(Serialize, Deserialize, PartialEq)]
pub struct TripSpawner { pub struct TripSpawner {
// TODO tmp pub
pub(crate) car_id_counter: usize,
ped_id_counter: usize,
parked_cars_claimed: BTreeSet<CarID>, parked_cars_claimed: BTreeSet<CarID>,
trips: Vec<(Duration, TripSpec)>, trips: Vec<(Duration, Option<PedestrianID>, Option<CarID>, TripSpec)>,
} }
impl TripSpawner { impl TripSpawner {
pub fn new() -> TripSpawner { pub fn new() -> TripSpawner {
TripSpawner { TripSpawner {
car_id_counter: 0,
ped_id_counter: 0,
parked_cars_claimed: BTreeSet::new(), parked_cars_claimed: BTreeSet::new(),
trips: Vec::new(), trips: Vec::new(),
} }
@ -41,6 +36,8 @@ impl TripSpawner {
pub fn schedule_trip( pub fn schedule_trip(
&mut self, &mut self,
start_time: Duration, start_time: Duration,
ped_id: Option<PedestrianID>,
car_id: Option<CarID>,
spec: TripSpec, spec: TripSpec,
map: &Map, map: &Map,
parking: &ParkingSimState, parking: &ParkingSimState,
@ -108,7 +105,7 @@ impl TripSpawner {
TripSpec::UsingTransit(_, _, _, _, _) => {} TripSpec::UsingTransit(_, _, _, _, _) => {}
}; };
self.trips.push((start_time, spec)); self.trips.push((start_time, ped_id, car_id, spec));
} }
pub fn spawn_all( pub fn spawn_all(
@ -123,11 +120,11 @@ impl TripSpawner {
map, map,
self.trips self.trips
.iter() .iter()
.map(|(_, spec)| spec.get_pathfinding_request(map, parking)) .map(|(_, _, _, spec)| spec.get_pathfinding_request(map, parking))
.collect(), .collect(),
timer, timer,
); );
for ((start_time, spec), maybe_path) in self.trips.drain(..).zip(paths) { for ((start_time, ped_id, car_id, spec), maybe_path) in self.trips.drain(..).zip(paths) {
if maybe_path.is_none() { if maybe_path.is_none() {
timer.warn(format!("{:?} couldn't find the first path", spec)); timer.warn(format!("{:?} couldn't find the first path", spec));
continue; continue;
@ -135,12 +132,7 @@ impl TripSpawner {
let path = maybe_path.unwrap(); let path = maybe_path.unwrap();
match spec { match spec {
TripSpec::CarAppearing(start_pos, vehicle_spec, goal) => { TripSpec::CarAppearing(start_pos, vehicle_spec, goal) => {
let car_id = CarID(self.car_id_counter, VehicleType::Car); let mut legs = vec![TripLeg::Drive(car_id.unwrap(), goal.clone())];
self.car_id_counter += 1;
let ped_id = PedestrianID(self.ped_id_counter);
self.ped_id_counter += 1;
let mut legs = vec![TripLeg::Drive(car_id, goal.clone())];
let router = match goal { let router = match goal {
DrivingGoal::ParkNear(b) => { DrivingGoal::ParkNear(b) => {
legs.push(TripLeg::Walk(SidewalkSpot::building(b, map))); legs.push(TripLeg::Walk(SidewalkSpot::building(b, map)));
@ -150,12 +142,12 @@ impl TripSpawner {
Router::stop_suddenly(path, map.get_l(last_lane).length()) Router::stop_suddenly(path, map.get_l(last_lane).length())
} }
}; };
let trip = trips.new_trip(start_time, Some(ped_id), legs); let trip = trips.new_trip(start_time, ped_id, legs);
scheduler.enqueue_command(Command::SpawnCar( scheduler.enqueue_command(Command::SpawnCar(
start_time, start_time,
CreateCar::for_appearing( CreateCar::for_appearing(
vehicle_spec.make(car_id, None), vehicle_spec.make(car_id.unwrap(), None),
start_pos, start_pos,
router, router,
trip, trip,
@ -163,8 +155,6 @@ impl TripSpawner {
)); ));
} }
TripSpec::UsingParkedCar(start, spot, goal) => { TripSpec::UsingParkedCar(start, spot, goal) => {
let ped_id = PedestrianID(self.ped_id_counter);
self.ped_id_counter += 1;
let vehicle = &parking.get_car_at_spot(spot).unwrap().vehicle; let vehicle = &parking.get_car_at_spot(spot).unwrap().vehicle;
match start.connection { match start.connection {
SidewalkPOI::Building(b) => assert_eq!(vehicle.owner, Some(b)), SidewalkPOI::Building(b) => assert_eq!(vehicle.owner, Some(b)),
@ -183,12 +173,12 @@ impl TripSpawner {
} }
DrivingGoal::Border(_, _) => {} DrivingGoal::Border(_, _) => {}
} }
let trip = trips.new_trip(start_time, Some(ped_id), legs); let trip = trips.new_trip(start_time, ped_id, legs);
scheduler.enqueue_command(Command::SpawnPed( scheduler.enqueue_command(Command::SpawnPed(
start_time, start_time,
CreatePedestrian { CreatePedestrian {
id: ped_id, id: ped_id.unwrap(),
start, start,
goal: parking_spot, goal: parking_spot,
path, path,
@ -197,16 +187,13 @@ impl TripSpawner {
)); ));
} }
TripSpec::JustWalking(start, goal) => { TripSpec::JustWalking(start, goal) => {
let ped_id = PedestrianID(self.ped_id_counter);
self.ped_id_counter += 1;
let trip = let trip =
trips.new_trip(start_time, Some(ped_id), vec![TripLeg::Walk(goal.clone())]); trips.new_trip(start_time, ped_id, vec![TripLeg::Walk(goal.clone())]);
scheduler.enqueue_command(Command::SpawnPed( scheduler.enqueue_command(Command::SpawnPed(
start_time, start_time,
CreatePedestrian { CreatePedestrian {
id: ped_id, id: ped_id.unwrap(),
start, start,
goal, goal,
path, path,
@ -215,15 +202,10 @@ impl TripSpawner {
)); ));
} }
TripSpec::UsingBike(start, vehicle, goal) => { TripSpec::UsingBike(start, vehicle, goal) => {
let ped_id = PedestrianID(self.ped_id_counter);
self.ped_id_counter += 1;
let bike_id = CarID(self.car_id_counter, VehicleType::Bike);
self.car_id_counter += 1;
let walk_to = SidewalkSpot::bike_rack(start.sidewalk_pos.lane(), map).unwrap(); let walk_to = SidewalkSpot::bike_rack(start.sidewalk_pos.lane(), map).unwrap();
let mut legs = vec![ let mut legs = vec![
TripLeg::Walk(walk_to.clone()), TripLeg::Walk(walk_to.clone()),
TripLeg::Bike(vehicle.make(bike_id, None), goal.clone()), TripLeg::Bike(vehicle.make(car_id.unwrap(), None), goal.clone()),
]; ];
match goal { match goal {
DrivingGoal::ParkNear(b) => { DrivingGoal::ParkNear(b) => {
@ -231,12 +213,12 @@ impl TripSpawner {
} }
DrivingGoal::Border(_, _) => {} DrivingGoal::Border(_, _) => {}
} }
let trip = trips.new_trip(start_time, Some(ped_id), legs); let trip = trips.new_trip(start_time, ped_id, legs);
scheduler.enqueue_command(Command::SpawnPed( scheduler.enqueue_command(Command::SpawnPed(
start_time, start_time,
CreatePedestrian { CreatePedestrian {
id: ped_id, id: ped_id.unwrap(),
start, start,
goal: walk_to, goal: walk_to,
path, path,

View File

@ -1,8 +1,8 @@
use crate::{ use crate::{
AgentID, Benchmark, CarID, DrawCarInput, DrawPedestrianInput, DrivingSimState, Event, AgentID, Benchmark, CarID, DrawCarInput, DrawPedestrianInput, DrivingGoal, DrivingSimState,
GetDrawAgents, IntersectionSimState, ParkedCar, ParkingSimState, ParkingSpot, PedestrianID, Event, GetDrawAgents, IntersectionSimState, ParkedCar, ParkingSimState, ParkingSpot,
Scheduler, ScoreSummary, SimStats, Summary, TripID, TripManager, TripSpawner, TripSpec, PedestrianID, Scheduler, ScoreSummary, SimStats, Summary, TripID, TripManager, TripSpawner,
VehicleSpec, VehicleType, WalkingSimState, TIMESTEP, TripSpec, VehicleSpec, VehicleType, WalkingSimState, TIMESTEP,
}; };
use abstutil::Timer; use abstutil::Timer;
use derivative::Derivative; use derivative::Derivative;
@ -27,6 +27,8 @@ pub struct Sim {
scheduler: Scheduler, scheduler: Scheduler,
spawner: TripSpawner, spawner: TripSpawner,
time: Duration, time: Duration,
car_id_counter: usize,
ped_id_counter: usize,
// TODO Reconsider these // TODO Reconsider these
pub(crate) map_name: String, pub(crate) map_name: String,
@ -55,6 +57,8 @@ impl Sim {
scheduler: Scheduler::new(), scheduler: Scheduler::new(),
spawner: TripSpawner::new(), spawner: TripSpawner::new(),
time: Duration::ZERO, time: Duration::ZERO,
car_id_counter: 0,
ped_id_counter: 0,
map_name: map.get_name().to_string(), map_name: map.get_name().to_string(),
edits_name: map.get_edits().edits_name.to_string(), edits_name: map.get_edits().edits_name.to_string(),
@ -64,9 +68,45 @@ impl Sim {
} }
} }
pub fn schedule_trip(&mut self, start_time: Duration, spec: TripSpec, map: &Map) { pub fn schedule_trip(
&mut self,
start_time: Duration,
spec: TripSpec,
map: &Map,
) -> (Option<PedestrianID>, Option<CarID>) {
let (ped_id, car_id) = match spec {
TripSpec::CarAppearing(_, ref spec, ref goal) => {
let car = CarID(self.car_id_counter, spec.vehicle_type);
self.car_id_counter += 1;
let ped = match goal {
DrivingGoal::ParkNear(_) => {
let id = PedestrianID(self.ped_id_counter);
self.ped_id_counter += 1;
Some(id)
}
_ => None,
};
(ped, Some(car))
}
TripSpec::UsingParkedCar(_, _, _)
| TripSpec::JustWalking(_, _)
| TripSpec::UsingTransit(_, _, _, _, _) => {
let id = PedestrianID(self.ped_id_counter);
self.ped_id_counter += 1;
(Some(id), None)
}
TripSpec::UsingBike(_, _, ref goal) => {
let ped = PedestrianID(self.ped_id_counter);
self.ped_id_counter += 1;
let car = CarID(self.car_id_counter, VehicleType::Bike);
self.car_id_counter += 1;
(Some(ped), Some(car))
}
};
self.spawner self.spawner
.schedule_trip(start_time, spec, map, &self.parking); .schedule_trip(start_time, ped_id, car_id, spec, map, &self.parking);
(ped_id, car_id)
} }
pub fn spawn_all_trips(&mut self, map: &Map, timer: &mut Timer) { pub fn spawn_all_trips(&mut self, map: &Map, timer: &mut Timer) {
@ -89,8 +129,9 @@ impl Sim {
spot: ParkingSpot, spot: ParkingSpot,
owner: Option<BuildingID>, owner: Option<BuildingID>,
) -> CarID { ) -> CarID {
let id = CarID(self.spawner.car_id_counter, VehicleType::Car); let id = CarID(self.car_id_counter, VehicleType::Car);
self.spawner.car_id_counter += 1; self.car_id_counter += 1;
self.parking.reserve_spot(spot); self.parking.reserve_spot(spot);
self.parking.add_parked_car(ParkedCar { self.parking.add_parked_car(ParkedCar {
vehicle: vehicle.make(id, owner), vehicle: vehicle.make(id, owner),

View File

@ -42,19 +42,20 @@ pub fn run(t: &mut TestRunner) {
let goal_bldg = map let goal_bldg = map
.get_l(map.get_bs(ped_stop2).sidewalk_pos.lane()) .get_l(map.get_bs(ped_stop2).sidewalk_pos.lane())
.building_paths[0]; .building_paths[0];
sim.schedule_trip( let ped = sim
Duration::ZERO, .schedule_trip(
TripSpec::UsingTransit( Duration::ZERO,
SidewalkSpot::building(start_bldg, &map), TripSpec::UsingTransit(
route.id, SidewalkSpot::building(start_bldg, &map),
ped_stop1, route.id,
ped_stop2, ped_stop1,
SidewalkSpot::building(goal_bldg, &map), ped_stop2,
), SidewalkSpot::building(goal_bldg, &map),
&map, ),
); &map,
// TODO ew )
let ped = sim::PedestrianID::tmp_new(0); .0
.unwrap();
h.setup_done(&sim); h.setup_done(&sim);
sim.run_until_expectations_met( sim.run_until_expectations_met(