1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright (c) 2020-2022  David Sorokin <david.sorokin@gmail.com>, based in Yoshkar-Ola, Russia
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use std::rc::Rc;

use crate::simulation::parameter::*;
use crate::simulation::parameter::random::*;
use crate::simulation::observable::*;
use crate::simulation::observable::disposable::*;
use crate::simulation::observable::source::*;
use crate::simulation::simulation::*;
use crate::simulation::composite::*;
use crate::simulation::process::*;

use dvcompute_utils::simulation::arrival::*;

/// Return an observable of random events that arrive with the specified delay.
pub fn new_random_observable<T, F, M>(f: F) -> impl Composite<Item = ObservableBox<Rc<Arrival<T>>>>
    where F: Fn() -> M + 'static,
          M: Parameter<Item = (f64, T)> + 'static,
          T: 'static
{
    let source = Rc::new(ObservableSource::new());
    ProcessId::new()
        .into_composite()
        .flat_map(move |pid| {
            let pid = Rc::new(pid);
            loop_random_observable(f, source.clone(), None)
                .run_using_pid(pid.clone())
                .into_composite()
                .flat_map(move |()| {
                    disposable_composite({
                        cons_disposable(move |p| {
                            ProcessId::cancel(pid)
                                .call_event(p)
                        })
                    })
                })
                .flat_map(move |()| {
                    return_composite(source.publish().into_boxed())
                })
        })
}

/// The computation loop for `new_random_observable`.
fn loop_random_observable<T, F, M>(f: F, source: Rc<ObservableSource<Rc<Arrival<T>>>>, t0: Option<f64>) -> ProcessBox<()>
    where F: Fn() -> M + 'static,
          M: Parameter<Item = (f64, T)> + 'static,
          T: 'static
{
    let p = f();
    p.into_process()
        .flat_map(move |(delay, a)| {
            let delay = delay.max(0.0);
            hold_process(delay)
                .flat_map(move |()| {
                    time_event()
                        .into_process()
                        .flat_map(move |t2| {
                            let arrival = Arrival {
                                val: a,
                                time: t2,
                                delay: {
                                    match t0 {
                                        None => None,
                                        Some(_) => Some(delay)
                                    }
                                }
                            };
                            let arrival = Rc::new(arrival);
                            source.trigger(arrival)
                                .into_process()
                                .flat_map(move |()| {
                                    loop_random_observable(f, source, Some(t2))
                                })
                        })
                })
        })
        .into_boxed()
}

/// An observable with random time delays distributed uniformly.
#[inline]
pub fn new_random_uniform_observable(min: f64, max: f64) -> impl Composite<Item = ObservableBox<Rc<Arrival<f64>>>> {
    new_random_observable(move || {
        random_uniform_parameter(min, max)
            .map(|x| (x, x))
    })
}

/// An observable with random integer time delays distributed uniformly.
#[inline]
pub fn new_random_int_uniform_observable(min: isize, max: isize) -> impl Composite<Item = ObservableBox<Rc<Arrival<isize>>>> {
    new_random_observable(move || {
        random_int_uniform_parameter(min, max)
            .map(|x| (x as f64, x))
    })
}

/// An observable with random time delays from the triangular distribution.
#[inline]
pub fn new_random_triangular_observable(min: f64, median: f64, max: f64) -> impl Composite<Item = ObservableBox<Rc<Arrival<f64>>>> {
    new_random_observable(move || {
        random_triangular_parameter(min, median, max)
            .map(|x| (x, x))
    })
}

/// An observable with random time delays distributed normally.
#[inline]
pub fn new_random_normal_observable(mu: f64, nu: f64) -> impl Composite<Item = ObservableBox<Rc<Arrival<f64>>>> {
    new_random_observable(move || {
        random_normal_parameter(mu, nu)
            .map(|x| {
                let x = x.max(0.0);
                (x, x)
            })
    })
}

/// An observable with random time delays distributed lognormally.
#[inline]
pub fn new_random_log_normal_observable(mu: f64, nu: f64) -> impl Composite<Item = ObservableBox<Rc<Arrival<f64>>>> {
    new_random_observable(move || {
        random_log_normal_parameter(mu, nu)
            .map(|x| (x, x))
    })
}

/// An observable with exponential random time delays with the specified mean.
#[inline]
pub fn new_random_exponential_observable(mu: f64) -> impl Composite<Item = ObservableBox<Rc<Arrival<f64>>>> {
    new_random_observable(move || {
        random_exponential_parameter(mu)
            .map(|x| (x, x))
    })
}

/// An observable with random time delays from the Erlang distribution with the specified scale
/// (the reciprocal of the rate) and integer shape.
#[inline]
pub fn new_random_erlang_observable(scale: f64, shape: isize) -> impl Composite<Item = ObservableBox<Rc<Arrival<f64>>>> {
    new_random_observable(move || {
        random_erlang_parameter(scale, shape)
            .map(|x| (x, x))
    })
}

/// An observable with the Poisson random time delays with the specified mean.
#[inline]
pub fn new_random_poisson_observable(mu: f64) -> impl Composite<Item = ObservableBox<Rc<Arrival<isize>>>> {
    new_random_observable(move || {
        random_poisson_parameter(mu)
            .map(|x| (x as f64, x))
    })
}

/// An observable with binomial random time delays with the specified probability and trials.
#[inline]
pub fn new_random_binomial_observable(prob: f64, trials: isize) -> impl Composite<Item = ObservableBox<Rc<Arrival<isize>>>> {
    new_random_observable(move || {
        random_binomial_parameter(prob, trials)
            .map(|x| (x as f64, x))
    })
}

/// An observable with random time delays from the Gamma distribution.
#[inline]
pub fn new_random_gamma_observable(kappa: f64, theta: f64) -> impl Composite<Item = ObservableBox<Rc<Arrival<f64>>>> {
    new_random_observable(move || {
        random_gamma_parameter(kappa, theta)
            .map(|x| (x, x))
    })
}

/// An observable with random time delays from the Beta distribution.
#[inline]
pub fn new_random_beta_observable(alpha: f64, beta: f64) -> impl Composite<Item = ObservableBox<Rc<Arrival<f64>>>> {
    new_random_observable(move || {
        random_beta_parameter(alpha, beta)
            .map(|x| (x, x))
    })
}

/// An observable with random time delays from the Weibull distribution.
#[inline]
pub fn new_random_weibull_observable(alpha: f64, beta: f64) -> impl Composite<Item = ObservableBox<Rc<Arrival<f64>>>> {
    new_random_observable(move || {
        random_weibull_parameter(alpha, beta)
            .map(|x| (x, x))
    })
}

/// An observable with random time delays from the specified discrete distribution.
#[inline]
pub fn new_random_discrete_observable(dpdf: Rc<Vec<(f64, f64)>>) -> impl Composite<Item = ObservableBox<Rc<Arrival<f64>>>> {
    new_random_observable(move || {
        random_discrete_parameter(dpdf.clone())
            .map(|x| (x, x))
    })
}