-
Notifications
You must be signed in to change notification settings - Fork 0
/
Network.py
330 lines (262 loc) · 11.8 KB
/
Network.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
'''
This module defines the wrapper for the
open AI gym. This will describe the basestation
which allocates bandwidth to the network slices
dynamically depending on the number of requests
at each interval
'''
from BaseStation import BaseStation
from Client import Client
from Slice import Slice
from Container import Container
from Coverage import Coverage
from Distributor import Distributor
from Stats import Stats
from utils import kdtree
from functools import partial
import numpy as np
import random
import os
import math
import gym
from gym import Env
from gym import spaces, logger
from gym.utils import seeding
from queue import Queue
from collections import defaultdict
class Network(Env):
"""
Description:
A base station has some maximum allocated bandwidth
which it shares among the network slices depending
on the instantaneous slice ratios. This enable dynamic
slicing of the network.
Episode Termination:
An episode terminates if all the user requests are accepted
or if all the users are out of coverage of the base station
or if the combined requests (new users + queue) exceed the
bandwidth restrictions of the base station
States:
{slice 1 allocated bandwidth ratio, slice 1 instantaneous bandwidth usage ratio, slice 1 client density,
slice 2 allocated bandwidth ratio, slice 2 instantaneous bandwidth usage ratio, slice 2 client density,
slice 3 allocated bandwidth ratio, slice 3 instantaneous bandwidth usage ratio, slice 3 client density}
Actions:
A={(0, 0, 0), (+0.05, -0.025, -0.025), (-0.05, +0.025,
+0.025), (-0.025, +0.05, -0.025), (+0.025, -0.05, +0.025), (-0.025,
-0.025, +0.05), (+0.025, +0.025, -0.05)}
"""
slices_info = {'emBB': 0.45, 'mMTC': 0.3, 'URLLC': 0.25}
collected, slice_weights = 0, []
for _, item in slices_info.items():
collected += item
slice_weights.append(collected)
mb_weights = []
def __init__(self, bs_params, slice_params, client_params):
self.n_clients = 100
self.clients = self.clients_init(self.n_clients, client_params)
self.base_stations = self.base_stations_init(bs_params, slice_params)
self.x_range = (0, 1000)
self.y_range = (0, 1000)
self.stats = Stats(self.base_stations, None, (self.x_range, self.y_range))
for client in self.clients:
client.stat_collector = self.stats
self.action_list = [(0, 0, 0), (0.05, -0.025, -0.025), (-0.05, +0.025,
+0.025), (-0.025, +0.05, -0.025), (+0.025, -0.05, +0.025), (-0.025,
-0.025, +0.05), (+0.025, +0.025, -0.05)]
self.action_space = spaces.Discrete(7)
self.state = None
high = np.ones(shape=(9, ))
low = -high
self.observation_space = spaces.Box(low, high, dtype=np.float32)
self.steps_beyond_done = None
self.user_threshold = 0.7
self.seed()
def seed(self, seed=None):
self.np_random, seed = seeding.np_random(seed)
return [seed]
def reset(self):
self.state = self.np_random.uniform(low=0, high=1, size=(9,))
self.steps_beyond_done = None
return np.array(self.state)
def step(self, action: int):
"""
A step is defined in the client
class which has 4 different parts
for each cycle: Lock, Stats, Release and Move
See method Client.iter()
Each part has a duration allocated and is
followed by a yeild(timeout)
In the Stats step, the get_stats()
method of the Stats class is called
which provides one observation in the
form of an array
"""
### Initialise the stat collector which gives state information
selected_action = self.SelectedAction(action)
## Changing the slice ratios in all base stations as per the action provided
for bs in self.base_stations:
for itr, slice in enumerate(bs.slices):
new_s_cap = (1 + selected_action[itr])*slice.init_capacity
slice.init_capacity = new_s_cap
slice.capacity = Container(init=new_s_cap, capacity=new_s_cap)
## Connecting base stations to clients and initialising
## clients attributes of stats
self.initialise_stats()
selected_clients = self.generate_user_requests()
slice_hash_table = defaultdict(lambda: np.zeros(3))
reward = self.reward(selected_clients)
total_connected_clients, clients_in_coverage = 0, 0
for client in selected_clients:
if client.base_station is not None:
total_connected_clients += 1
slice: Slice = client.get_slice()
if slice is None:
allotted_slice: Slice = client.base_station.slices[client.subscribed_slice_index]
slice_hash_table[allotted_slice.name] = np.zeros(3)
else:
slice_hash_table[slice.name][0] = (slice_hash_table[slice.name][0] + 1)/len(selected_clients)
slice_hash_table[slice.name][1] = slice.capacity.capacity/client.base_station.capacity_bandwidth
slice_hash_table[slice.name][2] += client.usage_freq
state_array = []
for _, item in slice_hash_table.items():
state_array.append(item)
self.state = (np.array(state_array)).flatten()
done = bool(total_connected_clients == len(selected_clients)
or total_connected_clients/len(selected_clients) >= self.user_threshold) ## TODO: done condition is too harsh! Should add used bandwidth condition
# if self.steps_beyond_done is None:
# self.steps_beyond_done = 0
# reward = -10
# else:
# if self.steps_beyond_done == 0:
# logger.warn(
# "You are calling 'step()' even though this "
# "environment has already returned done = True. You "
# "should always call 'reset()' once you receive 'done = "
# "True' -- any further steps are undefined behavior.")
# self.steps_beyond_done += 1
# reward = 0.0
return self.state, action, reward, done, slice_hash_table
def SelectedAction(self, action: int):
action = self.action_list[action]
return action
def generate_user_requests(self):
## A subset of clients are selected at each step
## this follows a normal distribution
n_active_clients = max(int(random.random()*self.n_clients), int(0.1*self.n_clients))
random_client_ids = np.random.randint(self.n_clients, size=n_active_clients)
all_clients = np.array(self.clients)
selected_clients = all_clients[random_client_ids]
for selected_client in selected_clients:
selected_client.iter()
return selected_clients
def reward(self, clients: np.ndarray):
"""
The reward function is defined in the
base paper: https://ieeexplore.ieee.org/abstract/document/9235006/references#references
It is a function of the latency requirements (inverse of the delay tolerance) of each
slice, the blocked request counts for that slice and the total request counts for that
slice. The net reward is the sum over all the slices. The request counts can be generated
from the Stats.get_stats() method.
"""
reward = 0
for client in clients:
if client.base_station is not None:
slice: Slice = client.base_station.slices[client.subscribed_slice_index]
stats: Stats = client.stat_collector
latency_requirements = 1/slice.delay_tolerance
connection_requests = stats.connect_attempt[-1]
blocked_requests = connection_requests - slice.connected_users
reward_slice = -(latency_requirements)*(blocked_requests/connection_requests)
reward += reward_slice
return reward
def is_done(self):
"""
Episode termination step is provided here. This is already described
above.
"""
pass
def connections_init(self):
"""
Initialise connections with KDTree
"""
# self.kdt.limit = 5
kdtree(self.clients, self.base_stations)
def initialise_stats(self):
"""
Assigns clients to the stats method
only after initialising the KDTree i.e.,
only after assigning closest base stations
to all the clients
"""
self.connections_init()
self.stats.clients = self.clients
@classmethod
def base_stations_init(cls, bs_params, slice_params):
base_stations = []
i = 0
usage_patterns = {}
for name, s in slice_params.items():
usage_patterns[name] = Distributor(name, get_dist(s['usage_pattern']['distribution']), *s['usage_pattern']['params'])
for bs in bs_params:
slices = []
ratios = bs['ratios']
capacity = bs['capacity_bandwidth']
for name, s in slice_params.items():
s_cap = capacity * ratios[name]
s = Slice(name, ratios[name], 0, s['client_weight'],
s['delay_tolerance'],
s['qos_class'], s['bandwidth_guaranteed'],
s['bandwidth_max'], s_cap, usage_patterns[name])
s.capacity = Container(init=s_cap, capacity=s_cap)
slices.append(s)
base_station = BaseStation(i, Coverage((bs['x'], bs['y']), bs['coverage']), capacity, slices)
base_stations.append(base_station)
i += 1
return base_stations
@classmethod
def clients_init(cls, n_clients, client_params):
i = 0
clients = []
ufp = client_params['usage_frequency']
usage_freq_pattern = Distributor(f'ufp', get_dist(ufp['distribution']),
*ufp['params'], divide_scale=ufp['divide_scale'])
for _ in range(n_clients):
loc_x = client_params['location']['x']
loc_y = client_params['location']['y']
location_x = get_dist(loc_x['distribution'])(*loc_x['params'])
location_y = get_dist(loc_y['distribution'])(*loc_y['params'])
connected_slice_index = get_random_slice_index(cls.slice_weights)
c = Client(i, location_x, location_y, usage_freq_pattern.generate_scaled(),
connected_slice_index, None, None)
clients.append(c)
i += 1
return clients
def get_dist(d):
return {
'randrange': random.randrange, # start, stop, step
'randint': random.randint, # a, b
'random': random.random,
'uniform': random, # a, b
'triangular': random.triangular, # low, high, mode
'beta': random.betavariate, # alpha, beta
'expo': random.expovariate, # lambda
'gamma': random.gammavariate, # alpha, beta
'gauss': random.gauss, # mu, sigma
'lognorm': random.lognormvariate, # mu, sigma
'normal': random.normalvariate, # mu, sigma
'vonmises': random.vonmisesvariate, # mu, kappa
'pareto': random.paretovariate, # alpha
'weibull': random.weibullvariate # alpha, beta
}.get(d)
def get_random_mobility_pattern(vals, mobility_patterns):
i = 0
r = random.random()
while vals[i] < r:
i += 1
return mobility_patterns[i]
def get_random_slice_index(vals):
i = 0
r = random.random()
while vals[i] < r:
i += 1
return i