Skip to content

Commit 877017e

Browse files
committed
sim-rs: implement constant distribution
1 parent 4b61bac commit 877017e

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

data/simulation/config.d.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ export interface Config {
6565
export type Distribution =
6666
| NormalDistribution
6767
| ExpDistribution
68-
| LogNormalDistribution;
68+
| LogNormalDistribution
69+
| ConstantDistribution;
6970

7071
export interface NormalDistribution {
7172
distribution: "normal";
@@ -85,6 +86,11 @@ export interface LogNormalDistribution {
8586
sigma: number;
8687
}
8788

89+
export interface ConstantDistribution {
90+
distribution: "constant";
91+
value: number;
92+
}
93+
8894
export enum DiffusionStrategy {
8995
PeerOrder = "peer-order",
9096
FreshestFirst = "freshest-first",

data/simulation/config.schema.json

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
{
22
"$schema": "http://json-schema.org/draft-07/schema#",
33
"definitions": {
4+
"ConstantDistribution": {
5+
"properties": {
6+
"distribution": {
7+
"const": "constant",
8+
"type": "string"
9+
},
10+
"value": {
11+
"type": "number"
12+
}
13+
},
14+
"type": "object"
15+
},
416
"DiffusionStrategy": {
517
"enum": ["peer-order", "freshest-first"],
618
"type": "string"
@@ -15,6 +27,9 @@
1527
},
1628
{
1729
"$ref": "#/definitions/LogNormalDistribution"
30+
},
31+
{
32+
"$ref": "#/definitions/ConstantDistribution"
1833
}
1934
]
2035
},

sim-rs/sim-core/src/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub enum DistributionConfig {
3232
Normal { mean: f64, std_dev: f64 },
3333
Exp { lambda: f64, scale: Option<f64> },
3434
LogNormal { mu: f64, sigma: f64 },
35+
Constant { value: f64 },
3536
}
3637
impl From<DistributionConfig> for FloatDistribution {
3738
fn from(value: DistributionConfig) -> Self {
@@ -43,6 +44,7 @@ impl From<DistributionConfig> for FloatDistribution {
4344
FloatDistribution::scaled_exp(lambda, scale.unwrap_or(1.))
4445
}
4546
DistributionConfig::LogNormal { mu, sigma } => FloatDistribution::log_normal(mu, sigma),
47+
DistributionConfig::Constant { value } => FloatDistribution::constant(value),
4648
}
4749
}
4850
}

sim-rs/sim-core/src/probability.rs

+22
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
use rand::Rng;
12
use rand_distr::{Distribution, Exp, LogNormal, Normal};
3+
use std::fmt::Debug;
24

35
#[derive(Debug, Clone, Copy)]
46
pub enum FloatDistribution {
57
Normal(Normal<f64>),
68
ScaledExp(Exp<f64>, f64),
79
LogNormal(LogNormal<f64>),
10+
Constant(Constant<f64>),
811
}
912
impl FloatDistribution {
1013
pub fn normal(mean: f64, std_dev: f64) -> Self {
@@ -16,6 +19,9 @@ impl FloatDistribution {
1619
pub fn log_normal(mu: f64, sigma: f64) -> Self {
1720
Self::LogNormal(LogNormal::new(mu, sigma).unwrap())
1821
}
22+
pub fn constant(value: f64) -> Self {
23+
Self::Constant(Constant::new(value))
24+
}
1925
}
2026

2127
impl Distribution<f64> for FloatDistribution {
@@ -24,6 +30,22 @@ impl Distribution<f64> for FloatDistribution {
2430
Self::Normal(d) => d.sample(rng),
2531
Self::ScaledExp(d, scale) => d.sample(rng) * scale,
2632
Self::LogNormal(d) => d.sample(rng),
33+
Self::Constant(d) => d.sample(rng),
2734
}
2835
}
2936
}
37+
38+
#[derive(Debug, Clone, Copy)]
39+
pub struct Constant<T>(T);
40+
impl<T> Constant<T> {
41+
pub fn new(value: T) -> Self {
42+
Self(value)
43+
}
44+
}
45+
46+
impl<T: Clone> Distribution<T> for Constant<T> {
47+
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> T {
48+
let _ = rng;
49+
self.0.clone()
50+
}
51+
}

0 commit comments

Comments
 (0)