use run_until_done in more places

This commit is contained in:
Dustin Carlino 2018-08-27 14:00:52 -07:00
parent 3333aa69a8
commit 9115a9e7f1
4 changed files with 52 additions and 42 deletions

View File

@ -29,7 +29,7 @@ struct Flags {
big_sim: bool, big_sim: bool,
/// Scenario name for savestating /// Scenario name for savestating
#[structopt(long = "scenario_name", default_value = "editor")] #[structopt(long = "scenario_name", default_value = "headless")]
scenario_name: String, scenario_name: String,
} }
@ -62,15 +62,15 @@ fn main() {
None None
}; };
let mut benchmark = sim.start_benchmark(); sim::init::run_until_done(
loop { &mut sim,
sim.step(&map, &control_map); &map,
if sim.time.is_multiple_of(sim::Tick::from_seconds(60)) { &control_map,
let speed = sim.measure_speed(&mut benchmark); |sim| {
println!("{0}, speed = {1:.2}x", sim.summary(), speed);
}
if Some(sim.time) == save_at { if Some(sim.time) == save_at {
sim.save(); sim.save();
} }
} },
|_parked| {},
);
} }

View File

@ -1,7 +1,7 @@
use abstutil; use abstutil;
use control::ControlMap; use control::ControlMap;
use map_model::{Edits, Map}; use map_model::{Edits, Map};
use {Sim, Tick}; use {ParkedCar, Sim, Tick};
// Convenience method to setup everything. // Convenience method to setup everything.
pub fn load( pub fn load(
@ -45,14 +45,24 @@ pub fn big_spawn(sim: &mut Sim, map: &Map) {
// TODO share the helpers for spawning specific parking spots and stuff? // TODO share the helpers for spawning specific parking spots and stuff?
// TODO time limit and a callback for the step results? // TODO time limit and a callback for the step results?
pub fn run_until_done(sim: &mut Sim, map: &Map, control_map: &ControlMap) { pub fn run_until_done<CB1, CB2>(
sim: &mut Sim,
map: &Map,
control_map: &ControlMap,
sim_cb: CB1,
handle_step: CB2,
) where
CB1: Fn(&Sim),
CB2: Fn(Vec<ParkedCar>),
{
let mut benchmark = sim.start_benchmark(); let mut benchmark = sim.start_benchmark();
loop { loop {
sim.step(&map, &control_map); handle_step(sim.step(&map, &control_map));
if sim.time.is_multiple_of(Tick::from_seconds(60)) { if sim.time.is_multiple_of(Tick::from_seconds(60)) {
let speed = sim.measure_speed(&mut benchmark); let speed = sim.measure_speed(&mut benchmark);
println!("{0}, speed = {1:.2}x", sim.summary(), speed); println!("{0}, speed = {1:.2}x", sim.summary(), speed);
} }
sim_cb(sim);
if sim.is_done() { if sim.is_done() {
break; break;
} }

View File

@ -12,7 +12,7 @@ fn aorta_model_completes() {
Some(sim::Tick::from_seconds(30)), Some(sim::Tick::from_seconds(30)),
); );
sim::init::small_spawn(&mut sim, &map); sim::init::small_spawn(&mut sim, &map);
sim::init::run_until_done(&mut sim, &map, &control_map); sim::init::run_until_done(&mut sim, &map, &control_map, |_sim| {}, |_parked| {});
} }
// TODO other tests (not completion) to add: // TODO other tests (not completion) to add:

View File

@ -20,19 +20,19 @@ fn park_on_goal_st() {
sim.seed_specific_parked_cars(parking2, (5..8).collect()); sim.seed_specific_parked_cars(parking2, (5..8).collect());
sim.start_parked_car_with_goal(&map, car, driving2); sim.start_parked_car_with_goal(&map, car, driving2);
loop { sim::init::run_until_done(
if let Some(p) = sim.step(&map, &control_map).first() { &mut sim,
&map,
&control_map,
|_sim| {},
|parked| {
if let Some(p) = parked.first() {
assert_eq!(p.car, car); assert_eq!(p.car, car);
assert_eq!(p.spot.lane, parking2); assert_eq!(p.spot.lane, parking2);
assert_eq!(p.spot.idx, 4); assert_eq!(p.spot.idx, 4);
break;
} }
if sim.time.is_multiple_of(sim::Tick::from_seconds(60)) { },
println!("{}", sim.summary()); );
}
// TODO time limit
}
println!("Expected conditions met at {}", sim.time);
} }
#[test] #[test]
@ -48,19 +48,19 @@ fn wander_around_for_parking() {
sim.seed_specific_parked_cars(parking2, (0..8).collect()); sim.seed_specific_parked_cars(parking2, (0..8).collect());
sim.start_parked_car_with_goal(&map, car, driving2); sim.start_parked_car_with_goal(&map, car, driving2);
loop { sim::init::run_until_done(
if let Some(p) = sim.step(&map, &control_map).first() { &mut sim,
&map,
&control_map,
|_sim| {},
|parked| {
if let Some(p) = parked.first() {
assert_eq!(p.car, car); assert_eq!(p.car, car);
assert_eq!(p.spot.lane, parking1); assert_eq!(p.spot.lane, parking1);
assert_eq!(p.spot.idx, 0); assert_eq!(p.spot.idx, 0);
break;
} }
if sim.time.is_multiple_of(sim::Tick::from_seconds(60)) { },
println!("{}", sim.summary()); );
}
// TODO time limit
}
println!("Expected conditions met at {}", sim.time);
} }
fn setup( fn setup(