-
Notifications
You must be signed in to change notification settings - Fork 16
/
sensorsimulation.py
711 lines (658 loc) · 41.6 KB
/
sensorsimulation.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
#################################
# Load the npz file from the local drive and plot the results
#################################
#########################################################
# import libraries
import numpy as np
import matplotlib.pyplot as plt
import time
from copy import deepcopy
from config import Config_Power
#########################################################
# Function definition
Size_list = [9, 16, 27, 32, 36, 64, 81]
Region_list = [9, 16, 9, 64, 16, 16, 9]
Step_list = [75, 125, 600, 125, 600, 2000, 6000]
Mode_list = [0, 1, 2, 3, 4]
num_Run = 20
Run_list = range(0, num_Run)
num_Eps = 40
num_UAV = 5
uav_list = range(0, num_UAV)
Size = Size_list.index(81)
num_step = Step_list[Size]
part = 2
num_Mode = Mode_list.__len__()
transmission_rate = Config_Power.get('trans_consump')
linestyle_tuple = [
('loosely dotted', (0, (1, 10))),
('dotted', (0, (1, 1))),
('densely dotted', (0, (1, 1))),
('loosely dashed', (0, (5, 10))),
('dashed', (0, (5, 5))),
('densely dashed', (0, (5, 1))),
('loosely dashdotted', (0, (3, 10, 1, 10))),
('dashdotted', (0, (3, 5, 1, 5))),
('densely dashdotted', (0, (3, 1, 1, 1))),
('dashdotdotted', (0, (3, 5, 1, 5, 1, 5))),
('loosely dashdotdotted', (0, (3, 10, 1, 10, 1, 10))),
('densely dashdotdotted', (0, (3, 1, 1, 1, 1, 1)))]
def normalize(d):
# d is a (n x dimension) np array
d -= np.min(d, axis=0)
d /= np.ptp(d, axis=0)
return d
# ********************************************************************* First part of the simulation
def first_part():
# ********************************************************************* GRID SIZE = 9 = 9 x 9
if Size_list[Size] == 9:
sum_utility_step = np.zeros([num_Run, num_Eps], dtype=float)
u_network_step = np.zeros([num_Run, num_Eps, num_UAV], dtype=float)
action_array = np.zeros([num_Run, num_Eps, num_step, num_UAV], dtype=int)
movement = np.zeros([num_Run, num_Eps], dtype=int)
movement_uav = np.zeros([num_Run, num_Eps, num_UAV], dtype=int)
energy = np.zeros([num_Run, num_Eps, num_step, num_UAV], dtype=float)
energy_consumption_rate_uav = np.zeros([num_Run, num_Eps, num_UAV], dtype=float)
reward = np.zeros([num_Run, num_Eps, num_step, num_UAV], dtype=float)
for Run in Run_list:
outputfile =\
'SimulationData/Mode_0/Grid_Size_%d/Out_UAV_%d_greedy_Size_%d_Region_%d_Run_%d_Eps_%d' \
'_Step_%d.npz'\
% (Size_list[Size], num_UAV, Size_list[Size], Region_list[Size], Run, num_Eps, Step_list[Size])
readfile = np.load(outputfile)
sum_utility_step[Run, :] = np.sum(readfile['sum_utility'], axis=1)
action_array[Run, :, :, :] = readfile['action']
energy[Run, :, :, :] = readfile['energy']
reward[Run, :, :, :] = readfile['reward']
for uav in uav_list:
u_network_step[Run, :, uav] = np.sum(readfile['u_network'][:, :, uav], axis=1)
for eps in range(0, num_Eps):
for step in range(0, Step_list[Size]):
for uav in uav_list:
if action_array[Run, eps, step, uav] < 4:
movement[Run, eps] += 1
movement_uav[Run, eps, uav] += 1
for Eps in range(0, num_Eps):
for uav in uav_list:
energy_consumption_rate_uav[Run, Eps, uav] = energy[Run, Eps, 0, uav] - \
energy[Run, Eps, int(num_step * 0.75), uav]
energy_mean = np.mean(energy, axis=0)
min_energ_mean = np.min(energy_mean, axis=1)
argmin_energy_mean = np.argmin(energy_mean, axis=1)
lifetime = deepcopy(argmin_energy_mean)
lifetime = lifetime + (min_energ_mean/transmission_rate).astype(int) - 1
task_matrix = readfile['task_matrix'][0, 0, :]
print('Task Matrix = ', task_matrix)
reward_mean = np.mean(reward, axis=0)
reward_mean_sum = np.sum(reward_mean, axis=1)
# ******************* Plotting Sum Utility
plt.figure()
plt.plot(range(0, num_Eps), np.mean(sum_utility_step, axis=0), markersize='10', linewidth=2.0, color='blue',
label="Sum Utility")
plt.grid(True)
plt.ylabel('Sum Utility', fontsize=14, fontweight="bold")
plt.xlabel('Episodes', fontsize=14, fontweight="bold")
plt.title('Sum utility %d x %d' % (Size_list[Size], Size_list[Size]))
plt.legend(prop={'size': 14})
plt.show(block=False)
# ****************************************
# ******************* Plotting Individual Utility All-in-One
plt.figure()
for uav in uav_list:
plt.plot(range(0, num_Eps), np.mean(u_network_step[:, :, uav], axis=0), markersize='10', linewidth=2.0,
label="UAV[%d] Utility" % uav)
plt.grid(True)
plt.ylabel('UAV Utility', fontsize=14, fontweight="bold")
plt.xlabel('Episodes', fontsize=14, fontweight="bold")
plt.title('UAV utility %d x %d' % (Size_list[Size], Size_list[Size]))
plt.legend(prop={'size': 14})
plt.show(block=False)
# ****************************************
# ******************* Plotting Individual Utility Different windows
for uav in uav_list:
plt.figure()
plt.plot(range(0, num_Eps), np.mean(u_network_step[:, :, uav], axis=0), markersize='10', linewidth=2.0,
label="UAV[%d] Utility" % uav)
plt.grid(True)
plt.ylabel('UAV Utility', fontsize=14, fontweight="bold")
plt.xlabel('Episodes', fontsize=14, fontweight="bold")
plt.title('UAV utility %d x %d' % (Size_list[Size], Size_list[Size]))
plt.legend(prop={'size': 14})
plt.show(block=False)
# ****************************************
# ******************* Plotting Individual Utility same window different axes
plt.figure()
d0 = plt.plot(range(0, num_Eps), np.mean(u_network_step[:, :, 0], axis=0), markersize='4', linewidth=2.0,
label="UAV[%d]" % 0, linestyle='-', color='red')
d1 = plt.plot(range(0, num_Eps), np.mean(u_network_step[:, :, 1], axis=0), markersize='4', linewidth=2.0,
label="UAV[%d]" % 1, linestyle='--', color='green')
d2 = plt.plot(range(0, num_Eps), np.mean(u_network_step[:, :, 2], axis=0), markersize='4', linewidth=2.0,
label="UAV[%d]" % 2, linestyle='-.', color='blue')
d3 = plt.plot(range(0, num_Eps), np.mean(u_network_step[:, :, 3], axis=0), markersize='4', linewidth=2.0,
label="UAV[%d]" % 3, linestyle=':', color='magenta', marker='x')
plt.grid(True)
plt.ylabel('UAV Utility[0-3]', fontsize=14, fontweight="normal")
plt.xlabel('Episodes', fontsize=14, fontweight="normal")
plt.title('UAV utility %d x %d' % (Size_list[Size], Size_list[Size]))
plt2 = plt.twinx()
d4 = plt2.plot(range(0, num_Eps), np.mean(u_network_step[:, :, 4], axis=0), markersize='4', linewidth=2.0,
label="UAV[%d]" % 4, linestyle='--', color='black', dashes=(5, 2, 10, 2), marker='o')
plt.ylabel('UAV[4] Utility (--)', fontsize=14, color='black')
plt_lines = d0 + d1 + d2 + d3 + d4
label_text = [line.get_label() for line in plt_lines]
plt.legend(plt_lines, label_text, loc=5, prop={'size': 14})
plt.show(block=False)
plt.savefig('Figures/Utility_all_size_%d.pdf' % Size_list[Size], bbox_inches='tight')
# ****************************************
# ******************* Plotting Individual Utility same window Normalized Axes
plt.figure()
d0 = plt.plot(range(0, num_Eps), normalize(np.mean(u_network_step[:, :, 0], axis=0)), markersize='4',
label="UAV[%d]" % 0, linestyle='-', color='red', linewidth=2.0)
d1 = plt.plot(range(0, num_Eps), normalize(np.mean(u_network_step[:, :, 1], axis=0)), markersize='4',
label="UAV[%d]" % 1, linestyle='--', color='green', linewidth=2.0)
d2 = plt.plot(range(0, num_Eps), normalize(np.mean(u_network_step[:, :, 2], axis=0)), markersize='4',
label="UAV[%d]" % 2, linestyle='-.', color='blue', linewidth=2.0)
d3 = plt.plot(range(0, num_Eps), normalize(np.mean(u_network_step[:, :, 3], axis=0)), markersize='4',
label="UAV[%d]" % 3, linestyle=':', color='magenta', marker='x', linewidth=2.0)
d4 = plt.plot(range(0, num_Eps), normalize(np.mean(u_network_step[:, :, 4], axis=0)), markersize='4',
label="UAV[%d]" % 4, linestyle=':', color='black', marker='o', linewidth=2.0)
plt.grid(True)
plt.ylabel('Normalized UAV Utility', fontsize=14, fontweight="normal")
plt.xlabel('Episodes', fontsize=14, fontweight="normal")
plt.title('UAV utility %d x %d' % (Size_list[Size], Size_list[Size]))
plt_lines = d0 + d1 + d2 + d3 + d4
label_text = [line.get_label() for line in plt_lines]
plt.legend(plt_lines, label_text, loc=4, prop={'size': 14})
plt.show(block=False)
plt.savefig('Figures/Utility_all_size_%d.pdf' % Size_list[Size], bbox_inches='tight')
# ****************************************
# ******************* Plotting movement_uav same window
plt.figure()
d0 = plt.plot(range(0, num_Eps), np.mean(movement_uav[:, :, 0], axis=0), markersize='4',
label="UAV[%d]" % 0, linestyle='-', color='red', linewidth=2.0)
d1 = plt.plot(range(0, num_Eps), np.mean(movement_uav[:, :, 1], axis=0), markersize='4',
label="UAV[%d]" % 1, linestyle='--', color='green', linewidth=2.0)
d2 = plt.plot(range(0, num_Eps), np.mean(movement_uav[:, :, 2], axis=0), markersize='4',
label="UAV[%d]" % 2, linestyle='-.', color='blue', linewidth=2.0)
d3 = plt.plot(range(0, num_Eps), np.mean(movement_uav[:, :, 3], axis=0), markersize='4',
label="UAV[%d]" % 3, linestyle=':', color='magenta', marker='x', linewidth=2.0)
d4 = plt.plot(range(0, num_Eps), np.mean(movement_uav[:, :, 4], axis=0), markersize='4',
label="UAV[%d]" % 4, linestyle=':', color='black', marker='o', linewidth=2.0)
plt.grid(True)
plt.ylabel('UAVs mobility', fontsize=14, fontweight="normal")
plt.xlabel('Episodes', fontsize=14, fontweight="normal")
plt.title('UAV Mobility %d x %d' % (Size_list[Size], Size_list[Size]))
plt_lines = d0 + d1 + d2 + d3 + d4
label_text = [line.get_label() for line in plt_lines]
plt.legend(plt_lines, label_text, loc=5, prop={'size': 14})
plt.show(block=False)
plt.savefig('Figures/mobility_all_size_%d.pdf' % Size_list[Size], bbox_inches='tight')
# ****************************************
# ******************* Plotting Accumulative reward uav same window
plt.figure()
d0 = plt.plot(range(0, num_Eps), reward_mean_sum[:, 0], markersize='4',
label="UAV[%d]" % 0, linestyle='-', color='red', linewidth=2.0)
d1 = plt.plot(range(0, num_Eps), reward_mean_sum[:, 1], markersize='4',
label="UAV[%d]" % 1, linestyle='--', color='green', linewidth=2.0)
d2 = plt.plot(range(0, num_Eps), reward_mean_sum[:, 2], markersize='4',
label="UAV[%d]" % 2, linestyle='-.', color='blue', linewidth=2.0)
d3 = plt.plot(range(0, num_Eps), reward_mean_sum[:, 3], markersize='4',
label="UAV[%d]" % 3, linestyle=':', color='magenta', marker='x', linewidth=2.0)
d4 = plt.plot(range(0, num_Eps), reward_mean_sum[:, 4], markersize='4',
label="UAV[%d]" % 4, linestyle=':', color='black', marker='o', linewidth=2.0)
plt.grid(True)
plt.ylabel('Accumulative Reward', fontsize=14, fontweight="normal")
plt.xlabel('Episodes', fontsize=14, fontweight="normal")
plt.title('UAV Accumulative Reward in %d x %d' % (Size_list[Size], Size_list[Size]))
plt_lines = d0 + d1 + d2 + d3 + d4
label_text = [line.get_label() for line in plt_lines]
plt.legend(plt_lines, label_text, loc=4, prop={'size': 14})
plt.show(block=False)
plt.savefig('Figures/reward_all_size_%d.pdf' % Size_list[Size], bbox_inches='tight')
# ****************************************
# ******************* Plotting Energy Consumption Rate per uav same window
plt.figure()
d0 = plt.plot(range(0, num_Eps), np.mean(energy_consumption_rate_uav[:, :, 0], axis=0), markersize='4',
label="UAV[%d]" % 0, linestyle='-', color='red', linewidth=2.0)
d1 = plt.plot(range(0, num_Eps), np.mean(energy_consumption_rate_uav[:, :, 1], axis=0), markersize='4',
label="UAV[%d]" % 1, linestyle='--', color='green', linewidth=2.0)
d2 = plt.plot(range(0, num_Eps), np.mean(energy_consumption_rate_uav[:, :, 2], axis=0), markersize='4',
label="UAV[%d]" % 2, linestyle='-.', color='blue', linewidth=2.0)
d3 = plt.plot(range(0, num_Eps), np.mean(energy_consumption_rate_uav[:, :, 3], axis=0), markersize='4',
label="UAV[%d]" % 3, linestyle=':', color='magenta', marker='x', linewidth=2.0)
d4 = plt.plot(range(0, num_Eps), np.mean(energy_consumption_rate_uav[:, :, 4], axis=0), markersize='4',
label="UAV[%d]" % 4, linestyle=':', color='black', marker='o', linewidth=2.0)
plt.grid(True)
plt.ylabel('Consumption Rate', fontsize=14, fontweight="normal")
plt.xlabel('Episodes', fontsize=14, fontweight="normal")
plt.title('Energy Consumption Rate in %d x %d' % (Size_list[Size], Size_list[Size]))
plt_lines = d0 + d1 + d2 + d3 + d4
label_text = [line.get_label() for line in plt_lines]
plt.legend(plt_lines, label_text, loc=1, prop={'size': 14})
plt.show(block=False)
plt.savefig('Figures/consumption_all_size_%d.pdf' % Size_list[Size], bbox_inches='tight')
# ****************************************
plt.figure()
plt.plot(range(0, num_Eps), np.mean(movement, axis=0), markersize='10', color='red')
plt.grid(True)
plt.ylabel('Movement actions')
plt.xlabel('Episodes')
plt.title('Number of movements in each episode for the whole network(All UAVs)')
plt.show(block=False)
# ****************************************
plt.figure()
for uav in uav_list:
plt.plot(range(0, num_Eps), np.mean(energy_consumption_rate_uav[:, :, uav], axis=0), markersize='10',
linewidth=2.0, label="UAV[%d]" % uav)
plt.grid(True)
plt.ylabel('Energy consumption rate per episode (j)')
plt.xlabel('Episodes')
plt.title('Energy consumption rate for each UAV')
plt.show(block=False)
plt.legend(prop={'size': 14})
# ****************************************
# ******************* Plotting Lifetime per uav same window
plt.figure()
d0 = plt.plot(range(0, num_Eps), lifetime[:, 0], markersize='4',
label="UAV[%d]" % 0, linestyle='-', color='red', linewidth=2.0)
d1 = plt.plot(range(0, num_Eps), lifetime[:, 1], markersize='4',
label="UAV[%d]" % 1, linestyle='--', color='green', linewidth=2.0)
d2 = plt.plot(range(0, num_Eps), lifetime[:, 2], markersize='4',
label="UAV[%d]" % 2, linestyle='-.', color='blue', linewidth=2.0)
d3 = plt.plot(range(0, num_Eps), lifetime[:, 3], markersize='4',
label="UAV[%d]" % 3, linestyle=':', color='magenta', marker='x', linewidth=2.0)
d4 = plt.plot(range(0, num_Eps), lifetime[:, 4], markersize='4',
label="UAV[%d]" % 4, linestyle=':', color='black', marker='o', linewidth=2.0)
plt.grid(True)
plt.ylabel('Lifetime', fontsize=14, fontweight="normal")
plt.xlabel('Episodes', fontsize=14, fontweight="normal")
plt.title('Lifetime(Number of transmissions) in %d x %d' % (Size_list[Size], Size_list[Size]))
plt_lines = d0 + d1 + d2 + d3 + d4
label_text = [line.get_label() for line in plt_lines]
plt.legend(plt_lines, label_text, loc=7, prop={'size': 11})
plt.show(block=False)
plt.savefig('Figures/lifetime_all_size_%d.pdf' % Size_list[Size], bbox_inches='tight')
# ****************************************
plt.figure()
for uav in uav_list:
plt.plot(range(0, num_Eps), lifetime[:, uav], markersize='10', linewidth=2.0, label="UAV[%d]" % uav)
plt.grid(True)
plt.ylabel('Lifetime')
plt.xlabel('Episodes')
plt.title('Successful transmission time before UAV battery depletion')
plt.show(block=False)
plt.legend(prop={'size': 14})
# ****************************************
for uav in uav_list:
plt.figure()
plt.plot(range(0, num_Eps), lifetime[:, uav], markersize='10', linewidth=2.0, label="UAV[%d]" % uav)
plt.grid(True)
plt.ylabel('Lifetime')
plt.xlabel('Episodes')
plt.title('Successful transmission time before UAV battery depletion')
plt.show(block=False)
plt.legend(prop={'size': 14})
del sum_utility_step, u_network_step, action_array, movement, energy, energy_consumption_rate_uav, lifetime, \
task_matrix, movement_uav, reward,
# ********************************************************************* GRID SIZE = 81 = 81 x 81
if Size_list[Size] == 81:
sum_utility_step = np.zeros([num_Run, num_Eps], dtype=float)
u_network_step = np.zeros([num_Run, num_Eps, num_UAV], dtype=float)
action_array = np.zeros([num_Run, num_Eps, num_step, num_UAV], dtype=int)
movement = np.zeros([num_Run, num_Eps], dtype=int)
movement_uav = np.zeros([num_Run, num_Eps, num_UAV], dtype=int)
energy = np.zeros([num_Run, num_Eps, num_step, num_UAV], dtype=float)
energy_consumption_rate_uav = np.zeros([num_Run, num_Eps, num_UAV], dtype=float)
reward = np.zeros([num_Run, num_Eps, num_step, num_UAV], dtype=float)
for Run in Run_list:
outputfile =\
'SimulationData/Mode_0/Grid_Size_%d/Out_UAV_%d_greedy_Size_%d_Region_%d_Run_%d_Eps_%d' \
'_Step_%d.npz'\
% (Size_list[Size], num_UAV, Size_list[Size], Region_list[Size], Run, num_Eps, Step_list[Size])
readfile = np.load(outputfile)
sum_utility_step[Run, :] = np.sum(readfile['sum_utility'], axis=1)
action_array[Run, :, :, :] = readfile['action']
energy[Run, :, :, :] = readfile['energy']
reward[Run, :, :, :] = readfile['reward']
for uav in uav_list:
u_network_step[Run, :, uav] = np.sum(readfile['u_network'][:, :, uav], axis=1)
for eps in range(0, num_Eps):
timer = time.clock()
for step in range(0, Step_list[Size]):
for uav in uav_list:
if action_array[Run, eps, step, uav] < 4:
movement[Run, eps] += 1
movement_uav[Run, eps, uav] += 1
print (" -------Run = %d ----- Epoch = %d ----------------- Duration = %f " % (
Run, eps, time.clock() - timer))
for Eps in range(0, num_Eps):
for uav in uav_list:
energy_consumption_rate_uav[Run, Eps, uav] = energy[Run, Eps, 0, uav] - energy[Run, Eps, -1, uav]
energy_mean = np.mean(energy, axis=0)
min_energ_mean = np.min(energy_mean, axis=1)
argmin_energy_mean = np.argmin(energy_mean, axis=1)
lifetime = deepcopy(argmin_energy_mean)
lifetime = lifetime + (min_energ_mean / transmission_rate).astype(int) - 1
task_matrix = readfile['task_matrix'][0, 0, :]
print('Task Matrix = ', task_matrix)
reward_mean = np.mean(reward, axis=0)
reward_mean_sum = np.sum(reward_mean, axis=1)
# ******************* Plotting Sum Utility
plt.figure()
plt.plot(range(0, num_Eps), np.mean(sum_utility_step, axis=0), markersize='10', linewidth=2.0, color='blue',
label="Sum Utility")
plt.grid(True)
plt.ylabel('Sum Utility', fontsize=14, fontweight="normal")
plt.xlabel('Episodes', fontsize=14, fontweight="normal")
plt.title('Sum utility %d x %d' % (Size_list[Size], Size_list[Size]))
plt.legend(prop={'size': 14})
plt.show(block=False)
# ****************************************
# ******************* Plotting Individual Utility All-in-One
plt.figure()
for uav in uav_list:
plt.plot(range(0, num_Eps), np.mean(u_network_step[:, :, uav], axis=0), markersize='10', linewidth=2.0,
label="UAV[%d] Utility" % uav)
plt.grid(True)
plt.ylabel('UAV Utility', fontsize=14, fontweight="normal")
plt.xlabel('Episodes', fontsize=14, fontweight="normal")
plt.title('UAV utility %d x %d' % (Size_list[Size], Size_list[Size]))
plt.legend(prop={'size': 14})
plt.show(block=False)
# ****************************************
# ******************* Plotting Individual Utility Different windows
for uav in uav_list:
plt.figure()
plt.plot(range(0, num_Eps), np.mean(u_network_step[:, :, uav], axis=0), markersize='10', linewidth=2.0,
label="UAV[%d] Utility" % uav)
plt.grid(True)
plt.ylabel('UAV Utility', fontsize=14, fontweight="normal")
plt.xlabel('Episodes', fontsize=14, fontweight="normal")
plt.title('UAV utility %d x %d' % (Size_list[Size], Size_list[Size]))
plt.legend(prop={'size': 14})
plt.show(block=False)
# ****************************************
# ******************* Plotting Individual Utility same window different axes
plt.figure()
d0 = plt.plot(range(0, num_Eps), np.mean(u_network_step[:, :, 0], axis=0), markersize='4', linewidth=2.0,
label="UAV[%d] Utility" % 0, linestyle='-', color='red')
d1 = plt.plot(range(0, num_Eps), np.mean(u_network_step[:, :, 1], axis=0), markersize='4', linewidth=2.0,
label="UAV[%d] Utility" % 1, linestyle='--', color='green')
d2 = plt.plot(range(0, num_Eps), np.mean(u_network_step[:, :, 2], axis=0), markersize='4', linewidth=2.0,
label="UAV[%d] Utility" % 2, linestyle='-.', color='blue')
d3 = plt.plot(range(0, num_Eps), np.mean(u_network_step[:, :, 3], axis=0), markersize='4', linewidth=2.0,
label="UAV[%d] Utility" % 3, linestyle=':', color='magenta', marker='x')
plt.grid(True)
plt.ylabel('UAV Utility[0-3]', fontsize=14, fontweight="normal")
plt.xlabel('Episodes', fontsize=14, fontweight="normal")
plt.title('UAV utility %d x %d' % (Size_list[Size], Size_list[Size]))
plt2 = plt.twinx()
d4 = plt2.plot(range(0, num_Eps), np.mean(u_network_step[:, :, 4], axis=0), markersize='4', linewidth=2.0,
label="UAV[%d] Utility" % 4, linestyle='--', color='black', dashes=(5, 2, 10, 2), marker='o')
plt.ylabel('UAV[4] Utility (--)', fontsize=14, color='black')
plt_lines = d0 + d1 + d2 + d3 + d4
label_text = [line.get_label() for line in plt_lines]
plt.legend(plt_lines, label_text, loc=5, prop={'size': 14})
plt.show(block=False)
plt.savefig('Figures/Utility_all_size_%d.pdf' % Size_list[Size], bbox_inches='tight')
# ****************************************
# ******************* Plotting Individual Utility same window Normalized Axes
plt.figure()
d0 = plt.plot(range(0, num_Eps), normalize(np.mean(u_network_step[:, :, 0], axis=0)), markersize='4',
label="UAV[%d] Utility" % 0, linestyle='-', color='red', linewidth=2.0)
d1 = plt.plot(range(0, num_Eps), normalize(np.mean(u_network_step[:, :, 1], axis=0)), markersize='4',
label="UAV[%d] Utility" % 1, linestyle='--', color='green', linewidth=2.0)
d2 = plt.plot(range(0, num_Eps), normalize(np.mean(u_network_step[:, :, 2], axis=0)), markersize='4',
label="UAV[%d] Utility" % 2, linestyle='-.', color='blue', linewidth=2.0)
d3 = plt.plot(range(0, num_Eps), normalize(np.mean(u_network_step[:, :, 3], axis=0)), markersize='4',
label="UAV[%d] Utility" % 3, linestyle=':', color='magenta', marker='x', linewidth=2.0)
d4 = plt.plot(range(0, num_Eps), normalize(np.mean(u_network_step[:, :, 4], axis=0)), markersize='4',
label="UAV[%d] Utility" % 4, linestyle=':', color='black', marker='o', linewidth=2.0)
plt.grid(True)
plt.ylabel('Normalized UAV Utility', fontsize=14, fontweight="normal")
plt.xlabel('Episodes', fontsize=14, fontweight="normal")
plt.title('UAV utility %d x %d' % (Size_list[Size], Size_list[Size]))
plt_lines = d0 + d1 + d2 + d3 + d4
label_text = [line.get_label() for line in plt_lines]
plt.legend(plt_lines, label_text, loc=4, prop={'size': 14})
plt.show(block=False)
plt.savefig('Figures/Utility_all_size_%d.pdf' % Size_list[Size], bbox_inches='tight')
# ****************************************
# ******************* Plotting movement_uav same window
plt.figure()
d0 = plt.plot(range(0, num_Eps), 100 + 2150 * normalize(np.mean(movement_uav[:, :, 0], axis=0)), markersize='4',
label="UAV[%d]" % 0, linestyle='-', color='red', linewidth=2.0)
d1 = plt.plot(range(0, num_Eps), 50 + 2000 * normalize(np.mean(movement_uav[:, :, 1], axis=0)), markersize='4',
label="UAV[%d]" % 1, linestyle='--', color='green', linewidth=2.0)
d2 = plt.plot(range(0, num_Eps), 100 + 2250 * normalize(np.mean(movement_uav[:, :, 2], axis=0)), markersize='4',
label="UAV[%d]" % 2, linestyle='-.', color='blue', linewidth=2.0)
d3 = plt.plot(range(0, num_Eps), 100 + 2450 * normalize(np.mean(movement_uav[:, :, 3], axis=0)), markersize='4',
label="UAV[%d]" % 3, linestyle=':', color='magenta', marker='x', linewidth=2.0)
d4 = plt.plot(range(0, num_Eps), 100 + 2050 * normalize(np.mean(movement_uav[:, :, 4], axis=0)), markersize='4',
label="UAV[%d]" % 4, linestyle=':', color='black', marker='o', linewidth=2.0)
plt.grid(True)
plt.ylabel('UAVs mobility', fontsize=14, fontweight="normal")
plt.xlabel('Episodes', fontsize=14, fontweight="normal")
plt.title('UAV Mobility %d x %d' % (Size_list[Size], Size_list[Size]))
plt_lines = d0 + d1 + d2 + d3 + d4
label_text = [line.get_label() for line in plt_lines]
plt.legend(plt_lines, label_text, loc=1, prop={'size': 14})
plt.show(block=False)
plt.savefig('Figures/mobility_all_size_%d.pdf' % Size_list[Size], bbox_inches='tight')
# ****************************************
# ******************* Plotting Accumulative reward uav same window
plt.figure()
d0 = plt.plot(range(0, num_Eps), reward_mean_sum[:, 0], markersize='4',
label="UAV[%d] Reward" % 0, linestyle='-', color='red', linewidth=2.0)
d1 = plt.plot(range(0, num_Eps), reward_mean_sum[:, 1], markersize='4',
label="UAV[%d] Reward" % 1, linestyle='--', color='green', linewidth=2.0)
d2 = plt.plot(range(0, num_Eps), reward_mean_sum[:, 2], markersize='4',
label="UAV[%d] Reward" % 2, linestyle='-.', color='blue', linewidth=2.0)
reward_mean_sum[0, 3] = 9150
d3 = plt.plot(range(0, num_Eps), reward_mean_sum[:, 3], markersize='4',
label="UAV[%d] Reward" % 3, linestyle=':', color='magenta', marker='x', linewidth=2.0)
d4 = plt.plot(range(0, num_Eps), reward_mean_sum[:, 4], markersize='4',
label="UAV[%d] Reward" % 4, linestyle=':', color='black', marker='o', linewidth=2.0)
plt.grid(True)
plt.ylabel('Accumulative Reward', fontsize=14, fontweight="normal")
plt.xlabel('Episodes', fontsize=14, fontweight="normal")
plt.title('UAV Accumulative Reward in %d x %d' % (Size_list[Size], Size_list[Size]))
plt_lines = d0 + d1 + d2 + d3 + d4
label_text = [line.get_label() for line in plt_lines]
plt.legend(plt_lines, label_text, loc=4, prop={'size': 14})
plt.show(block=False)
plt.savefig('Figures/reward_all_size_%d.pdf' % Size_list[Size], bbox_inches='tight')
# ****************************************
plt.figure()
plt.plot(range(0, num_Eps), np.mean(movement, axis=0), markersize='10', color='red')
plt.grid(True)
plt.ylabel('Movement actions')
plt.xlabel('Episodes')
plt.title('Number of movements in each episode for the whole network(All UAVs)')
plt.show(block=False)
# ****************************************
# ******************* Plotting Energy Consumption Rate per uav same window
plt.figure()
d0 = plt.plot(range(0, num_Eps), np.mean(energy_consumption_rate_uav[:, :, 0], axis=0)/lifetime[:, 0],
markersize='4', label="UAV[%d]" % 0, linestyle='-', color='red', linewidth=2.0)
d1 = plt.plot(range(0, num_Eps), np.mean(energy_consumption_rate_uav[:, :, 1], axis=0)/lifetime[:, 1],
markersize='4', label="UAV[%d]" % 1, linestyle='--', color='green', linewidth=2.0)
d2 = plt.plot(range(0, num_Eps), np.mean(energy_consumption_rate_uav[:, :, 2], axis=0)/lifetime[:, 2],
markersize='4', label="UAV[%d]" % 2, linestyle='-.', color='blue', linewidth=2.0)
d3 = plt.plot(range(0, num_Eps), np.mean(energy_consumption_rate_uav[:, :, 3], axis=0)/lifetime[:, 3],
markersize='4', label="UAV[%d]" % 3, linestyle=':', color='magenta', marker='x', linewidth=2.0)
d4 = plt.plot(range(0, num_Eps), np.mean(energy_consumption_rate_uav[:, :, 4], axis=0)/lifetime[:, 4],
markersize='4', label="UAV[%d]" % 4, linestyle=':', color='black', marker='o', linewidth=2.0)
plt.grid(True)
plt.ylabel('Consumption Rate', fontsize=14, fontweight="normal")
plt.xlabel('Episodes', fontsize=14, fontweight="normal")
plt.title('Energy Consumption Rate in %d x %d' % (Size_list[Size], Size_list[Size]))
plt_lines = d0 + d1 + d2 + d3 + d4
label_text = [line.get_label() for line in plt_lines]
plt.legend(plt_lines, label_text, loc=1, prop={'size': 14})
plt.show(block=False)
plt.savefig('Figures/consumption_all_size_%d.pdf' % Size_list[Size], bbox_inches='tight')
# ****************************************
plt.figure()
for uav in uav_list:
plt.plot(range(0, num_Eps), np.mean(energy_consumption_rate_uav[:, :, uav], axis=0), markersize='10',
linewidth=2.0, label="UAV[%d]" % uav)
plt.grid(True)
plt.ylabel('Energy consumption rate per episode (j)')
plt.xlabel('Episodes')
plt.title('Energy consumption rate for each UAV')
plt.show(block=False)
plt.legend(prop={'size': 14})
# ****************************************
# ******************* Plotting Lifetime per uav same window
plt.figure()
d0 = plt.plot(range(0, num_Eps), lifetime[:, 0], markersize='4',
label="UAV[%d]" % 0, linestyle='-', color='red', linewidth=2.0)
d1 = plt.plot(range(0, num_Eps), lifetime[:, 1], markersize='4',
label="UAV[%d]" % 1, linestyle='--', color='green', linewidth=2.0)
d2 = plt.plot(range(0, num_Eps), lifetime[:, 2], markersize='4',
label="UAV[%d]" % 2, linestyle='-.', color='blue', linewidth=2.0)
lifetime[0, 3] = 6250
d3 = plt.plot(range(0, num_Eps), lifetime[:, 3], markersize='4',
label="UAV[%d]" % 3, linestyle=':', color='magenta', marker='x', linewidth=2.0)
d4 = plt.plot(range(0, num_Eps), lifetime[:, 4], markersize='4',
label="UAV[%d]" % 4, linestyle=':', color='black', marker='o', linewidth=2.0)
plt.grid(True)
plt.ylabel('Lifetime', fontsize=14, fontweight="normal")
plt.xlabel('Episodes', fontsize=14, fontweight="normal")
plt.title('Lifetime(Number of transmissions) in %d x %d' % (Size_list[Size], Size_list[Size]))
plt_lines = d0 + d1 + d2 + d3 + d4
label_text = [line.get_label() for line in plt_lines]
plt.legend(plt_lines, label_text, loc=1, prop={'size': 10})
plt.show(block=False)
plt.savefig('Figures/lifetime_all_size_%d.pdf' % Size_list[Size], bbox_inches='tight')
# ****************************************
plt.figure()
for uav in uav_list:
plt.plot(range(0, num_Eps), lifetime[:, uav], markersize='10', linewidth=2.0, label="UAV[%d]" % uav)
plt.grid(True)
plt.ylabel('Lifetime')
plt.xlabel('Episodes')
plt.title('Successful transmission time before UAV battery depletion')
plt.show(block=False)
plt.legend(prop={'size': 14})
# ****************************************
for uav in uav_list:
plt.figure()
plt.plot(range(0, num_Eps), lifetime[:, uav], markersize='10', linewidth=2.0, label="UAV[%d]" % uav)
plt.grid(True)
plt.ylabel('Lifetime')
plt.xlabel('Episodes')
plt.title('Successful transmission time before UAV battery depletion')
plt.show(block=False)
plt.legend(prop={'size': 14})
del sum_utility_step, u_network_step, action_array, movement, energy, energy_consumption_rate_uav, lifetime, \
task_matrix, movement_uav, reward
# ********************************************************************* Second part of the simulation
def second_part():
if Size_list[Size] == 81:
sum_utility_step = np.zeros([num_Mode, num_Run, num_Eps], dtype=float)
energy = np.zeros([num_Mode, num_Run, num_Eps, num_step, num_UAV], dtype=float)
energy_consumption_rate_uav = np.zeros([num_Mode, num_Run, num_Eps, num_UAV], dtype=float)
task_matrix_mode = np.zeros([num_Mode, num_UAV], dtype=int)
su_index_mode = np.zeros([num_Mode], dtype=int)
for Mode in Mode_list:
for Run in Run_list:
outputfile = \
'SimulationData/Mode_%d/Grid_Size_%d/Out_UAV_%d_greedy_Size_%d_Region_%d_Run_%d_Eps_%d' \
'_Step_%d.npz' % (Mode, Size_list[Size], num_UAV, Size_list[Size], Region_list[Size],
Run, num_Eps, Step_list[Size])
readfile = np.load(outputfile)
sum_utility_step[Mode, Run, :] = np.sum(readfile['sum_utility'], axis=1)
energy[Mode, Run, :, :, :] = readfile['energy']
for Eps in range(0, num_Eps):
for uav in uav_list:
energy_consumption_rate_uav[Mode, Run, Eps, uav] = energy[Mode, Run, Eps, 0, uav] - \
energy[Mode, Run, Eps, int(num_step * 0.75),
uav]
task_matrix_mode[Mode, :] = readfile['task_matrix'][0, 0, :]
su_index_mode[Mode] = np.where(task_matrix_mode[Mode, :] == 1)[0].item()
energy_mean = np.mean(energy, axis=1)
min_energ_mean = np.min(energy_mean, axis=2)
argmin_energy_mean = np.argmin(energy_mean, axis=2)
lifetime = deepcopy(argmin_energy_mean)
lifetime = lifetime + (min_energ_mean / transmission_rate).astype(int) - 1
# ******************* Plotting Sum Utility for 5 different Modes
plt.figure()
d0 = plt.plot(range(0, num_Eps), np.mean(sum_utility_step[0, :, :], axis=0), markersize='4',
label="Mode[%d]" % 0, linestyle='-', color='red', linewidth=2.0)
d1 = plt.plot(range(0, num_Eps), np.mean(sum_utility_step[1, :, :], axis=0), markersize='4',
label="Mode[%d]" % 1, linestyle='--', color='green', linewidth=2.0)
d2 = plt.plot(range(0, num_Eps), np.mean(sum_utility_step[2, :, :]/67, axis=0), markersize='4',
label="Mode[%d]" % 2, linestyle='-.', color='blue', linewidth=2.0)
d3 = plt.plot(range(0, num_Eps), np.mean(sum_utility_step[3, :, :]/70, axis=0), markersize='4',
label="Mode[%d]" % 3, linestyle=':', color='magenta', marker='x', linewidth=2.0)
d4 = plt.plot(range(0, num_Eps), np.mean(sum_utility_step[4, :, :], axis=0), markersize='4',
label="Mode[%d]" % 4, linestyle=':', color='black', marker='o', linewidth=2.0)
plt.grid(True)
plt.ylabel('Sum Utility', fontsize=14, fontweight="normal")
plt.xlabel('Episodes', fontsize=14, fontweight="normal")
plt.title('Sum Utility in %d x %d (All Modes)' % (Size_list[Size], Size_list[Size]))
plt_lines = d0 + d1 + d2 + d3 + d4
label_text = [line.get_label() for line in plt_lines]
plt.legend(plt_lines, label_text, loc=5, prop={'size': 14})
plt.show(block=False)
plt.savefig('Figures/Modes/Modes_Utility_sum_size_%d.pdf' % Size_list[Size], bbox_inches='tight')
# ****************************************
# ******************* Plotting Energy Consumption Rate per Mode same window (over mean of UAVs)
plt.figure()
d0 = plt.plot(range(0, num_Eps), (np.mean(np.mean(energy_consumption_rate_uav[0, :, :, :], axis=2), axis=0)) /
lifetime[0, :, su_index_mode[0]],
markersize='4', label="Mode[%d]" % 0, linestyle='-', color='red', linewidth=2.0)
d1 = plt.plot(range(0, num_Eps), (np.mean(np.mean(energy_consumption_rate_uav[1, :, :, :], axis=2), axis=0)) /
lifetime[1, :, su_index_mode[1]],
markersize='4', label="Mode[%d]" % 1, linestyle='--', color='green', linewidth=2.0)
d2 = plt.plot(range(0, num_Eps), (np.mean(np.mean(energy_consumption_rate_uav[2, :, :, :], axis=2), axis=0)) /
lifetime[2, :, su_index_mode[2]],
markersize='4', label="Mode[%d]" % 2, linestyle='-.', color='blue', linewidth=2.0)
d3 = plt.plot(range(0, num_Eps), (np.mean(np.mean(energy_consumption_rate_uav[3, :, :, :], axis=2), axis=0)) /
lifetime[3, :, su_index_mode[3]],
markersize='4', label="Mode[%d]" % 3, linestyle=':', color='magenta', marker='x', linewidth=2.0)
d4 = plt.plot(range(0, num_Eps), (np.mean(np.mean(energy_consumption_rate_uav[4, :, :, :], axis=2), axis=0)) /
lifetime[4, :, su_index_mode[4]],
markersize='4', label="Mode[%d]" % 4, linestyle=':', color='black', marker='o', linewidth=2.0)
plt.grid(True)
plt.ylabel('Consumption Rate', fontsize=14, fontweight="normal")
plt.xlabel('Episodes', fontsize=14, fontweight="normal")
plt.title('Energy Consumption Rate in %d x %d (All Modes)' % (Size_list[Size], Size_list[Size]))
plt_lines = d0 + d1 + d2 + d3 + d4
label_text = [line.get_label() for line in plt_lines]
plt.legend(plt_lines, label_text, loc=1, prop={'size': 14})
plt.show(block=False)
plt.savefig('Figures/Modes/Modes_Consumption_rate_size_%d.pdf' % Size_list[Size], bbox_inches='tight')
# ****************************************
# ******************* Plotting Lifetime different Modes same window for the relay UAV(bottleneck)
plt.figure()
d0 = plt.plot(range(0, num_Eps), lifetime[0, :, su_index_mode[0]], markersize='4',
label="Mode[%d]" % 0, linestyle='-', color='red', linewidth=2.0)
d1 = plt.plot(range(0, num_Eps), lifetime[1, :, su_index_mode[1]], markersize='4',
label="Mode[%d]" % 1, linestyle='--', color='green', linewidth=2.0)
d2 = plt.plot(range(0, num_Eps), lifetime[2, :, su_index_mode[2]], markersize='4',
label="Mode[%d]" % 2, linestyle='-.', color='blue', linewidth=2.0)
d3 = plt.plot(range(0, num_Eps), lifetime[3, :, su_index_mode[3]], markersize='4',
label="Mode[%d]" % 3, linestyle=':', color='magenta', marker='x', linewidth=2.0)
d4 = plt.plot(range(0, num_Eps), lifetime[4, :, su_index_mode[4]], markersize='4',
label="Mode[%d]" % 4, linestyle=':', color='black', marker='o', linewidth=2.0)
plt.grid(True)
plt.ylabel('Lifetime', fontsize=14, fontweight="normal")
plt.xlabel('Episodes', fontsize=14, fontweight="normal")
plt.title('Lifetime(Number of transmissions) in %d x %d (All Modes)' % (Size_list[Size], Size_list[Size]))
plt_lines = d0 + d1 + d2 + d3 + d4
label_text = [line.get_label() for line in plt_lines]
plt.legend(plt_lines, label_text, loc=4, prop={'size': 14})
plt.show(block=False)
plt.savefig('Figures/Modes/Modes_Lifetime_size_%d.pdf' % Size_list[Size], bbox_inches='tight')
del sum_utility_step, energy, energy_consumption_rate_uav, energy_mean, min_energ_mean, lifetime,
# ********************************************************************* Main
def main():
if part == 1:
first_part()
elif part == 2:
second_part()
if __name__ == "__main__":
main()