/
MachRep2.hs
100 lines (82 loc) · 3.01 KB
/
MachRep2.hs
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
-- It corresponds to model MachRep2 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, but sometimes break down. Up time is exponentially
-- distributed with mean 1.0, and repair time is exponentially distributed
-- with mean 0.5. In this example, there is only one repairperson, so
-- the two machines cannot be repaired simultaneously if they are down
-- at the same time.
--
-- In addition to finding the long-run proportion of up time as in
-- model MachRep1, let’s also find the long-run proportion of the time
-- that a given machine does not have immediate access to the repairperson
-- when the machine breaks down. Output values should be about 0.6 and 0.67.
import Control.Monad
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 -- number of times the machines have broken down
nRep <- newRef 0
-- number of breakdowns in which the machine
-- started repair service right away
nImmedRep <- newRef 0
-- total up time for all machines
totalUpTime <- newRef 0.0
repairPerson <- newFCFSResource 1
let machine :: Process ()
machine =
do upTime <-
randomExponentialProcess meanUpTime
liftEvent $
modifyRef totalUpTime (+ upTime)
-- check the resource availability
liftEvent $
do modifyRef nRep (+ 1)
n <- resourceCount repairPerson
when (n == 1) $
modifyRef nImmedRep (+ 1)
requestResource repairPerson
repairTime <-
randomExponentialProcess meanRepairTime
releaseResource repairPerson
machine
runProcessInStartTime machine
runProcessInStartTime machine
let upTimeProp =
do x <- readRef totalUpTime
y <- liftDynamics time
return $ x / (2 * y)
immedProp :: Event Double
immedProp =
do n <- readRef nRep
nImmed <- readRef nImmedRep
return $
fromIntegral nImmed /
fromIntegral n
return $
results
[resultSource
"upTimeProp"
"The long-run proportion of up time (~ 0.6)"
upTimeProp,
--
resultSource
"immedProp"
"The proption of time of immediate access (~0.67)"
immedProp]
main =
printSimulationResultsInStopTime
printResultSourceInEnglish
model specs