flag to tell headless to savestate

This commit is contained in:
Dustin Carlino 2018-08-09 11:10:36 -07:00
parent 40cb2bf6f4
commit 3e9a2db0bd
4 changed files with 31 additions and 4 deletions

1
.gitignore vendored
View File

@ -5,5 +5,6 @@
editor/editor_state
editor/road_edits.json
editor/sim_state
headless/sim_state
data/*.abst
data/input/*

View File

@ -4,6 +4,7 @@ version = "0.1.0"
authors = ["Dustin Carlino <dabreegster@gmail.com>"]
[dependencies]
abstutil = { path = "../abstutil" }
control = { path = "../control" }
map_model = { path = "../map_model" }
pretty_assertions = "0.5.1"

View File

@ -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<u32>,
/// Optional savestate to load
#[structopt(long = "load_from")]
load_from: Option<String>,
}
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);
}
}
}
}

View File

@ -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
}