-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConad Simulator.py
More file actions
159 lines (155 loc) · 5.43 KB
/
Conad Simulator.py
File metadata and controls
159 lines (155 loc) · 5.43 KB
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
#!/usr/bin/python
import simpy
from random import randint, gauss, expovariate
from copy import deepcopy
from matplotlib import pyplot as plt
import numpy as np
class ConadSimulator:
def __init__(self, n_registers, duration, stable_at = 0):
self.env = simpy.Environment()
self.n_registers = n_registers
self.duration = duration
self.stable_at = stable_at
self.registers = list()
for i in xrange(n_registers):
self.registers.append(simpy.Resource(self.env, capacity = 1))
self.c_queue = 0
self.c_registers = 0
self.c_out = 0
self.ti = list()
self.ts = list()
self.tw = list()
def start(self):
self.env.process(self.entrance())
self.env.run(until = self.duration)
def customer(self):
arrival = self.env.now
j = randint(0, self.n_registers - 1)
with self.registers[j].request() as c:
yield c
if self.env.now > self.stable_at:
self.tw.append(self.env.now - arrival)
c_ts = -1
while c_ts < 0:
c_ts = gauss(94, 44)
if self.env.now > self.stable_at:
self.c_queue -= 1
self.c_registers += 1
yield self.env.timeout(c_ts)
if self.env.now > self.stable_at:
self.ts.append(c_ts)
self.c_registers -= 1
self.c_out += 1
def entrance(self):
i = 1
while True:
self.env.process(self.customer())
if self.env.now > self.stable_at:
self.c_queue += 1
i += 1
c_ti = expovariate(1.0 / 25)
if self.env.now > self.stable_at:
self.ti.append(c_ti)
yield self.env.timeout(c_ti)
def statistics(self):
print "####################################"
print "### Statistics ###"
print "####################################"
print
total = self.c_queue + self.c_registers + self.c_out
print "Total customers number: ", total
print "Enqueued customers number: ", self.c_queue
print "Customer at the cash registers: ", self.c_registers
print "Completed customers number: ", self.c_out
ti = np.mean(self.ti)
tw = np.mean(self.tw)
ts = np.mean(self.ts)
lambd = 1 / (self.n_registers * ti)
mu = 1 / ts
rho = lambd / mu
print "Mean arrival time: ", ti
print "Mean response time: ", tw + ts
print "Mean service time: ", ts
print "Mean waiting time: ", tw
print "Mean utilization: ", (float(sum(self.ts)) / (self.n_registers * self.duration))
print "Mean throughput: ", (float(self.c_out / sum(self.ts)))
print "Lambda: ", lambd
print "Mu: ", mu
print "Rho: ", rho,
if rho < 1:
print " -> Stable"
else:
print " -> Unstable"
def stabilize(n_registers, duration, p):
results = [list() for i in xrange(p)]
total_mean = 0
instable = True
iteration = 0
cont = 5
total = list()
while instable:
iteration += 1
for i in xrange(p):
sim = ConadSimulator(n_registers, iteration * duration)
sim.start()
results[i] = deepcopy(sim.tw)
old_mean = total_mean
means = [np.mean(results[i]) for i in xrange(p)]
total_mean = np.mean(means)
total.append(total_mean)
if abs(old_mean - total_mean) < 0.02 * total_mean:
cont -= 1
if cont == 0:
instable = False
else:
cont = 5
print "Stabilized at iteration %d" % iteration
print "Mean: %lf" % total_mean
p10 = np.percentile(means, 10)
p90 = np.percentile(means, 90)
print "10° percentile: %lf" % p10
print "90° percentile: %lf" % p90
plt.plot([i for i in xrange(len(total))], total, "rx")
plt.hold(True)
plt.axhline(y = total_mean, color = "black", linestyle = "--")
plt.ylabel("Mean E(Tw)")
plt.xlabel("Iteration")
plt.legend(["Mean of means"], loc = 4)
plt.show()
return [iteration, total_mean, p10, p90]
def repeated_measures(n_registers, duration, values, p):
outside = 0
inside = 0
results= list()
for i in xrange(p):
j = randint(values[0] / 2, values[0])
simulator = ConadSimulator(n_registers, (values[0] + j) * duration, values[0] * duration)
simulator.start()
total_mean = np.mean(simulator.tw)
results.append(total_mean)
if total_mean >= values[2] and total_mean <= values[3]:
inside += 1
else:
outside += 1
print "Experiments between P10 and P90: %d" % inside
print "Experiments out of range: %d" % outside
plt.plot([i for i in xrange(len(results))], results, "rx")
plt.hold(True)
plt.axhline(y = values[1], color = "black", linestyle = "--")
plt.axhline(y = values[2], color = "black", linestyle = "--")
plt.axhline(y = values[3], color = "black", linestyle = "--")
plt.ylabel("Mean E(tw)")
plt.xlabel("Experiments")
plt.legend(["Tw", "Mean", "P10", "P90"])
plt.show()
if outside <= 0.1 * p:
print "Validated!"
return True
else:
print "Not validated..."
return False
#values = stabilize(5, 1000, 100)
#repeated_measures(5, 1000, values, 100)
simulator = ConadSimulator(5, 10000)
simulator.start()
simulator.statistics()