Skip to content

Commit 5bb9246

Browse files
committed
Refine the organization of random variable, simulator, and error handling modules
1 parent a3259d4 commit 5bb9246

22 files changed

+98
-101
lines changed

sim/src/input_modeling/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@ pub mod random_variable;
88
pub mod thinning;
99
pub mod uniform_rng;
1010

11+
pub use random_variable::Boolean as BooleanRandomVariable;
12+
pub use random_variable::Continuous as ContinuousRandomVariable;
13+
pub use random_variable::Discrete as DiscreteRandomVariable;
14+
pub use random_variable::Index as IndexRandomVariable;
1115
pub use thinning::Thinning;
1216
pub use uniform_rng::UniformRNG;

sim/src/input_modeling/random_variable.rs

Lines changed: 45 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
//! Random variables underpin both stochastic and deterministic model
22
//! behaviors, in that deterministic operation is simply a random variable
33
//! with a single value of probability 1. Common distributions, with their
4-
//! common parameterizations, are wrapped in enums
5-
//! `ContinuousRandomVariable`, `BooleanRandomVariable`,
6-
//! `DiscreteRandomVariable`, and `IndexRandomVariable`.
4+
//! common parameterizations, are wrapped in enums `Continuous`, `Boolean`,
5+
//! `Discrete`, and `Index`.
76
87
use rand::distributions::Distribution;
98
use serde::{Deserialize, Serialize};
@@ -13,11 +12,11 @@ use rand_distr::{Beta, Exp, Gamma, LogNormal, Normal, Triangular, Uniform, Weibu
1312
use rand_distr::{Bernoulli, Geometric, Poisson, WeightedIndex};
1413

1514
use super::UniformRNG;
16-
use crate::utils::error::SimulationError;
15+
use crate::utils::errors::SimulationError;
1716

1817
#[derive(Debug, Clone, Serialize, Deserialize)]
1918
#[serde(rename_all = "camelCase")]
20-
pub enum ContinuousRandomVariable {
19+
pub enum Continuous {
2120
Beta { alpha: f64, beta: f64 },
2221
Exp { lambda: f64 },
2322
Gamma { shape: f64, scale: f64 },
@@ -30,13 +29,13 @@ pub enum ContinuousRandomVariable {
3029

3130
#[derive(Debug, Clone, Serialize, Deserialize)]
3231
#[serde(rename_all = "camelCase")]
33-
pub enum BooleanRandomVariable {
32+
pub enum Boolean {
3433
Bernoulli { p: f64 },
3534
}
3635

3736
#[derive(Debug, Clone, Serialize, Deserialize)]
3837
#[serde(rename_all = "camelCase")]
39-
pub enum DiscreteRandomVariable {
38+
pub enum Discrete {
4039
Geometric {
4140
p: f64,
4241
},
@@ -52,7 +51,7 @@ pub enum DiscreteRandomVariable {
5251

5352
#[derive(Debug, Clone, Serialize, Deserialize)]
5453
#[serde(rename_all = "camelCase")]
55-
pub enum IndexRandomVariable {
54+
pub enum Index {
5655
/// Range is inclusive of min, exclusive of max: [min, max)
5756
Uniform {
5857
min: usize,
@@ -63,41 +62,39 @@ pub enum IndexRandomVariable {
6362
},
6463
}
6564

66-
impl ContinuousRandomVariable {
65+
impl Continuous {
6766
/// The generation of random variates drives stochastic behaviors during
6867
/// simulation execution. This function requires the random number
6968
/// generator of the simulation, and produces a f64 random variate.
7069
pub fn random_variate(&mut self, uniform_rng: &mut UniformRNG) -> Result<f64, SimulationError> {
7170
match self {
72-
ContinuousRandomVariable::Beta { alpha, beta } => {
71+
Continuous::Beta { alpha, beta } => {
7372
Ok(Beta::new(*alpha, *beta)?.sample(uniform_rng.rng()))
7473
}
75-
ContinuousRandomVariable::Exp { lambda } => {
76-
Ok(Exp::new(*lambda)?.sample(uniform_rng.rng()))
77-
}
78-
ContinuousRandomVariable::Gamma { shape, scale } => {
74+
Continuous::Exp { lambda } => Ok(Exp::new(*lambda)?.sample(uniform_rng.rng())),
75+
Continuous::Gamma { shape, scale } => {
7976
Ok(Gamma::new(*shape, *scale)?.sample(uniform_rng.rng()))
8077
}
81-
ContinuousRandomVariable::LogNormal { mu, sigma } => {
78+
Continuous::LogNormal { mu, sigma } => {
8279
Ok(LogNormal::new(*mu, *sigma)?.sample(uniform_rng.rng()))
8380
}
84-
ContinuousRandomVariable::Normal { mean, std_dev } => {
81+
Continuous::Normal { mean, std_dev } => {
8582
Ok(Normal::new(*mean, *std_dev)?.sample(uniform_rng.rng()))
8683
}
87-
ContinuousRandomVariable::Triangular { min, max, mode } => {
84+
Continuous::Triangular { min, max, mode } => {
8885
Ok(Triangular::new(*min, *max, *mode)?.sample(uniform_rng.rng()))
8986
}
90-
ContinuousRandomVariable::Uniform { min, max } => {
87+
Continuous::Uniform { min, max } => {
9188
Ok(Uniform::new(*min, *max).sample(uniform_rng.rng()))
9289
}
93-
ContinuousRandomVariable::Weibull { shape, scale } => {
90+
Continuous::Weibull { shape, scale } => {
9491
Ok(Weibull::new(*shape, *scale)?.sample(uniform_rng.rng()))
9592
}
9693
}
9794
}
9895
}
9996

100-
impl BooleanRandomVariable {
97+
impl Boolean {
10198
/// The generation of random variates drives stochastic behaviors during
10299
/// simulation execution. This function requires the random number
103100
/// generator of the simulation, and produces a boolean random variate.
@@ -106,33 +103,29 @@ impl BooleanRandomVariable {
106103
uniform_rng: &mut UniformRNG,
107104
) -> Result<bool, SimulationError> {
108105
match self {
109-
BooleanRandomVariable::Bernoulli { p } => {
110-
Ok(Bernoulli::new(*p)?.sample(uniform_rng.rng()))
111-
}
106+
Boolean::Bernoulli { p } => Ok(Bernoulli::new(*p)?.sample(uniform_rng.rng())),
112107
}
113108
}
114109
}
115110

116-
impl DiscreteRandomVariable {
111+
impl Discrete {
117112
/// The generation of random variates drives stochastic behaviors during
118113
/// simulation execution. This function requires the random number
119114
/// generator of the simulation, and produces a u64 random variate.
120115
pub fn random_variate(&mut self, uniform_rng: &mut UniformRNG) -> Result<u64, SimulationError> {
121116
match self {
122-
DiscreteRandomVariable::Geometric { p } => {
123-
Ok(Geometric::new(*p)?.sample(uniform_rng.rng()))
124-
}
125-
DiscreteRandomVariable::Poisson { lambda } => {
117+
Discrete::Geometric { p } => Ok(Geometric::new(*p)?.sample(uniform_rng.rng())),
118+
Discrete::Poisson { lambda } => {
126119
Ok(Poisson::new(*lambda)?.sample(uniform_rng.rng()) as u64)
127120
}
128-
DiscreteRandomVariable::Uniform { min, max } => {
121+
Discrete::Uniform { min, max } => {
129122
Ok(Uniform::new(*min, *max).sample(uniform_rng.rng()))
130123
}
131124
}
132125
}
133126
}
134127

135-
impl IndexRandomVariable {
128+
impl Index {
136129
/// The generation of random variates drives stochastic behaviors during
137130
/// simulation execution. This function requires the random number
138131
/// generator of the simulation, and produces a usize random variate.
@@ -141,10 +134,8 @@ impl IndexRandomVariable {
141134
uniform_rng: &mut UniformRNG,
142135
) -> Result<usize, SimulationError> {
143136
match self {
144-
IndexRandomVariable::Uniform { min, max } => {
145-
Ok(Uniform::new(*min, *max).sample(uniform_rng.rng()))
146-
}
147-
IndexRandomVariable::WeightedIndex { weights } => {
137+
Index::Uniform { min, max } => Ok(Uniform::new(*min, *max).sample(uniform_rng.rng())),
138+
Index::WeightedIndex { weights } => {
148139
Ok(WeightedIndex::new(weights.clone())?.sample(uniform_rng.rng()))
149140
}
150141
}
@@ -156,25 +147,25 @@ mod tests {
156147
use super::*;
157148

158149
enum RandomVariable {
159-
Continuous(ContinuousRandomVariable),
160-
Discrete(DiscreteRandomVariable),
150+
Continuous(Continuous),
151+
Discrete(Discrete),
161152
}
162153

163154
enum ChiSquareTest {
164155
Continuous {
165-
variable: ContinuousRandomVariable,
156+
variable: Continuous,
166157
bin_mapping_fn: fn(f64) -> usize,
167158
},
168159
Boolean {
169-
variable: BooleanRandomVariable,
160+
variable: Boolean,
170161
bin_mapping_fn: fn(bool) -> usize,
171162
},
172163
Discrete {
173-
variable: DiscreteRandomVariable,
164+
variable: Discrete,
174165
bin_mapping_fn: fn(u64) -> usize,
175166
},
176167
Index {
177-
variable: IndexRandomVariable,
168+
variable: Index,
178169
bin_mapping_fn: fn(usize) -> usize,
179170
},
180171
}
@@ -231,7 +222,7 @@ mod tests {
231222

232223
#[test]
233224
fn beta_samples_match_expectation() {
234-
let variable = ContinuousRandomVariable::Beta {
225+
let variable = Continuous::Beta {
235226
alpha: 7.0,
236227
beta: 11.0,
237228
};
@@ -242,15 +233,15 @@ mod tests {
242233

243234
#[test]
244235
fn exponential_samples_match_expectation() {
245-
let variable = ContinuousRandomVariable::Exp { lambda: 7.0 };
236+
let variable = Continuous::Exp { lambda: 7.0 };
246237
let mean = empirical_mean(&mut RandomVariable::Continuous(variable), 10000);
247238
let expected = 1.0 / 7.0;
248239
assert!((mean - expected).abs() / expected < 0.025);
249240
}
250241

251242
#[test]
252243
fn gamma_samples_match_expectation() {
253-
let variable = ContinuousRandomVariable::Gamma {
244+
let variable = Continuous::Gamma {
254245
shape: 7.0,
255246
scale: 11.0,
256247
};
@@ -261,7 +252,7 @@ mod tests {
261252

262253
#[test]
263254
fn lognormal_samples_match_expectation() {
264-
let variable = ContinuousRandomVariable::LogNormal {
255+
let variable = Continuous::LogNormal {
265256
mu: 11.0,
266257
sigma: 1.0,
267258
};
@@ -293,7 +284,7 @@ mod tests {
293284
7
294285
}
295286
}
296-
let variable = ContinuousRandomVariable::Normal {
287+
let variable = Continuous::Normal {
297288
mean: 11.0,
298289
std_dev: 3.0,
299290
};
@@ -318,7 +309,7 @@ mod tests {
318309
fn bins_mapping(variate: f64) -> usize {
319310
((variate - 5.0) / 5.0) as usize
320311
}
321-
let variable = ContinuousRandomVariable::Triangular {
312+
let variable = Continuous::Triangular {
322313
min: 5.0,
323314
max: 25.0,
324315
mode: 15.0,
@@ -345,7 +336,7 @@ mod tests {
345336
let max = 11.0;
346337
((variate - min) * (max - 1.0)) as usize
347338
}
348-
let variable = ContinuousRandomVariable::Uniform {
339+
let variable = Continuous::Uniform {
349340
min: 7.0,
350341
max: 11.0,
351342
};
@@ -366,7 +357,7 @@ mod tests {
366357

367358
#[test]
368359
fn weibull_samples_match_expectation() {
369-
let variable = ContinuousRandomVariable::Weibull {
360+
let variable = Continuous::Weibull {
370361
shape: 7.0,
371362
scale: 0.5,
372363
};
@@ -380,7 +371,7 @@ mod tests {
380371
fn bins_mapping(variate: bool) -> usize {
381372
variate as usize
382373
}
383-
let variable = BooleanRandomVariable::Bernoulli { p: 0.3 };
374+
let variable = Boolean::Bernoulli { p: 0.3 };
384375
// Failures (false == 0) is 70% of trials and success (true == 1) is 30% of trials
385376
let expected_counts: [usize; 2] = [7000, 3000];
386377
let chi_square_actual = chi_square(
@@ -398,15 +389,15 @@ mod tests {
398389

399390
#[test]
400391
fn geometric_samples_match_expectation() {
401-
let variable = DiscreteRandomVariable::Geometric { p: 0.2 };
392+
let variable = Discrete::Geometric { p: 0.2 };
402393
let mean = empirical_mean(&mut RandomVariable::Discrete(variable), 10000);
403394
let expected = (1.0 - 0.2) / 0.2;
404395
assert!((mean - expected).abs() / expected < 0.025);
405396
}
406397

407398
#[test]
408399
fn poisson_samples_match_expectation() {
409-
let variable = DiscreteRandomVariable::Poisson { lambda: 7.0 };
400+
let variable = Discrete::Poisson { lambda: 7.0 };
410401
let mean = empirical_mean(&mut RandomVariable::Discrete(variable), 10000);
411402
let expected = 7.0;
412403
assert!((mean - expected).abs() / expected < 0.025);
@@ -418,7 +409,7 @@ mod tests {
418409
let min = 7;
419410
(variate - min) as usize
420411
}
421-
let variable = DiscreteRandomVariable::Uniform { min: 7, max: 11 };
412+
let variable = Discrete::Uniform { min: 7, max: 11 };
422413
// Constant bin counts, due to uniformity of distribution
423414
let expected_counts: [usize; 4] = [2500; 4];
424415
let chi_square_actual = chi_square(
@@ -439,7 +430,7 @@ mod tests {
439430
fn bins_mapping(variate: usize) -> usize {
440431
variate
441432
}
442-
let variable = IndexRandomVariable::WeightedIndex {
433+
let variable = Index::WeightedIndex {
443434
weights: vec![1, 2, 3, 4],
444435
};
445436
// The expected bin counts scale linearly with the weights
@@ -463,7 +454,7 @@ mod tests {
463454
let min = 7;
464455
variate - min
465456
}
466-
let variable = IndexRandomVariable::Uniform { min: 7, max: 11 };
457+
let variable = Index::Uniform { min: 7, max: 11 };
467458
// Constant bin counts, due to uniformity of distribution
468459
let expected_counts: [usize; 4] = [2500; 4];
469460
let chi_square_actual = chi_square(

sim/src/input_modeling/thinning.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use serde::{Deserialize, Serialize};
22

3-
use crate::utils::error::SimulationError;
3+
use crate::utils::errors::SimulationError;
44
use crate::utils::evaluate_polynomial;
55

66
#[derive(Debug, Clone, Serialize, Deserialize)]

sim/src/models/batcher.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
55
use super::model_trait::{AsModel, SerializableModel};
66
use super::ModelMessage;
77
use crate::simulator::Services;
8-
use crate::utils::error::SimulationError;
8+
use crate::utils::errors::SimulationError;
99

1010
use sim_derive::SerializableModel;
1111

sim/src/models/exclusive_gateway.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use serde::{Deserialize, Serialize};
44

55
use super::model_trait::{AsModel, SerializableModel};
66
use super::ModelMessage;
7-
use crate::input_modeling::random_variable::IndexRandomVariable;
7+
use crate::input_modeling::IndexRandomVariable;
88
use crate::simulator::Services;
99
use crate::utils::default_records_port_name;
10-
use crate::utils::error::SimulationError;
10+
use crate::utils::errors::SimulationError;
1111

1212
use sim_derive::SerializableModel;
1313

sim/src/models/gate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use super::model_trait::{AsModel, SerializableModel};
66
use super::ModelMessage;
77
use crate::simulator::Services;
88
use crate::utils::default_records_port_name;
9-
use crate::utils::error::SimulationError;
9+
use crate::utils::errors::SimulationError;
1010

1111
use sim_derive::SerializableModel;
1212

sim/src/models/generator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ use serde::{Deserialize, Serialize};
22

33
use super::model_trait::{AsModel, SerializableModel};
44
use super::ModelMessage;
5-
use crate::input_modeling::random_variable::ContinuousRandomVariable;
5+
use crate::input_modeling::ContinuousRandomVariable;
66
use crate::input_modeling::Thinning;
77
use crate::simulator::Services;
88
use crate::utils::default_records_port_name;
9-
use crate::utils::error::SimulationError;
9+
use crate::utils::errors::SimulationError;
1010

1111
use sim_derive::SerializableModel;
1212

sim/src/models/load_balancer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use super::model_trait::{AsModel, SerializableModel};
66
use super::ModelMessage;
77
use crate::simulator::Services;
88
use crate::utils::default_records_port_name;
9-
use crate::utils::error::SimulationError;
9+
use crate::utils::errors::SimulationError;
1010

1111
use sim_derive::SerializableModel;
1212

sim/src/models/model.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
44
use super::model_trait::{AsModel, SerializableModel};
55
use super::ModelMessage;
66
use crate::simulator::Services;
7-
use crate::utils::error::SimulationError;
7+
use crate::utils::errors::SimulationError;
88

99
/// `Model` wraps `model_type` and provides common ID functionality (a struct
1010
/// field and associated accessor method). The simulator requires all models

0 commit comments

Comments
 (0)