Don't touch SimFlags in headless at all

This commit is contained in:
Dustin Carlino 2020-09-24 17:10:07 -07:00
parent da1921ae74
commit e1b079900a
3 changed files with 48 additions and 41 deletions

View File

@ -35,13 +35,12 @@ lazy_static::lazy_static! {
static ref MAP: RwLock<Map> = RwLock::new(Map::blank()); static ref MAP: RwLock<Map> = RwLock::new(Map::blank());
static ref SIM: RwLock<Sim> = RwLock::new(Sim::new(&Map::blank(), SimOptions::new("tmp"), &mut Timer::throwaway())); static ref SIM: RwLock<Sim> = RwLock::new(Sim::new(&Map::blank(), SimOptions::new("tmp"), &mut Timer::throwaway()));
static ref LOAD: RwLock<LoadSim> = RwLock::new({ static ref LOAD: RwLock<LoadSim> = RwLock::new({
let flags = SimFlags::for_test("tmp");
LoadSim { LoadSim {
scenario: abstutil::path_scenario("montlake", "weekday"), scenario: abstutil::path_scenario("montlake", "weekday"),
modifiers: Vec::new(), modifiers: Vec::new(),
edits: None, edits: None,
rng_seed: flags.rng_seed, rng_seed: SimFlags::RNG_SEED,
opts: flags.opts, opts: SimOptions::default(),
} }
}); });
} }
@ -50,15 +49,17 @@ lazy_static::lazy_static! {
async fn main() { async fn main() {
let mut args = CmdArgs::new(); let mut args = CmdArgs::new();
let mut timer = Timer::new("setup headless"); let mut timer = Timer::new("setup headless");
// TODO Misleading, because we ignore the free argument (load) let rng_seed = args
let sim_flags = SimFlags::from_args(&mut 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::<u16>().unwrap(); let port = args.required("--port").parse::<u16>().unwrap();
args.done(); args.done();
{ {
let mut load = LOAD.write().unwrap(); let mut load = LOAD.write().unwrap();
load.rng_seed = sim_flags.rng_seed; load.rng_seed = rng_seed;
load.opts = sim_flags.opts; load.opts = opts;
let (map, sim) = load.setup(&mut timer); let (map, sim) = load.setup(&mut timer);
*MAP.write().unwrap() = map; *MAP.write().unwrap() = map;

View File

@ -1,11 +1,9 @@
use crate::{AlertHandler, Scenario, ScenarioModifier, Sim, SimOptions}; use crate::{Scenario, ScenarioModifier, Sim, SimOptions};
use abstutil::CmdArgs; use abstutil::CmdArgs;
use map_model::{Map, MapEdits}; use map_model::{Map, MapEdits};
use rand::SeedableRng; use rand::SeedableRng;
use rand_xorshift::XorShiftRng; use rand_xorshift::XorShiftRng;
const RNG_SEED: u8 = 42;
#[derive(Clone)] #[derive(Clone)]
pub struct SimFlags { pub struct SimFlags {
pub load: String, pub load: String,
@ -15,44 +13,19 @@ pub struct SimFlags {
} }
impl SimFlags { impl SimFlags {
pub const RNG_SEED: u8 = 42;
pub fn from_args(args: &mut CmdArgs) -> SimFlags { pub fn from_args(args: &mut CmdArgs) -> SimFlags {
let rng_seed = args let rng_seed = args
.optional_parse("--rng_seed", |s| s.parse()) .optional_parse("--rng_seed", |s| s.parse())
.unwrap_or(RNG_SEED); .unwrap_or(SimFlags::RNG_SEED);
SimFlags { SimFlags {
load: args load: args
.optional_free() .optional_free()
.unwrap_or_else(|| abstutil::path_map("montlake")), .unwrap_or_else(|| abstutil::path_map("montlake")),
modifiers: Vec::new(), modifiers: Vec::new(),
rng_seed, rng_seed,
opts: SimOptions { opts: SimOptions::from_args(args, rng_seed),
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"),
},
} }
} }
@ -65,7 +38,7 @@ impl SimFlags {
SimFlags { SimFlags {
load: abstutil::path_map(map), load: abstutil::path_map(map),
modifiers: Vec::new(), modifiers: Vec::new(),
rng_seed: RNG_SEED, rng_seed: SimFlags::RNG_SEED,
opts: SimOptions::new(run_name), opts: SimOptions::new(run_name),
} }
} }

View File

@ -10,13 +10,14 @@ use crate::{
VehicleSpec, VehicleType, WalkingSimState, BUS_LENGTH, LIGHT_RAIL_LENGTH, MIN_CAR_LENGTH, VehicleSpec, VehicleType, WalkingSimState, BUS_LENGTH, LIGHT_RAIL_LENGTH, MIN_CAR_LENGTH,
SPAWN_DIST, 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 geom::{Distance, Duration, Speed, Time};
use instant::Instant; use instant::Instant;
use map_model::{ use map_model::{
BuildingID, BusRoute, LaneID, Map, ParkingLotID, Path, PathConstraints, PathRequest, Position, BuildingID, BusRoute, LaneID, Map, ParkingLotID, Path, PathConstraints, PathRequest, Position,
Traversable, Traversable,
}; };
use rand::SeedableRng;
use rand_xorshift::XorShiftRng; use rand_xorshift::XorShiftRng;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::{BTreeSet, HashSet}; 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)] #[derive(Clone)]
pub enum AlertHandler { pub enum AlertHandler {
// Just print the alert to STDOUT // Just print the alert to STDOUT