-
Notifications
You must be signed in to change notification settings - Fork 0
/
mm1n.html
570 lines (540 loc) · 16.8 KB
/
mm1n.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Experiment 4</title>
<link
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
/>
<style>
/* Add a black background color to the top navigation */
.topnav {
background-color: #333;
overflow: hidden;
width: 100vw;
display: flex;
flex-direction: row;
justify-content: center;
}
/* Style the links inside the navigation bar */
.topnav a {
float: left;
color: #f2f2f2;
text-align: center;
padding: 20px 30px;
text-decoration: none;
font-size: 17px;
}
.topnav span {
font-size: x-large;
position: relative;
left: 0;
float: left;
color: #f2f2f2;
text-align: center;
padding: 14px 30px;
}
/* Change the color of links on hover */
.topnav a:hover {
background-color: #ddd;
color: black;
}
/* Add a color to the active/current link */
.topnav a.active {
background-color: #04aa6d;
color: white;
}
#chart_div {
width: 100vw;
height: 80vh;
}
#table_div {
display: none;
font-size: x-large;
margin: 5px;
height: 80vh;
}
body {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
</style>
<script>
let data;
let options;
let chart;
let chartId;
let rho;
let av_cust_s;
let av_cust_q;
let time;
let cust;
let ar;
let sr;
let ns = 1;
let end_times;
let start_times;
let av_time_s;
let cust_total;
let av_st;
let busy_servers;
let mc;
let blocked;
class Queue {
constructor() {
this.elements = {};
this.head = 0;
this.tail = 0;
}
enqueue(element) {
this.elements[this.tail] = element;
this.tail++;
}
dequeue() {
const item = this.elements[this.head];
delete this.elements[this.head];
this.head++;
return item;
}
peek() {
return this.elements[this.head];
}
get length() {
return this.tail - this.head;
}
get isEmpty() {
return this.length === 0;
}
}
window.onload = function () {
google.charts.load("current", {
packages: ["corechart"],
});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
data = google.visualization.arrayToDataTable([
["Time", "Customers"],
[0, 0],
]);
options = {
title: "No. of Customers in System vs Time",
width: "100%",
height: "100%",
hAxis: {
title: "Time",
},
vAxis: {
title: "Customers",
},
chartArea: { width: "80%", height: "80%" },
legend: { position: "bottom" },
};
chart = new google.visualization.LineChart(
document.getElementById("chart_div")
);
chart.draw(data, options);
}
};
function factorial(n) {
let answer = 1;
if (n == 0 || n == 1) {
return answer;
} else {
for (var i = n; i >= 1; i--) {
answer = answer * i;
}
return answer;
}
}
function check(a, b, c) {
if (a == "" || b == "" || c == "") {
alert("Values cannot be empty");
return false;
}
if (parseFloat(a) <= 0) {
alert("Arrival rate should be positive");
return false;
}
if (parseFloat(b) <= 0) {
alert("Service rate should be positive");
return false;
}
if (parseFloat(c) != parseInt(c)) {
alert("Maximum Customers should be an integer");
return false;
}
if (parseFloat(c) <= 0) {
alert("Maximum Customers should be positive");
return false;
}
return true;
}
function startSimulation(
arrival_rate,
service_rate,
max_customers,
speed
) {
let valid = check(arrival_rate, service_rate, max_customers);
if (!valid) return;
clearChart();
let sTime = Number.MAX_VALUE;
let in_use = false;
if (!arrival_rate) arrival_rate = 1.0;
if (!service_rate) service_rate = 1.0;
rho = arrival_rate / (ns * service_rate);
ar = arrival_rate;
sr = service_rate;
mc = max_customers;
busy_servers = 0;
end_times = [];
start_times = [];
for (let i = 0; i < ns; i++) {
end_times[i] = Number.MAX_VALUE;
start_times[i] = Number.MAX_VALUE;
}
let U = Math.random();
let nextTime = -Math.log(U) / ar;
let q = new Queue();
chartId = setInterval(function () {
/* stime = service time end, nextTime = next arrival time*/
/*Departure*/
if (sTime < nextTime) {
av_cust_s += cust * (sTime - time);
av_cust_q += Math.max(0, cust - ns) * (sTime - time);
time = sTime;
data.addRow([time, cust]);
let idx = -1;
for (let i = 0; i < ns; i++) {
if (end_times[i] == sTime) {
idx = i;
break;
}
}
av_time_s += time - start_times[idx];
cust--;
if (q.isEmpty) {
start_times[idx] = Number.MAX_VALUE;
end_times[idx] = Number.MAX_VALUE;
busy_servers--;
} else {
start_times[idx] = q.dequeue();
U = Math.random();
end_times[idx] = time - Math.log(U) / sr;
av_st -= Math.log(U) / sr;
}
// update service end time
sTime = end_times[0];
for (let i = 1; i < ns; i++) {
sTime = Math.min(sTime, end_times[i]);
}
} else {
// Arrival occurs here
av_cust_s += cust * (nextTime - time);
av_cust_q += Math.max(0, cust - ns) * (nextTime - time);
time = nextTime;
data.addRow([time, cust]);
if (cust < mc) {
cust++;
cust_total++;
console.log("Arrival occured");
console.log(busy_servers);
console.log(sTime);
console.log(end_times);
if (busy_servers == ns) {
q.enqueue(time);
} else {
let idx = -1;
for (let i = 0; i < ns; i++) {
if (end_times[i] == Number.MAX_VALUE) {
idx = i;
break;
}
}
console.log(idx);
start_times[idx] = time;
U = Math.random();
end_times[idx] = time - Math.log(U) / sr;
av_st -= Math.log(U) / sr;
busy_servers++;
sTime = end_times[0];
for (let i = 1; i < ns; i++) {
sTime = Math.min(sTime, end_times[i]);
}
}
} else {
blocked++;
}
U = Math.random();
nextTime = time - Math.log(U) / ar;
}
data.addRow([time, cust]);
chart.draw(data, options);
}, 2010 - speed);
}
function drawTable() {
document.getElementById("table_div").style.width = "40vw";
document.getElementById("chart_div").style.width = "40vw";
chart.draw(data, options);
document.getElementById("table_div").style.display = "block";
rho = ar / (ns * sr);
let t0 = 0.0;
for (let i = 0; i < ns; i++) {
t0 += Math.pow(ar / sr, i) / factorial(i);
}
console.log(t0);
let pa1 = 0;
let tmp1 = ar / (ns * sr);
if (tmp1 != 1) {
let h1 = Math.pow(ar, ns) * (1 - Math.pow(tmp1, mc - ns + 1));
console.log("h1", h1);
let h2 = Math.pow(sr, ns) * factorial(ns) * (1 - tmp1);
pa1 = h1 / h2;
} else {
let h1 = Math.pow(ar, ns) * (mc - ns + 1);
console.log("h1", h1);
let h2 = Math.pow(sr, ns) * factorial(ns);
pa1 = h1 / h2;
}
// console.log(Math.pow(ns, ns) / factorial(ns));
// console.log(Math.pow(ar / (ns * sr), ns) / (1 - ar / (ns * sr)));
console.log(pa1);
t0 += pa1;
console.log(t0);
let P0 = 1 / t0;
console.log(P0);
let Pk = (Math.pow(ar, ns) * P0) / (factorial(ns) * Math.pow(sr, ns));
let t1 = P0 * Math.pow(ar, ns) * tmp1;
let t2 = factorial(ns) * (1 - tmp1) * (1 - tmp1) * Math.pow(sr, ns);
let t3 =
1 -
Math.pow(rho, mc - ns + 1) -
(1 - rho) * (mc - ns + 1) * Math.pow(rho, mc - ns);
console.log(Math.pow(ar, ns) * tmp1);
console.log(t1);
console.log(t2);
console.log(t3);
let Lq = (t1 * t3) / t2;
console.log(Lq);
let ar_eff = ar * (1 - Pk);
// let Lq = t1/(factorial(ns)*Math.pow(1-rho, 2));
let Tq = Lq / ar_eff;
let Ts = Tq + 1 / sr;
let Ls = Lq + ar_eff / sr;
console.log(P0);
console.log(Lq);
let th_cs = Ls;
let ex_cs = av_cust_s / time;
let th_cq = Lq;
let ex_cq = av_cust_q / time;
let ex_st = av_st / (cust_total - cust + busy_servers);
let th_ts = th_cs / ar;
let ex_ts = av_time_s / cust_total;
let th_st = 1 / sr;
console.log(ar);
console.log(sr);
console.log(ar >= sr);
if (ar / sr >= 1) {
console.log("Hello");
document.getElementById("th_cs").innerHTML =
"Steady state solution does not exist";
document.getElementById("ex_cs").innerHTML =
ex_cs >= 0 && ex_cs != NaN
? ex_cs.toFixed(2)
: "Unable to calculate results";
document.getElementById("th_cq").innerHTML =
"Steady state solution does not exist";
document.getElementById("ex_cq").innerHTML =
ex_cq >= 0 && ex_cq != NaN
? ex_cq.toFixed(2)
: "Unable to calculate results";
document.getElementById("th_ts").innerHTML =
"Steady state solution does not exist";
document.getElementById("ex_ts").innerHTML =
ex_ts >= 0 && ex_ts != NaN
? ex_ts.toFixed(2)
: "Unable to calculate results";
document.getElementById("th_st").innerHTML =
"Steady state solution does not exist";
document.getElementById("ex_st").innerHTML =
ex_st >= 0 && ex_st != NaN
? ex_st.toFixed(2)
: "Unable to calculate results";
document.getElementById("ex").innerHTML =
"Experimental (" +
(time.toFixed(2) >= 0
? time.toFixed(2)
: "Time for simulation cannot be generated for given inputs") +
")";
} else {
console.log("Hello22");
document.getElementById("th_cs").innerHTML =
th_cs >= 0 && th_cs != NaN
? th_cs.toFixed(2)
: "Unable to calculate results";
document.getElementById("ex_cs").innerHTML =
ex_cs >= 0 && ex_cs != NaN
? ex_cs.toFixed(2)
: "Unable to calculate results";
document.getElementById("th_cq").innerHTML =
th_cq >= 0 && th_cq != NaN
? th_cq.toFixed(2)
: "Unable to calculate results";
document.getElementById("ex_cq").innerHTML =
ex_cq >= 0 && ex_cq != NaN
? ex_cq.toFixed(2)
: "Unable to calculate results";
document.getElementById("th_ts").innerHTML =
th_ts >= 0 && th_ts != NaN
? th_ts.toFixed(2)
: "Unable to calculate results";
document.getElementById("ex_ts").innerHTML =
ex_ts >= 0 && ex_ts != NaN
? ex_ts.toFixed(2)
: "Unable to calculate results";
document.getElementById("th_st").innerHTML =
th_st >= 0 && th_st != NaN
? th_st.toFixed(2)
: "Unable to calculate results";
document.getElementById("ex_st").innerHTML =
ex_st >= 0 && ex_st != NaN
? ex_st.toFixed(2)
: "Unable to calculate results";
document.getElementById("ex").innerHTML =
"Experimental (" +
(time.toFixed(2) >= 0
? time.toFixed(2)
: "Time for simulation cannot be generated for given inputs") +
")";
}
}
function stopSimulation() {
clearInterval(chartId);
drawTable();
}
function clearChart() {
document.getElementById("table_div").style.display = "none";
document.getElementById("chart_div").style.width = "100vw";
if (chartId) clearInterval(chartId);
data = google.visualization.arrayToDataTable([
["Time", "Customers"],
[0, 0],
]);
chart.draw(data, options);
cust = 0;
time = 0;
av_cust_q = 0;
av_cust_s = 0;
av_time_s = 0;
cust_total = 0;
av_st = 0;
busy_servers = 0;
blocked = 0;
end_times = [];
start_times = [];
for (let i = 0; i < ns; i++) {
end_times[i] = Number.MAX_VALUE;
start_times[i] = Number.MAX_VALUE;
}
}
</script>
</head>
<body>
<div class="topnav">
<span>Virtual Lab</span>
<a href="index.html">M/M/1</a>
<a href="mminf.html">M/M/inf</a>
<a href="mmc.html">M/M/c</a>
<a href="#" class="active">M/M/1/N</a>
<a href="mmcn.html">M/M/c/N</a>
</div>
<div
style="
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
"
>
<h2>Experiment 3</h2>
</div>
<div
class="form-group"
style="border: 1px solid black; margin: 10px; padding: 15px; width: 400px"
>
<label for="ar">Mean Arrival Rate:</label>
<input class="form-control" type="number" id="ar" />
<br />
<label for="sr">Mean Service Rate:</label>
<input class="form-control" type="number" id="sr" />
<br />
<label for="mc">Maximum customers in System:</label>
<input class="form-control" type="number" id="mc" />
<br />
<label for="slider" class="form-label">Simulation speed: </label>
<input
type="range"
class="form-range"
id="slider"
min="10"
max="2000"
value="1000"
/>
</div>
<div style="margin-top: 50px">
<button
onclick="startSimulation(document.getElementById('ar').value, document.getElementById('sr').value, document.getElementById('mc').value, document.getElementById('slider').value)"
class="btn btn-primary"
>
Start
</button>
<button onclick="stopSimulation()" class="btn btn-primary">Stop</button>
<button onclick="clearChart()" class="btn btn-primary">Clear</button>
</div>
<div style="display: flex; flex-direction: row">
<div id="chart_div"></div>
<div class="table-responsive" id="table_div">
<table
class="table table-striped text-successtable-border border-light"
>
<thead class="border-light">
<tr>
<th scope="col"></th>
<th scope="col"><strong>Theoretical</strong></th>
<th scope="col" id="ex"><strong></strong></th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Avg. No. of Customers in System</th>
<td id="th_cs"></td>
<td id="ex_cs"></td>
</tr>
<tr>
<th scope="row">Avg. No. of Customers in Queue</th>
<td id="th_cq"></td>
<td id="ex_cq"></td>
</tr>
<tr>
<th scope="row">Avg. Time Spent in System</th>
<td id="th_ts"></td>
<td id="ex_ts"></td>
</tr>
<tr>
<th scope="row">Avg. Service Time</th>
<td id="th_st"></td>
<td id="ex_st"></td>
</tr>
</tbody>
</table>
</div>
</div>
<script
type="text/javascript"
src="https://www.gstatic.com/charts/loader.js"
></script>
</body>
</html>