forked from assantos/eon_simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheon_simulador.py
More file actions
316 lines (280 loc) · 9.53 KB
/
eon_simulador.py
File metadata and controls
316 lines (280 loc) · 9.53 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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import simpy
from random import *
from config import *
import numpy as np
import networkx as nx
import math
from itertools import islice
topology = nx.read_weighted_edgelist('topology/' + TOPOLOGY, nodetype=int)
class Desalocate(object):
def __init__(self, env):
self.env = env
def Run(self, count, path, spectro, holding_time):
global topology
yield self.env.timeout(holding_time)
for i in range(0, (len(path)-1)):
for slot in range(spectro[0],spectro[1]+1):
topology[path[i]][path[i+1]]['capacity'][slot] = 0
class Simulador(object):
def __init__(self, env):
self.env = env
global topology
for u, v in list(topology.edges):
topology[u][v]['capacity'] = [0] * SLOTS
self.nodes = list(topology.nodes())
self.random = Random()
self.NumReqBlocked = 0
self.cont_req = 0
self.NumReq_10 = 0
self.NumReq_20 = 0
self.NumReq_40 = 0
self.NumReq_80 = 0
self.NumReq_160 = 0
self.NumReq_200 = 0
self.NumReq_400 = 0
self.NumReq_classe1 = 0
self.NumReq_classe2 = 0
self.NumReq_classe3 = 0
self.NumReqBlocked_10 = 0
self.NumReqBlocked_20 = 0
self.NumReqBlocked_40 = 0
self.NumReqBlocked_80 = 0
self.NumReqBlocked_160 = 0
self.NumReqBlocked_200 = 0
self.NumReqBlocked_400 = 0
self.NumReqBlocked_classe1 = 0
self.NumReqBlocked_classe2 = 0
self.NumReqBlocked_classe3 = 0
self.k_paths = {}
def Run(self, rate):
global topology
for i in list(topology.nodes()):
for j in list(topology.nodes()):
if i!= j:
self.k_paths[i,j] = self.k_shortest_paths(topology, i, j, N_PATH, weight='weight')
for count in range(1, NUM_OF_REQUESTS + 1):
yield self.env.timeout(self.random.expovariate(rate))
class_type = np.random.choice(CLASS_TYPE, p=CLASS_WEIGHT)
src, dst = self.random.sample(self.nodes, 2)
bandwidth = self.random.choice(BANDWIDTH)
holding_time = self.random.expovariate(HOLDING_TIME)
self.conta_requisicao_banda(bandwidth)
self.conta_requisicao_classe(class_type)
paths = self.k_paths[src,dst]
flag = 0
for i in range(N_PATH):
distance = int(self.Distance(paths[i]))
num_slots = int(math.ceil(self.Modulation(distance, bandwidth)))
self.check_path = self.PathIsAble(num_slots,paths[i])
if self.check_path[0] == True:
self.cont_req += 1
self.FirstFit(count, self.check_path[1],self.check_path[2],paths[i])
spectro = [self.check_path[1], self.check_path[2]]
desalocate = Desalocate(self.env)
self.env.process(desalocate.Run(count,paths[i],spectro,holding_time))
flag = 1
break
if flag == 0:
self.NumReqBlocked +=1
self.conta_bloqueio_requisicao_banda(bandwidth)
self.conta_bloqueio_requisicao_classe(class_type)
# Calcula a distância do caminho de acordo com os pesos das arestas
def Distance(self, path):
global topology
soma = 0
for i in range(0, (len(path)-1)):
soma += topology[path[i]][path[i+1]]['weight']
return (soma)
#Calcula os k-menores caminhos entre pares o-d
def k_shortest_paths(self,G, source, target, k, weight='weight'):
return list(islice(nx.shortest_simple_paths(G, source, target, weight=weight), k))
# Calcula o formato de modulação de acordo com a distância do caminho
def Modulation(self, dist, demand):
if dist <= 500:
return (float(demand) / float(4 * SLOT_SIZE))
elif 500 < dist <= 1000:
return (float(demand) / float(3 * SLOT_SIZE))
elif 1000 < dist <= 2000:
return (float(demand) / float(2 * SLOT_SIZE))
else:
return (float(demand) / float(1 * SLOT_SIZE))
#Realiza a alocação de espectro utilizando First-fit
def FirstFit(self,count,i,j,path):
global topology
inicio = i
fim =j
for i in range(0,len(path)-1):
for slot in range(inicio,fim):
#print slot
topology[path[i]][path[i+1]]['capacity'][slot] = count
topology[path[i]][path[i+1]]['capacity'][fim] = 'GB'
# Verifica se o caminho escolhido possui espectro disponível para a demanda requisitada
def PathIsAble(self, nslots,path):
global topology
cont = 0
t = 0
for slot in range (0,len(topology[path[0]][path[1]]['capacity'])):
if topology[path[0]][path[1]]['capacity'][slot] == 0:
k = 0
for ind in range(0,len(path)-1):
if topology[path[ind]][path[ind+1]]['capacity'][slot] == 0:
k += 1
if k == len(path)-1:
cont += 1
if cont == 1:
i = slot
if cont > nslots:
j = slot
return [True,i,j]
if slot == len(topology[path[0]][path[1]]['capacity'])-1:
return [False,0,0]
else:
cont = 0
if slot == len(topology[path[0]][path[1]]['capacity'])-1:
return [False,0,0]
else:
cont = 0
if slot == len(topology[path[0]][path[1]]['capacity'])-1:
return [False,0,0]
# def FirstFit(self, u, v, num_slots, count):
# global topology
# cont_slot = []
# alocado = False
# for slot in range(0, len(topology[u][v]['capacity'])):
# if alocado == True:
# return
# if topology[u][v]['capacity'][slot] == 0:
# cont_slot.append(slot)
# if len(cont_slot) == num_slots + 1:
# for s in cont_slot:
# topology[u][v]['capacity'][s] = str(count)
# if s == cont_slot[-1]:
# topology[u][v]['capacity'][s] = 'GB'
# alocado = True
# break
# else:
# cont_slot = []
# def WorstFit(self, u, v, num_slots, count):
# global topology
# cont_slot = []
# best_slots = []
# slots_escolhidos = []
# cont = 0
# alocado = False
# for slot in range(0, len(topology[u][v]['capacity'])):
# if alocado == True:
# return
# if topology[u][v]['capacity'][slot] == 0:
# cont_slot.append(slot)
# else:
# if len(cont_slot) > num_slots:
# best_slots.append(cont_slot)
# cont_slot = []
# if len(cont_slot) > num_slots:
# best_slots.append(cont_slot)
# for b in best_slots:
# slots_escolhidos.append(len(b))
# slots = max(slots_escolhidos)
# for pos, num in enumerate(slots_escolhidos):
# if num == slots:
# index = pos
# for s in best_slots[index]:
# if cont == num_slots:
# topology[u][v]['capacity'][s] = 'GB'
# alocado = True
# break
# else:
# topology[u][v]['capacity'][s] = str(count)
# cont = cont + 1
# def BestFit(self, u, v, num_slots, count):
# global topology
# cont_slot = []
# best_slots = []
# slots_escolhidos = []
# cont = 0
# alocado = False
# for slot in range(0, len(topology[u][v]['capacity'])):
# if alocado == True:
# return
# if topology[u][v]['capacity'][slot] == 0:
# cont_slot.append(slot)
# else:
# if len(cont_slot) > num_slots:
# best_slots.append(cont_slot)
# cont_slot = []
# if len(cont_slot) > num_slots:
# best_slots.append(cont_slot)
# for l in best_slots:
# slots_escolhidos.append(len(l))
# slots = Simulador.BestSlots(self, slots_escolhidos, num_slots)
# for s in best_slots[slots]:
# if cont == num_slots:
# topology[u][v]['capacity'][s] = 'GB'
# alocado = True
# break
# else:
# topology[u][v]['capacity'][s] = str(count)
# cont = cont + 1
# def BestSlots(self, slots_escolhidos, num_slots):
# result = []
# menor = 0
# if len(slots_escolhidos) == 1 or len(slots_escolhidos) == 0:
# return 0
# else:
# for l in slots_escolhidos:
# result.append(l - num_slots)
# menor = min(result)
# for pos, num in enumerate(result):
# if num == menor:
# index = pos
# return index
# Computa numero de requesições por banda
def conta_requisicao_banda(self, banda):
if banda == 10:
self.NumReq_10 +=1
elif banda == 20:
self.NumReq_20 +=1
elif banda == 40:
self.NumReq_40 +=1
elif banda == 80:
self.NumReq_80 +=1
elif banda == 160:
self.NumReq_160 += 1
elif banda == 200:
self.NumReq_200 += 1
else:
self.NumReq_400 += 1
# Computa numero de bloqueio por banda
def conta_bloqueio_requisicao_banda(self, banda):
if banda == 10:
self.NumReqBlocked_10 +=1
elif banda == 20:
self.NumReqBlocked_20 +=1
elif banda == 40:
self.NumReqBlocked_40 +=1
elif banda == 80:
self.NumReqBlocked_80 +=1
elif banda == 160:
self.NumReqBlocked_160 +=1
elif banda == 200:
self.NumReqBlocked_200 +=1
else:
self.NumReqBlocked_400 +=1
# Computa o número de requisições por classe
def conta_requisicao_classe(self, classe):
if classe == 1:
self.NumReq_classe1 +=1
elif classe == 2:
self.NumReq_classe2 +=1
else:
self.NumReq_classe3 +=1
# Computa número de requisições bloqueadas por classe
def conta_bloqueio_requisicao_classe(self, classe):
if classe == 1:
self.NumReqBlocked_classe1 +=1
elif classe == 2:
self.NumReqBlocked_classe2 +=1
else:
self.NumReqBlocked_classe3 +=1