diff --git a/.gitignore b/.gitignore index 136173f2eb..cf82c386a9 100644 --- a/.gitignore +++ b/.gitignore @@ -5,5 +5,6 @@ editor/editor_state editor/road_edits.json editor/sim_state +headless/sim_state data/*.abst data/input/* diff --git a/headless/Cargo.toml b/headless/Cargo.toml index cbca8bdbd1..cfc20207d9 100644 --- a/headless/Cargo.toml +++ b/headless/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" authors = ["Dustin Carlino "] [dependencies] +abstutil = { path = "../abstutil" } control = { path = "../control" } map_model = { path = "../map_model" } pretty_assertions = "0.5.1" diff --git a/headless/src/main.rs b/headless/src/main.rs index 4d120a354b..5ade5cbef9 100644 --- a/headless/src/main.rs +++ b/headless/src/main.rs @@ -1,5 +1,6 @@ // Copyright 2018 Google LLC, licensed under http://www.apache.org/licenses/LICENSE-2.0 +extern crate abstutil; extern crate control; extern crate map_model; extern crate sim; @@ -22,6 +23,14 @@ struct Flags { /// Use the old parametric sim #[structopt(long = "parametric_sim")] parametric_sim: bool, + + /// Optional time to savestate + #[structopt(long = "save_at")] + save_at: Option, + + /// Optional savestate to load + #[structopt(long = "load_from")] + load_from: Option, } fn main() { @@ -33,10 +42,16 @@ fn main() { // TODO could load savestate let control_map = control::ControlMap::new(&map); let mut sim = sim::Sim::new(&map, flags.rng_seed, flags.parametric_sim); - // TODO need a notion of scenarios - sim.seed_parked_cars(0.5); - sim.seed_pedestrians(&map, 100); - sim.start_many_parked_cars(&map, 100); + + if let Some(path) = flags.load_from { + sim = abstutil::read_json(&path).expect("loading sim state failed"); + println!("Loaded {}", path); + } else { + // TODO need a notion of scenarios + sim.seed_parked_cars(0.5); + sim.seed_pedestrians(&map, 100); + sim.start_many_parked_cars(&map, 100); + } let mut benchmark = sim.start_benchmark(); loop { @@ -45,5 +60,11 @@ fn main() { let speed = sim.measure_speed(&mut benchmark); println!("{0}, speed = {1:.2}x", sim.summary(), speed); } + if let Some(ticks) = flags.save_at { + if sim.time == sim::Tick::from_raw(ticks) { + abstutil::write_json("sim_state", &sim).expect("Writing sim state failed"); + println!("Wrote sim_state at {}", sim.time); + } + } } } diff --git a/sim/src/lib.rs b/sim/src/lib.rs index 4098dfdcb1..ad1d15fb89 100644 --- a/sim/src/lib.rs +++ b/sim/src/lib.rs @@ -81,6 +81,10 @@ impl Tick { Tick(0) } + pub fn from_raw(ticks: u32) -> Tick { + Tick(ticks) + } + pub fn as_time(&self) -> Time { (self.0 as f64) * TIMESTEP }