-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab1.py
346 lines (306 loc) · 10.6 KB
/
Lab1.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
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
import random
import math
import sys
from queue_o import SimulationQueue
from queue_k import SimulationQueue_M_M_1_K
from packet import Packet
from simulation_config import Config
import matplotlib.pyplot as plt
def question1():
lambda1 = 75
random_numbers = []
# Generate 1000 random numbers
for i in range(1000):
x = - (1 / lambda1) * math.log(1 - random.uniform(0, 1))
random_numbers.append(x)
# Mean
mean = sum(random_numbers) / len(random_numbers)
# Variance
variance = sum([(x - mean) ** 2 for x in random_numbers]) / len(random_numbers)
# Print results
print("--------------------------------------------------")
print("-------------------QUESTION 1---------------------")
print("--------------------------------------------------")
print("Expected mean", 1/lambda1)
print("Mean:", mean)
print("--------------------------------------------------")
print("Expected variance", 1/lambda1**2)
print("Variance:", variance)
def question3():
# Print the header
print("--------------------------------------------------")
print("-------------------QUESTION 3---------------------")
print("--------------------------------------------------")
# Initialize variables
x_axis = []
y_axis_E_N = []
y_axis_Idle = []
E_N_list = []
Idle_list = []
y_axis_E_N.append(E_N_list)
y_axis_Idle.append(Idle_list)
List_K = ["K is infinite", "green"]
# Start a loop for every value of rho
for i in range(35, 95, 10):
i /= 100
print("p = ", i)
x_axis.append(i)
#Run the simulation
Idle_counter, E_N, N_a, N_d, N_o = simulator_M_M_1(i)
#Updarte the lists for the graph
E_N_list.append(E_N)
Idle_list.append(Idle_counter)
#Print the results
print("Idle: ", Idle_counter)
print("E[N]: ", E_N)
print("N_a: ", N_a)
print("N_d: ", N_d)
print("N_o: ", N_o)
print("--------------------------------------------------")
y_axis_E_N.append(E_N_list)
y_axis_Idle.append(Idle_list)
#Plot the graph
graph(x_axis, y_axis_E_N, 'Average number in the system E[N]', 'M_M_1 simulation', [List_K], "E_N_M_M_1.png")
graph(x_axis, y_axis_Idle, 'Idle queue probability', 'M_M_1 simulation', [List_K], "P_idle_M_M_1.png")
def question4():
# Print the header
print("--------------------------------------------------")
print("-------------------QUESTION 4---------------------")
print("--------------------------------------------------")
# Run the simulation for rho = 1.2
Idle_counter, E_N, N_a, N_d, N_o = simulator_M_M_1(1.2)
# Print the results
print("Idle: ", Idle_counter)
print("E[N]: ", E_N)
print("N_a: ", N_a)
print("N_d: ", N_d)
print("N_o: ", N_o)
def question6():
# Print the header
print("--------------------------------------------------")
print("-------------------QUESTION 6---------------------")
print("--------------------------------------------------")
# Initialize variables
x_axis = []
y_axis_E_N = []
y_axis_P_loss = []
K = [10, 25, 50]
colors = ["red", "green", "blue", "black"]
K_colors = []
#Start a loop for every value of K
for k in K:
print("-------------------- K =", k,"----------------------")
text = "K = " + str(k)
K_colors.append([text, colors.pop(0)])
# Initialize lists to store the results of the loop
E_N_list = []
P_loss_list = []
for i in range(60, 150, 10):
i /= 100
if k == K[0]:
x_axis.append(i)
# Run the simulation
E_N, P, N_a, N_d, N_o, Idle_counter = simulator_M_M_1_K(i, k)
# Print the results
print("Idle: ", Idle_counter)
print("E[N]: ", E_N)
print("P_loss: ", P)
print("N_a: ", N_a)
print("N_d: ", N_d)
print("N_o: ", N_o)
# Update the lists for the graph
E_N_list.append(E_N)
P_loss_list.append(P)
print("--------------------------------------------------")
# Update the lists for the graph for every value of K
y_axis_E_N.append(E_N_list)
y_axis_P_loss.append(P_loss_list)
# Plot the graph
graph(x_axis, y_axis_E_N, 'Average number in the system E[N]', 'M_M_1_K simulation', K_colors, "E_N_M_M1_K.png")
graph(x_axis, y_axis_P_loss, 'Probability of losing a packet', 'M_M_1_K simulation', K_colors, "P_loss_M_M_1_K.png")
def simulator_M_M_1_K(p, k):
# Initialize variables
Length = Config.Length
C = Config.C
lambda1 = p * C / Length
T = Config.T
DES = []
queue = SimulationQueue_M_M_1_K(k)
# Generate observers
observer_generator(lambda1, T, DES)
# Generate arrivals
generate_packets(lambda1, T, DES)
#Process events
return process_events_M_M_1_K(Length, C, DES, queue)
def generate_packets(lambda1, T, DES):
arrival_time = 0
# Generate arrivals until T is reached
while arrival_time <= T:
time = - (1 / lambda1) * math.log(1 - random.uniform(0, 1))
arrival_time += time
if arrival_time > T:
break
# Add the arrival to the DES
DES.append(Packet("A", arrival_time))
def process_events_M_M_1_K(Length, C, DES, queue):
# Initialize variables
N_a = 0
N_d = 0
N_o = 0
E_N = 0
Idle_counter = 0
last_departure = 0
departures = []
DES = sorted(DES, key=lambda packet: packet._arrival_time)
index = 0
while index != len(DES):
# Get the next event
if departures:
# If the next event is an arrival and the next departure is before the arrival, process the departure
if DES[index]._arrival_time > departures[0]._arrival_time:
element = departures.pop(0)
index -= 1
else:
element = DES[index]
else:
element = DES[index]
# Process an arrival
if element.event_type == "A":
N_a += 1
# If the queue is not full, enqueue the packet
if (queue.is_full() == False):
queue.enqueue(element)
# Generate a departure for the packet
length = - (Length) * math.log(1 - random.uniform(0, 1))
service_time = length / C
packet = Packet("A", element._arrival_time, length, service_time)
packet.departure(last_departure)
last_departure = packet._departure
# Add the departure to the DES
departures.append(Packet("D", packet._departure))
# Process a departure
elif element.event_type == "D":
N_d += 1
queue.dequeue()
# Process an observer
elif element.event_type == "O":
N_o += 1
# If the queue is empty, increment the idle counter
if queue.size() == 0:
Idle_counter += 1
# Else, update the average number of packets in the system
else:
E_N += queue.size()
index += 1
# Process the remaining departures
while departures:
departures.pop(0)
N_d += 1
queue.dequeue()
# Calculate the results
Idle_counter /= N_o
E_N /= N_o
P_loss = 1 - (N_d / N_a)
return E_N, P_loss, N_a, N_d, N_o, Idle_counter
def simulator_M_M_1(p):
# Initialize variables
Length = Config.Length
C = Config.C
lambda1 = p * C / Length
T = Config.T
# Generate events
DES = events_generator_M_M_1(Length, C, lambda1, T)
#Process the DES
return simulation(DES)
def simulation(DES):
# Initialize variables
N_a = 0
N_d = 0
N_o = 0
E_N = 0
Idle_counter = 0
queue = SimulationQueue()
# Process events
for element in DES:
# Process an arrival
if element.event_type == "A":
N_a += 1
queue.enqueue(element)
# Process a departure
elif element.event_type == "D":
N_d += 1
queue.dequeue()
# Process an observer
elif element.event_type == "O":
N_o += 1
# If the queue is empty, increment the idle counter
if queue.size() == 0:
Idle_counter += 1
# Else, update the average number of packets in the system
else:
E_N += queue.size()
# Calculate the results
Idle_counter /= N_o
E_N /= N_o
return Idle_counter, E_N, N_a, N_d, N_o
def events_generator_M_M_1(Length, C, lambda1, T):
# Initialize variables
packets = []
DES = []
arrival_time = 0
last_departure = 0
# Initialize arrivals and departures
while arrival_time <= T:
# Generate arrivals until T is reached
time = - (1 / lambda1) * math.log(1 - random.uniform(0, 1))
arrival_time += time
if arrival_time > T:
break
DES.append(Packet("A", arrival_time))
# Generate departures
lenght = - (Length) * math.log(1 - random.uniform(0, 1))
service_time = lenght / C
packet = Packet("A", arrival_time, lenght, service_time)
packet.departure(last_departure)
last_departure = packet._departure
# Add the departure to the DES
DES.append(Packet("D", packet._departure))
#Store the packet
packets.append(packet)
# Initialize observers
observer_generator(lambda1, T, DES)
# Sort events by arrival time
DES = sorted(DES, key=lambda packet: packet._arrival_time)
return DES
def observer_generator(lambda1, T, DES):
observer_time = 0
# Generate observers until T is reached
while observer_time <= T:
x = - (1 / (5 * lambda1)) * math.log(1 - random.uniform(0, 1))
observer_time += x
if observer_time <= T:
DES.append(Packet("O", observer_time))
def graph(x_values, y_values, y_text, title, K_colors, output_file):
# Plot the graph
for element, k_color in zip(y_values, K_colors):
plt.plot(x_values, element, marker='o', linestyle='-', color=k_color[1], label=k_color[0])
# Set labels and title
plt.xlabel('Traffic intensity p')
plt.ylabel(y_text)
plt.title(title)
plt.legend()
# save the graph in the output file
plt.savefig(output_file)
plt.close()
#Code necessary to run the program
functions = {
'question1': question1,
'question3': question3,
'question4': question4,
'question6': question6
}
if __name__ == '__main__':
print(sys.argv)
func = functions[sys.argv[1]]
args = sys.argv[6:]
sys.exit(func(*args))