-
Notifications
You must be signed in to change notification settings - Fork 0
/
randompolicy.py
97 lines (85 loc) · 3.88 KB
/
randompolicy.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
"""
#################################
# Random Policy functions
#################################
"""
#########################################################
# import libraries
import time
from random import seed
from random import randint
from datetime import datetime
from config import Config_Flags
import matplotlib.pyplot as plt
from config import Config_Power
from location import reset_axes
from location import update_axes
from config import Config_General
from config import Config_requirement
from config import Config_BehavioralCloning
#########################################################
# General Parameters
num_cells = Config_General.get('NUM_CELLS')
tx_powers = Config_Power.get('UAV_Tr_power')
dist_limit = Config_requirement.get('dist_limit')
NUM_EPISODES = Config_BehavioralCloning.get('NUM_TRAJECTORIES_EXPERT')
cell_source = 0
cell_destination = num_cells - 1
#########################################################
# Function definition
def random_action(uav, ues_objects, ax_objects, cell_objects):
print(" ****** Mode: Random policy by the drone ")
Config_Flags['SingleArrow'] = False
seed(1732)
prev_cell = 1
episode = 0
arrow_patch_list = []
timer_start = time.perf_counter()
print("......... TOTAL EPOCHS = ", NUM_EPISODES)
while episode < NUM_EPISODES:
distance = 0
done = False
arrow_patch_list = reset_axes(ax_objects=ax_objects, cell_source=cell_source,
cell_destination=cell_destination, arrow_patch_list=arrow_patch_list)
uav.uav_reset(cell_objects)
while distance < dist_limit and not done:
# Set the transmission power for the throughput (Done!)
# Calculate the interference from other neighbor UEs on the UAV's base station (Done!)
# Calculate the UAV's interference effect on neighbor UEs because of the transmission power
# allocation (Done!)
cell = uav.get_cell_id()
avail_neighbors = cell_objects[cell].get_neighbor()
avail_actions = cell_objects[cell].get_actions()
idx_rand = randint(0, len(avail_actions)-1)
action_rand = avail_actions[idx_rand]
neighbor_rand = avail_neighbors[idx_rand]
uav.set_action_movement(action=action_rand)
uav.set_cell_id(cid=neighbor_rand)
uav.set_location(loc=cell_objects[neighbor_rand].get_location())
tx_index = randint(0, len(tx_powers)-1)
tx_power = tx_powers[tx_index]
uav.set_power(tr_power=tx_power)
update_axes(ax_objects, prev_cell, cell_source, cell_destination, neighbor_rand, tx_power,
cell_objects[neighbor_rand].get_location(), action_rand, cell_objects[cell].get_location(),
arrow_patch_list)
# interference = uav.calc_interference(cell_objects, ues_objects)
# sinr, snr = uav.calc_sinr(cell_objects)
# throughput = uav.calc_throughput()
# interference_ues = uav.calc_interference_ues(cell_objects, ues_objects)
# Should remove these above lines
uav.uav_perform_task(cell_objects, ues_objects)
if Config_Flags.get('Display_map'):
plt.pause(0.1)
prev_cell = neighbor_rand
if neighbor_rand == cell_destination:
done = True
distance += 1
episode += 1
if episode % 200 == 0:
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
timer_end = time.perf_counter()
print(" ......... EPISODE = ", episode, "......... Current Time = ", current_time,
" ..... ELAPSED TIME = ", round(timer_end - timer_start, 2), " Seconds, ",
round((timer_end - timer_start) / 60, 2), " mins, ",
round((timer_end - timer_start) / 3600, 2), " hour")