Skip to content

Commit 5bc2163

Browse files
committed
juliabox 2017-09-11 12:11:27.939117+00:00
1 parent 34d1891 commit 5bc2163

2 files changed

Lines changed: 375 additions & 29 deletions

File tree

Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
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": 1,
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": 2,
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": 7,
100+
"metadata": {
101+
"collapsed": false
102+
},
103+
"outputs": [
104+
{
105+
"ename": "LoadError",
106+
"evalue": "\u001b[91mMethodError: no method matching start(::Void)\u001b[0m\nClosest candidates are:\n start(\u001b[91m::SimpleVector\u001b[39m) at essentials.jl:258\n start(\u001b[91m::Base.MethodList\u001b[39m) at reflection.jl:560\n start(\u001b[91m::ExponentialBackOff\u001b[39m) at error.jl:107\n ...\u001b[39m",
107+
"output_type": "error",
108+
"traceback": [
109+
"\u001b[91mMethodError: no method matching start(::Void)\u001b[0m\nClosest candidates are:\n start(\u001b[91m::SimpleVector\u001b[39m) at essentials.jl:258\n start(\u001b[91m::Base.MethodList\u001b[39m) at reflection.jl:560\n start(\u001b[91m::ExponentialBackOff\u001b[39m) at error.jl:107\n ...\u001b[39m",
110+
""
111+
]
112+
}
113+
],
114+
"source": [
115+
"@resumable function machine(sim::Simulation, repair_facility::Resource, spares::Store{Coroutine})\n",
116+
" dist_work = Exponential(LAMBDA)\n",
117+
" dist_repair = Exponential(MU)\n",
118+
" while true\n",
119+
" try \n",
120+
" @yield Timeout(sim, Inf)\n",
121+
" catch\n",
122+
" end\n",
123+
" #println(\"At time $(now(sim)): $(active_process(sim)) starts working.\")\n",
124+
" @yield Timeout(sim, rand(dist_work))\n",
125+
" #println(\"At time $(now(sim)): $(active_process(sim)) stops working.\")\n",
126+
" get_spare = Get(spares)\n",
127+
" @yield get_spare | Timeout(sim, 0.0)\n",
128+
" if state(get_spare) != SimJulia.idle\n",
129+
" interrupt(value(get_spare))\n",
130+
" else\n",
131+
" throw(SimJulia.StopSimulation(\"No more spares!\"))\n",
132+
" end\n",
133+
" @yield Request(repair_facility)\n",
134+
" #println(\"At time $(now(sim)): $(active_process(sim)) repair starts.\")\n",
135+
" @yield Timeout(sim, rand(dist_repair))\n",
136+
" @yield Release(repair_facility)\n",
137+
" #println(\"At time $(now(sim)): $(active_process(sim)) is repaired.\")\n",
138+
" @yield Put(spares, active_process(sim))\n",
139+
" end\n",
140+
"end\n"
141+
]
142+
},
143+
{
144+
"cell_type": "markdown",
145+
"metadata": {},
146+
"source": [
147+
"## Startup procedure"
148+
]
149+
},
150+
{
151+
"cell_type": "code",
152+
"execution_count": 4,
153+
"metadata": {
154+
"collapsed": false
155+
},
156+
"outputs": [
157+
{
158+
"data": {
159+
"text/plain": [
160+
"start_sim (generic function with 1 method)"
161+
]
162+
},
163+
"execution_count": 4,
164+
"metadata": {},
165+
"output_type": "execute_result"
166+
}
167+
],
168+
"source": [
169+
"@resumable function start_sim(sim::Simulation, repair_facility::Resource, spares::Store{Coroutine})\n",
170+
" procs = Coroutine[]\n",
171+
" for i=1:N\n",
172+
" push!(procs, @coroutine machine(sim, repair_facility, spares))\n",
173+
" end\n",
174+
" @yield Timeout(sim, 0.0)\n",
175+
" for proc in procs\n",
176+
" interrupt(proc)\n",
177+
" end\n",
178+
" for i=1:S\n",
179+
" @yield Put(spares, @coroutine machine(sim, repair_facility, spares))\n",
180+
" end\n",
181+
"end"
182+
]
183+
},
184+
{
185+
"cell_type": "markdown",
186+
"metadata": {},
187+
"source": [
188+
"## One simulation run"
189+
]
190+
},
191+
{
192+
"cell_type": "code",
193+
"execution_count": 5,
194+
"metadata": {
195+
"collapsed": false
196+
},
197+
"outputs": [
198+
{
199+
"data": {
200+
"text/plain": [
201+
"sim_repair (generic function with 1 method)"
202+
]
203+
},
204+
"execution_count": 5,
205+
"metadata": {},
206+
"output_type": "execute_result"
207+
}
208+
],
209+
"source": [
210+
"function sim_repair()\n",
211+
" sim = Simulation()\n",
212+
" repair_facility = Resource(sim)\n",
213+
" spares = Store(Coroutine, sim)\n",
214+
" @coroutine start_sim(sim, repair_facility, spares)\n",
215+
" msg = run(sim)\n",
216+
" stop_time = now(sim)\n",
217+
" println(\"At time $stop_time: $msg\")\n",
218+
" stop_time\n",
219+
"end"
220+
]
221+
},
222+
{
223+
"cell_type": "markdown",
224+
"metadata": {},
225+
"source": [
226+
"## Multiple simulations"
227+
]
228+
},
229+
{
230+
"cell_type": "code",
231+
"execution_count": 6,
232+
"metadata": {
233+
"collapsed": false
234+
},
235+
"outputs": [
236+
{
237+
"ename": "LoadError",
238+
"evalue": "\u001b[91mUndefVarError: machine not defined\u001b[39m",
239+
"output_type": "error",
240+
"traceback": [
241+
"\u001b[91mUndefVarError: machine not defined\u001b[39m",
242+
"",
243+
"Stacktrace:",
244+
" [1] \u001b[1mmacro expansion\u001b[22m\u001b[22m at \u001b[1m/home/juser/.julia/v0.6/ResumableFunctions/src/transforms.jl:16\u001b[22m\u001b[22m [inlined]",
245+
" [2] \u001b[1mmacro expansion\u001b[22m\u001b[22m at \u001b[1m./In[4]:14\u001b[22m\u001b[22m [inlined]",
246+
" [3] \u001b[1mmacro expansion\u001b[22m\u001b[22m at \u001b[1m/home/juser/.julia/v0.6/ResumableFunctions/src/macro.jl:66\u001b[22m\u001b[22m [inlined]",
247+
" [4] \u001b[1m(::##660)\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::Void\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m/home/juser/.julia/v0.6/ResumableFunctions/src/macrotoolutils.jl:99\u001b[22m\u001b[22m",
248+
" [5] \u001b[1mexecute\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::SimJulia.Timeout, ::SimJulia.Coroutine\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m/home/juser/.julia/v0.6/SimJulia/src/coroutines.jl:27\u001b[22m\u001b[22m",
249+
" [6] \u001b[1m(::SimJulia.##3#4{SimJulia.#execute,SimJulia.Timeout,Tuple{SimJulia.Coroutine}})\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m/home/juser/.julia/v0.6/SimJulia/src/base.jl:43\u001b[22m\u001b[22m",
250+
" [7] \u001b[1mstep\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::SimJulia.Simulation\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m/home/juser/.julia/v0.6/SimJulia/src/simulations.jl:39\u001b[22m\u001b[22m",
251+
" [8] \u001b[1mrun\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::SimJulia.Simulation, ::SimJulia.Timeout\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m/home/juser/.julia/v0.6/SimJulia/src/base.jl:87\u001b[22m\u001b[22m",
252+
" [9] \u001b[1msim_repair\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m./In[5]:6\u001b[22m\u001b[22m",
253+
" [10] \u001b[1mmacro expansion\u001b[22m\u001b[22m at \u001b[1m./In[6]:4\u001b[22m\u001b[22m [inlined]",
254+
" [11] \u001b[1manonymous\u001b[22m\u001b[22m at \u001b[1m./<missing>:?\u001b[22m\u001b[22m"
255+
]
256+
}
257+
],
258+
"source": [
259+
"srand(SEED)\n",
260+
"results = Float64[]\n",
261+
"for i=1:RUNS\n",
262+
" push!(results, sim_repair())\n",
263+
"end\n",
264+
"println(sum(results)/RUNS)"
265+
]
266+
},
267+
{
268+
"cell_type": "code",
269+
"execution_count": null,
270+
"metadata": {
271+
"collapsed": true
272+
},
273+
"outputs": [],
274+
"source": []
275+
}
276+
],
277+
"metadata": {
278+
"kernelspec": {
279+
"display_name": "Julia 0.6.0",
280+
"language": "julia",
281+
"name": "julia-0.6"
282+
},
283+
"language_info": {
284+
"file_extension": ".jl",
285+
"mimetype": "application/julia",
286+
"name": "julia",
287+
"version": "0.6.0"
288+
}
289+
},
290+
"nbformat": 4,
291+
"nbformat_minor": 2
292+
}

0 commit comments

Comments
 (0)