Remove unused RNG stuff from abstutil, and start to think about how to tease it apart a bit

This commit is contained in:
Dustin Carlino 2020-10-05 11:48:29 -07:00
parent a5cf5877e6
commit 178404bd0e
7 changed files with 20 additions and 45 deletions

2
Cargo.lock generated
View File

@ -30,8 +30,6 @@ dependencies = [
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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)", "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 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)", "serde_json 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -15,8 +15,6 @@ itertools = "0.9.0"
lazy_static = "1.4.0" lazy_static = "1.4.0"
log = { version = "0.4.11", features=["std"] } log = { version = "0.4.11", features=["std"] }
num_cpus = "1.13.0" num_cpus = "1.13.0"
rand = "0.7.0"
rand_xorshift = "0.2.0"
scoped_threadpool = "0.1.9" scoped_threadpool = "0.1.9"
serde = "1.0.116" serde = "1.0.116"
serde_json = "1.0.57" serde_json = "1.0.57"

View File

@ -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 cli;
mod collections; mod collections;
mod io; mod io;
mod random;
mod time; mod time;
pub use crate::cli::CmdArgs; pub use crate::cli::CmdArgs;
@ -16,7 +22,6 @@ pub use crate::io::{
serialize_btreemap, serialize_multimap, serialize_usize, serialized_size_bytes, slurp_file, serialize_btreemap, serialize_multimap, serialize_usize, serialized_size_bytes, slurp_file,
to_json, write_binary, write_json, FileWithProgress, to_json, write_binary, write_json, FileWithProgress,
}; };
pub use crate::random::{fork_rng, WeightedUsizeChoice};
pub use crate::time::{ pub use crate::time::{
elapsed_seconds, prettyprint_usize, start_profiler, stop_profiler, Parallelism, Profiler, elapsed_seconds, prettyprint_usize, start_profiler, stop_profiler, Parallelism, Profiler,
Timer, TimerSink, Timer, TimerSink,

View File

@ -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<usize>,
}
impl WeightedUsizeChoice {
pub fn parse(string: &str) -> Option<WeightedUsizeChoice> {
let parts: Vec<&str> = string.split(',').collect();
if parts.is_empty() {
return None;
}
let mut weights: Vec<usize> = Vec::new();
for x in parts.into_iter() {
let x = x.parse::<usize>().ok()?;
weights.push(x);
}
Some(WeightedUsizeChoice { weights })
}
pub fn sample(&self, rng: &mut XorShiftRng) -> usize {
WeightedIndex::new(&self.weights).unwrap().sample(rng)
}
}

View File

@ -1,3 +1,4 @@
use crate::make::fork_rng;
use crate::{ use crate::{
IndividTrip, PersonID, PersonSpec, Scenario, ScenarioGenerator, SpawnTrip, TripEndpoint, IndividTrip, PersonID, PersonSpec, Scenario, ScenarioGenerator, SpawnTrip, TripEndpoint,
TripMode, TripPurpose, TripMode, TripPurpose,
@ -153,7 +154,7 @@ impl ScenarioGenerator {
} }
}; };
(home, work, abstutil::fork_rng(rng)) (home, work, fork_rng(rng))
}) })
.collect(); .collect();

View File

@ -16,3 +16,11 @@ pub use self::scenario::{
IndividTrip, OffMapLocation, PersonSpec, Scenario, SpawnTrip, TripPurpose, IndividTrip, OffMapLocation, PersonSpec, Scenario, SpawnTrip, TripPurpose,
}; };
pub use self::spawner::{TripSpawner, TripSpec}; 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])
}

View File

@ -1,3 +1,4 @@
use crate::make::fork_rng;
use crate::{ use crate::{
CarID, DrivingGoal, OrigPersonID, ParkingSpot, PersonID, SidewalkPOI, SidewalkSpot, Sim, CarID, DrivingGoal, OrigPersonID, ParkingSpot, PersonID, SidewalkPOI, SidewalkSpot, Sim,
TripEndpoint, TripMode, TripSpec, Vehicle, VehicleSpec, VehicleType, BIKE_LENGTH, 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) { 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 // The RNG call might change over edits for picking the spawning lane from a border
// with multiple choices for a vehicle type. // 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( let spec = t.trip.clone().to_trip_spec(
maybe_idx.map(|idx| person.vehicles[idx].id), maybe_idx.map(|idx| person.vehicles[idx].id),
&mut tmp_rng, &mut tmp_rng,
@ -351,7 +352,7 @@ fn seed_parked_cars(
} }
// Changing parking on one road shouldn't affect far-off roads. Fork carefully. // Changing parking on one road shouldn't affect far-off roads. Fork carefully.
for r in map.all_roads() { 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) { if let Some(ref mut spots) = open_spots_per_road.get_mut(&r.id) {
spots.shuffle(&mut tmp_rng); spots.shuffle(&mut tmp_rng);
} }