-- |
-- Module     : Simulation.Aivika.Trans.Stream.Random
-- Copyright  : Copyright (c) 2009-2017, David Sorokin <david.sorokin@gmail.com>
-- License    : BSD3
-- Maintainer : David Sorokin <david.sorokin@gmail.com>
-- Stability  : experimental
-- Tested with: GHC 8.0.1
--
-- This module defines random streams of events, which are useful
-- for describing the input of the model.
--

module Simulation.Aivika.Trans.Stream.Random
       (-- * Stream of Random Events
        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

-- | Return a stream of random events that arrive with the specified delay.
randomStream :: MonadDES m
                => Parameter m (Double, a)
                -- ^ compute a pair of the delay and event of type @a@
                -> Stream m (Arrival a)
                -- ^ a stream of delayed events
{-# 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))

-- | Create a new stream with delays distributed uniformly.
randomUniformStream :: MonadDES m
                       => Double
                       -- ^ the minimum delay
                       -> Double
                       -- ^ the maximum delay
                       -> Stream m (Arrival Double)
                       -- ^ the stream of random events with the delays generated
{-# 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)

-- | Create a new stream with integer delays distributed uniformly.
randomUniformIntStream :: MonadDES m
                          => Int
                          -- ^ the minimum delay
                          -> Int
                          -- ^ the maximum delay
                          -> Stream m (Arrival Int)
                          -- ^ the stream of random events with the delays generated
{-# 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)

-- | Create a new stream with random delays having the triangular distribution.
randomTriangularStream :: MonadDES m
                          => Double
                          -- ^ the minimum delay
                          -> Double
                          -- ^ the median of the delay
                          -> Double
                          -- ^ the maximum delay
                          -> Stream m (Arrival Double)
                          -- ^ the stream of random events with the delays generated
{-# 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)

-- | Create a new stream with delays distributed normally.
randomNormalStream :: MonadDES m
                      => Double
                      -- ^ the mean delay
                      -> Double
                      -- ^ the delay deviation
                      -> Stream m (Arrival Double)
                      -- ^ the stream of random events with the delays generated
{-# 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)

-- | Create a new stream with random delays having the lognormal distribution.
randomLogNormalStream :: MonadDES m
                         => Double
                         -- ^ the mean of a normal distribution which
                         -- this distribution is derived from
                         -> Double
                         -- ^ the deviation of a normal distribution which
                         -- this distribution is derived from
                         -> Stream m (Arrival Double)
                         -- ^ the stream of random events with the delays generated
{-# 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)
         
-- | Return a new stream with delays distibuted exponentially with the specified mean
-- (the reciprocal of the rate).
randomExponentialStream :: MonadDES m
                           => Double
                           -- ^ the mean delay (the reciprocal of the rate)
                           -> Stream m (Arrival Double)
                           -- ^ the stream of random events with the delays generated
{-# 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)
         
-- | Return a new stream with delays having the Erlang distribution with the specified
-- scale (the reciprocal of the rate) and shape parameters.
randomErlangStream :: MonadDES m
                      => Double
                      -- ^ the scale (the reciprocal of the rate)
                      -> Int
                      -- ^ the shape
                      -> Stream m (Arrival Double)
                      -- ^ the stream of random events with the delays generated
{-# 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)

-- | Return a new stream with delays having the Poisson distribution with
-- the specified mean.
randomPoissonStream :: MonadDES m
                       => Double
                       -- ^ the mean delay
                       -> Stream m (Arrival Int)
                       -- ^ the stream of random events with the delays generated
{-# 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)

-- | Return a new stream with delays having the binomial distribution with the specified
-- probability and trials.
randomBinomialStream :: MonadDES m
                        => Double
                        -- ^ the probability
                        -> Int
                        -- ^ the number of trials
                        -> Stream m (Arrival Int)
                        -- ^ the stream of random events with the delays generated
{-# 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)

-- | Return a new stream with random delays having the Gamma distribution by the specified
-- shape and scale.
randomGammaStream :: MonadDES m
                     => Double
                     -- ^ the shape
                     -> Double
                     -- ^ the scale (a reciprocal of the rate)
                     -> Stream m (Arrival Double)
                     -- ^ the stream of random events with the delays generated
{-# 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)

-- | Return a new stream with random delays having the Beta distribution by the specified
-- shape parameters (alpha and beta).
randomBetaStream :: MonadDES m
                    => Double
                    -- ^ the shape (alpha)
                    -> Double
                    -- ^ the shape (beta)
                    -> Stream m (Arrival Double)
                    -- ^ the stream of random events with the delays generated
{-# 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)

-- | Return a new stream with random delays having the Weibull distribution by the specified
-- shape and scale.
randomWeibullStream :: MonadDES m
                       => Double
                       -- ^ shape
                       -> Double
                       -- ^ scale
                       -> Stream m (Arrival Double)
                       -- ^ the stream of random events with the delays generated
{-# 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)

-- | Return a new stream with random delays having the specified discrete distribution.
randomDiscreteStream :: MonadDES m
                        => DiscretePDF Double
                        -- ^ the discrete probability density function
                        -> Stream m (Arrival Double)
                        -- ^ the stream of random events with the delays generated
{-# 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)