-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_MRO_SanityCheck_v2.py
383 lines (333 loc) · 16.5 KB
/
test_MRO_SanityCheck_v2.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
"""Script by QL: working in process
Patent thread of socket client and 2 child threads:
1. listen: listen message in the background and put all it in the received message queue "receive_q"
2. run_agent: write message to config or kpi report, get the next config with predefined fixed values, and send back to
season
Experiment: randomly choose
"""
import socket
import struct
import threading
import queue
import time
import numpy as np
import pandas as pd
import json
import copy
import pickle
from time import time
import matplotlib.pyplot as plt
class Configuration:
# test: C = Configuration(TC.agent.current_cfg, '*.RRH_cells.*.transceivers.*.HO_triggers'.split('.'))
def __init__(self, dict_cfg, cfg_path):
self.cfg_paths = []
self.cfg_values = []
self.ho_offsets = []
self.ho_ttt = []
self.df_cfg_ho = pd.DataFrame(columns=['unique_name', 'offset', 'TTT'])
self.Groups = None
self.cnt = 0
self.get_path_values(dict_cfg['RRH'], cfg_path)
self.get_cfg_df()
# keep initial group definition for incremental changes
if self.Groups is None:
self.Groups = copy.deepcopy(dict_cfg['Groups'])
'''define traffic mask of 24 hours'''
self.group24 = [0.8, 0.65, 0.4, 0.15, 0.2, 0.3, 0.6, 0.8, 0.9, 0.8, 1.2, 1.3,
1.5, 1.8, 2.0, 1.7, 1.8, 1.5, 1.4, 1.5, 1.1, 1.0, 0.9, 0.8]
def get_path_values(self, tree, path, indx=0):
"""walks down a json tree and stores all possible paths and values of a Season configuration
:param tree: dictionary containing json tree
:param path: path specifier
:param indx: index level
"""
if indx < len(path):
# not the final path end reached continue walk down
if isinstance(path[indx], str):
# print(path[indx])
if path[indx] == '*':
# wildcard found iterate through set of options
for k in tree.keys():
# print(k)
new_path = path.copy()
new_path[indx] = k
# recursive walk down on next level of tree
self.get_path_values(tree[k], new_path, indx + 1)
else:
# recursive walk down on next level of tree
self.get_path_values(tree[path[indx]], path, indx + 1)
else:
# final end of path reached keep full path string and value in separate arrays
self.cfg_paths.append(path)
# self.values.append(int(tree))
'''note that 'HO_triggers' is a list of dictionaries, e.g., [{'event_type': 'A3', 'offset': 3, 'TTT': 1}]'''
self.cfg_values.append(tree)
self.ho_offsets.append(tree[0]['offset'])
self.ho_ttt.append(tree[0]['TTT'])
def get_cfg_df(self):
ls_site_uniquename = [n[0] + '[' + n[2] + '][' + n[4] + ']' for n in self.cfg_paths]
self.df_cfg_ho['unique_name'] = ls_site_uniquename
self.df_cfg_ho['offset'] = self.ho_offsets
self.df_cfg_ho['TTT'] = self.ho_ttt
def get_cfg(self, values=None, timestamp=None):
""" get new Season configuration for given set of values
:param values: values: overwrites internal configuration values
:param timestamp: 24 hour seasonality to be used for group size modification
:return: new Season configuration
"""
myvalues = self.cfg_values if values is None else values
cfg = {}
for i in range(len(self.cfg_paths)):
self.get_path_config(cfg, self.cfg_paths[i], myvalues[i])
cfg = {'RRH': cfg}
# change group sizes acc. to group24 parameter - adds 24h seasonality
if len(self.group24) == 24 and timestamp is not None:
new_groups = copy.deepcopy(self.Groups)
# take time tick 0 as 0:00 a.m.
indx = int((timestamp // 3600) % 24)
print('[INFO]: change traffic mask config, hour of the day', indx)
for group in new_groups.values():
# modify all groups acc. group24 parameter
group['parameters']['group_size'] = int(group['parameters']['group_size'] * self.group24[indx])
cfg['Groups'] = new_groups
return cfg
def get_path_config(self, config, path, value):
"""set value of given configuration for path path and value
:param config: Season configuration
:param path: actual path
:param value: actual value
:return: None
"""
if len(path) == 1:
# final end of path reached, set value
config[path[0]] = value
else:
# intermediate stage reached
if path[0] not in config.keys():
# generate empty dicts for all possible keys
config[path[0]] = {}
# walk down next level on path
self.get_path_config(config[path[0]], path[1:], value)
class RlAgent:
def __init__(self):
self.configs = [] # list of ho parameters for all trx's along time
self.reports = []
self.phases = []
self.current_cfg = None # current cfg as a class
self.timestamp = 0
self.realtime = 0
self.ls_realtime = []
self.ls_simtime = []
self.offset_range = [-10, 10] # HO offset specifies the RSRP offset value in dB
self.TTT_range = [0, 3] # time to trigger in seconds
self.cfg_timestamp = []
self.save_path = 'sim_results/cfg_kpi_results.pickle'
self.config_max = 50 # maximum number of the configuration changes
self.num_report_per_config = 10 # number of report to list per config
'''
# interested config parameters
self.ls_cfg_path_in_json = [
'*.RRH_cells.*.transceivers.*.' + x for x in
['slices',
'full_bandwidth_tx_power_dBm',
'antenna_parameters.electrical_downtilt_degrees',
'HO_triggers']
]
self.ls_action_path_in_json = ['eMBB_slice.serving_weight',
'URLLC_slice.serving_weight',
'IoT_slice.serving_weight']
'''
self.ls_config_path = '*.RRH_cells.*.transceivers.*.HO_triggers'
# note that 'HO_triggers' is a list of HO parameters, e.g., [{'event_type': 'A3', 'offset': 3, 'TTT': 1}]
def add_config(self, dict_cfg):
""" add new configurtaion to the list
:param dict_cfg: dictionary of actual season configuration
:return:
"""
"""get interested config parameters per site under
dict_cfg['config']['RRH']['site*']['RRH_cells'][*]['transceivers'][*]:
1. ['slices']
2. ['full_bandwidth_tx_power_dBm']
3. ['antenna_parameters']['electrical_downtilt_degrees']
4. ['HO_triggers']: a list with each element as a dictionary with keys 'event_type', 'offset', 'TTT'
----
Interested network slicing parameters: general for all sites:
- dict_cfg['config']['NetworkSlices']['eMBB_slice']['serving_weight']
- dict_cfg['config']['NetworkSlices']['URLLC_slice']['serving_weight']
- dict_cfg['config']['NetworkSlices']['IoT_slice']['serving_weight']
----
Interested network slicing parameters: for each site:
Todo: can be defined under ['RRH']['site*']['RRH_cells'][*]['transceivers'] the Cell Transceiver object
['transceivers'][*]['weight_per_slice']
note that this parameter overrides "serving_weight" defined in "NetworkSlices" section
"""
cfg = Configuration(dict_cfg, self.ls_config_path.split('.'))
self.configs.append(cfg.df_cfg_ho)
self.cfg_timestamp.append(self.timestamp)
print('[INFO] number of collected config: ', len(self.configs))
self.current_cfg = cfg
def add_report(self, dict_rpt):
# write report dict in a sub df
realtime = time()
diff_realtime = realtime - self.realtime
self.ls_realtime.append(diff_realtime)
print('[INFO] real time interval between the reports: ', diff_realtime)
self.realtime = realtime
#
diff_simtime = (dict_rpt['kpi_report']['all_cell_trx_kpis']['timestamp'] - self.timestamp)/1e3
self.ls_simtime.append(diff_simtime)
print('[INFO] simulation time interval between the reports: ', diff_simtime)
self.timestamp = dict_rpt['kpi_report']['all_cell_trx_kpis']['timestamp']
print('[INFO] new timestamp: ', self.timestamp)
#
ls_kpi_names = dict_rpt['kpi_report']['all_cell_trx_kpis']['kpi_names']
ls_kpi_values = dict_rpt['kpi_report']['all_cell_trx_kpis']['kpi_values']
sub_df = pd.DataFrame(columns=['timestamp'] + ls_kpi_names)
num_trx = len(dict_rpt['kpi_report']['all_cell_trx_kpis']['kpi_values'])
for idx_trx in range(num_trx):
ls_trx_values = [self.timestamp] + ls_kpi_values[idx_trx]
sub_df.at[idx_trx, :] = ls_trx_values
self.reports.append(sub_df)
print('[INFO] number of collected report: ', len(self.reports))
def next_config(self, timestamp=None):
# if only updates the traffic mask, but not the HO parameters
'''temporary placeholder for config optimization: now just randomly assign offset and TTT'''
# note that 'HO_triggers' is a list of dictionaries, e.g., [{'event_type': 'A3', 'offset': 3, 'TTT': 1}]
num_trx = len(self.configs[0])
offset = np.around(np.random.uniform(low=self.offset_range[0], high=self.offset_range[1], size=num_trx),
decimals=1)
ttt = np.around(np.random.uniform(low=self.TTT_range[0], high=self.TTT_range[1], size=num_trx), decimals=1)
ls_values = [[{'event_type': 'A3', 'offset': o, 'TTT': t}] for o, t in zip(offset, ttt)]
# return self.current_cfg.get_cfg(values=None, timestamp=timestamp) # update group mask
return self.current_cfg.get_cfg(values=ls_values, timestamp=timestamp) # update parameters
def save_results(self):
pickle.dump((self.configs, self.cfg_timestamp, self.reports), open(self.save_path, 'wb'))
def evaluate_results(self):
"""
get the corresponding config and average KPIs, plot performance
:return:
"""
# ---- Preprocess: HO counts are incremental, need to extract from the previous report
ls_ho_reports = []
ls_ho_colnames = ['successful_handovers_count', 'handover_drops_count',
'late_handovers_count', 'early_handovers_count', 'wrong_cell_handovers_count',
'pingpong_handovers_count', 'radio_link_failures_count', 'handover_attempts_count']
ls_ho_kpis = ['ho_success_ratio', 'ho_drop_ratio', 'late_ho_ratio', 'early_ho_ratio', 'wrong_ho_ratio',
'pingpong_ho_ratio', 'rlf_rate', 'ho_rate']
ls_rep_timestamp = [np.unique(r['timestamp'].values)[0] for r in self.reports]
#
for i in range(len(self.reports)):
rep_ho = self.reports[i][['timestamp', 'unique_id', 'unique_name'] + ls_ho_colnames].copy(deep=True)
if i > 0:
diff_time = ls_rep_timestamp[i] - ls_rep_timestamp[i-1]
for colname in ls_ho_colnames:
rep_ho[colname] = rep_ho[colname].values - self.reports[i-1][colname].values
else:
diff_time = ls_rep_timestamp[i]
for c, cnt in zip(ls_ho_kpis[-2:], ls_ho_colnames[-2:]):
rep_ho[c] = rep_ho[cnt].values/diff_time
b = rep_ho['handover_attempts_count'].values
for c, cnt in zip(ls_ho_kpis[:-2], ls_ho_colnames[:-2]):
a = rep_ho[cnt].values
rep_ho[c] = np.divide(a, b, out=np.zeros_like(a), where=(b != 0))
# --- find config for each report based on timestamp
ls_ho_reports.append(rep_ho)
class ThreadedClient(threading.Thread):
def __init__(self, host, port):
threading.Thread.__init__(self)
# set up queues
self.receive_q = queue.Queue()
self.send_q = queue.Queue()
self.msgs = ''
self.flag_run = True
self.last_msg = 'report'
# declare instance variables
self.host = host
self.port = port
# connect to socket
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.connect((self.host, self.port))
self.s.settimeout(.1)
self.agent = RlAgent()
# self.df_kpi_report = pd.DataFrame()
self.report_cnt = 0
'''LISTEN to season, but if send_q has new config, send to season'''
def listen(self):
""" listen to season and add the config and report messages in the queue
:return:
"""
while self.flag_run:
try:
# if there is a updated configuration, then push config to season
if not self.send_q.empty():
print('[INFO] send a new configuration...')
self.send_config()
print('listening...')
m = self.s.recv(26240000).decode("utf-8").split('\0')
if 'config' in m[0][:20]:
print('[INFO] RECEIVED: config head message...')
elif 'kpi_report' in m[0][:20]:
print('[INFO] RECEIVED: KPI head message...')
if ('kpi_report' in m[0][:20]) or ('config' in m[0][:20]):
self.msgs = m[0]
else:
self.msgs += m[0]
try:
msg_json = json.loads(self.msgs)
# put all kpi_reports in the queue
self.receive_q.put(msg_json)
# print('report queue size: ', self.receive_q.qsize())
self.msgs = ''
except:
pass
except socket.timeout:
pass
def start_listen(self):
t_listen = threading.Thread(target=self.listen)
t_listen.start()
print('started listen')
'''RUN AGENT: write received messages to report and config, decide next config, put in to the send_q queue'''
def run_agent(self):
# keep reading self.receive_q and collect the report into a data frame
while self.flag_run:
try:
# get report dict from the queue
dict_msg = self.receive_q.get()
if list(dict_msg.keys())[0] == 'kpi_report':
"""add report to the RL Agent"""
self.agent.add_report(dict_msg)
self.report_cnt += 1
self.last_msg = 'report'
elif list(dict_msg.keys())[0] == 'config':
self.agent.add_config(dict_msg['config'])
self.last_msg = 'config'
# should not reconfig again immediately after the last config, reconfig only after receiving reports
# of previous new config
if (len(self.agent.reports) % self.agent.num_report_per_config == 0) & (self.last_msg == 'report'):
# - giving timestamp changes both config and group size
# next_config = self.agent.next_config(timestamp=self.agent.timestamp)
# - if giving timestamp=None, change only configs, not the group size
next_config = self.agent.next_config()
# put it in send queue
self.send_q.put(next_config)
if len(self.agent.configs) > self.agent.config_max:
self.flag_run = False
self.agent.save_results()
except queue.Empty:
pass
def start_run_agent(self):
t_agent = threading.Thread(target=self.run_agent)
t_agent.start()
print('started run agent')
def send_config(self):
next_config = self.send_q.get()
cmd = json.dumps(next_config) + '\0'
self.s.send(cmd.encode())
if __name__ == '__main__':
port = 8000
address = 'localhost'
TC = ThreadedClient(address, port)
TC.start()
print('Server started, port: ', port)
TC.start_listen()
TC.start_run_agent()