Skip to content

Commit 88fa272

Browse files
committed
Update example Ross 7.7
1 parent 5bc2163 commit 88fa272

2 files changed

Lines changed: 74 additions & 127 deletions

File tree

examples/.ipynb_checkpoints/Ross_Simulation_7.7_A_repair_problem-checkpoint.ipynb

Lines changed: 28 additions & 78 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": 1,
51+
"execution_count": null,
5752
"metadata": {
5853
"collapsed": true
5954
},
@@ -73,7 +68,7 @@
7368
},
7469
{
7570
"cell_type": "code",
76-
"execution_count": 2,
71+
"execution_count": null,
7772
"metadata": {
7873
"collapsed": false
7974
},
@@ -96,32 +91,22 @@
9691
},
9792
{
9893
"cell_type": "code",
99-
"execution_count": 7,
94+
"execution_count": null,
10095
"metadata": {
10196
"collapsed": false
10297
},
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-
],
98+
"outputs": [],
11499
"source": [
115100
"@resumable function machine(sim::Simulation, repair_facility::Resource, spares::Store{Coroutine})\n",
116-
" dist_work = Exponential(LAMBDA)\n",
117-
" dist_repair = Exponential(MU)\n",
101+
" F = Exponential(LAMBDA)\n",
102+
" G = Exponential(MU)\n",
118103
" while true\n",
119-
" try \n",
104+
" try\n",
120105
" @yield Timeout(sim, Inf)\n",
121-
" catch\n",
106+
" catch exc\n",
122107
" end\n",
123108
" #println(\"At time $(now(sim)): $(active_process(sim)) starts working.\")\n",
124-
" @yield Timeout(sim, rand(dist_work))\n",
109+
" @yield Timeout(sim, rand(F))\n",
125110
" #println(\"At time $(now(sim)): $(active_process(sim)) stops working.\")\n",
126111
" get_spare = Get(spares)\n",
127112
" @yield get_spare | Timeout(sim, 0.0)\n",
@@ -132,12 +117,12 @@
132117
" end\n",
133118
" @yield Request(repair_facility)\n",
134119
" #println(\"At time $(now(sim)): $(active_process(sim)) repair starts.\")\n",
135-
" @yield Timeout(sim, rand(dist_repair))\n",
120+
" @yield Timeout(sim, rand(G))\n",
136121
" @yield Release(repair_facility)\n",
137122
" #println(\"At time $(now(sim)): $(active_process(sim)) is repaired.\")\n",
138123
" @yield Put(spares, active_process(sim))\n",
139124
" end\n",
140-
"end\n"
125+
"end"
141126
]
142127
},
143128
{
@@ -149,22 +134,11 @@
149134
},
150135
{
151136
"cell_type": "code",
152-
"execution_count": 4,
137+
"execution_count": null,
153138
"metadata": {
154139
"collapsed": false
155140
},
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-
],
141+
"outputs": [],
168142
"source": [
169143
"@resumable function start_sim(sim::Simulation, repair_facility::Resource, spares::Store{Coroutine})\n",
170144
" procs = Coroutine[]\n",
@@ -190,22 +164,11 @@
190164
},
191165
{
192166
"cell_type": "code",
193-
"execution_count": 5,
167+
"execution_count": null,
194168
"metadata": {
195169
"collapsed": false
196170
},
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-
],
171+
"outputs": [],
209172
"source": [
210173
"function sim_repair()\n",
211174
" sim = Simulation()\n",
@@ -228,33 +191,11 @@
228191
},
229192
{
230193
"cell_type": "code",
231-
"execution_count": 6,
194+
"execution_count": null,
232195
"metadata": {
233196
"collapsed": false
234197
},
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-
],
198+
"outputs": [],
258199
"source": [
259200
"srand(SEED)\n",
260201
"results = Float64[]\n",
@@ -264,6 +205,15 @@
264205
"println(sum(results)/RUNS)"
265206
]
266207
},
208+
{
209+
"cell_type": "code",
210+
"execution_count": null,
211+
"metadata": {
212+
"collapsed": true
213+
},
214+
"outputs": [],
215+
"source": []
216+
},
267217
{
268218
"cell_type": "code",
269219
"execution_count": null,

examples/Ross_Simulation_7.7_A_repair_problem.ipynb

Lines changed: 46 additions & 49 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": 1,
51+
"execution_count": 13,
5752
"metadata": {
5853
"collapsed": true
5954
},
@@ -73,7 +68,7 @@
7368
},
7469
{
7570
"cell_type": "code",
76-
"execution_count": 2,
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,32 +95,31 @@
9695
},
9796
{
9897
"cell_type": "code",
99-
"execution_count": 7,
98+
"execution_count": 15,
10099
"metadata": {
101100
"collapsed": false
102101
},
103102
"outputs": [
104103
{
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-
]
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"
112112
}
113113
],
114114
"source": [
115115
"@resumable function machine(sim::Simulation, repair_facility::Resource, spares::Store{Coroutine})\n",
116-
" dist_work = Exponential(LAMBDA)\n",
117-
" dist_repair = Exponential(MU)\n",
118116
" while true\n",
119-
" try \n",
117+
" try\n",
120118
" @yield Timeout(sim, Inf)\n",
121-
" catch\n",
119+
" catch exc\n",
122120
" end\n",
123121
" #println(\"At time $(now(sim)): $(active_process(sim)) starts working.\")\n",
124-
" @yield Timeout(sim, rand(dist_work))\n",
122+
" @yield Timeout(sim, rand(F))\n",
125123
" #println(\"At time $(now(sim)): $(active_process(sim)) stops working.\")\n",
126124
" get_spare = Get(spares)\n",
127125
" @yield get_spare | Timeout(sim, 0.0)\n",
@@ -132,12 +130,12 @@
132130
" end\n",
133131
" @yield Request(repair_facility)\n",
134132
" #println(\"At time $(now(sim)): $(active_process(sim)) repair starts.\")\n",
135-
" @yield Timeout(sim, rand(dist_repair))\n",
133+
" @yield Timeout(sim, rand(G))\n",
136134
" @yield Release(repair_facility)\n",
137135
" #println(\"At time $(now(sim)): $(active_process(sim)) is repaired.\")\n",
138136
" @yield Put(spares, active_process(sim))\n",
139137
" end\n",
140-
"end\n"
138+
"end"
141139
]
142140
},
143141
{
@@ -149,7 +147,7 @@
149147
},
150148
{
151149
"cell_type": "code",
152-
"execution_count": 4,
150+
"execution_count": 16,
153151
"metadata": {
154152
"collapsed": false
155153
},
@@ -160,7 +158,7 @@
160158
"start_sim (generic function with 1 method)"
161159
]
162160
},
163-
"execution_count": 4,
161+
"execution_count": 16,
164162
"metadata": {},
165163
"output_type": "execute_result"
166164
}
@@ -190,7 +188,7 @@
190188
},
191189
{
192190
"cell_type": "code",
193-
"execution_count": 5,
191+
"execution_count": 17,
194192
"metadata": {
195193
"collapsed": false
196194
},
@@ -201,7 +199,7 @@
201199
"sim_repair (generic function with 1 method)"
202200
]
203201
},
204-
"execution_count": 5,
202+
"execution_count": 17,
205203
"metadata": {},
206204
"output_type": "execute_result"
207205
}
@@ -228,42 +226,41 @@
228226
},
229227
{
230228
"cell_type": "code",
231-
"execution_count": 6,
229+
"execution_count": 18,
232230
"metadata": {
233231
"collapsed": false
234232
},
235233
"outputs": [
236234
{
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"
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"
255244
]
256245
}
257246
],
258247
"source": [
259-
"srand(SEED)\n",
260248
"results = Float64[]\n",
261249
"for i=1:RUNS\n",
262250
" push!(results, sim_repair())\n",
263251
"end\n",
264252
"println(sum(results)/RUNS)"
265253
]
266254
},
255+
{
256+
"cell_type": "code",
257+
"execution_count": null,
258+
"metadata": {
259+
"collapsed": true
260+
},
261+
"outputs": [],
262+
"source": []
263+
},
267264
{
268265
"cell_type": "code",
269266
"execution_count": null,

0 commit comments

Comments
 (0)