From e1b079900a389da1e464ebecbcfe12b98817e854 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Thu, 24 Sep 2020 17:10:07 -0700 Subject: [PATCH] Don't touch SimFlags in headless at all --- headless/src/main.rs | 15 ++++++++------- sim/src/make/load.rs | 39 ++++++--------------------------------- sim/src/sim/mod.rs | 35 ++++++++++++++++++++++++++++++++++- 3 files changed, 48 insertions(+), 41 deletions(-) diff --git a/headless/src/main.rs b/headless/src/main.rs index 423fb7d21d..831a356911 100644 --- a/headless/src/main.rs +++ b/headless/src/main.rs @@ -35,13 +35,12 @@ lazy_static::lazy_static! { static ref MAP: RwLock = RwLock::new(Map::blank()); static ref SIM: RwLock = RwLock::new(Sim::new(&Map::blank(), SimOptions::new("tmp"), &mut Timer::throwaway())); static ref LOAD: RwLock = RwLock::new({ - let flags = SimFlags::for_test("tmp"); LoadSim { scenario: abstutil::path_scenario("montlake", "weekday"), modifiers: Vec::new(), edits: None, - rng_seed: flags.rng_seed, - opts: flags.opts, + rng_seed: SimFlags::RNG_SEED, + opts: SimOptions::default(), } }); } @@ -50,15 +49,17 @@ lazy_static::lazy_static! { async fn main() { let mut args = CmdArgs::new(); let mut timer = Timer::new("setup headless"); - // TODO Misleading, because we ignore the free argument (load) - let sim_flags = SimFlags::from_args(&mut args); + let rng_seed = args + .optional_parse("--rng_seed", |s| s.parse()) + .unwrap_or(SimFlags::RNG_SEED); + let opts = SimOptions::from_args(&mut args, rng_seed); let port = args.required("--port").parse::().unwrap(); args.done(); { let mut load = LOAD.write().unwrap(); - load.rng_seed = sim_flags.rng_seed; - load.opts = sim_flags.opts; + load.rng_seed = rng_seed; + load.opts = opts; let (map, sim) = load.setup(&mut timer); *MAP.write().unwrap() = map; diff --git a/sim/src/make/load.rs b/sim/src/make/load.rs index f3f1fb54df..ff35db8a23 100644 --- a/sim/src/make/load.rs +++ b/sim/src/make/load.rs @@ -1,11 +1,9 @@ -use crate::{AlertHandler, Scenario, ScenarioModifier, Sim, SimOptions}; +use crate::{Scenario, ScenarioModifier, Sim, SimOptions}; use abstutil::CmdArgs; use map_model::{Map, MapEdits}; use rand::SeedableRng; use rand_xorshift::XorShiftRng; -const RNG_SEED: u8 = 42; - #[derive(Clone)] pub struct SimFlags { pub load: String, @@ -15,44 +13,19 @@ pub struct SimFlags { } impl SimFlags { + pub const RNG_SEED: u8 = 42; + pub fn from_args(args: &mut CmdArgs) -> SimFlags { let rng_seed = args .optional_parse("--rng_seed", |s| s.parse()) - .unwrap_or(RNG_SEED); - + .unwrap_or(SimFlags::RNG_SEED); SimFlags { load: args .optional_free() .unwrap_or_else(|| abstutil::path_map("montlake")), modifiers: Vec::new(), rng_seed, - opts: SimOptions { - run_name: args - .optional("--run_name") - .unwrap_or_else(|| "unnamed".to_string()), - use_freeform_policy_everywhere: args.enabled("--freeform_policy"), - dont_block_the_box: !args.enabled("--disable_block_the_box"), - recalc_lanechanging: !args.enabled("--disable_recalc_lc"), - break_turn_conflict_cycles: !args.enabled("--disable_break_turn_conflict_cycles"), - handle_uber_turns: !args.enabled("--disable_handle_uber_turns"), - enable_pandemic_model: if args.enabled("--pandemic") { - Some(XorShiftRng::from_seed([rng_seed; 16])) - } else { - None - }, - alerts: args - .optional("--alerts") - .map(|x| match x.as_ref() { - "print" => AlertHandler::Print, - "block" => AlertHandler::Block, - "silence" => AlertHandler::Silence, - _ => panic!("Bad --alerts={}. Must be print|block|silence", x), - }) - .unwrap_or(AlertHandler::Print), - pathfinding_upfront: args.enabled("--pathfinding_upfront"), - live_map_edits: args.enabled("--live_map_edits"), - infinite_parking: args.enabled("--infinite_parking"), - }, + opts: SimOptions::from_args(args, rng_seed), } } @@ -65,7 +38,7 @@ impl SimFlags { SimFlags { load: abstutil::path_map(map), modifiers: Vec::new(), - rng_seed: RNG_SEED, + rng_seed: SimFlags::RNG_SEED, opts: SimOptions::new(run_name), } } diff --git a/sim/src/sim/mod.rs b/sim/src/sim/mod.rs index d8c98d9cdb..ba905d1535 100644 --- a/sim/src/sim/mod.rs +++ b/sim/src/sim/mod.rs @@ -10,13 +10,14 @@ use crate::{ VehicleSpec, VehicleType, WalkingSimState, BUS_LENGTH, LIGHT_RAIL_LENGTH, MIN_CAR_LENGTH, SPAWN_DIST, }; -use abstutil::{prettyprint_usize, serialized_size_bytes, Parallelism, Timer}; +use abstutil::{prettyprint_usize, serialized_size_bytes, CmdArgs, Parallelism, Timer}; use geom::{Distance, Duration, Speed, Time}; use instant::Instant; use map_model::{ BuildingID, BusRoute, LaneID, Map, ParkingLotID, Path, PathConstraints, PathRequest, Position, Traversable, }; +use rand::SeedableRng; use rand_xorshift::XorShiftRng; use serde::{Deserialize, Serialize}; use std::collections::{BTreeSet, HashSet}; @@ -85,6 +86,38 @@ impl std::default::Default for SimOptions { } } +impl SimOptions { + pub fn from_args(args: &mut CmdArgs, rng_seed: u8) -> SimOptions { + SimOptions { + run_name: args + .optional("--run_name") + .unwrap_or_else(|| "unnamed".to_string()), + use_freeform_policy_everywhere: args.enabled("--freeform_policy"), + dont_block_the_box: !args.enabled("--disable_block_the_box"), + recalc_lanechanging: !args.enabled("--disable_recalc_lc"), + break_turn_conflict_cycles: !args.enabled("--disable_break_turn_conflict_cycles"), + handle_uber_turns: !args.enabled("--disable_handle_uber_turns"), + enable_pandemic_model: if args.enabled("--pandemic") { + Some(XorShiftRng::from_seed([rng_seed; 16])) + } else { + None + }, + alerts: args + .optional("--alerts") + .map(|x| match x.as_ref() { + "print" => AlertHandler::Print, + "block" => AlertHandler::Block, + "silence" => AlertHandler::Silence, + _ => panic!("Bad --alerts={}. Must be print|block|silence", x), + }) + .unwrap_or(AlertHandler::Print), + pathfinding_upfront: args.enabled("--pathfinding_upfront"), + live_map_edits: args.enabled("--live_map_edits"), + infinite_parking: args.enabled("--infinite_parking"), + } + } +} + #[derive(Clone)] pub enum AlertHandler { // Just print the alert to STDOUT