Skip to content

Commit de32d75

Browse files
authored
Merge pull request #40 from BenLauwens/release-0.4
Update example Ross 7.7
2 parents 36d7176 + f8cd46f commit de32d75

1 file changed

Lines changed: 91 additions & 40 deletions

File tree

examples/Ross_Simulation_7.7_A_repair_problem.ipynb

Lines changed: 91 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,8 @@
1212
"cell_type": "markdown",
1313
"metadata": {},
1414
"source": [
15-
"## Description"
16-
]
17-
},
18-
{
19-
"cell_type": "markdown",
20-
"metadata": {},
21-
"source": [
15+
"## Description\n",
16+
"\n",
2217
"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",
2318
"\n",
2419
"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."
@@ -53,7 +48,7 @@
5348
},
5449
{
5550
"cell_type": "code",
56-
"execution_count": null,
51+
"execution_count": 13,
5752
"metadata": {
5853
"collapsed": true
5954
},
@@ -73,7 +68,7 @@
7368
},
7469
{
7570
"cell_type": "code",
76-
"execution_count": null,
71+
"execution_count": 14,
7772
"metadata": {
7873
"collapsed": false
7974
},
@@ -84,7 +79,11 @@
8479
"const S = 3\n",
8580
"const SEED = 150\n",
8681
"const LAMBDA = 100\n",
87-
"const MU = 1;"
82+
"const MU = 1\n",
83+
"\n",
84+
"srand(SEED)\n",
85+
"F = Exponential(LAMBDA)\n",
86+
"G = Exponential(MU);"
8887
]
8988
},
9089
{
@@ -96,38 +95,47 @@
9695
},
9796
{
9897
"cell_type": "code",
99-
"execution_count": null,
98+
"execution_count": 15,
10099
"metadata": {
101100
"collapsed": false
102101
},
103-
"outputs": [],
102+
"outputs": [
103+
{
104+
"data": {
105+
"text/plain": [
106+
"machine (generic function with 1 method)"
107+
]
108+
},
109+
"execution_count": 15,
110+
"metadata": {},
111+
"output_type": "execute_result"
112+
}
113+
],
104114
"source": [
105115
"@resumable function machine(sim::Simulation, repair_facility::Resource, spares::Store{Coroutine})\n",
106-
" dist_work = Exponential(LAMBDA)\n",
107-
" dist_repair = Exponential(MU)\n",
108116
" while true\n",
109117
" try\n",
110118
" @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",
119+
" catch exc\n",
120+
" end\n",
121+
" #println(\"At time $(now(sim)): $(active_process(sim)) starts working.\")\n",
122+
" @yield Timeout(sim, rand(F))\n",
123+
" #println(\"At time $(now(sim)): $(active_process(sim)) stops working.\")\n",
124+
" get_spare = Get(spares)\n",
125+
" @yield get_spare | Timeout(sim, 0.0)\n",
126+
" if state(get_spare) != SimJulia.idle\n",
127+
" interrupt(value(get_spare))\n",
128+
" else\n",
129+
" throw(SimJulia.StopSimulation(\"No more spares!\"))\n",
130+
" end\n",
131+
" @yield Request(repair_facility)\n",
132+
" #println(\"At time $(now(sim)): $(active_process(sim)) repair starts.\")\n",
133+
" @yield Timeout(sim, rand(G))\n",
134+
" @yield Release(repair_facility)\n",
135+
" #println(\"At time $(now(sim)): $(active_process(sim)) is repaired.\")\n",
136+
" @yield Put(spares, active_process(sim))\n",
122137
" 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"
138+
"end"
131139
]
132140
},
133141
{
@@ -139,11 +147,22 @@
139147
},
140148
{
141149
"cell_type": "code",
142-
"execution_count": null,
150+
"execution_count": 16,
143151
"metadata": {
144152
"collapsed": false
145153
},
146-
"outputs": [],
154+
"outputs": [
155+
{
156+
"data": {
157+
"text/plain": [
158+
"start_sim (generic function with 1 method)"
159+
]
160+
},
161+
"execution_count": 16,
162+
"metadata": {},
163+
"output_type": "execute_result"
164+
}
165+
],
147166
"source": [
148167
"@resumable function start_sim(sim::Simulation, repair_facility::Resource, spares::Store{Coroutine})\n",
149168
" procs = Coroutine[]\n",
@@ -169,11 +188,22 @@
169188
},
170189
{
171190
"cell_type": "code",
172-
"execution_count": null,
191+
"execution_count": 17,
173192
"metadata": {
174193
"collapsed": false
175194
},
176-
"outputs": [],
195+
"outputs": [
196+
{
197+
"data": {
198+
"text/plain": [
199+
"sim_repair (generic function with 1 method)"
200+
]
201+
},
202+
"execution_count": 17,
203+
"metadata": {},
204+
"output_type": "execute_result"
205+
}
206+
],
177207
"source": [
178208
"function sim_repair()\n",
179209
" sim = Simulation()\n",
@@ -196,20 +226,41 @@
196226
},
197227
{
198228
"cell_type": "code",
199-
"execution_count": null,
229+
"execution_count": 18,
200230
"metadata": {
201231
"collapsed": false
202232
},
203-
"outputs": [],
233+
"outputs": [
234+
{
235+
"name": "stdout",
236+
"output_type": "stream",
237+
"text": [
238+
"At time 5573.772841846017: No more spares!\n",
239+
"At time 1438.0294516073466: No more spares!\n",
240+
"At time 7077.413276961621: No more spares!\n",
241+
"At time 7286.490682742159: No more spares!\n",
242+
"At time 6820.788098062124: No more spares!\n",
243+
"5639.298870243853\n"
244+
]
245+
}
246+
],
204247
"source": [
205-
"srand(SEED)\n",
206248
"results = Float64[]\n",
207249
"for i=1:RUNS\n",
208250
" push!(results, sim_repair())\n",
209251
"end\n",
210252
"println(sum(results)/RUNS)"
211253
]
212254
},
255+
{
256+
"cell_type": "code",
257+
"execution_count": null,
258+
"metadata": {
259+
"collapsed": true
260+
},
261+
"outputs": [],
262+
"source": []
263+
},
213264
{
214265
"cell_type": "code",
215266
"execution_count": null,

0 commit comments

Comments
 (0)