Coverage for flexnetsim/simulator.py: 19%
248 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-22 20:03 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-22 20:03 +0000
1from typing import DefaultDict
2from .bitrate import Bitrate
3from .network import Network
4from .controller import Controller
5from .event import Event
6import time
7from .random.pyunivariable import pyUniformVariable
8from .random.pyexpvariable import pyExpVariable
11class Simulator():
13 def __init__(self, network_filename: str = None, path_filename: str = None, bitrate_filename: str = None):
15 self.__network_filename = network_filename
16 self.__path_filename = path_filename
17 self.__bitrate_filename = bitrate_filename
19 self.__events = []
20 self.__controller = Controller()
21 self.__current_event = None
22 # vector
23 self.__bit_rates = []
25 self.__initReady = None
26 self.__lambdaS = None
27 self.__mu = None
28 self.__seedArrive = None
29 self.__seedDeparture = None
30 self.__seedSrc = None
31 self.__seedDst = None
32 self.__seedBitRate = None
33 self.__numberOfConnections = None
34 self.__numberOfEvents = None
35 self.__goalConnections = None
36 self.__nextEventTime = None
37 self.__arrival_variable = None
38 self.__departure_variable = None
39 self.__source_variable = None
40 self.__destination_variable = None
41 self.__bitrate_variable = None
42 self.__rtn_allocation = None
44 self.__src = None
45 self.__dst = None
46 # int
47 self.__bitRate = None
49 # vector
50 self.__bitRatesDefault = []
51 self.__allocatedConnections = None
52 self.__columnWidth = None
53 # time
54 self.__clock = None
55 self.__starting_time = None
56 self.__check_time = None
57 self.__time_duration = None
59 self.default_values()
61 if(network_filename == None and path_filename == None and bitrate_filename == None):
62 self.default_bit_rates()
63 self.__allocatedConnections = 0
65 elif(network_filename != None and path_filename != None):
67 self.__controller.network = Network(self.__network_filename)
68 self.__controller.set_paths(self.__path_filename)
70 self.default_bit_rates()
72 self.__allocatedConnections = 0
74 elif(network_filename != None and path_filename != None and bitrate_filename != None):
75 self.__controller.network(Network(self.__network_filename))
76 self.__controller.set_paths(self.__path_filename)
77 self.__bitRatesDefault = Bitrate().read_bit_rate_file(self.__path_filename)
78 self.__allocatedConnections = 0
80 else:
81 print("You do not have been submitted the parameters correctly")
83 def default_values(self):
84 self.__initReady = False
85 self.__lambdaS = 3
86 self.__mu = 10
87 self.__seedArrive = 12345
88 self.__seedDeparture = 12345
89 self.__seedSrc = 12345
90 self.__seedDst = 12345
91 self.__seedBitRate = 12345
92 self.__numberOfConnections = -1
93 self.__numberOfEvents = 0
94 self.__goalConnections = 10000
95 self.__columnWidth = 10
97 @property
98 def lambdaS(self):
99 """
100 Get or set the attribute lambda.
101 """
102 return self.__lambdaS
104 @lambdaS.setter
105 def lambdaS(self, lambdaS):
106 if(self.__initReady):
107 print("You can not set mu parameter AFTER calling init simulator "
108 "method.")
109 return
110 self.__lambdaS = lambdaS
112 @property
113 def mu(self):
114 """
115 Get or set the attribute mu.
116 """
117 return self.__mu
119 @mu.setter
120 def mu(self, mu):
121 if(self.__initReady):
122 print("You can not set mu parameter AFTER calling init simulator "
123 "method.")
124 return
125 self.__mu = mu
127 @property
128 def seedArrive(self):
129 """
130 Get or set the attribute seedArrive.
131 """
132 return self.__seedArrive
134 @seedArrive.setter
135 def seedArrive(self, seedArrive):
136 if(self.__initReady):
137 print("You can not set mu parameter AFTER calling init simulator "
138 "method.")
139 return
140 self.__seedArrive = seedArrive
142 @property
143 def seedDeparture(self):
144 """
145 Get or set the attribute seedDeparture.
146 """
147 return self.__seedDeparture
149 @seedDeparture.setter
150 def seedDeparture(self, seedDeparture):
151 if(self.__initReady):
152 print("You can not set mu parameter AFTER calling init simulator "
153 "method.")
154 return
155 self.__seedDeparture = seedDeparture
157 @property
158 def seedBitRate(self):
159 """
160 Get or set the attribute seedBitRate.
161 """
162 return self.__seedBitRate
164 @seedBitRate.setter
165 def seedBitRate(self, seedBitRate):
166 if(self.__initReady):
167 print("You can not set mu parameter AFTER calling init simulator "
168 "method.")
169 return
170 self.__seedBitRate = seedBitRate
172 @property
173 def goalConnections(self):
174 """
175 Get or set the attribute goalConnections.
176 """
177 return self.__goalConnections
179 @goalConnections.setter
180 def goalConnections(self, goalConnections):
181 if(self.__initReady):
182 print("You can not set mu parameter AFTER calling init simulator "
183 "method.")
184 return
185 self.__goalConnections = goalConnections
187 @property
188 def bitRates(self):
189 """
190 Get or set the attribute bitRates.
191 """
192 return self.__bitRates
194 @bitRates.setter
195 def bitRates(self, bitRates):
196 if(self.__initReady):
197 print("You can not set mu parameter AFTER calling init simulator "
198 "method.")
199 return
200 self.__bitRates = bitRates
202 def default_bit_rates(self):
203 auxB = Bitrate(10.0)
204 auxB.add_modulation("BPSK", 1, 5520)
205 self.__bitRatesDefault.append(auxB)
207 auxB = Bitrate(40.0)
208 auxB.add_modulation("BPSK", 4, 5520)
209 self.__bitRatesDefault.append(auxB)
211 auxB = Bitrate(100.0)
212 auxB.add_modulation("BPSK", 8, 5520)
213 self.__bitRatesDefault.append(auxB)
215 auxB = Bitrate(400.0)
216 auxB.add_modulation("BPSK", 32, 5520)
217 self.__bitRatesDefault.append(auxB)
219 auxB = Bitrate(1000.0)
220 auxB.add_modulation("BPSK", 80, 5520)
221 self.__bitRatesDefault.append(auxB)
223 def print_initial_info(self):
224 print("Nodes:\t %s" % self.__controller.network.node_counter)
226 print("Links:\t %s" % self.__controller.network.link_counter)
228 print("Goal Connections:\t %s" % self.__goalConnections)
230 print("Lambda:\t %s" % self.__lambdaS)
232 print("Mu:\t %s" % self.__mu)
234 # print("Algorithm:\t %s" % self.__controller.__allocator.__name)
236 # Header
237 print("="*97)
238 print("| \t Progress \t", end='')
239 print("| \t Arrives \t", end='')
240 print("| \t Blocking \t", end='')
241 print("| \t Time(s) \t|")
242 print("="*97)
244 self.__starting_time = time.time()
246 def print_row(self, percentage: int):
247 self.__check_time = time.time()
248 self.__time_duration = self.__check_time - self.__starting_time
249 self.__time_duration = round(self.__time_duration, 2)
251 # print("-"*97)
252 text = "| \t"
253 if(percentage < 100):
254 text += str(percentage) + " % \t\t"
255 else:
256 text += str(percentage) + " % \t"
258 text += "| \t"
259 text += str(self.__numberOfConnections-1)
261 if(len(str(self.__numberOfConnections)) <= 6):
262 text += " \t\t"
263 else:
264 text += " \t"
266 blocking = 1 - self.__allocatedConnections / self.__numberOfConnections
268 text += "| \t"
269 text += "{:.1e}".format(blocking)
271 text += " \t"
273 # if(len(str(blocking)) < 6):
274 # text += " \t\t"
275 # else:
276 # text += " \t"
278 text += "| \t"
279 text += str(self.__time_duration)
281 if(len(str(self.__time_duration)) < 6):
282 text += " \t\t"
283 else:
284 text += " \t"
286 text += "|"
288 print(text)
289 if(percentage == 100):
290 print("-"*97)
292 def event_routine(self):
293 self.__current_event = self.__events[0]
294 self.__rtn_allocation = None
295 self.__clock = self.__current_event.time
296 if self.__current_event.type == Event.event.ARRIVE:
297 next_event_time = self.__clock + \
298 self.__arrival_variable.getNextValue()
299 for pos in range(len(self.__events)-1, -1, -1):
300 if self.__events[pos].time < next_event_time:
301 self.__events.insert(pos+1, Event(
302 Event.event.ARRIVE, next_event_time, self.__numberOfConnections))
303 self.__numberOfConnections += 1
304 break
306 self.__src = self.__source_variable.getNextIntValue()
307 self.__dst = self.__destination_variable.getNextIntValue()
308 while self.__dst == self.__src:
309 self.__dst = self.__destination_variable.getNextIntValue()
310 self.__bitRate = self.__bitrate_variable.getNextIntValue()
311 self.__rtn_allocation = self.__controller.assignConnection(
312 int(self.__src), int(self.__dst), self.__bitRates[int(self.__bitRate)], self.__current_event.id_connection)
313 if self.__rtn_allocation == Controller.status.ALLOCATED:
314 next_event_time = self.__clock + \
315 self.__departure_variable.getNextValue()
316 for pos in range(len(self.__events)-1, -1, -1):
317 if self.__events[pos].time < next_event_time:
318 self.__events.insert(pos+1, Event(
319 Event.event.DEPARTURE, next_event_time, self.__current_event.id_connection))
320 break
321 self.__allocatedConnections += 1
322 elif self.__current_event.type == Event.event.DEPARTURE:
323 self.__controller.unassignConnection(
324 self.__current_event.id_connection)
325 self.__events.pop(0)
327 return self.__rtn_allocation
329 def init(self):
330 self.__initReady = True
331 self.__clock = 0
332 self.__bitRates = self.__bitRatesDefault
333 self.__arrival_variable = pyExpVariable(
334 self.__seedArrive, self.__lambdaS)
335 self.__departure_variable = pyExpVariable(
336 self.__seedDeparture, self.__mu)
337 self.__source_variable = pyUniformVariable(
338 self.__seedSrc, self.__controller.network.node_counter-1)
339 self.__destination_variable = pyUniformVariable(
340 self.__seedDst, self.__controller.network.node_counter-1)
341 self.__bitrate_variable = pyUniformVariable(
342 self.__seedBitRate, len(self.__bitRates)-1)
343 self.__events.append(Event(
344 Event.event.ARRIVE, self.__arrival_variable.getNextValue(), self.__numberOfConnections))
345 self.__numberOfConnections += 1
346 return
348 def run(self):
349 timesToShow = 20
350 arrivesByCycle = self.goalConnections / timesToShow
351 self.print_initial_info()
352 i = 1
353 while(i <= timesToShow):
354 while(self.__numberOfConnections <= i*arrivesByCycle):
355 self.event_routine()
356 self.print_row((100/timesToShow) * i)
357 i += 1
359 def time_duration(self):
360 return self.__time_duration
362 def get_Blocking_Probability(self):
363 blocking = round(1 - self.__allocatedConnections /
364 self.__numberOfConnections, 2)
365 return blocking
367 def set_allocation_algorithm(self, alloc_alg):
368 self.__controller.allocator = alloc_alg