-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualization_functions.py
More file actions
1448 lines (1157 loc) · 62.9 KB
/
visualization_functions.py
File metadata and controls
1448 lines (1157 loc) · 62.9 KB
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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
def scene_visualization(ground_users = None, UAV_nodes = None, air_base_station = None, scene_info = None,line_alpha=0, show_axes_labels=True):
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# print(scene_info['xLength'])
blocks = scene_info['blocks']
# print(scene_info['scenario'])
ax.set_xlim([0, scene_info['scenario']['xLength']])
ax.set_ylim([0, scene_info['scenario']['yLength']])
max_block_height = 0
max_block_height = max((block['height'] for block in blocks), default=0) if blocks else 0
max_uav_height = max((UAV.position[2] for UAV in UAV_nodes), default=0) if UAV_nodes else 0
max_abs_height = max((ABS.position[2] for ABS in air_base_station), default=0) if air_base_station else 0
max_height = max(max_block_height, max_uav_height, max_abs_height) * 1.2 # 取最大值并乘以120%
# max_height = max(scene_info['scenario']['xLength'], scene_info['scenario']['yLength']) * 1.2 # 取最大值并乘以120%
ax.set_zlim([0, max_height])
if show_axes_labels:
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
ax.set_zlabel('Z Axis')
else:
# ax.set_xticks(ax.get_xticks())
# ax.set_yticks(ax.get_yticks())
# ax.set_zticks(ax.get_zticks())
# ax.set_xticklabels([])
# ax.set_yticklabels([])
# ax.set_zticklabels([])
# ax.set_xlabel('')
# ax.set_ylabel('')
# ax.set_zlabel('')
# ax.set_xlabel('X Axis')
# ax.set_ylabel('Y Axis')
# ax.set_zlabel('Z Axis')
ax.grid(False)
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_zticklabels([])
# set box size of each UAV_nodes
# node_size = 2
node_size = min(scene_info['scenario']['xLength'], scene_info['scenario']['yLength'])/40
if blocks:
for block in blocks:
# print(block)
# print(block['bottomCorner'])
x, y, z = block['bottomCorner']
dx, dy = block['size']
dz = block['height']
color = (1, 1, 1, 0.5)
# label = block['label']
ax.bar3d(x, y, z, dx, dy, dz, shade=True, color=color)
# 添加标记文字在块的中心
# ax.text(x + dx/2, y + dy/2, dz/2, label, color='black', ha='center', va='center')
if ground_users:
for user in ground_users:
# print(user.position[0])
x, y = user.position[0], user.position[1]
dx, dy, dz= node_size, node_size, node_size
color = (0, 0, 1, 0.5)
# label = block['label']
# print("GU")
# print(x, y, 0, dx, dy, dz)
ax.bar3d(x, y, 0, dx, dy, dz, shade=True, color=color)
if UAV_nodes:
for UAV in UAV_nodes:
x, y, z = UAV.position[0], UAV.position[1], UAV.position[2]
dx, dy, dz= node_size, node_size, node_size
color = (0, 1, 0, 0.5)
# label = block['label']
# print("UAV")
# print(x, y, z, dx, dy, dz)
ax.bar3d(x, y, z, dx, dy, dz, shade=True, color=color)
if air_base_station:
for ABS in air_base_station:
x, y, z = ABS.position[0], ABS.position[1], ABS.position[2]
dx, dy, dz= node_size, node_size, node_size
color = (1, 0, 0, 0.5)
# label = block['label']
ax.bar3d(x, y, z, dx, dy, dz, shade=True, color=color)
# # 可视化heatmap
# if heatmap is not None:
# max_users = np.max(heatmap) # 获取最大的ground_user数
# for x in range(heatmap.shape[0]):
# for y in range(heatmap.shape[1]):
# for z in range(heatmap.shape[2]):
# value = heatmap[x, y, z]
# if value > 0: # 如果该点的值大于0
# alpha = (value / max_users) * 0.02 # 根据ground_user的数量调整透明度
# ax.bar3d(x, y, z+min_height, 1, 1, 1, shade=False, color=(0, 1, 0, alpha))
# if connection_GU_UAV and ground_users and UAV_nodes:
# for gu_index, uav_index in connection_GU_UAV:
# gu = ground_users[gu_index]
# uav = UAV_nodes[uav_index]
# gu_pos = np.array([gu.position[0], gu.position[1], 0])
# uav_pos = np.array([uav.position[0], uav.position[1], uav.position[2]])
# ax.plot([gu_pos[0], uav_pos[0]], [gu_pos[1], uav_pos[1]], [gu_pos[2], uav_pos[2]], color='k')
# if connection_GU_UAV:
# for start, end in connection_GU_UAV:
# print(start)
# print(end)
# start_pos = get_position_by_index(start, ground_users, UAV_nodes, air_base_station)
# end_pos = get_position_by_index(end, ground_users, UAV_nodes, air_base_station)
# ax.plot([start_pos[0], end_pos[0]], [start_pos[1], end_pos[1]], [start_pos[2], end_pos[2]], color='k')
line_color = (0.5,0,0)
from node_functions import print_node
# print_node(UAV_nodes)
if ground_users:
for gu in ground_users:
gu_x, gu_y, gu_z = gu.position[0], gu.position[1], gu.position[2]
if gu.connected_nodes:
uav_index = gu.connected_nodes[0]
if(UAV_nodes):
uav_x, uav_y, uav_z = UAV_nodes[uav_index].position[0], UAV_nodes[uav_index].position[1], UAV_nodes[uav_index].position[2]
ax.plot([gu_x, uav_x], [gu_y, uav_y], [gu_z, uav_z], color=(0.5,0,0), alpha=line_alpha)
if UAV_nodes:
for uav in UAV_nodes:
start_uav_x, start_uav_y, start_uav_z = uav.position[0], uav.position[1], uav.position[2]
for target_uav_index in uav.connected_nodes:
if target_uav_index >= len(UAV_nodes): continue
# print(target_uav_index)
target_uav_x, target_uav_y, target_uav_z = UAV_nodes[target_uav_index].position[0], UAV_nodes[target_uav_index].position[1], UAV_nodes[target_uav_index].position[2]
ax.plot([start_uav_x, target_uav_x], [start_uav_y, target_uav_y], [start_uav_z, target_uav_z], color=(0.5,0,0), alpha=line_alpha)
if air_base_station:
for bs in air_base_station:
bs_x, bs_y, bs_z = bs.position[0], bs.position[1], bs.position[2]
if UAV_nodes:
for target_uav_index in bs.connected_nodes:
target_uav_x, target_uav_y, target_uav_z = UAV_nodes[target_uav_index].position[0], UAV_nodes[target_uav_index].position[1], UAV_nodes[target_uav_index].position[2]
ax.plot([bs_x, target_uav_x], [bs_y, target_uav_y], [bs_z, target_uav_z], color=(0.5,0,0), alpha=line_alpha)
# GU-UAV连接线
# if ground_users and UAV_nodes:
# for user in ground_users:
# # print(user.position)
# gu_x, gu_y, gu_z = user.position[0], user.position[1], 0 # Ground users 的 z 坐标为 0
# for UAV in UAV_nodes: # 假设所有 GU 都与 UAV 相连
# print(UAV.position)
# uav_x, uav_y, uav_z = UAV.position[0], UAV.position[1], UAV.position[2]
# ax.plot([gu_x, uav_x], [gu_y, uav_y], [gu_z, uav_z], color=line_color, alpha=line_alpha)
# UAV-UAV连接线
# if UAV_nodes:
# for i, UAV1 in enumerate(UAV_nodes):
# for j, UAV2 in enumerate(UAV_nodes):
# if i < j: # 避免重复连接
# uav1_x, uav1_y, uav1_z = UAV1.position[0], UAV1.position[1], UAV1.position[2]
# uav2_x, uav2_y, uav2_z = UAV2.position[0], UAV2.position[1], UAV2.position[2]
# ax.plot([uav1_x, uav2_x], [uav1_y, uav2_y], [uav1_z, uav2_z], color=line_color, alpha=line_alpha)
# # UAV-BS连接线
# if UAV_nodes and air_base_station:
# for UAV in UAV_nodes:
# uav_x, uav_y, uav_z = UAV.position[0], UAV.position[1], UAV.position[2]
# for ABS in air_base_station:
# abs_x, abs_y, abs_z = ABS.position[0], ABS.position[1], ABS.position[2]
# ax.plot([uav_x, abs_x], [uav_y, abs_y], [uav_z, abs_z], color=line_color, alpha=line_alpha)
# if connection_GU_UAV:
# for gu, path in connection_GU_UAV.items():
# if path:
# start = gu
# end = path[0]+len(ground_users)
# start_pos = get_position_by_index(start, ground_users, UAV_nodes, air_base_station)
# end_pos = get_position_by_index(end, ground_users, UAV_nodes, air_base_station)
# ax.plot([start_pos[0], end_pos[0]], [start_pos[1], end_pos[1]], [start_pos[2], end_pos[2]], color=line_color, alpha=line_alpha)
# if connection_UAV_BS:
# for uav, path in connection_UAV_BS.items():
# for i in range(len(path) - 1):
# start = path[i]+len(ground_users)
# end = path[i + 1]+len(ground_users)
# start_pos = get_position_by_index(start, ground_users, UAV_nodes, air_base_station)
# end_pos = get_position_by_index(end, ground_users, UAV_nodes, air_base_station)
# ax.plot([start_pos[0], end_pos[0]], [start_pos[1], end_pos[1]], [start_pos[2], end_pos[2]], color=line_color, alpha=line_alpha)
plt.show()
def visualize_all_gu_capacity(gu_capacity_TD):
"""
Visualizes the data rate of each ground user (GU) over time.
Parameters:
gu_capacity_TD (list of lists): A list where each inner list contains data rates of GUs at a specific time step.
Returns:
None
"""
plt.figure(figsize=(10, 6))
# Plot each ground user's capacity over time
for gu_index in range(len(gu_capacity_TD[0])): # assuming all time steps have the same number of GUs
gu_data = [time_step[gu_index] for time_step in gu_capacity_TD]
plt.plot(gu_data, label=f'GU {gu_index + 1}')
plt.xlabel("Time Step")
plt.ylabel("Data Rate (bps)")
plt.title("Ground User Capacity Over Time")
plt.legend(loc="upper right")
plt.grid(True)
plt.show()
def visualize_all_min_gu_capacity(min_gu_capacity_TD):
plt.figure(figsize=(10, 6))
gu_indices = list(range(1, len(min_gu_capacity_TD) + 1))
plt.plot(gu_indices, min_gu_capacity_TD, marker='o')
plt.xlabel("Time Step")
plt.ylabel("Data Rate (bps)")
plt.title("Minimum Ground User Capacity Over Time")
plt.legend(loc="upper right")
plt.grid(True)
plt.show()
def visualize_metrics(max_reward_TD, max_RS_TD, max_OL_TD=None):
"""
可视化 max_reward_TD, max_RS_TD 和 max_OL_TD 在同一张图中
:param max_reward_TD: List of max reward over time
:param max_RS_TD: List of max resilience score over time
:param max_OL_TD: List of max overload score over time
"""
# 假设每个列表的数据长度相同,并且每个索引对应同一个时间点
time_points = list(range(len(max_reward_TD)))
# 创建图表
plt.figure(figsize=(10, 6))
# 绘制每条曲线
plt.plot(time_points, max_reward_TD, label='Max Reward TD', marker='o')
plt.plot(time_points, max_RS_TD, label='Max RS TD', marker='s')
# plt.plot(time_points, max_OL_TD, label='Max OL TD', marker='^')
# 添加标签和标题
plt.xlabel('Time Points')
plt.ylabel('Scores')
plt.title('Max Reward TD, Max RS TD Over Time')
plt.legend()
plt.grid(True)
# 显示图表
plt.show()
def get_position_by_index(index, ground_users, UAV_nodes, air_base_station):
all_nodes = (ground_users or []) + (UAV_nodes or []) + (air_base_station or [])
if 0 <= index < len(all_nodes):
node = all_nodes[index]
return node.position
return [0, 0, 0]
def visualize_heatmap_slice(heatmap, target_height):
"""
可视化特定高度的二维热图切片,包括 connection_score 和 gu_bottleneck。
参数:
- heatmap: 字典格式的热图数据,键为 (x, y, z),值为 (connection_score, gu_bottleneck)。
- target_height: 想要可视化的高度。
- colormap: 颜色映射,默认 'hot'。
"""
# 提取该高度的x, y以及相应的connection_score和gu_bottleneck
x_vals, y_vals, connection_scores, gu_bottlenecks = [], [], [], []
for (x, y, z), (connection_score, gu_bottleneck) in heatmap.items():
if z == target_height:
x_vals.append(x)
y_vals.append(y)
connection_scores.append(connection_score)
gu_bottlenecks.append(gu_bottleneck)
if not connection_scores or not gu_bottlenecks:
print(f"No data available for height {target_height}.")
return
# 创建网格数据
x_vals = np.array(x_vals)
y_vals = np.array(y_vals)
x_unique = np.unique(x_vals)
y_unique = np.unique(y_vals)
x_grid, y_grid = np.meshgrid(x_unique, y_unique)
# 重塑连接分数和瓶颈值为网格格式
connection_score_grid = np.full(x_grid.shape, np.nan)
gu_bottleneck_grid = np.full(x_grid.shape, np.nan)
for i, (x, y) in enumerate(zip(x_vals, y_vals)):
x_index = np.where(x_unique == x)[0][0]
y_index = np.where(y_unique == y)[0][0]
connection_score_grid[y_index, x_index] = connection_scores[i]
gu_bottleneck_grid[y_index, x_index] = gu_bottlenecks[i]
# 可视化 connection_score 和 gu_bottleneck
fig, axes = plt.subplots(1, 2, figsize=(14, 6))
# 可视化 connection_score
c1 = axes[0].pcolormesh(x_grid, y_grid, connection_score_grid, cmap='hot', shading='auto')
fig.colorbar(c1, ax=axes[0], label='Connection Score')
axes[0].set_title(f'Connection Score at Height {target_height}')
axes[0].set_xlabel("X Axis")
axes[0].set_ylabel("Y Axis")
# 可视化 gu_bottleneck
c2 = axes[1].pcolormesh(x_grid, y_grid, gu_bottleneck_grid, cmap='viridis', shading='auto')
fig.colorbar(c2, ax=axes[1], label='GU Bottleneck')
axes[1].set_title(f'GU Bottleneck at Height {target_height}')
axes[1].set_xlabel("X Axis")
axes[1].set_ylabel("Y Axis")
plt.tight_layout()
plt.show()
def visualize_heatmap_slices(heatmap, target_heights, normalize=False):
fig, axes = plt.subplots(2, 2, figsize=(14, 12))
colormaps = ['hot', 'viridis']
for idx, target_height in enumerate(target_heights):
x_vals, y_vals, connection_scores, gu_bottlenecks = [], [], [], []
for (x, y, z), (connection_score, gu_bottleneck) in heatmap.items():
if z == target_height:
x_vals.append(x)
y_vals.append(y)
connection_scores.append(connection_score)
gu_bottlenecks.append(gu_bottleneck)
if not connection_scores or not gu_bottlenecks:
print(f"No data available for height {target_height}.")
continue
# 如果需要归一化,使用min-max归一化
if normalize:
max_conn = max(connection_scores)
min_conn = min(connection_scores)
max_bottle = max(gu_bottlenecks)
min_bottle = min(gu_bottlenecks)
connection_scores = [(cs - min_conn) / (max_conn - min_conn) if max_conn != min_conn else 0 for cs in connection_scores]
gu_bottlenecks = [(gb - min_bottle) / (max_bottle - min_bottle) if max_bottle != min_bottle else 0 for gb in gu_bottlenecks]
x_vals = np.array(x_vals)
y_vals = np.array(y_vals)
x_unique = np.unique(x_vals)
y_unique = np.unique(y_vals)
x_grid, y_grid = np.meshgrid(x_unique, y_unique)
connection_score_grid = np.full(x_grid.shape, np.nan)
gu_bottleneck_grid = np.full(x_grid.shape, np.nan)
for i, (x, y) in enumerate(zip(x_vals, y_vals)):
x_index = np.where(x_unique == x)[0][0]
y_index = np.where(y_unique == y)[0][0]
connection_score_grid[y_index, x_index] = connection_scores[i]
gu_bottleneck_grid[y_index, x_index] = gu_bottlenecks[i]
c1 = axes[idx, 0].pcolormesh(x_grid, y_grid, connection_score_grid, cmap=colormaps[0], shading='auto')
fig.colorbar(c1, ax=axes[idx, 0], label='Connection Score' + (' (Normalized)' if normalize else ''))
axes[idx, 0].set_title(f'Connection Score at Height {target_height}')
axes[idx, 0].set_xlabel("X Axis")
axes[idx, 0].set_ylabel("Y Axis")
c2 = axes[idx, 1].pcolormesh(x_grid, y_grid, gu_bottleneck_grid, cmap=colormaps[1], shading='auto')
fig.colorbar(c2, ax=axes[idx, 1], label='GU Bottleneck' + (' (Normalized)' if normalize else ''))
axes[idx, 1].set_title(f'GU Bottleneck at Height {target_height}')
axes[idx, 1].set_xlabel("X Axis")
axes[idx, 1].set_ylabel("Y Axis")
plt.tight_layout()
plt.show()
import matplotlib.patches as patches
from collections import defaultdict
def merge_clusters(clusters_records):
"""
根据给定逻辑合并聚类,提取指定的较短或较长的 GU 列表作为最终类。
参数:
- clusters_records: 每次聚类的结果记录列表 [{0: [1, 2, 3], 1: [0, 4, 5]}, {0: [2, 3], 1: [1]}].
返回:
- classes: 最终的GU分组列表,按照给定规则从输入中提取。
"""
classes = []
# 遍历每个聚类记录
for i, clusters in enumerate(clusters_records):
lists = list(clusters.values())
# 选择较短的列表作为类,若长度相同,选择第一个
if len(lists[0]) < len(lists[1]):
classes.append(lists[0])
elif len(lists[0]) > len(lists[1]):
classes.append(lists[1])
else:
classes.append(lists[0])
# 将最后一个元素的较长列表记录为类,若长度相同,选择第二个
final_lists = list(clusters_records[-1].values())
if len(final_lists[0]) > len(final_lists[1]):
classes.append(final_lists[0])
elif len(final_lists[0]) < len(final_lists[1]):
classes.append(final_lists[1])
else:
classes.append(final_lists[1])
return classes
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np
from itertools import cycle
def visualize_hierarchical_clustering(ground_users, clusters_records, blocks, scene):
"""
可视化层次聚类的最终GU分类。
参数:
- ground_users: 所有GU节点的列表,包含其位置属性 (x, y)。
- clusters_records: 每次聚类的结果记录列表 [{0: [0, 1], 1: [2, 3, 4]}]。
- blocks: 障碍物位置列表,每个元素是一个包含 "bottomCorner" 和 "size" 的字典。
- scene: 场景信息,包含边界信息,用于设置绘图范围。
"""
# # 合并聚类结果
merged_clusters = merge_clusters(clusters_records)
# 删除空聚类
merged_clusters = [cluster for cluster in merged_clusters if cluster]
other_gu_idx = []
for gu_idx in range(len(ground_users)):
found = False
for clustered_gu_idxes in merged_clusters:
if gu_idx in clustered_gu_idxes:
found = True
break
if not found:
other_gu_idx.append(gu_idx)
merged_clusters.append(other_gu_idx) if len(other_gu_idx) > 0 else None
print(merged_clusters)
# 手动定义一组高度对比的颜色
high_contrast_colors = [
'#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd',
'#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf',
'#a55194', '#ffbb78', '#98df8a', '#ff9896', '#c5b0d5',
'#c49c94', '#f7b6d2', '#c7c7c7', '#dbdb8d', '#9edae5'
]
color_cycle = cycle(high_contrast_colors)
colors = [next(color_cycle) for _ in range(len(merged_clusters))]
fig, ax = plt.subplots(figsize=(10, 10))
# 绘制障碍物
for block in blocks:
x, y, _ = block["bottomCorner"]
width, height = block["size"]
block_patch = patches.Rectangle((x, y), width, height, linewidth=1, edgecolor='black', facecolor='gray', alpha=0.5)
ax.add_patch(block_patch)
# 绘制合并后的每个类
for cluster_idx, gu_indices in enumerate(merged_clusters):
cluster_positions = [ground_users[index].position[:2] for index in gu_indices]
x_vals, y_vals = zip(*cluster_positions)
ax.scatter(x_vals, y_vals, color=colors[cluster_idx], label=f"Cluster {cluster_idx}", s=120, alpha=0.8, edgecolors='w')
# 设置图形边界和标题
ax.set_xlim(0, scene["xLength"])
ax.set_ylim(0, scene["yLength"])
ax.set_xlabel("X Axis")
ax.set_ylabel("Y Axis")
ax.set_title("Final Hierarchical Clustering of Ground Users")
ax.legend()
plt.grid(True)
plt.show()
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from itertools import cycle
def visualize_capacity_and_load(gu_capacities_records, uav_load_records, normalize=False):
# Calculate time points
time_points = list(range(len(gu_capacities_records)))
# Normalize GU Capacity if specified
if normalize:
all_capacities = [capacity for record in gu_capacities_records for capacity in record.values()]
min_val, max_val = min(all_capacities), max(all_capacities)
gu_capacities_records = [
{gu_index: (capacity - min_val) / (max_val - min_val) if max_val != min_val else 0
for gu_index, capacity in record.items()}
for record in gu_capacities_records
]
# Calculate min, max, and mean capacities
min_capacities = [min(capacities.values()) for capacities in gu_capacities_records]
max_capacities = [max(capacities.values()) for capacities in gu_capacities_records]
mean_capacities = [np.mean(list(capacities.values())) for capacities in gu_capacities_records]
# Get UAV IDs and track their load over time
uav_ids = sorted(set(uav_id for record in uav_load_records for uav_id in record.keys()))
uav_loads_over_time = {uav_id: [record.get(uav_id, 0) for record in uav_load_records] for uav_id in uav_ids}
# Plot GU Capacity
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 6))
ax1.plot(time_points, min_capacities, label='Min Throughput', marker='o', color='blue')
ax1.plot(time_points, max_capacities, label='Max Throughput', marker='o', color='red')
ax1.plot(time_points, mean_capacities, label='Mean Throughput', marker='o', color='green')
ax1.set_xlabel('Time (New UAV Found)')
ax1.set_ylabel('User Throughput (bps)' + (' (Normalized)' if normalize else ''))
# ax1.set_title('GU Capacity Over Time')
ax1.grid(True)
ax1.set_xticks(time_points) # Set x-axis to display only integer time points
# Customize legend for ax1
legend1 = ax1.legend(
loc="upper center",
bbox_to_anchor=(0.5, 1.15),
ncol=3,
fontsize=14,
labelspacing=0.5,
handlelength=1,
handletextpad=0.5,
borderaxespad=0.3,
borderpad=0.5
)
legend1.get_frame().set_facecolor('white')
legend1.get_frame().set_alpha(0.9)
# Plot UAV Load Distribution
bar_width = 0.35
bottom = np.zeros(len(time_points))
colors = plt.cm.Paired(np.linspace(0, 1, len(uav_ids)))
for i, uav_id in enumerate(uav_ids):
ax2.bar(time_points, uav_loads_over_time[uav_id], bottom=bottom, label=f'UAV {uav_id}', color=colors[i])
bottom += np.array(uav_loads_over_time[uav_id])
ax2.set_xlabel('Time (New UAV Found)')
ax2.set_ylabel('Number of GUs')
# ax2.set_title('UAV Load Distribution Over Time')
ax2.grid(True, axis='y')
ax2.set_xticks(time_points) # Set x-axis to display only integer time points
# Customize legend for ax2
legend2 = ax2.legend(
loc="upper center",
bbox_to_anchor=(0.5, 1.15),
ncol=5,
fontsize=14,
labelspacing=0.5,
handlelength=1,
handletextpad=0.5,
borderaxespad=0.3,
borderpad=0.5,
# title="UAV ID",
title_fontsize=14
)
legend2.get_frame().set_facecolor('white')
legend2.get_frame().set_alpha(0.9)
plt.tight_layout()
plt.show()
def visualize_scores(reward_track, RS_track, best_reward_track, best_RS_track):
# 确定episode数
episodes = np.arange(len(reward_track))
fig, ax = plt.subplots(figsize=(14, 7))
# 折线图:reward_track 和 RS_track
ax.plot(episodes, reward_track, label='Reward Track', color='blue', linestyle='-', marker='o')
ax.plot(episodes, RS_track, label='RS Track', color='green', linestyle='-', marker='o')
# 柱状图:best_reward_track 和 best_RS_track
width = 0.4 # 定义柱状图的宽度
ax.bar(episodes - width/2, best_reward_track, width=width, color='blue', alpha=0.5, label='Best Reward Track')
ax.bar(episodes + width/2, best_RS_track, width=width, color='green', alpha=0.5, label='Best RS Track')
# 添加图例和标签
ax.set_xlabel('Episode')
ax.set_ylabel('Score')
ax.set_title('Reward and RS Tracks with Best Scores')
ax.legend()
plt.grid(True)
plt.show()
def visualize_best_scores(best_reward_track, best_RS_track):
# 确定 episode 数
episodes = np.arange(len(best_reward_track))
fig, ax = plt.subplots(figsize=(14, 7))
# 使用折线图绘制 best_reward_track 和 best_RS_track
ax.plot(episodes, best_reward_track, label='Best Reward Track', color='blue', linestyle='-', marker='o')
ax.plot(episodes, best_RS_track, label='Best RS Track', color='green', linestyle='-', marker='o')
# 添加图例和标签
ax.set_xlabel('Episode')
ax.set_ylabel('Best Score')
ax.set_title('Best Reward and RS Tracks Over Episodes')
ax.legend()
plt.grid(True)
plt.show()
def visualize_simulation(uav_connections_TD, gu_capacity_TD, num_uavs):
# 初始化存储每个时间步的最小和平均容量
min_capacity_over_time = []
avg_capacity_over_time = []
uav_connections_over_time = []
# 遍历每个时间步的数据
for step, (gu_to_uav_connections, gu_to_bs_capacity) in enumerate(zip(uav_connections_TD, gu_capacity_TD)):
# 确保gu_to_bs_capacity是数值列表
if isinstance(gu_to_bs_capacity, dict):
capacities = list(gu_to_bs_capacity.values())
else:
capacities = gu_to_bs_capacity # 如果已经是列表,则直接使用
# 计算当前时间步的最小和平均容量
min_capacity = np.min(capacities)
avg_capacity = np.mean(capacities)
min_capacity_over_time.append(min_capacity)
avg_capacity_over_time.append(avg_capacity)
# 将gu_to_uav_connections转换为整数值的列表
gu_to_uav_connections = {k: v[0] if isinstance(v, list) else v for k, v in gu_to_uav_connections.items()}
# 统计每个UAV的GU连接数量
uav_connection_counts = [sum(1 for uav in gu_to_uav_connections.values() if uav == i) for i in range(num_uavs)]
uav_connections_over_time.append(uav_connection_counts)
# 转置数据以便绘制堆叠柱形图
uav_connections_over_time = np.array(uav_connections_over_time).T
# 可视化
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 8))
# 绘制GU到BS的capacity的最小值和平均值折线图
time_steps = np.arange(len(uav_connections_TD))
ax1.plot(time_steps, min_capacity_over_time, label="Min Capacity")
ax1.plot(time_steps, avg_capacity_over_time, label="Avg Capacity")
ax1.set_title("GU to BS Capacity Over Time")
ax1.set_xlabel("Time Steps")
ax1.set_ylabel("Capacity")
ax1.legend()
ax1.set_xticks(time_steps) # 设置x轴刻度为整数
# 绘制堆叠柱形图表示每个时间步的UAV连接数量
bottom = np.zeros(len(uav_connections_TD)) # 初始化底部位置为0
for i in range(num_uavs):
ax2.bar(time_steps, uav_connections_over_time[i], bottom=bottom, label=f"UAV {i}")
bottom += uav_connections_over_time[i] # 更新底部位置,堆叠下一个UAV的值
ax2.set_title("Number of GUs Connected to Each UAV Over Time")
ax2.set_xlabel("Time Steps")
ax2.set_ylabel("Total Number of GUs Connected")
ax2.legend(loc="upper left")
ax2.set_xticks(time_steps) # 设置x轴刻度为整数
plt.tight_layout()
plt.show()
def visualize_simulation_together(uav_connections_TD, gu_capacity_TD, num_uavs):
# 初始化存储每个时间步的最小和平均容量
min_capacity_over_time = []
avg_capacity_over_time = []
uav_connections_over_time = []
# 遍历每个时间步的数据
for step, (gu_to_uav_connections, gu_to_bs_capacity) in enumerate(zip(uav_connections_TD, gu_capacity_TD)):
# 确保gu_to_bs_capacity是数值列表
if isinstance(gu_to_bs_capacity, dict):
capacities = list(gu_to_bs_capacity.values())
else:
capacities = gu_to_bs_capacity # 如果已经是列表,则直接使用
# 计算当前时间步的最小和平均容量
min_capacity = np.min(capacities)
avg_capacity = np.mean(capacities)
min_capacity_over_time.append(min_capacity)
avg_capacity_over_time.append(avg_capacity)
# 将gu_to_uav_connections转换为整数值的列表
gu_to_uav_connections = {k: v[0] if isinstance(v, list) else v for k, v in gu_to_uav_connections.items()}
# 统计每个UAV的GU连接数量
uav_connection_counts = [sum(1 for uav in gu_to_uav_connections.values() if uav == i) for i in range(num_uavs)]
uav_connections_over_time.append(uav_connection_counts)
# 转置数据以便绘制堆叠柱形图
uav_connections_over_time = np.array(uav_connections_over_time).T
# 可视化
fig, ax1 = plt.subplots(figsize=(12, 8))
# 绘制GU到BS的capacity的最小值和平均值折线图
time_steps = np.arange(len(uav_connections_TD))
ax1.plot(time_steps, min_capacity_over_time, label="Min Capacity", color="blue", marker="o")
ax1.plot(time_steps, avg_capacity_over_time, label="Avg Capacity", color="green", marker="x")
ax1.set_xlabel("Time Steps")
ax1.set_ylabel("Capacity", color="black")
ax1.legend(loc="upper left")
ax1.set_xticks(time_steps) # 设置x轴刻度为整数
# 使用相同的x轴,增加堆叠柱状图
ax2 = ax1.twinx()
ax2.set_ylabel("Total Number of GUs Connected")
bottom = np.zeros(len(uav_connections_TD))
for i in range(num_uavs):
ax2.bar(time_steps, uav_connections_over_time[i], bottom=bottom, label=f"UAV {i}", alpha=0.6)
bottom += uav_connections_over_time[i]
ax2.legend(loc="upper right")
ax2.set_xticks(time_steps) # 设置x轴刻度为整数
plt.title("GU to BS Capacity and UAV Connections Over Time")
plt.tight_layout()
plt.show()
def visualize_simulation_with_baseline(uav_connections_TD, gu_capacity_TD, baseline_uav_connections_TD, baseline_gu_capacity_TD, num_uavs):
import numpy as np
import matplotlib.pyplot as plt
# 初始化存储每个时间步的最小和平均容量
min_capacity_over_time = []
avg_capacity_over_time = []
baseline_min_capacity_over_time = []
baseline_avg_capacity_over_time = []
uav_connections_over_time = []
baseline_uav_connections_over_time = []
# print(uav_connections_TD)
# print(baseline_uav_connections_TD)
# 遍历每个时间步的数据
for step, (gu_to_uav_connections, gu_to_bs_capacity, baseline_gu_to_bs_capacity, baseline_gu_to_uav_connections) in enumerate(
zip(uav_connections_TD, gu_capacity_TD, baseline_gu_capacity_TD, baseline_uav_connections_TD)):
# 处理gu_capacity_TD和baseline_gu_capacity_TD
if isinstance(gu_to_bs_capacity, dict):
capacities = list(gu_to_bs_capacity.values())
else:
capacities = gu_to_bs_capacity
if isinstance(baseline_gu_to_bs_capacity, dict):
baseline_capacities = list(baseline_gu_to_bs_capacity.values())
else:
baseline_capacities = baseline_gu_to_bs_capacity
# 计算当前时间步的最小和平均容量
min_capacity = np.min(capacities)
avg_capacity = np.mean(capacities)
baseline_min_capacity = np.min(baseline_capacities)
baseline_avg_capacity = np.mean(baseline_capacities)
min_capacity_over_time.append(min_capacity)
avg_capacity_over_time.append(avg_capacity)
baseline_min_capacity_over_time.append(baseline_min_capacity)
baseline_avg_capacity_over_time.append(baseline_avg_capacity)
# 处理gu_to_uav_connections和baseline_gu_to_uav_connections
gu_to_uav_connections = {k: v[0] if isinstance(v, list) else v for k, v in gu_to_uav_connections.items()}
baseline_gu_to_uav_connections = {k: v[0] if isinstance(v, list) else v for k, v in baseline_gu_to_uav_connections.items()}
# 统计每个UAV的GU连接数量
uav_connection_counts = [sum(1 for uav in gu_to_uav_connections.values() if uav == i) for i in range(num_uavs)]
baseline_uav_connection_counts = [sum(1 for uav in baseline_gu_to_uav_connections.values() if uav == i) for i in range(num_uavs)]
uav_connections_over_time.append(uav_connection_counts)
baseline_uav_connections_over_time.append(baseline_uav_connection_counts)
# print("uav_connection_counts"+str(uav_connection_counts))
# print("baseline_uav_connection_counts"+str(baseline_uav_connection_counts))
# 转置数据以便绘制堆叠柱形图
uav_connections_over_time = np.array(uav_connections_over_time).T
baseline_uav_connections_over_time = np.array(baseline_uav_connections_over_time).T
# print("uav_connections_over_time: "+str(uav_connections_over_time))
# print("baseline_uav_connections_over_time"+str(baseline_uav_connections_over_time))
# 可视化
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(14, 10))
# 绘制GU到BS的capacity的最小值和平均值折线图
time_steps = np.arange(len(uav_connections_TD))
ax1.plot(time_steps, min_capacity_over_time, label="Min Capacity")
ax1.plot(time_steps, avg_capacity_over_time, label="Avg Capacity")
ax1.plot(time_steps, baseline_min_capacity_over_time, label="Baseline Min Capacity", linestyle="--")
ax1.plot(time_steps, baseline_avg_capacity_over_time, label="Baseline Avg Capacity", linestyle="--")
ax1.set_title("GU to BS Capacity Over Time")
ax1.set_xlabel("Time Steps")
ax1.set_ylabel("Capacity")
ax1.legend()
ax1.set_xticks(time_steps) # 设置x轴刻度为整数
# 绘制堆叠柱形图表示每个时间步的UAV连接数量,以及baseline的柱形图
bottom = np.zeros(len(uav_connections_TD)) # 初始化底部位置为0
baseline_bottom = np.zeros(len(baseline_uav_connections_TD)) # 初始化底部位置为0
for i in range(num_uavs):
# 绘制baseline的柱形图
ax2.bar(time_steps - 0.2, baseline_uav_connections_over_time[i], width=0.4, bottom=baseline_bottom, label=f"Baseline UAV {i}", alpha=0.5)
# 更新底部位置
baseline_bottom += baseline_uav_connections_over_time[i]
for i in range(num_uavs):
# 绘制uav_connections的柱形图
ax2.bar(time_steps + 0.2, uav_connections_over_time[i], width=0.4, bottom=bottom, label=f"UAV {i}")
# 更新底部位置
bottom += uav_connections_over_time[i]
ax2.set_title("Number of GUs Connected to Each UAV Over Time (Baseline vs Current)")
ax2.set_xlabel("Time Steps")
ax2.set_ylabel("Total Number of GUs Connected")
ax2.legend(loc="upper left")
ax2.set_xticks(time_steps) # 设置x轴刻度为整数
plt.tight_layout()
plt.show()
def visualize_simulation_with_multiple_baselines(
uav_connections_TD, gu_capacity_TD,
baseline1_uav_connections_TD, baseline1_gu_capacity_TD,
baseline2_uav_connections_TD, baseline2_gu_capacity_TD,
num_uavs
):
import numpy as np
import matplotlib.pyplot as plt
min_capacity_over_time = []
avg_capacity_over_time = []
baseline1_min_capacity_over_time = []
baseline1_avg_capacity_over_time = []
baseline2_min_capacity_over_time = []
baseline2_avg_capacity_over_time = []
uav_connections_over_time = []
baseline1_uav_connections_over_time = []
baseline2_uav_connections_over_time = []
for step, (gu_to_uav_connections, gu_to_bs_capacity,
baseline1_gu_to_bs_capacity, baseline1_gu_to_uav_connections,
baseline2_gu_to_bs_capacity, baseline2_gu_to_uav_connections) in enumerate(
zip(uav_connections_TD, gu_capacity_TD,
baseline1_gu_capacity_TD, baseline1_uav_connections_TD,
baseline2_gu_capacity_TD, baseline2_uav_connections_TD)):
if isinstance(gu_to_bs_capacity, dict):
capacities = list(gu_to_bs_capacity.values())
else:
capacities = gu_to_bs_capacity
if isinstance(baseline1_gu_to_bs_capacity, dict):
baseline1_capacities = list(baseline1_gu_to_bs_capacity.values())
else:
baseline1_capacities = baseline1_gu_to_bs_capacity
if isinstance(baseline2_gu_to_bs_capacity, dict):
baseline2_capacities = list(baseline2_gu_to_bs_capacity.values())
else:
baseline2_capacities = baseline2_gu_to_bs_capacity
min_capacity = np.min(capacities)
avg_capacity = np.mean(capacities)
baseline1_min_capacity = np.min(baseline1_capacities)
baseline1_avg_capacity = np.mean(baseline1_capacities)
baseline2_min_capacity = np.min(baseline2_capacities)
baseline2_avg_capacity = np.mean(baseline2_capacities)
min_capacity_over_time.append(min_capacity)
avg_capacity_over_time.append(avg_capacity)
baseline1_min_capacity_over_time.append(baseline1_min_capacity)
baseline1_avg_capacity_over_time.append(baseline1_avg_capacity)
baseline2_min_capacity_over_time.append(baseline2_min_capacity)
baseline2_avg_capacity_over_time.append(baseline2_avg_capacity)
gu_to_uav_connections = {k: v[0] if isinstance(v, list) else v for k, v in gu_to_uav_connections.items()}
baseline1_gu_to_uav_connections = {k: v[0] if isinstance(v, list) else v for k, v in baseline1_gu_to_uav_connections.items()}
baseline2_gu_to_uav_connections = {k: v[0] if isinstance(v, list) else v for k, v in baseline2_gu_to_uav_connections.items()}
uav_connection_counts = [sum(1 for uav in gu_to_uav_connections.values() if uav == i) for i in range(num_uavs)]
baseline1_uav_connection_counts = [sum(1 for uav in baseline1_gu_to_uav_connections.values() if uav == i) for i in range(num_uavs)]
baseline2_uav_connection_counts = [sum(1 for uav in baseline2_gu_to_uav_connections.values() if uav == i) for i in range(num_uavs)]
uav_connections_over_time.append(uav_connection_counts)
baseline1_uav_connections_over_time.append(baseline1_uav_connection_counts)
baseline2_uav_connections_over_time.append(baseline2_uav_connection_counts)
uav_connections_over_time = np.array(uav_connections_over_time).T
baseline1_uav_connections_over_time = np.array(baseline1_uav_connections_over_time).T
baseline2_uav_connections_over_time = np.array(baseline2_uav_connections_over_time).T
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(14, 10))
time_steps = np.arange(len(uav_connections_TD))
ax1.plot(time_steps, min_capacity_over_time, label="Min Throughput")
ax1.plot(time_steps, avg_capacity_over_time, label="Avg Throughput")
ax1.plot(time_steps, baseline1_min_capacity_over_time, label="Baseline 1 Min Throughput", linestyle="--")
ax1.plot(time_steps, baseline1_avg_capacity_over_time, label="Baseline 1 Avg Throughput", linestyle="--")
ax1.plot(time_steps, baseline2_min_capacity_over_time, label="Baseline 2 Min Throughput", linestyle=":")
ax1.plot(time_steps, baseline2_avg_capacity_over_time, label="Baseline 2 Throughput", linestyle=":")
ax1.set_title("Ground Users to Base Station Throughput Over Time")
ax1.set_xlabel("Time Steps")
ax1.set_ylabel("Throughput")
ax1.legend()
ax1.set_xticks(time_steps)
bottom = np.zeros(len(uav_connections_TD))
baseline1_bottom = np.zeros(len(baseline1_uav_connections_TD))
baseline2_bottom = np.zeros(len(baseline2_uav_connections_TD))
for i in range(num_uavs):
ax2.bar(time_steps - 0.2, baseline1_uav_connections_over_time[i], width=0.2, bottom=baseline1_bottom, label=f"Baseline 1 UAV {i}", alpha=0.5)
baseline1_bottom += baseline1_uav_connections_over_time[i]
ax2.bar(time_steps, baseline2_uav_connections_over_time[i], width=0.2, bottom=baseline2_bottom, label=f"Baseline 2 UAV {i}", alpha=0.5)
baseline2_bottom += baseline2_uav_connections_over_time[i]
ax2.bar(time_steps + 0.2, uav_connections_over_time[i], width=0.2, bottom=bottom, label=f"UAV {i}")
bottom += uav_connections_over_time[i]
ax2.set_title("Number of Ground Users Connected to Each UAV Over Time (Baseline vs Ours)")
ax2.set_xlabel("Time Steps")
ax2.set_ylabel("Total Number of GUs Connected")
ax2.legend(loc="upper left")
ax2.set_xticks(time_steps)
plt.tight_layout()
plt.show()
def visualize_simulation_with_multiple_baselines_styled(
uav_connections_TD, gu_capacity_TD,
baseline1_uav_connections_TD, baseline1_gu_capacity_TD,
baseline2_uav_connections_TD, baseline2_gu_capacity_TD,
num_uavs,
time_gap=1
):
import numpy as np
import matplotlib.pyplot as plt
min_capacity_over_time = []
avg_capacity_over_time = []
baseline1_min_capacity_over_time = []
baseline1_avg_capacity_over_time = []