-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
443 lines (353 loc) · 14.5 KB
/
utils.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
"""
#################################
# Utility functions
#################################
"""
#########################################################
# import libraries
import numpy as np
from config import Config_Power
from config import Config_General
from config import Config_interference
from config import config_movement_step
from config import movement_actions_list
from scipy.spatial.distance import cdist
from config import Number_of_neighbor_UEs
from scipy.spatial.distance import euclidean
#########################################################
# General Parameters
radius = Config_General.get('Radius')
num_ues = Config_General.get("NUM_UEs")
num_cells = Config_General.get("NUM_CELLS")
tx_powers = Config_Power.get('UAV_Tr_power')
ue_tr_power = Config_Power.get("UE_Tr_power")
bandwidth = Config_interference.get('Bandwidth')
float_acc = Config_General.get('FLOAT_ACCURACY')
antenna_gain = Config_interference.get('AntennaGain')
#########################################################
# Class and Function definitions
# ******************************************************
# ******************* CELL CLASS ***********************
# ******************************************************
class Cell:
def __init__(self, x_loc=None, y_loc=None, num_ues_cell=-1, unique_id=-1):
self.x_loc = x_loc
self.y_loc = y_loc
self.z_loc = 0.0
self.num_ues_cell = num_ues_cell
self.num_neighbor_ues = 0
self.cell_id = unique_id
self.location = [self.x_loc, self.y_loc, self.z_loc]
self.ues_idx = None
self.coordinate = None
self.neighbors = None
self.available_actions = None
self.dist_destination = float('inf')
def print_info(self):
print("Cell ID = ", self.cell_id, "\n",
"Location = ", self.location, "\n",
"Num UEs = ", self.num_ues_cell, "\n",
"UEs index = ", self.ues_idx, "\n",
"Neighbors = ", self.neighbors, "\n",
"Actions = ", self.available_actions, "\n")
def set_location(self, loc):
self.x_loc = loc[0]
self.y_loc = loc[1]
self.location = [self.x_loc, self.y_loc, self.z_loc]
def set_num_ues(self, num_ues_cell):
self.num_ues_cell = num_ues_cell
def set_num_neighbor_ues(self, num_neighbor_ues):
self.num_neighbor_ues = num_neighbor_ues
def set_id(self, uid):
self.cell_id = uid
def set_ues_ids(self, ues_idx):
self.ues_idx = ues_idx
def set_coord(self, coord):
self.coordinate = coord
def set_neighbors(self, neighbors):
self.neighbors = neighbors
def set_available_actions(self, actions):
self.available_actions = actions
def set_distance(self, distance):
self.dist_destination = distance
def get_location(self):
return self.location
def get_num_ues(self):
return self.num_ues_cell
def get_num_neighbor_ues(self):
return self.num_neighbor_ues
def get_id(self):
return self.cell_id
def get_ues_idx(self):
return self.ues_idx
def get_coord(self):
return self.coordinate
def get_neighbor(self):
return self.neighbors
def get_actions(self):
return self.available_actions
def get_distance(self):
return self.dist_destination
# ******************************************************
# ******************* UAV CLASS ***********************
# ******************************************************
class UAV:
def __init__(self, x_loc=None, y_loc=None, z_loc=None, cell_id=-1, tr_power=0):
self.x_loc = x_loc
self.y_loc = y_loc
self.z_loc = z_loc
self.cell_id = cell_id
self.power = tr_power
self.location = [self.x_loc, self.y_loc, self.z_loc]
self.action_movement = 0
self.interference = 0
self.snr = 0
self.sinr = 0
self.throughput = 0
self.throughput_snr = 0
self.interference_over_ues = 0
self.hop = 0
def set_location(self, loc):
self.x_loc = loc[0]
self.y_loc = loc[1]
self.location = [self.x_loc, self.y_loc, self.z_loc]
def set_cell_id(self, cid):
self.cell_id = cid
def set_power(self, tr_power):
self.power = tr_power
def set_action_movement(self, action):
self.action_movement = action
def set_hop(self, hop):
self.hop = hop
def get_location(self):
return self.location
def get_cell_id(self):
return self.cell_id
def get_tr_power(self):
return self.power
def get_action_movement(self):
return self.action_movement
def get_hop(self):
return self.hop
def send_pkt(self):
pass
def calc_interference(self, cells_objects, ues_objects):
current_cell = self.get_cell_id()
neighbors = cells_objects[current_cell].get_neighbor()
interference = 0
for neighbor in neighbors:
ues = cells_objects[neighbor].get_ues_idx()
for ue in ues:
csi = get_csi(ues_objects[ue].get_location(), cells_objects[current_cell].get_location())
interference += (ues_objects[ue].get_power()) * ((abs(csi))**2)
# print(interference)
self.interference = interference
return self.interference
def calc_sinr(self, cell_objects):
cell = self.get_cell_id()
csi = get_csi(self.location, cell_objects[cell].get_location())
csi_abs = (abs(csi))**2
sinr = (self.get_tr_power()) * csi_abs / (1 + self.interference)
snr = (self.get_tr_power()) * csi_abs
self.sinr = sinr
self.snr = snr
return self.sinr, self.snr
def calc_throughput(self):
throughput = np.log2(1 + self.sinr)
throughput_snr = np.log2(1 + self.snr)
self.throughput = throughput
self.throughput_snr = throughput_snr
return self.throughput_snr
# return self.throughput
def calc_max_throughput(self, cell_objects):
cell = self.get_cell_id()
csi = get_csi(self.location, cell_objects[cell].get_location())
csi_abs = (abs(csi))**2
max_power = max(tx_powers)
snr = max_power * csi_abs
throughput_max = np.log2(1 + snr)
return throughput_max
def calc_interference_ues(self, cells_objects, ues_objects):
current_cell = self.get_cell_id()
neighbors = cells_objects[current_cell].get_neighbor()
interference = 0
for neighbor in neighbors:
ues = cells_objects[neighbor].get_ues_idx()
for ue in ues:
csi = get_csi(self.location, ues_objects[ue].get_location())
interference_ue = self.power * ((abs(csi))**2)
# print(interference_ue)
ues_objects[ue].set_interference(interference_ue)
interference += interference_ue
self.interference_over_ues = interference
return self.interference_over_ues
def get_interference(self):
return self.interference
def get_sinr(self):
return self.sinr, self.snr
def get_throughput(self):
return self.throughput
def uav_perform_task(self, cell_objects, ues_objects):
interference = self.calc_interference(cell_objects, ues_objects)
sinr, snr = self.calc_sinr(cell_objects)
throughput = self.calc_throughput()
interference_ues = self.calc_interference_ues(cell_objects, ues_objects)
max_throughput = self.calc_max_throughput(cell_objects=cell_objects)
return interference, sinr, throughput, interference_ues, max_throughput
def uav_reset(self, cell_objects):
self.set_cell_id(cid=0)
self.set_location(loc=cell_objects[0].get_location())
self.set_hop(hop=0)
self.set_power(tr_power=0)
# ******************************************************
# ******************* UE CLASS ***********************
# ******************************************************
class UE:
def __init__(self, x_loc=None, y_loc=None, ue_id=-1, cell_id=-1, tr_power=0):
self.x_loc = x_loc
self.y_loc = y_loc
self.z_loc = 0.0
self.ue_id = ue_id
self.cell_id = cell_id
self.power = tr_power
self.location = [self.x_loc, self.y_loc, self.z_loc]
self.interference = 0
def set_location(self, loc):
self.x_loc = loc[0]
self.y_loc = loc[1]
self.location = [self.x_loc, self.y_loc, self.z_loc]
def set_ue_id(self, ue_id):
self.ue_id = ue_id
def set_cell_id(self, cell_id):
self.cell_id = cell_id
def set_power(self, tr_power):
self.power = tr_power
def set_interference(self, interference):
self.interference = interference
def get_location(self):
return self.location
def get_ue_id(self):
return self.ue_id
def get_cell_id(self):
return self.cell_id
def get_power(self):
return self.power
def get_interference(self):
return self.interference
def find_closest_cell(h_coord_cells, v_coord_cells, x_coord_ues, y_coord_ues):
ue_cell_ids = np.zeros([num_ues], dtype=np.int16) - 1
cell_coord_pairs = np.concatenate((h_coord_cells.reshape(-1, 1), v_coord_cells.reshape(-1, 1)), axis=1)
for index in range(0, num_ues):
dist = cdist(np.array([[x_coord_ues[index], y_coord_ues[index]]]), cell_coord_pairs, 'euclidean')
min_index = np.argmin(dist)
ue_cell_ids[index] = min_index
return ue_cell_ids
def create_ues(x_coord_ues, y_coord_ues, ue_cell_ids):
ues_objects = np.empty(num_ues, dtype=object)
for ue in range(0, num_ues):
ues_objects[ue] = UE(x_loc=x_coord_ues[ue], y_loc=y_coord_ues[ue])
ues_objects[ue].set_ue_id(ue)
ues_objects[ue].set_cell_id(ue_cell_ids[ue])
ues_objects[ue].set_power(ue_tr_power)
return ues_objects
def create_cells(h_coord_cells, v_coord_cells, cell_ids, ue_cell_ids, coordinates):
cells_objects = np.empty(num_cells, dtype=object)
counts = np.zeros(num_cells, dtype=np.int16)
_, counts[0+1:num_cells-1] = np.unique(ue_cell_ids, return_counts=True)
for cell in range(0, num_cells):
cells_objects[cell] = Cell(h_coord_cells[cell], v_coord_cells[cell])
cells_objects[cell].set_id(cell_ids[cell])
cells_objects[cell].set_num_ues(counts[cell])
cells_objects[cell].set_ues_ids(np.where(ue_cell_ids == cell)[0])
cells_objects[cell].set_coord(coordinates[cell])
for cell in range(0, num_cells):
available_neighbor, available_action = find_neighbors(cells_objects[cell], cells_objects)
cells_objects[cell].set_neighbors(available_neighbor)
cells_objects[cell].set_available_actions(available_action)
for cell in range(0, num_cells):
num_neighbor_ues = 0
for neighbor in cells_objects[cell].get_neighbor():
num_neighbor_ues += cells_objects[neighbor].get_num_ues()
cells_objects[cell].set_num_neighbor_ues(num_neighbor_ues)
list_neighbor_ues_ordered = []
for cell in range(0, num_cells):
list_neighbor_ues_ordered.append(cells_objects[cell].get_num_neighbor_ues())
# print("Sorted Summation Number of Neighbor UEs: ", sorted(list_neighbor_ues_ordered))
Number_of_neighbor_UEs['Min'], Number_of_neighbor_UEs['Max'] = min(list_neighbor_ues_ordered),\
max(list_neighbor_ues_ordered)
cell = num_cells - 1
dest_cell = cell_ids[-1]
source_cell = cell_ids[0]
while cell >= 0:
if cells_objects[cell].get_id() == dest_cell:
cells_objects[cell].set_distance(0)
else:
neighbors = cells_objects[cell].get_neighbor()
distances = []
for neighbor in neighbors:
distances.append(cells_objects[neighbor].get_distance())
cells_objects[cell].set_distance(int(min(distances)) + 1)
# print("cell = ", cell, " distance = ", cells_objects[cell].get_distance())
cell -= 1
return cells_objects
def check_neighbor_availability(location, cells_objects):
for cell in range(0, len(cells_objects)):
if round(cells_objects[cell].get_location()[0], float_acc) == round(location[0], float_acc) and \
round(cells_objects[cell].get_location()[1], float_acc) == round(location[1], float_acc):
return True, cells_objects[cell].get_id()
return False, None
def find_neighbors(cell_object, cell_objects):
available_action = []
available_neighbor = []
x_cell = cell_object.get_location()[0]
y_cell = cell_object.get_location()[1]
for action in movement_actions_list:
x_change, y_change = action_to_location(action)
new_x = x_cell + x_change
new_y = y_cell + y_change
check_flag, neighbor = check_neighbor_availability([new_x, new_y], cell_objects)
if check_flag:
available_action.append(action)
available_neighbor.append(neighbor)
return available_neighbor, available_action
def action_to_location(action):
x_change, y_change = None, None
x_step, y_step = config_movement_step.get('x_step'), config_movement_step.get('y_step')
if action == 1:
x_change = 0
y_change = y_step
elif action == 2:
x_change = x_step
y_change = (1./2.) * y_step
elif action == 3:
x_change = x_step
y_change = (-1./2.) * y_step
elif action == 4:
x_change = 0
y_change = -y_step
elif action == 5:
x_change = -x_step
y_change = (-1./2.) * y_step
elif action == 6:
x_change = -x_step
y_change = (1./2.) * y_step
else:
exit('Error: Not a defined action for the movement')
return x_change, y_change
def get_csi(loc_source, loc_destination):
distance = euclidean(loc_source, loc_destination)
csi = antenna_gain * (1/distance**2) * (1 + 1j)
return csi
def power_to_radius(power):
min_power = min(tx_powers)
max_power = max(tx_powers)
diff = power - min_power
radius_circle = (diff/(max_power-min_power)) * radius * np.sqrt(3) + 0.5 * radius * np.sqrt(3)
return radius_circle
def multi_actions_to_action(action_movement, action_tx):
return np.where(action_tx == np.array(tx_powers))[0] * len(movement_actions_list) + \
np.where(action_movement == np.array(movement_actions_list))[0]
def action_to_multi_actions(action):
action_movement = int(action % len(movement_actions_list))
action_tx = int(action / len(movement_actions_list))
return action_movement, action_tx