Skip to content

Commit 34d1891

Browse files
committed
Add example Ross Simulation 7.7
1 parent 67a1fb5 commit 34d1891

1 file changed

Lines changed: 238 additions & 0 deletions

File tree

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# A Repair Problem\n",
8+
"Ross, Simulation 5th edition, Section 7.7, p. 124-126"
9+
]
10+
},
11+
{
12+
"cell_type": "markdown",
13+
"metadata": {},
14+
"source": [
15+
"## Description"
16+
]
17+
},
18+
{
19+
"cell_type": "markdown",
20+
"metadata": {},
21+
"source": [
22+
"A system needs $n$ working machines to be operational. To guard against machine breakdown, additional machines are kept available as spares. Whenever a machine breaks down it is immediately replaced by a spare and is itself sent to the repair facility, which consists of a single repairperson who repairs failed machines one at a time. Once a failed machine has been repaired it becomes available as a spare to be used when the need arises. All repair times are independent random variables having the common distribution function $G$. Each time a machine is put into use the amount of time it functions before breaking down is a random variable, independent of the past, having distribution function $F$.\n",
23+
"\n",
24+
"The system is said to “crash” when a machine fails and no spares are available. Assuming that there are initially $n + s$ functional machines of which $n$ are put in use and $s$ are kept as spares, we are interested in simulating this system so as to approximate $E[T]$, where $T$ is the time at which the system crashes."
25+
]
26+
},
27+
{
28+
"cell_type": "markdown",
29+
"metadata": {},
30+
"source": [
31+
"## Install packages"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": null,
37+
"metadata": {
38+
"collapsed": false
39+
},
40+
"outputs": [],
41+
"source": [
42+
"Pkg.update()\n",
43+
"Pkg.add(\"Distributions\")\n",
44+
"Pkg.add(\"SimJulia\")"
45+
]
46+
},
47+
{
48+
"cell_type": "markdown",
49+
"metadata": {},
50+
"source": [
51+
"## Use Distributions.jl and SimJulia.jl"
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": null,
57+
"metadata": {
58+
"collapsed": true
59+
},
60+
"outputs": [],
61+
"source": [
62+
"using Distributions\n",
63+
"using ResumableFunctions\n",
64+
"using SimJulia"
65+
]
66+
},
67+
{
68+
"cell_type": "markdown",
69+
"metadata": {},
70+
"source": [
71+
"## Define constants"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": null,
77+
"metadata": {
78+
"collapsed": false
79+
},
80+
"outputs": [],
81+
"source": [
82+
"const RUNS = 5\n",
83+
"const N = 10\n",
84+
"const S = 3\n",
85+
"const SEED = 150\n",
86+
"const LAMBDA = 100\n",
87+
"const MU = 1;"
88+
]
89+
},
90+
{
91+
"cell_type": "markdown",
92+
"metadata": {},
93+
"source": [
94+
"## Define the behaviour of a machine"
95+
]
96+
},
97+
{
98+
"cell_type": "code",
99+
"execution_count": null,
100+
"metadata": {
101+
"collapsed": false
102+
},
103+
"outputs": [],
104+
"source": [
105+
"@resumable function machine(sim::Simulation, repair_facility::Resource, spares::Store{Coroutine})\n",
106+
" dist_work = Exponential(LAMBDA)\n",
107+
" dist_repair = Exponential(MU)\n",
108+
" while true\n",
109+
" try\n",
110+
" @yield Timeout(sim, Inf)\n",
111+
" catch(exc)\n",
112+
" end\n",
113+
" #println(\"At time $(now(sim)): $(active_process(sim)) starts working.\")\n",
114+
" @yield Timeout(sim, rand(dist_work))\n",
115+
" #println(\"At time $(now(sim)): $(active_process(sim)) stops working.\")\n",
116+
" get_spare = Get(spares)\n",
117+
" @yield get_spare | Timeout(sim, 0.0)\n",
118+
" if state(get_spare) != SimJulia.idle\n",
119+
" interrupt(value(get_spare))\n",
120+
" else\n",
121+
" throw(SimJulia.StopSimulation(\"No more spares!\"))\n",
122+
" end\n",
123+
" @yield Request(repair_facility)\n",
124+
" #println(\"At time $(now(sim)): $(active_process(sim)) repair starts.\")\n",
125+
" @yield Timeout(sim, rand(dist_repair))\n",
126+
" @yield Release(repair_facility)\n",
127+
" #println(\"At time $(now(sim)): $(active_process(sim)) is repaired.\")\n",
128+
" @yield Put(spares, active_process(sim))\n",
129+
" end\n",
130+
"end\n"
131+
]
132+
},
133+
{
134+
"cell_type": "markdown",
135+
"metadata": {},
136+
"source": [
137+
"## Startup procedure"
138+
]
139+
},
140+
{
141+
"cell_type": "code",
142+
"execution_count": null,
143+
"metadata": {
144+
"collapsed": false
145+
},
146+
"outputs": [],
147+
"source": [
148+
"@resumable function start_sim(sim::Simulation, repair_facility::Resource, spares::Store{Coroutine})\n",
149+
" procs = Coroutine[]\n",
150+
" for i=1:N\n",
151+
" push!(procs, @coroutine machine(sim, repair_facility, spares))\n",
152+
" end\n",
153+
" @yield Timeout(sim, 0.0)\n",
154+
" for proc in procs\n",
155+
" interrupt(proc)\n",
156+
" end\n",
157+
" for i=1:S\n",
158+
" @yield Put(spares, @coroutine machine(sim, repair_facility, spares))\n",
159+
" end\n",
160+
"end"
161+
]
162+
},
163+
{
164+
"cell_type": "markdown",
165+
"metadata": {},
166+
"source": [
167+
"## One simulation run"
168+
]
169+
},
170+
{
171+
"cell_type": "code",
172+
"execution_count": null,
173+
"metadata": {
174+
"collapsed": false
175+
},
176+
"outputs": [],
177+
"source": [
178+
"function sim_repair()\n",
179+
" sim = Simulation()\n",
180+
" repair_facility = Resource(sim)\n",
181+
" spares = Store(Coroutine, sim)\n",
182+
" @coroutine start_sim(sim, repair_facility, spares)\n",
183+
" msg = run(sim)\n",
184+
" stop_time = now(sim)\n",
185+
" println(\"At time $stop_time: $msg\")\n",
186+
" stop_time\n",
187+
"end"
188+
]
189+
},
190+
{
191+
"cell_type": "markdown",
192+
"metadata": {},
193+
"source": [
194+
"## Multiple simulations"
195+
]
196+
},
197+
{
198+
"cell_type": "code",
199+
"execution_count": null,
200+
"metadata": {
201+
"collapsed": false
202+
},
203+
"outputs": [],
204+
"source": [
205+
"srand(SEED)\n",
206+
"results = Float64[]\n",
207+
"for i=1:RUNS\n",
208+
" push!(results, sim_repair())\n",
209+
"end\n",
210+
"println(sum(results)/RUNS)"
211+
]
212+
},
213+
{
214+
"cell_type": "code",
215+
"execution_count": null,
216+
"metadata": {
217+
"collapsed": true
218+
},
219+
"outputs": [],
220+
"source": []
221+
}
222+
],
223+
"metadata": {
224+
"kernelspec": {
225+
"display_name": "Julia 0.6.0",
226+
"language": "julia",
227+
"name": "julia-0.6"
228+
},
229+
"language_info": {
230+
"file_extension": ".jl",
231+
"mimetype": "application/julia",
232+
"name": "julia",
233+
"version": "0.6.0"
234+
}
235+
},
236+
"nbformat": 4,
237+
"nbformat_minor": 2
238+
}

0 commit comments

Comments
 (0)