aivika-2.0: examples/MachRep1EventDriven.hs
-- It corresponds to model MachRep1 described in document
-- Introduction to Discrete-Event Simulation and the SimPy Language
-- [http://heather.cs.ucdavis.edu/~matloff/156/PLN/DESimIntro.pdf].
-- SimPy is available on [http://simpy.sourceforge.net/].
--
-- The model description is as follows.
--
-- Two machines, which sometimes break down.
-- Up time is exponentially distributed with mean 1.0, and repair time is
-- exponentially distributed with mean 0.5. There are two repairpersons,
-- so the two machines can be repaired simultaneously if they are down
-- at the same time.
--
-- Output is long-run proportion of up time. Should get value of about
-- 0.66.
import Control.Monad.Trans
import Simulation.Aivika
meanUpTime = 1.0
meanRepairTime = 0.5
specs = Specs { spcStartTime = 0.0,
spcStopTime = 1000.0,
spcDT = 1.0,
spcMethod = RungeKutta4,
spcGeneratorType = SimpleGenerator }
model :: Simulation Results
model =
do totalUpTime <- newRef 0.0
let machineBroken :: Double -> Event ()
machineBroken startUpTime =
do finishUpTime <- liftDynamics time
modifyRef totalUpTime (+ (finishUpTime - startUpTime))
repairTime <-
liftParameter $
randomExponential meanRepairTime
-- enqueue a new event
let t = finishUpTime + repairTime
enqueueEvent t machineRepaired
machineRepaired :: Event ()
machineRepaired =
do startUpTime <- liftDynamics time
upTime <-
liftParameter $
randomExponential meanUpTime
-- enqueue a new event
let t = startUpTime + upTime
enqueueEvent t $ machineBroken startUpTime
runEventInStartTime $
do -- start the first machine
machineRepaired
-- start the second machine
machineRepaired
let upTimeProp =
do x <- readRef totalUpTime
y <- liftDynamics time
return $ x / (2 * y)
return $
results
[resultSource
"upTimeProp"
"The long-run proportion of up time (~ 0.66)"
upTimeProp]
main =
printSimulationResultsInStopTime
printResultSourceInEnglish
model specs