-
Notifications
You must be signed in to change notification settings - Fork 9
/
DSA_env.py
362 lines (287 loc) · 15 KB
/
DSA_env.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
import numpy as np
import matplotlib.pyplot as plt
import copy
class DSA_Markov():
def __init__(
self,
n_channel,
n_su,
sense_error_prob_max = 0.2,
punish_interfer_PU = -2
):
self.n_channel = n_channel # The number of the channels
self.n_su = n_su # The number of the SUs
# Initialize the Markov channels
self._build_Markov_channel()
# Initialize the locations of SUs and PUs
self._build_location()
# Set the noise (mW)
self.Noise = 1 * np.float_power(10, -8)
# Set the carrier frequency (5 GHz)
self.fc = 5
# Set the K in channel gain
self.K = 8
# Set the power of PU and SU (mW)
self.SU_power = 20
self.PU_power = 40
# Initialize SINR (no consider interference of SUs)
self.render_SINR()
self.n_actions = n_channel + 1 # The action space size
self.n_features = n_channel # The sensing result space
self.sense_error_prob_max = sense_error_prob_max
self.sense_error_prob = np.random.uniform(0, self.sense_error_prob_max, size=(self.n_su, self.n_channel))
# The punishment for interfering PUs
self.punish_interfer_PU = punish_interfer_PU
def _build_Markov_channel(self):
# Initialize channel state (uniform distribution)
# 1: Inactive state (primary user is not using)
# 0: Active state (primary user is using)
self.channel_state = np.random.choice(2, self.n_channel)
# Initialize the transition probability of independent channels
self.stayGood_prob = np.random.uniform(0.7, 1, self.n_channel)
self.stayBad_prob = np.random.uniform(0, 0.3, self.n_channel)
self.goodToBad_prob = 1 - self.stayGood_prob
self.badToGood_prob = 1 - self.stayBad_prob
def _build_location(self):
# Initialize the location of PUs
self.PU_TX_x = np.random.uniform(0, 150, self.n_channel)
self.PU_TX_y = np.random.uniform(0, 150, self.n_channel)
self.PU_RX_x = np.random.uniform(0, 150, self.n_channel)
self.PU_RX_y = np.random.uniform(0, 150, self.n_channel)
# Initialize the location of SUs transmitters
self.SU_TX_x = np.random.uniform(0+40, 150-40, self.n_su)
self.SU_TX_y = np.random.uniform(0+40, 150-40, self.n_su)
# Initialize the distance between SUs' transmitter and receiver
self.SU_d = np.random.uniform(20, 40, self.n_su)
# Initialize the location of SUs receivers
SU_theda = 2 * np.pi * np.random.uniform(0, 1, self.n_su)
SU_dx = self.SU_d * np.cos(SU_theda)
SU_dy = self.SU_d * np.sin(SU_theda)
self.SU_RX_x = self.SU_TX_x + SU_dx
self.SU_RX_y = self.SU_TX_y + SU_dy
# Compute the distance between PU_TX and SU_RX
self.SU_RX_PU_TX_d = np.zeros((self.n_su, self.n_channel))
for k in range(self.n_su):
for l in range(self.n_channel):
self.SU_RX_PU_TX_d[k][l] = np.sqrt(
np.float_power(self.SU_RX_x[k] - self.PU_TX_x[l], 2) + np.float_power(
self.SU_RX_y[k] - self.PU_TX_y[l], 2))
# Compute the distance between PU_TX and SU_RX
self.SU_RX_SU_TX_d = np.zeros((self.n_su, self.n_su))
for k1 in range(self.n_su):
for k2 in range(self.n_su):
self.SU_RX_SU_TX_d[k1][k2] = np.sqrt(
np.float_power(self.SU_RX_x[k1] - self.SU_TX_x[k2], 2) + np.float_power(
self.SU_RX_y[k1] - self.SU_TX_y[k2], 2))
# Plot the locations
plt.plot(self.PU_TX_x, self.PU_TX_y, 'ro', label='PU_TX')
plt.plot(self.PU_RX_x, self.PU_RX_y, 'rx', label='PU_RX')
plt.plot(self.SU_TX_x, self.SU_TX_y, 'bs', label='SU_TX')
plt.plot(self.SU_RX_x, self.SU_RX_y, 'b^', label='SU_RX')
plt.legend(loc='lower right')
plt.ylabel('y')
plt.xlabel('x')
plt.show()
def store_action(self, action):
self.action = action
def sense(self):
tmp_dice = np.random.uniform(0, 1, size=(self.n_su, self.n_channel)) # roll the dice between 0 and 1
error_index = tmp_dice < self.sense_error_prob # True: sensing error happens, False: sensing is correct
# Get the sensing result
self.sensing_result = self.channel_state*(1-error_index) + (1-self.channel_state)*(error_index)
return self.sensing_result
def access(self, action):
# action = [0, n_channel-1]: access the selected channel
# action = n_channel: do not access the channel
self.success = 0
self.fail_PU = 0
self.fail_collision = 0
self.reward = np.zeros(self.n_su)
# Calculate the interference of SUs
Interferecne_SU = 0
SU_sigma2 = np.float_power(10, -((41 + 22.7 * np.log10(self.SU_RX_SU_TX_d) + 20 * np.log10(self.fc / 5)) / 10))
for k in range(self.n_su):
SU_sigma2[k][k] = 0
for k in range(self.n_su):
if (action[k] == self.n_channel): # action is not choosing any channel
self.reward[k] = 0
else: # action is choosing one of channels
for q in range(self.n_su):
if (action[q] == action[k]):
Interferecne_SU = Interferecne_SU + SU_sigma2[k][q]*self.SU_power
SINR = self.H2[k, action[k]] * self.SU_power / (Interferecne_SU + self.Interferecne_PU[k, action[k]] + self.Noise)
self.reward[k] = np.log2(1 + SINR)
if (self.channel_state[action[k]] == 1):
if (len(np.where(action == action[k])[0]) == 1):
# successful transmission
self.success = self.success + 1
else:
# collision with SU
self.fail_collision = self.fail_collision + 1
else:
# collision with PU
self.fail_PU = self.fail_PU + 1
self.reward[k] = self.punish_interfer_PU
if (len(np.where(action == action[k])[0]) > 1):
# collision with SU
self.fail_collision = self.fail_collision + 1
return self.reward
def render(self):
# The probability of staying in current state in next time slot
stay_prob = self.channel_state*self.stayGood_prob + (1-self.channel_state)*self.stayBad_prob
tmp_dice = np.random.uniform(0, 1, self.n_channel) # roll the dice between 0 and 1
stay_index = tmp_dice < stay_prob # 1: stay in current state, 0: change state
# Update the channel state
self.channel_state = self.channel_state*stay_index + (1-self.channel_state)*(1-stay_index)
def render_SINR(self):
# Update the SINR
# Calculate the channel gain
SU_d = copy.deepcopy(np.reshape(self.SU_d, (-1, 1)))
for n in range(self.n_channel-1):
SU_d = np.hstack( (SU_d, np.reshape(self.SU_d, (-1, 1))) )
SU_sigma2 = np.float_power(10, -((41+22.7*np.log10(SU_d)+20*np.log10(self.fc/5))/10))
CN_real = np.random.normal(0, 1, size=(self.n_su, self.n_channel))
CN_imag = np.random.normal(0, 1, size=(self.n_su, self.n_channel))
theda = np.random.uniform(0, 1, size=(self.n_su, self.n_channel))
H = np.sqrt(self.K/(self.K+1)*SU_sigma2)*np.exp(1j*2*np.pi*theda) + np.sqrt(1/(self.K+1)*SU_sigma2/2)*(CN_real + 1j*CN_imag)
self.H2 = np.float_power(np.absolute(H), 2)
# Calculate the interference of PUs
PU_sigma2 = np.float_power(10, -((41 + 22.7 * np.log10(self.SU_RX_PU_TX_d) + 20 * np.log10(self.fc / 5)) / 10))
channel_state = np.array([self.channel_state for k in range(self.n_su)])
self.Interferecne_PU = self.PU_power * PU_sigma2 * (1 - channel_state)
self.SINR = self.H2*self.SU_power/(self.Interferecne_PU + self.Noise)
class DSA_Period():
def __init__(
self,
n_channel,
n_su,
sense_error_prob_max=0.0,
punish_interfer_PU=-4,
punish_idle = 0
):
self.n_channel = n_channel
self.n_su = n_su
self._build_periodic_channel()
self._build_location()
# Set the noise (6*10^(-8) mW)
self.Noise = 1 * np.float_power(10, -8)
# Set the carrier frequency (5 GHz)
self.fc = 5
# Set the K in channel gain
self.K = 8
# Set the power
self.SU_power = 20
self.PU_power = 40
# Initialize SINR (no consider interference of SUs)
self.render_SINR()
self.n_actions = n_channel+1 # select at most one channel
self.n_features = n_channel
self.sense_error_prob_max = sense_error_prob_max
self.sense_error_prob = np.random.uniform(0, self.sense_error_prob_max, size=(self.n_su, self.n_channel))
self.punish_interfer_PU = punish_interfer_PU
self.punish_idle = punish_idle
self.channel_state_his = np.zeros(self.n_channel)
def _build_periodic_channel(self):
self.channel_state = np.ones(self.n_channel)
# Initialize period
self.period = np.ones(self.n_channel, dtype=int) * 3
self.count = np.ones(self.n_channel)
def _build_location(self):
# Initialize the location of PUs
self.PU_TX_x = np.random.uniform(0, 150, self.n_channel)
self.PU_TX_y = np.random.uniform(0, 150, self.n_channel)
self.PU_RX_x = np.random.uniform(0, 150, self.n_channel)
self.PU_RX_y = np.random.uniform(0, 150, self.n_channel)
# Initialize the location of SUs transmitters
self.SU_TX_x = np.random.uniform(0+40, 150-40, self.n_su)
self.SU_TX_y = np.random.uniform(0+40, 150-40, self.n_su)
# Initialize the location of SUs receivers
self.SU_d = np.array([30])
# self.SU_d = np.random.uniform(20, 40, self.n_su)
SU_theda = 2 * np.pi * np.random.uniform(0, 1, self.n_su)
SU_dx = self.SU_d * np.cos(SU_theda)
SU_dy = self.SU_d * np.sin(SU_theda)
self.SU_RX_x = self.SU_TX_x + SU_dx
self.SU_RX_y = self.SU_TX_y + SU_dy
# Compute the distance between PU_TX and SU_RX
self.SU_RX_PU_TX_d = np.zeros((self.n_su, self.n_channel))
for k in range(self.n_su):
for l in range(self.n_channel):
self.SU_RX_PU_TX_d[k][l] = np.sqrt(np.float_power(self.SU_RX_x[k]-self.PU_TX_x[l], 2) + np.float_power(self.SU_RX_y[k] - self.PU_TX_y[l],2))
# Compute the distance between PU_TX and SU_RX
self.SU_RX_SU_TX_d = np.zeros((self.n_su, self.n_su))
for k1 in range(self.n_su):
for k2 in range(self.n_su):
self.SU_RX_SU_TX_d[k1][k2] = np.sqrt(np.float_power(self.SU_RX_x[k1]-self.SU_TX_x[k2],2) + np.float_power(self.SU_RX_y[k1]-self.SU_TX_y[k2],2) )
# Plot the locations
plt.plot(self.PU_TX_x, self.PU_TX_y, 'ro', label='PU_TX')
plt.plot(self.PU_RX_x, self.PU_RX_y, 'rx', label='PU_RX')
plt.plot(self.SU_TX_x, self.SU_TX_y, 'bo', label='SU_TX')
plt.plot(self.SU_RX_x, self.SU_RX_y, 'bx', label='SU_RX')
plt.legend(loc='lower right')
plt.ylabel('y')
plt.xlabel('x')
plt.show()
def store_action(self, action):
self.action = action
def sense(self):
tmp_dice = np.random.uniform(0, 1, size=(self.n_su, self.n_channel)) # roll the dice between 0 and 1
error_index = tmp_dice < self.sense_error_prob # True: sensing error happens, False: sensing is correct
# Get the sensing result
self.sensing_result = self.channel_state*(1-error_index) + (1-self.channel_state)*(error_index)
return self.sensing_result
def access(self, action):
# action = [0, n_channel-1]: access the selected channel
# action = n_channel: do not access the channel
self.success = 0
self.fail_PU = 0
self.fail_collision = 0
self.ACK = np.zeros((self.n_su, self.n_channel))
self.reward = np.zeros(self.n_su)
# Calculate the interference of PUs
Interferecne_SU = 0
SU_sigma2 = np.float_power(10, -((41 + 22.7 * np.log10(self.SU_RX_SU_TX_d) + 20 * np.log10(self.fc / 5)) / 10))
for k in range(self.n_su):
SU_sigma2[k][k] = 0
for k in range(self.n_su):
if (action[k] == self.n_channel):
self.reward[k] = self.punish_idle
else:
for q in range(self.n_su):
if (action[q] == action[k]):
Interferecne_SU = Interferecne_SU + SU_sigma2[k][q]*self.SU_power
SINR = self.H2[k, action[k]] * self.SU_power / (Interferecne_SU + self.Interferecne_PU[k, action[k]] + self.Noise)
self.reward[k] = np.log2(1 + SINR)
if (self.channel_state[action[k]] == 1):
if (len(np.where(action == action[k])[0]) == 1):
self.success = self.success + 1
else:
self.fail_collision = self.fail_collision + 1
else:
self.fail_PU = self.fail_PU + 1
self.reward[k] = self.punish_interfer_PU
if (len(np.where(action == action[k])[0]) > 1):
self.fail_collision = self.fail_collision + 1
return self.reward
def render(self):
self.count = self.count + 1
for k in range(self.n_channel):
self.channel_state[k] = 1
if (self.count[k] % self.period[k] == 0):
self.channel_state[k] = 0
def render_SINR(self):
# Calculate the channel gain
SU_d = copy.deepcopy(np.reshape(self.SU_d, (-1, 1)))
for n in range(self.n_channel-1):
SU_d = np.hstack( (SU_d, np.reshape(self.SU_d, (-1, 1))) )
SU_sigma2 = np.float_power(10, -((41+22.7*np.log10(SU_d)+20*np.log10(self.fc/5))/10))
CN_real = np.random.normal(0, 1, size=(self.n_su, self.n_channel))
CN_imag = np.random.normal(0, 1, size=(self.n_su, self.n_channel))
theda = np.random.uniform(0, 1, size=(self.n_su, self.n_channel))
H = np.sqrt(self.K/(self.K+1)*SU_sigma2)*np.exp(1j*2*np.pi*theda) + np.sqrt(1/(self.K+1)*SU_sigma2/2)*(CN_real + 1j*CN_imag)
self.H2 = np.float_power(np.absolute(H), 2)
# Calculate the interference of PUs
PU_sigma2 = np.float_power(10, -((41 + 22.7 * np.log10(self.SU_RX_PU_TX_d) + 20 * np.log10(self.fc / 5)) / 10))
channel_state = np.array([self.channel_state for k in range(self.n_su)])
self.Interferecne_PU = self.PU_power * PU_sigma2 * (1 - channel_state)
self.SINR = self.H2*self.SU_power/(self.Interferecne_PU + self.Noise)