|
12 | 12 | "cell_type": "markdown", |
13 | 13 | "metadata": {}, |
14 | 14 | "source": [ |
15 | | - "## Description" |
16 | | - ] |
17 | | - }, |
18 | | - { |
19 | | - "cell_type": "markdown", |
20 | | - "metadata": {}, |
21 | | - "source": [ |
| 15 | + "## Description\n", |
| 16 | + "\n", |
22 | 17 | "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 | 18 | "\n", |
24 | 19 | "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 | 48 | }, |
54 | 49 | { |
55 | 50 | "cell_type": "code", |
56 | | - "execution_count": null, |
| 51 | + "execution_count": 13, |
57 | 52 | "metadata": { |
58 | 53 | "collapsed": true |
59 | 54 | }, |
|
73 | 68 | }, |
74 | 69 | { |
75 | 70 | "cell_type": "code", |
76 | | - "execution_count": null, |
| 71 | + "execution_count": 14, |
77 | 72 | "metadata": { |
78 | 73 | "collapsed": false |
79 | 74 | }, |
|
84 | 79 | "const S = 3\n", |
85 | 80 | "const SEED = 150\n", |
86 | 81 | "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);" |
88 | 87 | ] |
89 | 88 | }, |
90 | 89 | { |
|
96 | 95 | }, |
97 | 96 | { |
98 | 97 | "cell_type": "code", |
99 | | - "execution_count": null, |
| 98 | + "execution_count": 15, |
100 | 99 | "metadata": { |
101 | 100 | "collapsed": false |
102 | 101 | }, |
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 | + ], |
104 | 114 | "source": [ |
105 | 115 | "@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 | 116 | " while true\n", |
109 | 117 | " try\n", |
110 | 118 | " @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", |
122 | 137 | " 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" |
131 | 139 | ] |
132 | 140 | }, |
133 | 141 | { |
|
139 | 147 | }, |
140 | 148 | { |
141 | 149 | "cell_type": "code", |
142 | | - "execution_count": null, |
| 150 | + "execution_count": 16, |
143 | 151 | "metadata": { |
144 | 152 | "collapsed": false |
145 | 153 | }, |
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 | + ], |
147 | 166 | "source": [ |
148 | 167 | "@resumable function start_sim(sim::Simulation, repair_facility::Resource, spares::Store{Coroutine})\n", |
149 | 168 | " procs = Coroutine[]\n", |
|
169 | 188 | }, |
170 | 189 | { |
171 | 190 | "cell_type": "code", |
172 | | - "execution_count": null, |
| 191 | + "execution_count": 17, |
173 | 192 | "metadata": { |
174 | 193 | "collapsed": false |
175 | 194 | }, |
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 | + ], |
177 | 207 | "source": [ |
178 | 208 | "function sim_repair()\n", |
179 | 209 | " sim = Simulation()\n", |
|
196 | 226 | }, |
197 | 227 | { |
198 | 228 | "cell_type": "code", |
199 | | - "execution_count": null, |
| 229 | + "execution_count": 18, |
200 | 230 | "metadata": { |
201 | 231 | "collapsed": false |
202 | 232 | }, |
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 | + ], |
204 | 247 | "source": [ |
205 | | - "srand(SEED)\n", |
206 | 248 | "results = Float64[]\n", |
207 | 249 | "for i=1:RUNS\n", |
208 | 250 | " push!(results, sim_repair())\n", |
209 | 251 | "end\n", |
210 | 252 | "println(sum(results)/RUNS)" |
211 | 253 | ] |
212 | 254 | }, |
| 255 | + { |
| 256 | + "cell_type": "code", |
| 257 | + "execution_count": null, |
| 258 | + "metadata": { |
| 259 | + "collapsed": true |
| 260 | + }, |
| 261 | + "outputs": [], |
| 262 | + "source": [] |
| 263 | + }, |
213 | 264 | { |
214 | 265 | "cell_type": "code", |
215 | 266 | "execution_count": null, |
|
0 commit comments