mirror of
https://github.com/a-b-street/abstreet.git
synced 2024-11-24 01:15:12 +03:00
do ID allocation more up-front
This commit is contained in:
parent
84c1a94f49
commit
e6ff4481e7
@ -66,12 +66,6 @@ impl fmt::Display for CarID {
|
||||
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
|
||||
pub struct PedestrianID(pub usize);
|
||||
|
||||
impl PedestrianID {
|
||||
pub fn tmp_new(idx: usize) -> PedestrianID {
|
||||
PedestrianID(idx)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for PedestrianID {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "PedestrianID({0})", self.0)
|
||||
|
@ -1,11 +1,11 @@
|
||||
mod a_b_test;
|
||||
mod load;
|
||||
mod scenario;
|
||||
mod spawn;
|
||||
mod spawner;
|
||||
|
||||
pub use self::a_b_test::{ABTest, ABTestResults};
|
||||
pub use self::load::SimFlags;
|
||||
pub use self::scenario::{
|
||||
BorderSpawnOverTime, OriginDestination, Scenario, SeedParkedCars, SpawnOverTime,
|
||||
};
|
||||
pub use self::spawn::{TripSpawner, TripSpec};
|
||||
pub use self::spawner::{TripSpawner, TripSpec};
|
||||
|
@ -21,18 +21,13 @@ pub enum TripSpec {
|
||||
|
||||
#[derive(Serialize, Deserialize, PartialEq)]
|
||||
pub struct TripSpawner {
|
||||
// TODO tmp pub
|
||||
pub(crate) car_id_counter: usize,
|
||||
ped_id_counter: usize,
|
||||
parked_cars_claimed: BTreeSet<CarID>,
|
||||
trips: Vec<(Duration, TripSpec)>,
|
||||
trips: Vec<(Duration, Option<PedestrianID>, Option<CarID>, TripSpec)>,
|
||||
}
|
||||
|
||||
impl TripSpawner {
|
||||
pub fn new() -> TripSpawner {
|
||||
TripSpawner {
|
||||
car_id_counter: 0,
|
||||
ped_id_counter: 0,
|
||||
parked_cars_claimed: BTreeSet::new(),
|
||||
trips: Vec::new(),
|
||||
}
|
||||
@ -41,6 +36,8 @@ impl TripSpawner {
|
||||
pub fn schedule_trip(
|
||||
&mut self,
|
||||
start_time: Duration,
|
||||
ped_id: Option<PedestrianID>,
|
||||
car_id: Option<CarID>,
|
||||
spec: TripSpec,
|
||||
map: &Map,
|
||||
parking: &ParkingSimState,
|
||||
@ -108,7 +105,7 @@ impl TripSpawner {
|
||||
TripSpec::UsingTransit(_, _, _, _, _) => {}
|
||||
};
|
||||
|
||||
self.trips.push((start_time, spec));
|
||||
self.trips.push((start_time, ped_id, car_id, spec));
|
||||
}
|
||||
|
||||
pub fn spawn_all(
|
||||
@ -123,11 +120,11 @@ impl TripSpawner {
|
||||
map,
|
||||
self.trips
|
||||
.iter()
|
||||
.map(|(_, spec)| spec.get_pathfinding_request(map, parking))
|
||||
.map(|(_, _, _, spec)| spec.get_pathfinding_request(map, parking))
|
||||
.collect(),
|
||||
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() {
|
||||
timer.warn(format!("{:?} couldn't find the first path", spec));
|
||||
continue;
|
||||
@ -135,12 +132,7 @@ impl TripSpawner {
|
||||
let path = maybe_path.unwrap();
|
||||
match spec {
|
||||
TripSpec::CarAppearing(start_pos, vehicle_spec, goal) => {
|
||||
let car_id = CarID(self.car_id_counter, VehicleType::Car);
|
||||
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 mut legs = vec![TripLeg::Drive(car_id.unwrap(), goal.clone())];
|
||||
let router = match goal {
|
||||
DrivingGoal::ParkNear(b) => {
|
||||
legs.push(TripLeg::Walk(SidewalkSpot::building(b, map)));
|
||||
@ -150,12 +142,12 @@ impl TripSpawner {
|
||||
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(
|
||||
start_time,
|
||||
CreateCar::for_appearing(
|
||||
vehicle_spec.make(car_id, None),
|
||||
vehicle_spec.make(car_id.unwrap(), None),
|
||||
start_pos,
|
||||
router,
|
||||
trip,
|
||||
@ -163,8 +155,6 @@ impl TripSpawner {
|
||||
));
|
||||
}
|
||||
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;
|
||||
match start.connection {
|
||||
SidewalkPOI::Building(b) => assert_eq!(vehicle.owner, Some(b)),
|
||||
@ -183,12 +173,12 @@ impl TripSpawner {
|
||||
}
|
||||
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(
|
||||
start_time,
|
||||
CreatePedestrian {
|
||||
id: ped_id,
|
||||
id: ped_id.unwrap(),
|
||||
start,
|
||||
goal: parking_spot,
|
||||
path,
|
||||
@ -197,16 +187,13 @@ impl TripSpawner {
|
||||
));
|
||||
}
|
||||
TripSpec::JustWalking(start, goal) => {
|
||||
let ped_id = PedestrianID(self.ped_id_counter);
|
||||
self.ped_id_counter += 1;
|
||||
|
||||
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(
|
||||
start_time,
|
||||
CreatePedestrian {
|
||||
id: ped_id,
|
||||
id: ped_id.unwrap(),
|
||||
start,
|
||||
goal,
|
||||
path,
|
||||
@ -215,15 +202,10 @@ impl TripSpawner {
|
||||
));
|
||||
}
|
||||
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 mut legs = vec![
|
||||
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 {
|
||||
DrivingGoal::ParkNear(b) => {
|
||||
@ -231,12 +213,12 @@ impl TripSpawner {
|
||||
}
|
||||
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(
|
||||
start_time,
|
||||
CreatePedestrian {
|
||||
id: ped_id,
|
||||
id: ped_id.unwrap(),
|
||||
start,
|
||||
goal: walk_to,
|
||||
path,
|
@ -1,8 +1,8 @@
|
||||
use crate::{
|
||||
AgentID, Benchmark, CarID, DrawCarInput, DrawPedestrianInput, DrivingSimState, Event,
|
||||
GetDrawAgents, IntersectionSimState, ParkedCar, ParkingSimState, ParkingSpot, PedestrianID,
|
||||
Scheduler, ScoreSummary, SimStats, Summary, TripID, TripManager, TripSpawner, TripSpec,
|
||||
VehicleSpec, VehicleType, WalkingSimState, TIMESTEP,
|
||||
AgentID, Benchmark, CarID, DrawCarInput, DrawPedestrianInput, DrivingGoal, DrivingSimState,
|
||||
Event, GetDrawAgents, IntersectionSimState, ParkedCar, ParkingSimState, ParkingSpot,
|
||||
PedestrianID, Scheduler, ScoreSummary, SimStats, Summary, TripID, TripManager, TripSpawner,
|
||||
TripSpec, VehicleSpec, VehicleType, WalkingSimState, TIMESTEP,
|
||||
};
|
||||
use abstutil::Timer;
|
||||
use derivative::Derivative;
|
||||
@ -27,6 +27,8 @@ pub struct Sim {
|
||||
scheduler: Scheduler,
|
||||
spawner: TripSpawner,
|
||||
time: Duration,
|
||||
car_id_counter: usize,
|
||||
ped_id_counter: usize,
|
||||
|
||||
// TODO Reconsider these
|
||||
pub(crate) map_name: String,
|
||||
@ -55,6 +57,8 @@ impl Sim {
|
||||
scheduler: Scheduler::new(),
|
||||
spawner: TripSpawner::new(),
|
||||
time: Duration::ZERO,
|
||||
car_id_counter: 0,
|
||||
ped_id_counter: 0,
|
||||
|
||||
map_name: map.get_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
|
||||
.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) {
|
||||
@ -89,8 +129,9 @@ impl Sim {
|
||||
spot: ParkingSpot,
|
||||
owner: Option<BuildingID>,
|
||||
) -> CarID {
|
||||
let id = CarID(self.spawner.car_id_counter, VehicleType::Car);
|
||||
self.spawner.car_id_counter += 1;
|
||||
let id = CarID(self.car_id_counter, VehicleType::Car);
|
||||
self.car_id_counter += 1;
|
||||
|
||||
self.parking.reserve_spot(spot);
|
||||
self.parking.add_parked_car(ParkedCar {
|
||||
vehicle: vehicle.make(id, owner),
|
||||
|
@ -42,19 +42,20 @@ pub fn run(t: &mut TestRunner) {
|
||||
let goal_bldg = map
|
||||
.get_l(map.get_bs(ped_stop2).sidewalk_pos.lane())
|
||||
.building_paths[0];
|
||||
sim.schedule_trip(
|
||||
Duration::ZERO,
|
||||
TripSpec::UsingTransit(
|
||||
SidewalkSpot::building(start_bldg, &map),
|
||||
route.id,
|
||||
ped_stop1,
|
||||
ped_stop2,
|
||||
SidewalkSpot::building(goal_bldg, &map),
|
||||
),
|
||||
&map,
|
||||
);
|
||||
// TODO ew
|
||||
let ped = sim::PedestrianID::tmp_new(0);
|
||||
let ped = sim
|
||||
.schedule_trip(
|
||||
Duration::ZERO,
|
||||
TripSpec::UsingTransit(
|
||||
SidewalkSpot::building(start_bldg, &map),
|
||||
route.id,
|
||||
ped_stop1,
|
||||
ped_stop2,
|
||||
SidewalkSpot::building(goal_bldg, &map),
|
||||
),
|
||||
&map,
|
||||
)
|
||||
.0
|
||||
.unwrap();
|
||||
h.setup_done(&sim);
|
||||
|
||||
sim.run_until_expectations_met(
|
||||
|
Loading…
Reference in New Issue
Block a user