CSC 121, Jan-Apr 2017, Large Assignment #1.
> source("lga1-defs.R")
PART 1:
Tests of the simulate_queue and run_simulations functions.
> # Try the example in the assignment handout.
>
> set.seed(1)
> simulate_queue (10,0.7,1,2)
$arrival_times
[1] 1.079 2.767 2.967 3.426 3.795 7.931 8.552 8.929
$queue_lengths
[1] 0 0 1 2 3 1 2 3
$departure_times
[1] 2.652 4.712 6.096 7.866 9.363 10.630 12.017 13.030
>
> # Some other tests of simulate_queue.
>
> set.seed(1)
> simulate_queue (10,5,1,2) # high arrival rate
$arrival_times
[1] 0.1510 0.3874 0.4153 0.5025 1.0815 1.3274 1.4354 1.6267 1.6561 1.9343
[11] 2.1818 3.0666 3.2736 3.6488 3.7798 3.8471 3.9648 4.4377 4.4966 4.6097
[21] 4.6310 4.6428 4.7586 5.5504 5.7850 5.9844 6.2715 6.3363 6.6004 6.6411
[31] 6.8456 6.9060 7.0510 7.2013 7.2483 7.4643 7.6699 7.9284 8.0393 8.0996
[41] 8.3582 8.5571 8.6599 9.0615 9.4973
$queue_lengths
[1] 0 1 2 3 4 5 6 7 8 8 9 9 10 11 12 13 14 14 15 16 17 18 19
[24] 20 21 22 22 23 24 25 26 27 28 29 30 31 32 32 33 34 35 36 37 37 38
$departure_times
[1] 1.724 2.991 4.331 6.152 7.671 9.061 10.772 12.551 14.348 15.804
[11] 17.214 19.025 20.630 22.284 23.637 24.908 26.900 28.534 29.747 30.876
[21] 32.355 34.279 35.877 37.854 39.585 40.942 42.374 43.522 44.535 46.250
[31] 47.354 48.800 50.440 52.432 53.927 55.412 56.585 58.340 59.794 61.305
[41] 62.513 63.741 65.337 66.912 67.989
>
> set.seed(1)
> simulate_queue (10,0.1,1,2) # low arrival rate
$arrival_times
[1] 7.552
$queue_lengths
[1] 0
$departure_times
[1] 9.125
>
> set.seed(1)
> simulate_queue (10,0.01,1,2) # very low arrival rate - probably none served
$arrival_times
numeric(0)
$queue_lengths
numeric(0)
$departure_times
numeric(0)
>
> set.seed(1)
> simulate_queue (10,0.7,0.1,2.9) # wide service time distribution
$arrival_times
[1] 1.079 2.767 2.967 3.426 3.795 7.931 9.297
$queue_lengths
[1] 0 1 1 2 3 2 2
$departure_times
[1] 2.783 5.528 6.703 8.959 11.676 12.524 13.705
>
> # Some tests of run_simulations.
>
> run_simulations (1:3,10,0.7,1,2) # first should be same as test above
seed open_time rate low high served maxqueue avewait overtime
1 1 10 0.7 1 2 8 3 3.365 3.030
2 2 10 0.7 1 2 6 2 2.727 2.432
3 3 10 0.7 1 2 7 2 2.524 2.874
> run_simulations (3:1,10,0.7,1,2) # should be the same in reversed order
seed open_time rate low high served maxqueue avewait overtime
1 3 10 0.7 1 2 7 2 2.524 2.874
2 2 10 0.7 1 2 6 2 2.727 2.432
3 1 10 0.7 1 2 8 3 3.365 3.030
PART 2:
Results from four groups of simulations with varying low, high, and rate.
> # Do the four groups of simulations.
>
> df1 <- run_simulations (101:150, open_time=100, rate=0.5, low=1.0, high=2.0)
> df2 <- run_simulations (201:250, open_time=100, rate=0.8, low=1.0, high=2.0)
> df3 <- run_simulations (301:350, open_time=100, rate=0.5, low=1.4, high=1.6)
> df4 <- run_simulations (401:450, open_time=100, rate=0.8, low=1.4, high=1.6)
>
> # Combine the four groups into one data frame, and show it.
>
> df <- rbind(df1,df2,df3,df4)
> print(df)
seed open_time rate low high served maxqueue avewait overtime
1 101 100 0.5 1.0 2.0 60 7 4.061 4.28103
2 102 100 0.5 1.0 2.0 46 5 3.579 1.68206
3 103 100 0.5 1.0 2.0 55 9 6.649 0.12005
4 104 100 0.5 1.0 2.0 41 4 2.891 2.11585
5 105 100 0.5 1.0 2.0 51 6 4.069 2.67474
6 106 100 0.5 1.0 2.0 51 4 2.731 1.93062
7 107 100 0.5 1.0 2.0 57 5 3.903 3.01259
8 108 100 0.5 1.0 2.0 48 3 2.082 1.38019
9 109 100 0.5 1.0 2.0 45 4 2.631 0.06419
10 110 100 0.5 1.0 2.0 44 3 2.327 1.76400
11 111 100 0.5 1.0 2.0 68 13 7.237 13.78136
12 112 100 0.5 1.0 2.0 58 5 3.844 0.73442
13 113 100 0.5 1.0 2.0 51 3 2.489 1.67733
14 114 100 0.5 1.0 2.0 49 5 3.613 3.55317
15 115 100 0.5 1.0 2.0 44 2 1.912 2.00434
16 116 100 0.5 1.0 2.0 51 6 4.848 0.29790
17 117 100 0.5 1.0 2.0 55 5 4.050 3.36366
18 118 100 0.5 1.0 2.0 33 3 2.401 0.00788
19 119 100 0.5 1.0 2.0 52 6 5.758 0.00000
20 120 100 0.5 1.0 2.0 53 6 4.102 2.28606
21 121 100 0.5 1.0 2.0 44 4 2.743 1.46865
22 122 100 0.5 1.0 2.0 41 4 2.925 0.00000
23 123 100 0.5 1.0 2.0 47 6 3.876 5.74398
24 124 100 0.5 1.0 2.0 44 3 1.962 0.00000
25 125 100 0.5 1.0 2.0 50 4 3.015 1.86390
26 126 100 0.5 1.0 2.0 52 5 3.907 0.00000
27 127 100 0.5 1.0 2.0 60 8 4.265 7.43370
28 128 100 0.5 1.0 2.0 50 4 3.289 0.63226
29 129 100 0.5 1.0 2.0 54 10 4.715 4.30582
30 130 100 0.5 1.0 2.0 60 6 3.812 1.54283
31 131 100 0.5 1.0 2.0 44 3 2.546 5.54552
32 132 100 0.5 1.0 2.0 47 4 2.984 0.00000
33 133 100 0.5 1.0 2.0 58 3 2.807 3.74361
34 134 100 0.5 1.0 2.0 50 5 3.783 5.09621
35 135 100 0.5 1.0 2.0 56 8 6.136 0.00000
36 136 100 0.5 1.0 2.0 50 7 3.624 4.08397
37 137 100 0.5 1.0 2.0 61 6 3.453 5.64384
38 138 100 0.5 1.0 2.0 53 3 2.812 2.94473
39 139 100 0.5 1.0 2.0 48 5 3.206 1.22263
40 140 100 0.5 1.0 2.0 58 8 5.494 0.00000
41 141 100 0.5 1.0 2.0 67 7 5.166 5.83137
42 142 100 0.5 1.0 2.0 46 4 2.527 1.76526
43 143 100 0.5 1.0 2.0 54 4 2.830 6.05917
44 144 100 0.5 1.0 2.0 32 3 2.150 2.48208
45 145 100 0.5 1.0 2.0 63 5 3.185 6.13284
46 146 100 0.5 1.0 2.0 43 3 2.337 0.00000
47 147 100 0.5 1.0 2.0 71 9 8.584 7.67825
48 148 100 0.5 1.0 2.0 44 2 2.213 0.00000
49 149 100 0.5 1.0 2.0 38 4 2.481 1.69150
50 150 100 0.5 1.0 2.0 55 9 4.807 3.45715
51 201 100 0.8 1.0 2.0 78 18 13.826 25.81523
52 202 100 0.8 1.0 2.0 85 23 19.501 30.27401
53 203 100 0.8 1.0 2.0 90 27 21.544 38.79734
54 204 100 0.8 1.0 2.0 83 24 18.012 38.39903
55 205 100 0.8 1.0 2.0 74 9 8.894 11.28738
56 206 100 0.8 1.0 2.0 70 11 11.849 10.46401
57 207 100 0.8 1.0 2.0 80 14 9.771 18.80759
58 208 100 0.8 1.0 2.0 88 23 21.958 32.68995
59 209 100 0.8 1.0 2.0 88 20 14.511 32.27665
60 210 100 0.8 1.0 2.0 78 22 20.373 18.06953
61 211 100 0.8 1.0 2.0 80 21 13.432 22.09327
62 212 100 0.8 1.0 2.0 85 20 12.942 30.44792
63 213 100 0.8 1.0 2.0 70 13 13.046 18.29063
64 214 100 0.8 1.0 2.0 80 14 13.795 23.13529
65 215 100 0.8 1.0 2.0 59 8 5.722 5.27932
66 216 100 0.8 1.0 2.0 87 23 23.225 32.86880
67 217 100 0.8 1.0 2.0 81 18 13.562 23.66266
68 218 100 0.8 1.0 2.0 76 18 15.154 13.51419
69 219 100 0.8 1.0 2.0 66 6 4.913 5.16800
70 220 100 0.8 1.0 2.0 68 12 10.327 9.45372
71 221 100 0.8 1.0 2.0 72 12 10.527 13.96850
72 222 100 0.8 1.0 2.0 78 23 16.625 30.55946
73 223 100 0.8 1.0 2.0 84 19 16.223 25.01209
74 224 100 0.8 1.0 2.0 85 20 17.636 30.52356
75 225 100 0.8 1.0 2.0 81 21 19.117 30.08232
76 226 100 0.8 1.0 2.0 77 13 6.323 19.94842
77 227 100 0.8 1.0 2.0 74 13 8.933 18.07366
78 228 100 0.8 1.0 2.0 87 25 17.091 36.53372
79 229 100 0.8 1.0 2.0 75 10 8.084 16.72909
80 230 100 0.8 1.0 2.0 82 19 14.024 24.32143
81 231 100 0.8 1.0 2.0 84 19 17.760 25.01128
82 232 100 0.8 1.0 2.0 81 19 14.086 25.72870
83 233 100 0.8 1.0 2.0 87 23 21.804 33.76154
84 234 100 0.8 1.0 2.0 98 30 23.880 42.23234
85 235 100 0.8 1.0 2.0 81 19 14.482 25.05183
86 236 100 0.8 1.0 2.0 83 16 15.066 24.36447
87 237 100 0.8 1.0 2.0 81 17 17.864 20.74866
88 238 100 0.8 1.0 2.0 88 29 26.450 37.86946
89 239 100 0.8 1.0 2.0 108 38 28.383 55.10715
90 240 100 0.8 1.0 2.0 63 9 6.503 0.00000
91 241 100 0.8 1.0 2.0 85 21 18.285 29.84408
92 242 100 0.8 1.0 2.0 71 16 8.839 22.29637
93 243 100 0.8 1.0 2.0 88 21 17.163 30.11538
94 244 100 0.8 1.0 2.0 77 19 11.397 26.52653
95 245 100 0.8 1.0 2.0 78 12 7.863 17.89180
96 246 100 0.8 1.0 2.0 73 9 9.322 12.85595
97 247 100 0.8 1.0 2.0 96 29 26.068 40.28893
98 248 100 0.8 1.0 2.0 90 26 21.749 33.12631
99 249 100 0.8 1.0 2.0 76 10 8.113 15.22512
100 250 100 0.8 1.0 2.0 78 12 11.712 17.95381
101 301 100 0.5 1.4 1.6 53 5 3.111 0.20746
102 302 100 0.5 1.4 1.6 50 4 2.678 6.60708
103 303 100 0.5 1.4 1.6 52 3 2.913 0.64808
104 304 100 0.5 1.4 1.6 51 3 2.669 0.53007
105 305 100 0.5 1.4 1.6 50 6 3.962 0.00000
106 306 100 0.5 1.4 1.6 43 5 2.825 1.31222
107 307 100 0.5 1.4 1.6 51 5 3.295 1.40552
108 308 100 0.5 1.4 1.6 58 3 2.877 2.35620
109 309 100 0.5 1.4 1.6 50 4 3.014 1.41318
110 310 100 0.5 1.4 1.6 52 2 2.492 1.20841
111 311 100 0.5 1.4 1.6 63 9 5.827 1.03607
112 312 100 0.5 1.4 1.6 48 4 2.383 0.45036
113 313 100 0.5 1.4 1.6 50 5 3.217 0.00000
114 314 100 0.5 1.4 1.6 52 5 3.801 1.48208
115 315 100 0.5 1.4 1.6 48 4 2.843 0.00000
116 316 100 0.5 1.4 1.6 58 5 3.656 0.00000
117 317 100 0.5 1.4 1.6 51 6 3.525 0.88727
118 318 100 0.5 1.4 1.6 45 3 2.290 2.87197
119 319 100 0.5 1.4 1.6 63 6 3.749 2.05276
120 320 100 0.5 1.4 1.6 56 7 5.092 2.86087
121 321 100 0.5 1.4 1.6 49 3 2.481 0.60678
122 322 100 0.5 1.4 1.6 54 4 3.043 3.85114
123 323 100 0.5 1.4 1.6 42 6 3.452 0.00000
124 324 100 0.5 1.4 1.6 52 3 2.370 1.35159
125 325 100 0.5 1.4 1.6 56 7 4.435 2.31368
126 326 100 0.5 1.4 1.6 47 3 2.512 1.04399
127 327 100 0.5 1.4 1.6 58 5 4.511 0.00000
128 328 100 0.5 1.4 1.6 46 5 3.045 3.23456
129 329 100 0.5 1.4 1.6 56 6 3.561 0.00000
130 330 100 0.5 1.4 1.6 61 5 3.552 6.55797
131 331 100 0.5 1.4 1.6 50 7 4.635 0.93468
132 332 100 0.5 1.4 1.6 67 9 7.974 2.20217
133 333 100 0.5 1.4 1.6 48 4 2.529 1.96437
134 334 100 0.5 1.4 1.6 52 5 3.481 1.38148
135 335 100 0.5 1.4 1.6 29 2 1.929 1.19967
136 336 100 0.5 1.4 1.6 47 5 3.588 0.00000
137 337 100 0.5 1.4 1.6 52 4 2.779 1.09671
138 338 100 0.5 1.4 1.6 41 3 2.620 0.65798
139 339 100 0.5 1.4 1.6 60 5 3.874 2.68610
140 340 100 0.5 1.4 1.6 59 8 5.669 2.10649
141 341 100 0.5 1.4 1.6 39 3 2.373 0.55345
142 342 100 0.5 1.4 1.6 46 4 2.650 1.20787
143 343 100 0.5 1.4 1.6 44 4 2.875 0.00000
144 344 100 0.5 1.4 1.6 48 3 2.422 1.37159
145 345 100 0.5 1.4 1.6 49 4 2.696 0.00000
146 346 100 0.5 1.4 1.6 51 7 3.823 2.78418
147 347 100 0.5 1.4 1.6 50 5 2.987 1.32600
148 348 100 0.5 1.4 1.6 47 4 2.683 1.44204
149 349 100 0.5 1.4 1.6 68 12 9.312 7.89013
150 350 100 0.5 1.4 1.6 48 4 2.692 0.00000
151 401 100 0.8 1.4 1.6 71 10 10.164 8.17523
152 402 100 0.8 1.4 1.6 69 6 5.128 9.01923
153 403 100 0.8 1.4 1.6 71 9 7.796 14.24329
154 404 100 0.8 1.4 1.6 93 28 20.989 41.18749
155 405 100 0.8 1.4 1.6 71 11 10.233 10.69934
156 406 100 0.8 1.4 1.6 68 7 5.022 7.73128
157 407 100 0.8 1.4 1.6 73 8 5.795 11.08005
158 408 100 0.8 1.4 1.6 74 15 10.663 23.79429
159 409 100 0.8 1.4 1.6 77 14 14.929 16.29340
160 410 100 0.8 1.4 1.6 80 18 15.938 22.88481
161 411 100 0.8 1.4 1.6 85 19 14.361 27.79537
162 412 100 0.8 1.4 1.6 89 26 19.708 36.33453
163 413 100 0.8 1.4 1.6 94 28 25.372 41.73144
164 414 100 0.8 1.4 1.6 97 32 24.523 46.57966
165 415 100 0.8 1.4 1.6 82 15 14.385 22.65513
166 416 100 0.8 1.4 1.6 74 15 9.893 18.05167
167 417 100 0.8 1.4 1.6 85 19 13.790 27.73633
168 418 100 0.8 1.4 1.6 71 8 7.891 8.72625
169 419 100 0.8 1.4 1.6 74 11 8.363 11.63700
170 420 100 0.8 1.4 1.6 78 17 14.078 20.63940
171 421 100 0.8 1.4 1.6 88 21 15.312 32.23813
172 422 100 0.8 1.4 1.6 74 11 6.928 17.27670
173 423 100 0.8 1.4 1.6 84 20 17.925 28.85381
174 424 100 0.8 1.4 1.6 87 23 16.219 33.86447
175 425 100 0.8 1.4 1.6 84 21 17.887 26.90011
176 426 100 0.8 1.4 1.6 83 18 14.432 25.57846
177 427 100 0.8 1.4 1.6 91 27 18.960 40.89584
178 428 100 0.8 1.4 1.6 84 22 21.365 25.98788
179 429 100 0.8 1.4 1.6 90 28 21.802 36.38700
180 430 100 0.8 1.4 1.6 85 20 17.061 29.34930
181 431 100 0.8 1.4 1.6 83 20 14.958 25.11497
182 432 100 0.8 1.4 1.6 73 13 13.337 13.42974
183 433 100 0.8 1.4 1.6 84 20 17.098 30.10695
184 434 100 0.8 1.4 1.6 73 9 10.048 9.77405
185 435 100 0.8 1.4 1.6 90 26 22.718 36.41945
186 436 100 0.8 1.4 1.6 80 19 18.417 20.20428
187 437 100 0.8 1.4 1.6 76 11 10.007 14.14837
188 438 100 0.8 1.4 1.6 83 18 16.610 25.52479
189 439 100 0.8 1.4 1.6 75 10 8.536 14.34776
190 440 100 0.8 1.4 1.6 74 10 8.663 11.76472
191 441 100 0.8 1.4 1.6 74 10 10.151 12.93391
192 442 100 0.8 1.4 1.6 91 26 23.683 38.37799
193 443 100 0.8 1.4 1.6 68 10 7.620 4.95810
194 444 100 0.8 1.4 1.6 77 14 9.577 19.29298
195 445 100 0.8 1.4 1.6 67 10 5.799 11.45546
196 446 100 0.8 1.4 1.6 77 12 8.908 17.29114
197 447 100 0.8 1.4 1.6 93 27 24.594 40.76221
198 448 100 0.8 1.4 1.6 93 28 25.154 40.50517
199 449 100 0.8 1.4 1.6 91 26 19.265 38.19678
200 450 100 0.8 1.4 1.6 79 13 9.406 18.52030
>
> # Produce the plots for rate=0.5 and rate=0.8.
>
> for (rate in c(0.5,0.8)) {
+
+ # Extract the data for this rate, with wide and narrow range of service time
+
+ wide <- df [df$rate==rate & df$low==1.0, ]
+ narrow <- df [df$rate==rate & df$low==1.4, ]
+
+ # Produce the plot, with red points for wide and green points for narrow
+
+ plot (c(wide$served,narrow$served), c(wide$avewait,narrow$avewait),
+ col = c (rep("red",nrow(wide)), rep("green",nrow(narrow))),
+ xlab="Number served", ylab="Average wait time")
+
+ # Add title identifying the rate and colour scheme
+
+ title (paste ("Results with arrival rate", rate,
+ "(red 1.0,2.0; green 1.4,1.6)"))
+ }
As one would expect, both the number served and the average wait time tends to be higher when the arrival rate is higher. Simulations in which a larger number were served tend to have a higher average wait time, again as one would expect.
Whether the service time distribution is wide or narrow seems to have no noticeable effect, since in these plots, the distribution of the red dots seems to be about the same as the distribution of the green dots.
There must be some difference, however, since when low=1.4, it's not possible for the average wait time to be less than 1.4, whereas this is possible (though unlikely) when low=1.0.