From 178404bd0e95c89a89c38bedfd0ebd258fa0a9d2 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Mon, 5 Oct 2020 11:48:29 -0700 Subject: [PATCH] Remove unused RNG stuff from abstutil, and start to think about how to tease it apart a bit --- Cargo.lock | 2 -- abstutil/Cargo.toml | 2 -- abstutil/src/lib.rs | 9 +++++++-- abstutil/src/random.rs | 36 ---------------------------------- sim/src/make/activity_model.rs | 3 ++- sim/src/make/mod.rs | 8 ++++++++ sim/src/make/scenario.rs | 5 +++-- 7 files changed, 20 insertions(+), 45 deletions(-) delete mode 100644 abstutil/src/random.rs diff --git a/Cargo.lock b/Cargo.lock index 2df2aa9ff1..d66013a0e7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -30,8 +30,6 @@ dependencies = [ "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.13.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", - "rand_xorshift 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "scoped_threadpool 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.116 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/abstutil/Cargo.toml b/abstutil/Cargo.toml index 2fd87523a9..a275773d4d 100644 --- a/abstutil/Cargo.toml +++ b/abstutil/Cargo.toml @@ -15,8 +15,6 @@ itertools = "0.9.0" lazy_static = "1.4.0" log = { version = "0.4.11", features=["std"] } num_cpus = "1.13.0" -rand = "0.7.0" -rand_xorshift = "0.2.0" scoped_threadpool = "0.1.9" serde = "1.0.116" serde_json = "1.0.57" diff --git a/abstutil/src/lib.rs b/abstutil/src/lib.rs index 9ae3fe84e9..7d459630dc 100644 --- a/abstutil/src/lib.rs +++ b/abstutil/src/lib.rs @@ -1,7 +1,13 @@ +// The contents of this crate need to be organized better: +// +// - Timer (a mix of logging, profiling, and even parallel execution) +// - IO utilities, some of which have web equivalents using include_dir +// - A/B Street-specific filesystem paths +// - true utility functions (collections, prettyprinting, CLI parsing + mod cli; mod collections; mod io; -mod random; mod time; pub use crate::cli::CmdArgs; @@ -16,7 +22,6 @@ pub use crate::io::{ serialize_btreemap, serialize_multimap, serialize_usize, serialized_size_bytes, slurp_file, to_json, write_binary, write_json, FileWithProgress, }; -pub use crate::random::{fork_rng, WeightedUsizeChoice}; pub use crate::time::{ elapsed_seconds, prettyprint_usize, start_profiler, stop_profiler, Parallelism, Profiler, Timer, TimerSink, diff --git a/abstutil/src/random.rs b/abstutil/src/random.rs deleted file mode 100644 index ea280de940..0000000000 --- a/abstutil/src/random.rs +++ /dev/null @@ -1,36 +0,0 @@ -use rand::distributions::{Distribution, WeightedIndex}; -use rand::{RngCore, SeedableRng}; -use rand_xorshift::XorShiftRng; -use serde::{Deserialize, Serialize}; - -// Need to explain this trick -- basically keeps consistency between two different simulations when -// each one might make slightly different sequences of calls to the RNG. -pub fn fork_rng(base_rng: &mut XorShiftRng) -> XorShiftRng { - XorShiftRng::from_seed([base_rng.next_u32() as u8; 16]) -} - -// Represents the probability of sampling 0, 1, 2, 3... The sum can be anything. -// TODO Now unused -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct WeightedUsizeChoice { - pub weights: Vec, -} - -impl WeightedUsizeChoice { - pub fn parse(string: &str) -> Option { - let parts: Vec<&str> = string.split(',').collect(); - if parts.is_empty() { - return None; - } - let mut weights: Vec = Vec::new(); - for x in parts.into_iter() { - let x = x.parse::().ok()?; - weights.push(x); - } - Some(WeightedUsizeChoice { weights }) - } - - pub fn sample(&self, rng: &mut XorShiftRng) -> usize { - WeightedIndex::new(&self.weights).unwrap().sample(rng) - } -} diff --git a/sim/src/make/activity_model.rs b/sim/src/make/activity_model.rs index 06996a9839..180852a69d 100644 --- a/sim/src/make/activity_model.rs +++ b/sim/src/make/activity_model.rs @@ -1,3 +1,4 @@ +use crate::make::fork_rng; use crate::{ IndividTrip, PersonID, PersonSpec, Scenario, ScenarioGenerator, SpawnTrip, TripEndpoint, TripMode, TripPurpose, @@ -153,7 +154,7 @@ impl ScenarioGenerator { } }; - (home, work, abstutil::fork_rng(rng)) + (home, work, fork_rng(rng)) }) .collect(); diff --git a/sim/src/make/mod.rs b/sim/src/make/mod.rs index 9374c50801..af5f8cf3ab 100644 --- a/sim/src/make/mod.rs +++ b/sim/src/make/mod.rs @@ -16,3 +16,11 @@ pub use self::scenario::{ IndividTrip, OffMapLocation, PersonSpec, Scenario, SpawnTrip, TripPurpose, }; pub use self::spawner::{TripSpawner, TripSpec}; +use rand::{RngCore, SeedableRng}; +use rand_xorshift::XorShiftRng; + +// Need to explain this trick -- basically keeps consistency between two different simulations when +// each one might make slightly different sequences of calls to the RNG. +pub fn fork_rng(base_rng: &mut XorShiftRng) -> XorShiftRng { + XorShiftRng::from_seed([base_rng.next_u32() as u8; 16]) +} diff --git a/sim/src/make/scenario.rs b/sim/src/make/scenario.rs index 40780be20b..0ba2c775c2 100644 --- a/sim/src/make/scenario.rs +++ b/sim/src/make/scenario.rs @@ -1,3 +1,4 @@ +use crate::make::fork_rng; use crate::{ CarID, DrivingGoal, OrigPersonID, ParkingSpot, PersonID, SidewalkPOI, SidewalkSpot, Sim, TripEndpoint, TripMode, TripSpec, Vehicle, VehicleSpec, VehicleType, BIKE_LENGTH, @@ -181,7 +182,7 @@ impl Scenario { for (t, maybe_idx) in p.trips.iter().zip(vehicle_foreach_trip) { // The RNG call might change over edits for picking the spawning lane from a border // with multiple choices for a vehicle type. - let mut tmp_rng = abstutil::fork_rng(rng); + let mut tmp_rng = fork_rng(rng); let spec = t.trip.clone().to_trip_spec( maybe_idx.map(|idx| person.vehicles[idx].id), &mut tmp_rng, @@ -351,7 +352,7 @@ fn seed_parked_cars( } // Changing parking on one road shouldn't affect far-off roads. Fork carefully. for r in map.all_roads() { - let mut tmp_rng = abstutil::fork_rng(base_rng); + let mut tmp_rng = fork_rng(base_rng); if let Some(ref mut spots) = open_spots_per_road.get_mut(&r.id) { spots.shuffle(&mut tmp_rng); }