The residence time generation is now seedable

pull/33/head
Meliurwen 3 years ago
parent 651148fffd
commit 185e1756ca
  1. 4
      src/params.rs
  2. 1
      src/tools.rs
  3. 4
      tests/params.rs

@ -36,7 +36,7 @@ pub trait ParamsTrait {
/// Randomly generate a residence time for the given node taking into account the node state /// Randomly generate a residence time for the given node taking into account the node state
/// and its parent set. /// and its parent set.
fn get_random_residence_time(&self, state: usize, u: usize) -> Result<f64, ParamsError>; fn get_random_residence_time(&self, state: usize, u: usize, rng: &mut ChaCha8Rng) -> Result<f64, ParamsError>;
/// Randomly generate a possible state for the given node taking into account the node state /// Randomly generate a possible state for the given node taking into account the node state
/// and its parent set. /// and its parent set.
@ -143,7 +143,7 @@ impl ParamsTrait for DiscreteStatesContinousTimeParams {
StateType::Discrete(rng.gen_range(0..(self.domain.len()))) StateType::Discrete(rng.gen_range(0..(self.domain.len())))
} }
fn get_random_residence_time(&self, state: usize, u: usize) -> Result<f64, ParamsError> { fn get_random_residence_time(&self, state: usize, u: usize, rng: &mut ChaCha8Rng) -> Result<f64, ParamsError> {
// Generate a random residence time given the current state of the node and its parent set. // Generate a random residence time given the current state of the node and its parent set.
// The method used is described in: // The method used is described in:
// https://en.wikipedia.org/wiki/Exponential_distribution#Generating_exponential_variates // https://en.wikipedia.org/wiki/Exponential_distribution#Generating_exponential_variates

@ -56,6 +56,7 @@ pub fn trajectory_generator<T: network::Network>(
.get_random_residence_time( .get_random_residence_time(
net.get_node(idx).params.state_to_index(&current_state[idx]), net.get_node(idx).params.state_to_index(&current_state[idx]),
net.get_param_index_network(idx, &current_state), net.get_param_index_network(idx, &current_state),
&mut rng,
) )
.unwrap() .unwrap()
+ t, + t,

@ -61,7 +61,9 @@ fn test_random_generation_residence_time() {
let param = create_ternary_discrete_time_continous_param(); let param = create_ternary_discrete_time_continous_param();
let mut states = Array1::<f64>::zeros(10000); let mut states = Array1::<f64>::zeros(10000);
states.mapv_inplace(|_| param.get_random_residence_time(1, 0).unwrap()); let mut rng = ChaCha8Rng::seed_from_u64(123456);
states.mapv_inplace(|_| param.get_random_residence_time(1, 0, &mut rng).unwrap());
assert_relative_eq!(1.0 / 5.0, states.mean().unwrap(), epsilon = 0.01); assert_relative_eq!(1.0 / 5.0, states.mean().unwrap(), epsilon = 0.01);
} }

Loading…
Cancel
Save