-
Notifications
You must be signed in to change notification settings - Fork 0
/
Master.py
119 lines (112 loc) · 6.34 KB
/
Master.py
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
from time import time
from Simulator import SimulatorFCFS, SimulatorLCFS
from Statistics import Statistics
# noinspection PyPep8Naming
class Master:
@staticmethod
def run_FCFS(rho, k, seed=None):
"""Runs the simulation for FCFS discipline with given parameters,
returning its statistics"""
results_w = []
results_nq = []
stat_w = Statistics()
stat_nq = Statistics()
simulator = SimulatorFCFS(rho, seed)
simulator.transient_phase()
for i in range(0, 3200):
res = simulator.simulate_FCFS(k)
mean_w = stat_w.calculate_incremental_mean(res[0])
var_w = stat_w.calculate_incremental_variance(res[0])
mean_nq = stat_nq.calculate_incremental_time_mean(res[1], res[2])
var_nq = stat_nq.calculate_incremental_variance(res[1])
center_ew, lower_ew, upper_ew, precision_ew = stat_w.confidence_interval_for_mean(mean_w, var_w,
len(res[0]), 0.95)
center_vw, lower_vw, upper_vw, precision_vw = stat_w.confidence_interval_for_variance(var_w, len(res[0]),
0.95)
center_enq, lower_enq, upper_enq, precision_enq = stat_nq.confidence_interval_for_mean(mean_nq, var_nq,
res[2], 0.95)
center_vnq, lower_vnq, upper_vnq, precision_vnq = stat_nq.confidence_interval_for_variance(var_nq,
res[2], 0.95)
results_w.append(((mean_w, center_ew, lower_ew, upper_ew, precision_ew),
(var_w, center_vw, lower_vw, upper_vw, precision_vw)))
results_nq.append(((mean_nq, center_enq, lower_enq, upper_enq, precision_enq),
(var_nq, center_vnq, lower_vnq, upper_vnq, precision_vnq)))
return results_w, results_nq
@staticmethod
def run_LCFS(rho, k, seed=None):
"""Runs the simulation for LCFS discipline with given parameters,
returning its statistics"""
results_w = []
results_nq = []
stat_w = Statistics()
stat_nq = Statistics()
simulator = SimulatorLCFS(rho, seed)
simulator.transient_phase()
for i in range(0, 3200):
res = simulator.simulate_LCFS(k)
mean_w = stat_w.calculate_incremental_mean(res[0])
var_w = stat_w.calculate_incremental_variance(res[0])
mean_nq = stat_nq.calculate_incremental_time_mean(res[1], res[2])
var_nq = stat_nq.calculate_incremental_variance(res[1])
center_ew, lower_ew, upper_ew, precision_ew = stat_w.confidence_interval_for_mean(mean_w, var_w,
len(res[0]), 0.95)
center_vw, lower_vw, upper_vw, precision_vw = stat_w.confidence_interval_for_variance(var_w, len(res[0]),
0.95)
center_enq, lower_enq, upper_enq, precision_enq = stat_nq.confidence_interval_for_mean(mean_nq, var_nq,
res[2], 0.95)
center_vnq, lower_vnq, upper_vnq, precision_vnq = stat_nq.confidence_interval_for_variance(var_nq,
res[2], 0.95)
results_w.append(((mean_w, center_ew, lower_ew, upper_ew, precision_ew),
(var_w, center_vw, lower_vw, upper_vw, precision_vw)))
results_nq.append(((mean_nq, center_enq, lower_enq, upper_enq, precision_enq),
(var_nq, center_vnq, lower_vnq, upper_vnq, precision_vnq)))
return results_w, results_nq
def webmain(self, discipline, rho, k=1_280, seed=None):
"""Main function for running the simulator, to be called by Flask front-end.
Returns the means and variances with all confidence intervals for both
waiting times and nunber of client at the queue."""
discipline -= 1
start_time = time()
if discipline != 0 and discipline != 1:
print("invalid input")
return
if discipline: # LCFS
if rho == 0.2 or rho == 0.4 or rho == 0.6 or rho == 0.8 or rho == 0.9:
print("Simulating...")
results_w, results_nq = self.run_LCFS(rho, k, seed)
else:
print("invalid input")
return
else: # FCFS
if rho == 0.2 or rho == 0.4 or rho == 0.6 or rho == 0.8 or rho == 0.9:
print("Simulating...")
results_w, results_nq = self.run_FCFS(rho, k, seed)
else:
print("invalid input")
return
end_time = time()
execution_time = end_time - start_time
print(f"Execution time: {execution_time}")
w_means, w_means_icl, w_means_icu, w_vars, w_vars_icl, w_vars_icu = [], [], [], [], [], []
nq_means, nq_means_icl, nq_means_icu, nq_vars, nq_vars_icl, nq_vars_icu = [], [], [], [], [], []
results_w = results_w[::10]
results_nq = results_nq[::10]
for i in range(len(results_w)):
w_means.append(results_w[i][0][0])
w_means_icl.append(results_w[i][0][2])
w_means_icu.append(results_w[i][0][3])
w_vars.append(results_w[i][1][0])
w_vars_icl.append(results_w[i][1][2])
w_vars_icu.append(results_w[i][1][3])
for i in range(len(results_nq)):
nq_means.append(results_nq[i][0][0])
nq_means_icl.append(results_nq[i][0][2])
nq_means_icu.append(results_nq[i][0][3])
nq_vars.append(results_nq[i][1][0])
nq_vars_icl.append(results_nq[i][1][2])
nq_vars_icu.append(results_nq[i][1][3])
return w_means, w_means_icl, w_means_icu, \
w_vars, w_vars_icl, w_vars_icu, \
nq_means, nq_means_icl, nq_means_icu, \
nq_vars, nq_vars_icl, nq_vars_icu, \
execution_time