module Simulation.Aivika.Stream.Random
(
randomStream,
randomUniformStream,
randomNormalStream,
randomExponentialStream,
randomErlangStream,
randomPoissonStream,
randomBinomialStream) where
import System.Random
import Control.Monad
import Control.Monad.Trans
import Simulation.Aivika.Parameter
import Simulation.Aivika.Parameter.Random
import Simulation.Aivika.Simulation
import Simulation.Aivika.Dynamics
import Simulation.Aivika.Event
import Simulation.Aivika.Process
import Simulation.Aivika.Processor
import Simulation.Aivika.Stream
import Simulation.Aivika.Statistics
import Simulation.Aivika.Ref
import Simulation.Aivika.Arrival
randomStream :: Parameter Double -> Stream Arrival
randomStream delay = Cons z0 where
z0 =
do t0 <- liftDynamics time
loop t0
loop t0 =
do t1 <- liftDynamics time
when (t1 /= t0) $
error $
"The time of requesting for a new random event is different from " ++
"the time when the previous event has arrived. Probably, your model " ++
"contains a logical error. The random events should be requested permanently. " ++
"At least, they can be lost, for example, when trying to enqueue them, but " ++
"the random stream itself must always work: randomStream."
delay <- liftParameter delay
holdProcess delay
t2 <- liftDynamics time
let arrival = Arrival { arrivalTime = t2,
arrivalDelay = delay }
return (arrival, Cons $ loop t2)
randomUniformStream :: Double
-> Double
-> Stream Arrival
randomUniformStream min max =
randomStream $ randomUniform min max
randomNormalStream :: Double
-> Double
-> Stream Arrival
randomNormalStream mu nu =
randomStream $ randomNormal mu nu
randomExponentialStream :: Double
-> Stream Arrival
randomExponentialStream mu =
randomStream $ randomExponential mu
randomErlangStream :: Double
-> Int
-> Stream Arrival
randomErlangStream beta m =
randomStream $ randomErlang beta m
randomPoissonStream :: Double
-> Stream Arrival
randomPoissonStream mu =
randomStream $ fmap fromIntegral $ randomPoisson mu
randomBinomialStream :: Double
-> Int
-> Stream Arrival
randomBinomialStream prob trials =
randomStream $ fmap fromIntegral $ randomBinomial prob trials