-
Notifications
You must be signed in to change notification settings - Fork 94
/
Environment_marl_test.py
715 lines (584 loc) · 41.8 KB
/
Environment_marl_test.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
from __future__ import division
import numpy as np
import time
import random
import math
np.random.seed(1234)
class V2Vchannels:
# Simulator of the V2V Channels
def __init__(self):
self.t = 0
self.h_bs = 1.5
self.h_ms = 1.5
self.fc = 2
self.decorrelation_distance = 10
self.shadow_std = 3
def get_path_loss(self, position_A, position_B):
d1 = abs(position_A[0] - position_B[0])
d2 = abs(position_A[1] - position_B[1])
d = math.hypot(d1, d2) + 0.001
d_bp = 4 * (self.h_bs - 1) * (self.h_ms - 1) * self.fc * (10 ** 9) / (3 * 10 ** 8)
def PL_Los(d):
if d <= 3:
return 22.7 * np.log10(3) + 41 + 20 * np.log10(self.fc / 5)
else:
if d < d_bp:
return 22.7 * np.log10(d) + 41 + 20 * np.log10(self.fc / 5)
else:
return 40.0 * np.log10(d) + 9.45 - 17.3 * np.log10(self.h_bs) - 17.3 * np.log10(self.h_ms) + 2.7 * np.log10(self.fc / 5)
def PL_NLos(d_a, d_b):
n_j = max(2.8 - 0.0024 * d_b, 1.84)
return PL_Los(d_a) + 20 - 12.5 * n_j + 10 * n_j * np.log10(d_b) + 3 * np.log10(self.fc / 5)
if min(d1, d2) < 7:
PL = PL_Los(d)
else:
PL = min(PL_NLos(d1, d2), PL_NLos(d2, d1))
return PL # + self.shadow_std * np.random.normal()
def get_shadowing(self, delta_distance, shadowing):
return np.exp(-1 * (delta_distance / self.decorrelation_distance)) * shadowing \
+ math.sqrt(1 - np.exp(-2 * (delta_distance / self.decorrelation_distance))) * np.random.normal(0, 3) # standard dev is 3 db
class V2Ichannels:
# Simulator of the V2I channels
def __init__(self):
self.h_bs = 25
self.h_ms = 1.5
self.Decorrelation_distance = 50
self.BS_position = [750 / 2, 1299 / 2] # center of the grids
self.shadow_std = 8
def get_path_loss(self, position_A):
d1 = abs(position_A[0] - self.BS_position[0])
d2 = abs(position_A[1] - self.BS_position[1])
distance = math.hypot(d1, d2)
return 128.1 + 37.6 * np.log10(math.sqrt(distance ** 2 + (self.h_bs - self.h_ms) ** 2) / 1000) # + self.shadow_std * np.random.normal()
def get_shadowing(self, delta_distance, shadowing):
nVeh = len(shadowing)
self.R = np.sqrt(0.5 * np.ones([nVeh, nVeh]) + 0.5 * np.identity(nVeh))
return np.multiply(np.exp(-1 * (delta_distance / self.Decorrelation_distance)), shadowing) \
+ np.sqrt(1 - np.exp(-2 * (delta_distance / self.Decorrelation_distance))) * np.random.normal(0, 8, nVeh)
class Vehicle:
# Vehicle simulator: include all the information for a vehicle
def __init__(self, start_position, start_direction, velocity):
self.position = start_position
self.direction = start_direction
self.velocity = velocity
self.neighbors = []
self.destinations = []
class Environ:
def __init__(self, down_lane, up_lane, left_lane, right_lane, width, height, n_veh, n_neighbor):
self.down_lanes = down_lane
self.up_lanes = up_lane
self.left_lanes = left_lane
self.right_lanes = right_lane
self.width = width
self.height = height
self.V2Vchannels = V2Vchannels()
self.V2Ichannels = V2Ichannels()
self.vehicles = []
self.demand = []
self.V2V_Shadowing = []
self.V2I_Shadowing = []
self.delta_distance = []
self.V2V_channels_abs = []
self.V2I_channels_abs = []
self.V2I_power_dB = 23 # dBm
self.V2V_power_dB_List = [23, 15, 5, -100] # the power levels
self.sig2_dB = -114
self.bsAntGain = 8
self.bsNoiseFigure = 5
self.vehAntGain = 3
self.vehNoiseFigure = 9
self.sig2 = 10 ** (self.sig2_dB / 10)
self.n_RB = n_veh
self.n_Veh = n_veh
self.n_neighbor = n_neighbor
self.time_fast = 0.001
self.time_slow = 0.1 # update slow fading/vehicle position every 100 ms
self.bandwidth = int(1e6) # bandwidth per RB, 1 MHz
# self.bandwidth = 1500
self.demand_size = int((4 * 190 + 300) * 8 * 2) # V2V payload: 1060 Bytes every 100 ms
# self.demand_size = 20
self.V2V_Interference_all = np.zeros((self.n_Veh, self.n_neighbor, self.n_RB)) + self.sig2
self.V2V_Interference_all_sarl = np.zeros((self.n_Veh, self.n_neighbor, self.n_RB)) + self.sig2
self.V2V_Interference_all_dpra = np.zeros((self.n_Veh, self.n_neighbor, self.n_RB)) + self.sig2
def add_new_vehicles(self, start_position, start_direction, start_velocity):
self.vehicles.append(Vehicle(start_position, start_direction, start_velocity))
def add_new_vehicles_by_number(self, n):
for i in range(n):
ind = np.random.randint(0, len(self.down_lanes))
start_position = [self.down_lanes[ind], np.random.randint(0, self.height)]
start_direction = 'd' # velocity: 10 ~ 15 m/s, random
self.add_new_vehicles(start_position, start_direction, np.random.randint(10, 15))
start_position = [self.up_lanes[ind], np.random.randint(0, self.height)]
start_direction = 'u'
self.add_new_vehicles(start_position, start_direction, np.random.randint(10, 15))
start_position = [np.random.randint(0, self.width), self.left_lanes[ind]]
start_direction = 'l'
self.add_new_vehicles(start_position, start_direction, np.random.randint(10, 15))
start_position = [np.random.randint(0, self.width), self.right_lanes[ind]]
start_direction = 'r'
self.add_new_vehicles(start_position, start_direction, np.random.randint(10, 15))
# initialize channels
self.V2V_Shadowing = np.random.normal(0, 3, [len(self.vehicles), len(self.vehicles)])
self.V2I_Shadowing = np.random.normal(0, 8, len(self.vehicles))
self.delta_distance = np.asarray([c.velocity*self.time_slow for c in self.vehicles])
def renew_positions(self):
# ===============
# This function updates the position of each vehicle
# ===============
i = 0
while (i < len(self.vehicles)):
delta_distance = self.vehicles[i].velocity * self.time_slow
change_direction = False
if self.vehicles[i].direction == 'u':
# print ('len of position', len(self.position), i)
for j in range(len(self.left_lanes)):
if (self.vehicles[i].position[1] <= self.left_lanes[j]) and ((self.vehicles[i].position[1] + delta_distance) >= self.left_lanes[j]): # came to an cross
if (np.random.uniform(0, 1) < 0.4):
self.vehicles[i].position = [self.vehicles[i].position[0] - (delta_distance - (self.left_lanes[j] - self.vehicles[i].position[1])), self.left_lanes[j]]
self.vehicles[i].direction = 'l'
change_direction = True
break
if change_direction == False:
for j in range(len(self.right_lanes)):
if (self.vehicles[i].position[1] <= self.right_lanes[j]) and ((self.vehicles[i].position[1] + delta_distance) >= self.right_lanes[j]):
if (np.random.uniform(0, 1) < 0.4):
self.vehicles[i].position = [self.vehicles[i].position[0] + (delta_distance + (self.right_lanes[j] - self.vehicles[i].position[1])), self.right_lanes[j]]
self.vehicles[i].direction = 'r'
change_direction = True
break
if change_direction == False:
self.vehicles[i].position[1] += delta_distance
if (self.vehicles[i].direction == 'd') and (change_direction == False):
# print ('len of position', len(self.position), i)
for j in range(len(self.left_lanes)):
if (self.vehicles[i].position[1] >= self.left_lanes[j]) and ((self.vehicles[i].position[1] - delta_distance) <= self.left_lanes[j]): # came to an cross
if (np.random.uniform(0, 1) < 0.4):
self.vehicles[i].position = [self.vehicles[i].position[0] - (delta_distance - (self.vehicles[i].position[1] - self.left_lanes[j])), self.left_lanes[j]]
# print ('down with left', self.vehicles[i].position)
self.vehicles[i].direction = 'l'
change_direction = True
break
if change_direction == False:
for j in range(len(self.right_lanes)):
if (self.vehicles[i].position[1] >= self.right_lanes[j]) and (self.vehicles[i].position[1] - delta_distance <= self.right_lanes[j]):
if (np.random.uniform(0, 1) < 0.4):
self.vehicles[i].position = [self.vehicles[i].position[0] + (delta_distance + (self.vehicles[i].position[1] - self.right_lanes[j])), self.right_lanes[j]]
# print ('down with right', self.vehicles[i].position)
self.vehicles[i].direction = 'r'
change_direction = True
break
if change_direction == False:
self.vehicles[i].position[1] -= delta_distance
if (self.vehicles[i].direction == 'r') and (change_direction == False):
# print ('len of position', len(self.position), i)
for j in range(len(self.up_lanes)):
if (self.vehicles[i].position[0] <= self.up_lanes[j]) and ((self.vehicles[i].position[0] + delta_distance) >= self.up_lanes[j]): # came to an cross
if (np.random.uniform(0, 1) < 0.4):
self.vehicles[i].position = [self.up_lanes[j], self.vehicles[i].position[1] + (delta_distance - (self.up_lanes[j] - self.vehicles[i].position[0]))]
change_direction = True
self.vehicles[i].direction = 'u'
break
if change_direction == False:
for j in range(len(self.down_lanes)):
if (self.vehicles[i].position[0] <= self.down_lanes[j]) and ((self.vehicles[i].position[0] + delta_distance) >= self.down_lanes[j]):
if (np.random.uniform(0, 1) < 0.4):
self.vehicles[i].position = [self.down_lanes[j], self.vehicles[i].position[1] - (delta_distance - (self.down_lanes[j] - self.vehicles[i].position[0]))]
change_direction = True
self.vehicles[i].direction = 'd'
break
if change_direction == False:
self.vehicles[i].position[0] += delta_distance
if (self.vehicles[i].direction == 'l') and (change_direction == False):
for j in range(len(self.up_lanes)):
if (self.vehicles[i].position[0] >= self.up_lanes[j]) and ((self.vehicles[i].position[0] - delta_distance) <= self.up_lanes[j]): # came to an cross
if (np.random.uniform(0, 1) < 0.4):
self.vehicles[i].position = [self.up_lanes[j], self.vehicles[i].position[1] + (delta_distance - (self.vehicles[i].position[0] - self.up_lanes[j]))]
change_direction = True
self.vehicles[i].direction = 'u'
break
if change_direction == False:
for j in range(len(self.down_lanes)):
if (self.vehicles[i].position[0] >= self.down_lanes[j]) and ((self.vehicles[i].position[0] - delta_distance) <= self.down_lanes[j]):
if (np.random.uniform(0, 1) < 0.4):
self.vehicles[i].position = [self.down_lanes[j], self.vehicles[i].position[1] - (delta_distance - (self.vehicles[i].position[0] - self.down_lanes[j]))]
change_direction = True
self.vehicles[i].direction = 'd'
break
if change_direction == False:
self.vehicles[i].position[0] -= delta_distance
# if it comes to an exit
if (self.vehicles[i].position[0] < 0) or (self.vehicles[i].position[1] < 0) or (self.vehicles[i].position[0] > self.width) or (self.vehicles[i].position[1] > self.height):
# delete
# print ('delete ', self.position[i])
if (self.vehicles[i].direction == 'u'):
self.vehicles[i].direction = 'r'
self.vehicles[i].position = [self.vehicles[i].position[0], self.right_lanes[-1]]
else:
if (self.vehicles[i].direction == 'd'):
self.vehicles[i].direction = 'l'
self.vehicles[i].position = [self.vehicles[i].position[0], self.left_lanes[0]]
else:
if (self.vehicles[i].direction == 'l'):
self.vehicles[i].direction = 'u'
self.vehicles[i].position = [self.up_lanes[0], self.vehicles[i].position[1]]
else:
if (self.vehicles[i].direction == 'r'):
self.vehicles[i].direction = 'd'
self.vehicles[i].position = [self.down_lanes[-1], self.vehicles[i].position[1]]
i += 1
def renew_neighbor(self):
""" Determine the neighbors of each vehicles """
for i in range(len(self.vehicles)):
self.vehicles[i].neighbors = []
self.vehicles[i].actions = []
z = np.array([[complex(c.position[0], c.position[1]) for c in self.vehicles]])
Distance = abs(z.T - z)
for i in range(len(self.vehicles)):
sort_idx = np.argsort(Distance[:, i])
for j in range(self.n_neighbor):
self.vehicles[i].neighbors.append(sort_idx[j + 1])
destination = self.vehicles[i].neighbors
self.vehicles[i].destinations = destination
def renew_channel(self):
""" Renew slow fading channel """
self.V2V_pathloss = np.zeros((len(self.vehicles), len(self.vehicles))) + 50 * np.identity(len(self.vehicles))
self.V2I_pathloss = np.zeros((len(self.vehicles)))
self.V2V_channels_abs = np.zeros((len(self.vehicles), len(self.vehicles)))
self.V2I_channels_abs = np.zeros((len(self.vehicles)))
for i in range(len(self.vehicles)):
for j in range(i + 1, len(self.vehicles)):
self.V2V_Shadowing[j][i] = self.V2V_Shadowing[i][j] = self.V2Vchannels.get_shadowing(self.delta_distance[i] + self.delta_distance[j], self.V2V_Shadowing[i][j])
self.V2V_pathloss[j,i] = self.V2V_pathloss[i][j] = self.V2Vchannels.get_path_loss(self.vehicles[i].position, self.vehicles[j].position)
self.V2V_channels_abs = self.V2V_pathloss + self.V2V_Shadowing
self.V2I_Shadowing = self.V2Ichannels.get_shadowing(self.delta_distance, self.V2I_Shadowing)
for i in range(len(self.vehicles)):
self.V2I_pathloss[i] = self.V2Ichannels.get_path_loss(self.vehicles[i].position)
self.V2I_channels_abs = self.V2I_pathloss + self.V2I_Shadowing
def renew_channels_fastfading(self):
""" Renew fast fading channel """
V2V_channels_with_fastfading = np.repeat(self.V2V_channels_abs[:, :, np.newaxis], self.n_RB, axis=2)
self.V2V_channels_with_fastfading = V2V_channels_with_fastfading - 20 * np.log10(
np.abs(np.random.normal(0, 1, V2V_channels_with_fastfading.shape) + 1j * np.random.normal(0, 1, V2V_channels_with_fastfading.shape)) / math.sqrt(2))
V2I_channels_with_fastfading = np.repeat(self.V2I_channels_abs[:, np.newaxis], self.n_RB, axis=1)
self.V2I_channels_with_fastfading = V2I_channels_with_fastfading - 20 * np.log10(
np.abs(np.random.normal(0, 1, V2I_channels_with_fastfading.shape) + 1j * np.random.normal(0, 1, V2I_channels_with_fastfading.shape))/ math.sqrt(2))
def Compute_Performance_Reward_Train(self, actions_power):
actions = actions_power[:, :, 0] # the channel_selection_part
power_selection = actions_power[:, :, 1] # power selection
# ------------ Compute V2I rate --------------------
V2I_Rate = np.zeros(self.n_RB)
V2I_Interference = np.zeros(self.n_RB) # V2I interference
for i in range(len(self.vehicles)):
for j in range(self.n_neighbor):
if not self.active_links[i, j]:
continue
V2I_Interference[actions[i][j]] += 10 ** ((self.V2V_power_dB_List[power_selection[i, j]] - self.V2I_channels_with_fastfading[i, actions[i, j]]
+ self.vehAntGain + self.bsAntGain - self.bsNoiseFigure) / 10)
self.V2I_Interference = V2I_Interference + self.sig2
V2I_Signals = 10 ** ((self.V2I_power_dB - self.V2I_channels_with_fastfading.diagonal() + self.vehAntGain + self.bsAntGain - self.bsNoiseFigure) / 10)
V2I_Rate = np.log2(1 + np.divide(V2I_Signals, self.V2I_Interference))
# ------------ Compute V2V rate -------------------------
V2V_Interference = np.zeros((len(self.vehicles), self.n_neighbor))
V2V_Signal = np.zeros((len(self.vehicles), self.n_neighbor))
actions[(np.logical_not(self.active_links))] = -1 # inactive links will not transmit regardless of selected power levels
for i in range(self.n_RB): # scanning all bands
indexes = np.argwhere(actions == i) # find spectrum-sharing V2Vs
for j in range(len(indexes)):
receiver_j = self.vehicles[indexes[j, 0]].destinations[indexes[j, 1]]
V2V_Signal[indexes[j, 0], indexes[j, 1]] = 10 ** ((self.V2V_power_dB_List[power_selection[indexes[j, 0], indexes[j, 1]]]
- self.V2V_channels_with_fastfading[indexes[j][0], receiver_j, i] + 2 * self.vehAntGain - self.vehNoiseFigure) / 10)
# V2I links interference to V2V links
V2V_Interference[indexes[j, 0], indexes[j, 1]] += 10 ** ((self.V2I_power_dB - self.V2V_channels_with_fastfading[i, receiver_j, i] + 2 * self.vehAntGain - self.vehNoiseFigure) / 10)
# V2V interference
for k in range(j + 1, len(indexes)): # spectrum-sharing V2Vs
receiver_k = self.vehicles[indexes[k][0]].destinations[indexes[k][1]]
V2V_Interference[indexes[j, 0], indexes[j, 1]] += 10 ** ((self.V2V_power_dB_List[power_selection[indexes[k, 0], indexes[k, 1]]]
- self.V2V_channels_with_fastfading[indexes[k][0]][receiver_j][i] + 2 * self.vehAntGain - self.vehNoiseFigure) / 10)
V2V_Interference[indexes[k, 0], indexes[k, 1]] += 10 ** ((self.V2V_power_dB_List[power_selection[indexes[j, 0], indexes[j, 1]]]
- self.V2V_channels_with_fastfading[indexes[j][0]][receiver_k][i] + 2 * self.vehAntGain - self.vehNoiseFigure) / 10)
self.V2V_Interference = V2V_Interference + self.sig2
V2V_Rate = np.log2(1 + np.divide(V2V_Signal, self.V2V_Interference))
self.demand -= V2V_Rate * self.time_fast * self.bandwidth
self.demand[self.demand < 0] = 0 # eliminate negative demands
self.individual_time_limit -= self.time_fast
reward_elements = V2V_Rate/10
reward_elements[self.demand <= 0] = 1
self.active_links[np.multiply(self.active_links, self.demand <= 0)] = 0 # transmission finished, turned to "inactive"
return V2I_Rate, V2V_Rate, reward_elements
def Compute_Performance_Reward_Test_rand(self, actions_power):
""" for random baseline computation """
actions = actions_power[:, :, 0] # the channel_selection_part
power_selection = actions_power[:, :, 1] # power selection
# ------------ Compute V2I rate --------------------
V2I_Rate = np.zeros(self.n_RB)
V2I_Interference = np.zeros(self.n_RB) # V2I interference
for i in range(len(self.vehicles)):
for j in range(self.n_neighbor):
if not self.active_links_rand[i, j]:
continue
V2I_Interference[actions[i][j]] += 10 ** ((self.V2V_power_dB_List[power_selection[i, j]] - self.V2I_channels_with_fastfading[i, actions[i, j]]
+ self.vehAntGain + self.bsAntGain - self.bsNoiseFigure) / 10)
self.V2I_Interference_random = V2I_Interference + self.sig2
V2I_Signals = 10 ** ((self.V2I_power_dB - self.V2I_channels_with_fastfading.diagonal() + self.vehAntGain + self.bsAntGain - self.bsNoiseFigure) / 10)
V2I_Rate = np.log2(1 + np.divide(V2I_Signals, self.V2I_Interference_random))
# ------------ Compute V2V rate -------------------------
V2V_Interference = np.zeros((len(self.vehicles), self.n_neighbor))
V2V_Signal = np.zeros((len(self.vehicles), self.n_neighbor))
actions[(np.logical_not(self.active_links_rand))] = -1
for i in range(self.n_RB): # scanning all bands
indexes = np.argwhere(actions == i) # find spectrum-sharing V2Vs
for j in range(len(indexes)):
receiver_j = self.vehicles[indexes[j, 0]].destinations[indexes[j, 1]]
V2V_Signal[indexes[j, 0], indexes[j, 1]] = 10 ** ((self.V2V_power_dB_List[power_selection[indexes[j, 0], indexes[j, 1]]]
- self.V2V_channels_with_fastfading[indexes[j][0], receiver_j, i] + 2 * self.vehAntGain - self.vehNoiseFigure) / 10)
# V2I links interference to V2V links
V2V_Interference[indexes[j, 0], indexes[j, 1]] += 10 ** ((self.V2I_power_dB - self.V2V_channels_with_fastfading[i, receiver_j, i] + 2 * self.vehAntGain - self.vehNoiseFigure) / 10)
# V2V interference
for k in range(j + 1, len(indexes)): # spectrum-sharing V2Vs
receiver_k = self.vehicles[indexes[k][0]].destinations[indexes[k][1]]
V2V_Interference[indexes[j, 0], indexes[j, 1]] += 10 ** ((self.V2V_power_dB_List[power_selection[indexes[k, 0], indexes[k, 1]]]
- self.V2V_channels_with_fastfading[indexes[k][0]][receiver_j][i] + 2 * self.vehAntGain - self.vehNoiseFigure) / 10)
V2V_Interference[indexes[k, 0], indexes[k, 1]] += 10 ** ((self.V2V_power_dB_List[power_selection[indexes[j, 0], indexes[j, 1]]]
- self.V2V_channels_with_fastfading[indexes[j][0]][receiver_k][i] + 2 * self.vehAntGain - self.vehNoiseFigure) / 10)
self.V2V_Interference_random = V2V_Interference + self.sig2
V2V_Rate = np.log2(1 + np.divide(V2V_Signal, self.V2V_Interference_random))
self.demand_rand -= V2V_Rate * self.time_fast * self.bandwidth
self.demand_rand[self.demand_rand < 0] = 0
self.individual_time_limit_rand -= self.time_fast
self.active_links_rand[np.multiply(self.active_links_rand, self.demand_rand <= 0)] = 0 # transmission finished, turned to "inactive"
return V2I_Rate, V2V_Rate
def Compute_Performance_Reward_Test_sarl(self, actions_power):
""" for random baseline computation """
actions = actions_power[:, :, 0] # the channel_selection_part
power_selection = actions_power[:, :, 1] # power selection
# ------------ Compute V2I rate --------------------
V2I_Rate = np.zeros(self.n_RB)
V2I_Interference = np.zeros(self.n_RB) # V2I interference
for i in range(len(self.vehicles)):
for j in range(self.n_neighbor):
if not self.active_links_sarl[i, j]:
continue
V2I_Interference[actions[i][j]] += 10 ** ((self.V2V_power_dB_List[power_selection[i, j]] - self.V2I_channels_with_fastfading[i, actions[i, j]]
+ self.vehAntGain + self.bsAntGain - self.bsNoiseFigure) / 10)
V2I_Interference_sarl = V2I_Interference + self.sig2
V2I_Signals = 10 ** ((self.V2I_power_dB - self.V2I_channels_with_fastfading.diagonal() + self.vehAntGain + self.bsAntGain - self.bsNoiseFigure) / 10)
V2I_Rate = np.log2(1 + np.divide(V2I_Signals, V2I_Interference_sarl))
# ------------ Compute V2V rate -------------------------
V2V_Interference = np.zeros((len(self.vehicles), self.n_neighbor))
V2V_Signal = np.zeros((len(self.vehicles), self.n_neighbor))
actions[(np.logical_not(self.active_links_sarl))] = -1
for i in range(self.n_RB): # scanning all bands
indexes = np.argwhere(actions == i) # find spectrum-sharing V2Vs
for j in range(len(indexes)):
receiver_j = self.vehicles[indexes[j, 0]].destinations[indexes[j, 1]]
V2V_Signal[indexes[j, 0], indexes[j, 1]] = 10 ** ((self.V2V_power_dB_List[power_selection[indexes[j, 0], indexes[j, 1]]]
- self.V2V_channels_with_fastfading[indexes[j][0], receiver_j, i] + 2 * self.vehAntGain - self.vehNoiseFigure) / 10)
# V2I links interference to V2V links
V2V_Interference[indexes[j, 0], indexes[j, 1]] += 10 ** ((self.V2I_power_dB - self.V2V_channels_with_fastfading[i, receiver_j, i] + 2 * self.vehAntGain - self.vehNoiseFigure) / 10)
# V2V interference
for k in range(j + 1, len(indexes)): # spectrum-sharing V2Vs
receiver_k = self.vehicles[indexes[k][0]].destinations[indexes[k][1]]
V2V_Interference[indexes[j, 0], indexes[j, 1]] += 10 ** ((self.V2V_power_dB_List[power_selection[indexes[k, 0], indexes[k, 1]]]
- self.V2V_channels_with_fastfading[indexes[k][0]][receiver_j][i] + 2 * self.vehAntGain - self.vehNoiseFigure) / 10)
V2V_Interference[indexes[k, 0], indexes[k, 1]] += 10 ** ((self.V2V_power_dB_List[power_selection[indexes[j, 0], indexes[j, 1]]]
- self.V2V_channels_with_fastfading[indexes[j][0]][receiver_k][i] + 2 * self.vehAntGain - self.vehNoiseFigure) / 10)
V2V_Interference_sarl = V2V_Interference + self.sig2
V2V_Rate = np.log2(1 + np.divide(V2V_Signal, V2V_Interference_sarl))
self.demand_sarl -= V2V_Rate * self.time_fast * self.bandwidth
self.demand_sarl[self.demand_sarl < 0] = 0
self.individual_time_limit_sarl -= self.time_fast
self.active_links_sarl[np.multiply(self.active_links_sarl, self.demand_sarl <= 0)] = 0 # transmission finished, turned to "inactive"
return V2I_Rate, V2V_Rate
def Compute_Performance_Reward_Test_dpra(self, actions_power):
""" for centralized-V2V and V2I-only baseline computation """
actions = actions_power[:, :, 0] # the channel_selection_part
power_selection = actions_power[:, :, 1] # power selection
# ------------ Compute V2I rate --------------------
V2I_Rate = np.zeros(self.n_RB)
V2I_Interference = np.zeros(self.n_RB) # V2I interference
for i in range(len(self.vehicles)):
for j in range(self.n_neighbor):
if not self.active_links_dpra[i, j]:
continue
V2I_Interference[actions[i][j]] += 10 ** ((self.V2V_power_dB_List[power_selection[i, j]] - self.V2I_channels_with_fastfading[i, actions[i, j]]
+ self.vehAntGain + self.bsAntGain - self.bsNoiseFigure) / 10)
self.V2I_Interference_dpra = V2I_Interference + self.sig2
V2I_Signals = 10 ** ((self.V2I_power_dB - self.V2I_channels_with_fastfading.diagonal() + self.vehAntGain + self.bsAntGain - self.bsNoiseFigure) / 10)
V2I_Rate = np.log2(1 + np.divide(V2I_Signals, self.V2I_Interference_dpra))
# ------------ Compute V2V rate -------------------------
V2V_Interference = np.zeros((len(self.vehicles), self.n_neighbor))
V2V_Signal = np.zeros((len(self.vehicles), self.n_neighbor))
actions[(np.logical_not(self.active_links_dpra))] = -1
for i in range(self.n_RB): # scanning all bands
indexes = np.argwhere(actions == i) # find spectrum-sharing V2Vs
for j in range(len(indexes)):
receiver_j = self.vehicles[indexes[j, 0]].destinations[indexes[j, 1]]
V2V_Signal[indexes[j, 0], indexes[j, 1]] = 10 ** ((self.V2V_power_dB_List[power_selection[indexes[j, 0], indexes[j, 1]]]
- self.V2V_channels_with_fastfading[indexes[j][0], receiver_j, i] + 2 * self.vehAntGain - self.vehNoiseFigure) / 10)
# V2I links interference to V2V links
V2V_Interference[indexes[j, 0], indexes[j, 1]] += 10 ** ((self.V2I_power_dB - self.V2V_channels_with_fastfading[i, receiver_j, i] + 2 * self.vehAntGain - self.vehNoiseFigure) / 10)
# V2V interference
for k in range(j + 1, len(indexes)): # spectrum-sharing V2Vs
receiver_k = self.vehicles[indexes[k][0]].destinations[indexes[k][1]]
V2V_Interference[indexes[j, 0], indexes[j, 1]] += 10 ** ((self.V2V_power_dB_List[power_selection[indexes[k, 0], indexes[k, 1]]]
- self.V2V_channels_with_fastfading[indexes[k][0]][receiver_j][i] + 2 * self.vehAntGain - self.vehNoiseFigure) / 10)
V2V_Interference[indexes[k, 0], indexes[k, 1]] += 10 ** ((self.V2V_power_dB_List[power_selection[indexes[j, 0], indexes[j, 1]]]
- self.V2V_channels_with_fastfading[indexes[j][0]][receiver_k][i] + 2 * self.vehAntGain - self.vehNoiseFigure) / 10)
self.V2V_Interference_dpra = V2V_Interference + self.sig2
V2V_Rate = np.log2(1 + np.divide(V2V_Signal, self.V2V_Interference_dpra))
self.demand_dpra -= V2V_Rate * self.time_fast * self.bandwidth
self.demand_dpra[self.demand_dpra < 0] = 0
self.individual_time_limit_dpra -= self.time_fast
self.active_links_dpra[np.multiply(self.active_links_dpra, self.demand_dpra <= 0)] = 0 # transmission finished, turned to "inactive"
return V2I_Rate, V2V_Rate
def Compute_Rate(self, actions_power):
""" Compute V2I and V2V rates for centralized maxV2V """
actions = actions_power[:, :, 0] # the channel_selection_part
power_selection = actions_power[:, :, 1] # power selection
# ------------ Compute V2I rate --------------------
V2I_Interference = np.zeros(self.n_RB) # V2I interference
for i in range(len(self.vehicles)):
for j in range(self.n_neighbor):
if not self.active_links_dpra[i, j]:
continue
V2I_Interference[actions[i][j]] += 10 ** ((self.V2V_power_dB_List[power_selection[i, j]] - self.V2I_channels_with_fastfading[i, actions[i, j]]
+ self.vehAntGain + self.bsAntGain - self.bsNoiseFigure) / 10)
V2I_Interference_dpra = V2I_Interference + self.sig2
V2I_Signals = 10 ** ((self.V2I_power_dB - self.V2I_channels_with_fastfading.diagonal() + self.vehAntGain + self.bsAntGain - self.bsNoiseFigure) / 10)
V2I_Rate = np.log2(1 + np.divide(V2I_Signals, V2I_Interference_dpra))
# ------------ Compute V2V rate -------------------------
V2V_Interference = np.zeros((len(self.vehicles), self.n_neighbor))
V2V_Signal = np.zeros((len(self.vehicles), self.n_neighbor))
actions[(np.logical_not(self.active_links_dpra))] = -1
for i in range(self.n_RB): # scanning all bands
indexes = np.argwhere(actions == i) # find spectrum-sharing V2Vs
for j in range(len(indexes)):
receiver_j = self.vehicles[indexes[j, 0]].destinations[indexes[j, 1]]
V2V_Signal[indexes[j, 0], indexes[j, 1]] = 10 ** ((self.V2V_power_dB_List[power_selection[indexes[j, 0], indexes[j, 1]]]
- self.V2V_channels_with_fastfading[indexes[j][0], receiver_j, i] + 2 * self.vehAntGain - self.vehNoiseFigure) / 10)
# V2I links interference to V2V links
V2V_Interference[indexes[j, 0], indexes[j, 1]] += 10 ** ((self.V2I_power_dB - self.V2V_channels_with_fastfading[i, receiver_j, i] + 2 * self.vehAntGain - self.vehNoiseFigure) / 10)
# V2V interference
for k in range(j + 1, len(indexes)): # spectrum-sharing V2Vs
receiver_k = self.vehicles[indexes[k][0]].destinations[indexes[k][1]]
V2V_Interference[indexes[j, 0], indexes[j, 1]] += 10 ** ((self.V2V_power_dB_List[power_selection[indexes[k, 0], indexes[k, 1]]]
- self.V2V_channels_with_fastfading[indexes[k][0]][receiver_j][i] + 2 * self.vehAntGain - self.vehNoiseFigure) / 10)
V2V_Interference[indexes[k, 0], indexes[k, 1]] += 10 ** ((self.V2V_power_dB_List[power_selection[indexes[j, 0], indexes[j, 1]]]
- self.V2V_channels_with_fastfading[indexes[j][0]][receiver_k][i] + 2 * self.vehAntGain - self.vehNoiseFigure) / 10)
V2V_Interference_dpra = V2V_Interference + self.sig2
V2V_Rate = np.log2(1 + np.divide(V2V_Signal, V2V_Interference_dpra))
return V2I_Rate, V2V_Rate
def Compute_Interference(self, actions):
V2V_Interference = np.zeros((len(self.vehicles), self.n_neighbor, self.n_RB)) + self.sig2
channel_selection = actions.copy()[:, :, 0]
power_selection = actions.copy()[:, :, 1]
channel_selection[np.logical_not(self.active_links)] = -1
# interference from V2I links
for i in range(self.n_RB):
for k in range(len(self.vehicles)):
for m in range(len(channel_selection[k, :])):
V2V_Interference[k, m, i] += 10 ** ((self.V2I_power_dB - self.V2V_channels_with_fastfading[i][self.vehicles[k].destinations[m]][i] + 2 * self.vehAntGain - self.vehNoiseFigure) / 10)
# interference from peer V2V links
for i in range(len(self.vehicles)):
for j in range(len(channel_selection[i, :])):
for k in range(len(self.vehicles)):
for m in range(len(channel_selection[k, :])):
# if i == k or channel_selection[i,j] >= 0:
if i == k and j == m or channel_selection[i, j] < 0:
continue
V2V_Interference[k, m, channel_selection[i, j]] += 10 ** ((self.V2V_power_dB_List[power_selection[i, j]]
- self.V2V_channels_with_fastfading[i][self.vehicles[k].destinations[m]][channel_selection[i,j]] + 2 * self.vehAntGain - self.vehNoiseFigure) / 10)
self.V2V_Interference_all = 10 * np.log10(V2V_Interference)
def Compute_Interference_sarl(self, actions):
V2V_Interference = np.zeros((len(self.vehicles), self.n_neighbor, self.n_RB)) + self.sig2
channel_selection = actions.copy()[:, :, 0]
power_selection = actions.copy()[:, :, 1]
channel_selection[np.logical_not(self.active_links_sarl)] = -1
# interference from V2I links
for i in range(self.n_RB):
for k in range(len(self.vehicles)):
for m in range(len(channel_selection[k, :])):
V2V_Interference[k, m, i] += 10 ** ((self.V2I_power_dB - self.V2V_channels_with_fastfading[i][self.vehicles[k].destinations[m]][i] + 2 * self.vehAntGain - self.vehNoiseFigure) / 10)
# interference from peer V2V links
for i in range(len(self.vehicles)):
for j in range(len(channel_selection[i, :])):
for k in range(len(self.vehicles)):
for m in range(len(channel_selection[k, :])):
# if i == k or channel_selection[i,j] >= 0:
if i == k and j == m or channel_selection[i, j] < 0:
continue
V2V_Interference[k, m, channel_selection[i, j]] += 10 ** ((self.V2V_power_dB_List[power_selection[i, j]]
- self.V2V_channels_with_fastfading[i][self.vehicles[k].destinations[m]][channel_selection[i,j]] + 2 * self.vehAntGain - self.vehNoiseFigure) / 10)
self.V2V_Interference_all_sarl = 10 * np.log10(V2V_Interference)
def Compute_Interference_dpra(self, actions):
V2V_Interference = np.zeros((len(self.vehicles), self.n_neighbor, self.n_RB)) + self.sig2
channel_selection = actions.copy()[:, :, 0]
power_selection = actions.copy()[:, :, 1]
channel_selection[np.logical_not(self.active_links_sarl)] = -1
# interference from V2I links
for i in range(self.n_RB):
for k in range(len(self.vehicles)):
for m in range(len(channel_selection[k, :])):
V2V_Interference[k, m, i] += 10 ** ((self.V2I_power_dB - self.V2V_channels_with_fastfading[i][self.vehicles[k].destinations[m]][i] + 2 * self.vehAntGain - self.vehNoiseFigure) / 10)
# interference from peer V2V links
for i in range(len(self.vehicles)):
for j in range(len(channel_selection[i, :])):
for k in range(len(self.vehicles)):
for m in range(len(channel_selection[k, :])):
# if i == k or channel_selection[i,j] >= 0:
if i == k and j == m or channel_selection[i, j] < 0:
continue
V2V_Interference[k, m, channel_selection[i, j]] += 10 ** ((self.V2V_power_dB_List[power_selection[i, j]]
- self.V2V_channels_with_fastfading[i][self.vehicles[k].destinations[m]][channel_selection[i,j]] + 2 * self.vehAntGain - self.vehNoiseFigure) / 10)
self.V2V_Interference_all_dpra = 10 * np.log10(V2V_Interference)
def act_for_training(self, actions):
action_temp = actions.copy()
V2I_Rate, V2V_Rate, reward_elements = self.Compute_Performance_Reward_Train(action_temp)
lambdda = 0.1
reward = lambdda * np.sum(V2I_Rate) / (self.n_Veh * 10) + (1 - lambdda) * np.sum(reward_elements) / (self.n_Veh * self.n_neighbor)
# reward = lambdda * np.sum(V2I_Rate)/(self.n_Veh*10) + (1-lambdda)*np.sum(V2V_Rate)/(self.n_Veh*self.n_neighbor*5)
return reward
def act_for_testing(self, actions):
action_temp = actions.copy()
V2I_Rate, V2V_Rate, reward_elements = self.Compute_Performance_Reward_Train(action_temp)
V2V_success = 1 - np.sum(self.active_links) / (self.n_Veh * self.n_neighbor) # V2V success rates
return V2I_Rate, V2V_success, V2V_Rate
def act_for_testing_rand(self, actions):
action_temp = actions.copy()
V2I_Rate, V2V_Rate = self.Compute_Performance_Reward_Test_rand(action_temp)
V2V_success = 1 - np.sum(self.active_links_rand) / (self.n_Veh * self.n_neighbor) # V2V success rates
return V2I_Rate, V2V_success, V2V_Rate
def act_for_testing_sarl(self, actions):
action_temp = actions.copy()
V2I_Rate, V2V_Rate = self.Compute_Performance_Reward_Test_sarl(action_temp)
V2V_success = 1 - np.sum(self.active_links_sarl) / (self.n_Veh * self.n_neighbor) # V2V success rates
return V2I_Rate, V2V_success, V2V_Rate
def act_for_testing_dpra(self, actions):
action_temp = actions.copy()
V2I_Rate, V2V_Rate = self.Compute_Performance_Reward_Test_dpra(action_temp)
V2V_success = 1 - np.sum(self.active_links_dpra) / (self.n_Veh * self.n_neighbor) # V2V success rates
return V2I_Rate, V2V_success, V2V_Rate
def new_random_game(self, n_Veh=0):
# make a new game
self.vehicles = []
if n_Veh > 0:
self.n_Veh = n_Veh
self.add_new_vehicles_by_number(int(self.n_Veh / 4))
self.renew_neighbor()
self.renew_channel()
self.renew_channels_fastfading()
self.demand = self.demand_size * np.ones((self.n_Veh, self.n_neighbor))
self.individual_time_limit = self.time_slow * np.ones((self.n_Veh, self.n_neighbor))
self.active_links = np.ones((self.n_Veh, self.n_neighbor), dtype='bool')
# random baseline
self.demand_rand = self.demand_size * np.ones((self.n_Veh, self.n_neighbor))
self.individual_time_limit_rand = self.time_slow * np.ones((self.n_Veh, self.n_neighbor))
self.active_links_rand = np.ones((self.n_Veh, self.n_neighbor), dtype='bool')
# sarl
self.demand_sarl = self.demand_size * np.ones((self.n_Veh, self.n_neighbor))
self.individual_time_limit_sarl = self.time_slow * np.ones((self.n_Veh, self.n_neighbor))
self.active_links_sarl = np.ones((self.n_Veh, self.n_neighbor), dtype='bool')
# DPRA
self.demand_dpra = self.demand_size * np.ones((self.n_Veh, self.n_neighbor))
self.individual_time_limit_dpra = self.time_slow * np.ones((self.n_Veh, self.n_neighbor))
self.active_links_dpra = np.ones((self.n_Veh, self.n_neighbor), dtype='bool')