module Simulation.Aivika.Trans.Stream.Random
(
randomStream,
randomUniformStream,
randomUniformIntStream,
randomTriangularStream,
randomNormalStream,
randomLogNormalStream,
randomExponentialStream,
randomErlangStream,
randomPoissonStream,
randomBinomialStream,
randomGammaStream,
randomBetaStream,
randomWeibullStream,
randomDiscreteStream) where
import Control.Monad
import Control.Monad.Trans
import Simulation.Aivika.Trans.DES
import Simulation.Aivika.Trans.Generator
import Simulation.Aivika.Trans.Parameter
import Simulation.Aivika.Trans.Parameter.Random
import Simulation.Aivika.Trans.Simulation
import Simulation.Aivika.Trans.Dynamics
import Simulation.Aivika.Trans.Event
import Simulation.Aivika.Trans.Process
import Simulation.Aivika.Trans.Processor
import Simulation.Aivika.Trans.Stream
import Simulation.Aivika.Trans.Statistics
import Simulation.Aivika.Trans.Ref
import Simulation.Aivika.Trans.Arrival
randomStream :: MonadDES m
=> Parameter m (Double, a)
-> Stream m (Arrival a)
{-# INLINE randomStream #-}
randomStream :: forall (m :: * -> *) a.
MonadDES m =>
Parameter m (Double, a) -> Stream m (Arrival a)
randomStream Parameter m (Double, a)
delay = forall (m :: * -> *) a. Process m (a, Stream m a) -> Stream m a
Cons forall a b. (a -> b) -> a -> b
$ Maybe Double -> Process m (Arrival a, Stream m (Arrival a))
loop forall a. Maybe a
Nothing where
loop :: Maybe Double -> Process m (Arrival a, Stream m (Arrival a))
loop Maybe Double
t0 =
do Double
t1 <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
DynamicsLift t m =>
Dynamics m a -> t m a
liftDynamics forall (m :: * -> *). Monad m => Dynamics m Double
time
case Maybe Double
t0 of
Maybe Double
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just Double
t0 ->
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Double
t1 forall a. Eq a => a -> a -> Bool
/= Double
t0) forall a b. (a -> b) -> a -> b
$
forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$
[Char]
"The time of requesting for a new random event is different from " forall a. [a] -> [a] -> [a]
++
[Char]
"the time when the previous event has arrived. Probably, your model " forall a. [a] -> [a] -> [a]
++
[Char]
"contains a logical error. The random events should be requested permanently. " forall a. [a] -> [a] -> [a]
++
[Char]
"At least, they can be lost, for example, when trying to enqueue them, but " forall a. [a] -> [a] -> [a]
++
[Char]
"the random stream itself must always execute: randomStream."
(Double
delay, a
a) <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
ParameterLift t m =>
Parameter m a -> t m a
liftParameter Parameter m (Double, a)
delay
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Double
delay forall a. Ord a => a -> a -> Bool
> Double
0) forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *). MonadDES m => Double -> Process m ()
holdProcess Double
delay
Double
t2 <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
DynamicsLift t m =>
Dynamics m a -> t m a
liftDynamics forall (m :: * -> *). Monad m => Dynamics m Double
time
let arrival :: Arrival a
arrival = Arrival { arrivalValue :: a
arrivalValue = a
a,
arrivalTime :: Double
arrivalTime = Double
t2,
arrivalDelay :: Maybe Double
arrivalDelay =
case Maybe Double
t0 of
Maybe Double
Nothing -> forall a. Maybe a
Nothing
Just Double
t0 -> forall a. a -> Maybe a
Just Double
delay }
forall (m :: * -> *) a. Monad m => a -> m a
return (Arrival a
arrival, forall (m :: * -> *) a. Process m (a, Stream m a) -> Stream m a
Cons forall a b. (a -> b) -> a -> b
$ Maybe Double -> Process m (Arrival a, Stream m (Arrival a))
loop (forall a. a -> Maybe a
Just Double
t2))
randomUniformStream :: MonadDES m
=> Double
-> Double
-> Stream m (Arrival Double)
{-# INLINABLE randomUniformStream #-}
randomUniformStream :: forall (m :: * -> *).
MonadDES m =>
Double -> Double -> Stream m (Arrival Double)
randomUniformStream Double
min Double
max =
forall (m :: * -> *) a.
MonadDES m =>
Parameter m (Double, a) -> Stream m (Arrival a)
randomStream forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *).
MonadComp m =>
Double -> Double -> Parameter m Double
randomUniform Double
min Double
max forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)
randomUniformIntStream :: MonadDES m
=> Int
-> Int
-> Stream m (Arrival Int)
{-# INLINABLE randomUniformIntStream #-}
randomUniformIntStream :: forall (m :: * -> *).
MonadDES m =>
Int -> Int -> Stream m (Arrival Int)
randomUniformIntStream Int
min Int
max =
forall (m :: * -> *) a.
MonadDES m =>
Parameter m (Double, a) -> Stream m (Arrival a)
randomStream forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *). MonadComp m => Int -> Int -> Parameter m Int
randomUniformInt Int
min Int
max forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Int
x ->
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
x, Int
x)
randomTriangularStream :: MonadDES m
=> Double
-> Double
-> Double
-> Stream m (Arrival Double)
{-# INLINABLE randomTriangularStream #-}
randomTriangularStream :: forall (m :: * -> *).
MonadDES m =>
Double -> Double -> Double -> Stream m (Arrival Double)
randomTriangularStream Double
min Double
median Double
max =
forall (m :: * -> *) a.
MonadDES m =>
Parameter m (Double, a) -> Stream m (Arrival a)
randomStream forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *).
MonadComp m =>
Double -> Double -> Double -> Parameter m Double
randomTriangular Double
min Double
median Double
max forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)
randomNormalStream :: MonadDES m
=> Double
-> Double
-> Stream m (Arrival Double)
{-# INLINABLE randomNormalStream #-}
randomNormalStream :: forall (m :: * -> *).
MonadDES m =>
Double -> Double -> Stream m (Arrival Double)
randomNormalStream Double
mu Double
nu =
forall (m :: * -> *) a.
MonadDES m =>
Parameter m (Double, a) -> Stream m (Arrival a)
randomStream forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *).
MonadComp m =>
Double -> Double -> Parameter m Double
randomNormal Double
mu Double
nu forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)
randomLogNormalStream :: MonadDES m
=> Double
-> Double
-> Stream m (Arrival Double)
{-# INLINABLE randomLogNormalStream #-}
randomLogNormalStream :: forall (m :: * -> *).
MonadDES m =>
Double -> Double -> Stream m (Arrival Double)
randomLogNormalStream Double
mu Double
nu =
forall (m :: * -> *) a.
MonadDES m =>
Parameter m (Double, a) -> Stream m (Arrival a)
randomStream forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *).
MonadComp m =>
Double -> Double -> Parameter m Double
randomLogNormal Double
mu Double
nu forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)
randomExponentialStream :: MonadDES m
=> Double
-> Stream m (Arrival Double)
{-# INLINABLE randomExponentialStream #-}
randomExponentialStream :: forall (m :: * -> *).
MonadDES m =>
Double -> Stream m (Arrival Double)
randomExponentialStream Double
mu =
forall (m :: * -> *) a.
MonadDES m =>
Parameter m (Double, a) -> Stream m (Arrival a)
randomStream forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *). MonadComp m => Double -> Parameter m Double
randomExponential Double
mu forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)
randomErlangStream :: MonadDES m
=> Double
-> Int
-> Stream m (Arrival Double)
{-# INLINABLE randomErlangStream #-}
randomErlangStream :: forall (m :: * -> *).
MonadDES m =>
Double -> Int -> Stream m (Arrival Double)
randomErlangStream Double
beta Int
m =
forall (m :: * -> *) a.
MonadDES m =>
Parameter m (Double, a) -> Stream m (Arrival a)
randomStream forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *).
MonadComp m =>
Double -> Int -> Parameter m Double
randomErlang Double
beta Int
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)
randomPoissonStream :: MonadDES m
=> Double
-> Stream m (Arrival Int)
{-# INLINABLE randomPoissonStream #-}
randomPoissonStream :: forall (m :: * -> *).
MonadDES m =>
Double -> Stream m (Arrival Int)
randomPoissonStream Double
mu =
forall (m :: * -> *) a.
MonadDES m =>
Parameter m (Double, a) -> Stream m (Arrival a)
randomStream forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *). MonadComp m => Double -> Parameter m Int
randomPoisson Double
mu forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Int
x ->
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
x, Int
x)
randomBinomialStream :: MonadDES m
=> Double
-> Int
-> Stream m (Arrival Int)
{-# INLINABLE randomBinomialStream #-}
randomBinomialStream :: forall (m :: * -> *).
MonadDES m =>
Double -> Int -> Stream m (Arrival Int)
randomBinomialStream Double
prob Int
trials =
forall (m :: * -> *) a.
MonadDES m =>
Parameter m (Double, a) -> Stream m (Arrival a)
randomStream forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *).
MonadComp m =>
Double -> Int -> Parameter m Int
randomBinomial Double
prob Int
trials forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Int
x ->
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
x, Int
x)
randomGammaStream :: MonadDES m
=> Double
-> Double
-> Stream m (Arrival Double)
{-# INLINABLE randomGammaStream #-}
randomGammaStream :: forall (m :: * -> *).
MonadDES m =>
Double -> Double -> Stream m (Arrival Double)
randomGammaStream Double
kappa Double
theta =
forall (m :: * -> *) a.
MonadDES m =>
Parameter m (Double, a) -> Stream m (Arrival a)
randomStream forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *).
MonadComp m =>
Double -> Double -> Parameter m Double
randomGamma Double
kappa Double
theta forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)
randomBetaStream :: MonadDES m
=> Double
-> Double
-> Stream m (Arrival Double)
{-# INLINABLE randomBetaStream #-}
randomBetaStream :: forall (m :: * -> *).
MonadDES m =>
Double -> Double -> Stream m (Arrival Double)
randomBetaStream Double
alpha Double
beta =
forall (m :: * -> *) a.
MonadDES m =>
Parameter m (Double, a) -> Stream m (Arrival a)
randomStream forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *).
MonadComp m =>
Double -> Double -> Parameter m Double
randomBeta Double
alpha Double
beta forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)
randomWeibullStream :: MonadDES m
=> Double
-> Double
-> Stream m (Arrival Double)
{-# INLINABLE randomWeibullStream #-}
randomWeibullStream :: forall (m :: * -> *).
MonadDES m =>
Double -> Double -> Stream m (Arrival Double)
randomWeibullStream Double
alpha Double
beta =
forall (m :: * -> *) a.
MonadDES m =>
Parameter m (Double, a) -> Stream m (Arrival a)
randomStream forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *).
MonadComp m =>
Double -> Double -> Parameter m Double
randomWeibull Double
alpha Double
beta forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)
randomDiscreteStream :: MonadDES m
=> DiscretePDF Double
-> Stream m (Arrival Double)
{-# INLINABLE randomDiscreteStream #-}
randomDiscreteStream :: forall (m :: * -> *).
MonadDES m =>
DiscretePDF Double -> Stream m (Arrival Double)
randomDiscreteStream DiscretePDF Double
dpdf =
forall (m :: * -> *) a.
MonadDES m =>
Parameter m (Double, a) -> Stream m (Arrival a)
randomStream forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) a.
MonadComp m =>
DiscretePDF a -> Parameter m a
randomDiscrete DiscretePDF Double
dpdf forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)