-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReport.py
1323 lines (1122 loc) · 67.4 KB
/
Report.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
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 numpy as np
from collections import defaultdict
from math import inf, isnan
import os
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.colors import TwoSlopeNorm
import seaborn as sns
from FilePaths import *
from FileManagement import load_pickle
from PathSolution import *
from PathInfo import *
# from TimeBetweenVisits import calculate_mean_tbv
def format_percentage(value):
return f"%{value:.2f}" # Add '%' before each value
def get_visit_times(sol):
info = sol.info
drone_path_matrix = sol.real_time_path_matrix[1:,:]
visit_times = [[] for _ in range(info.number_of_cells)]
# print(f"Path Matrix:\n{drone_path_matrix}")
for cell in range(info.number_of_cells):
# print(f"cell {cell} visit steps: {np.where(sol.real_time_path_matrix==cell)[1].tolist()}")
visit_times[cell] = np.sort(np.where(drone_path_matrix==cell)[1])[:info.min_visits] # Last bit is to exclude hovering steps
# print("visit times:", visit_times)
sol.visit_times = visit_times
return visit_times
def calculate_tbv(sol):
get_visit_times(sol)
tbv = [np.diff(x) for x in sol.visit_times]
sol.tbv = tbv
# print("tbv:", tbv)
return tbv
def calculate_mean_tbv(sol):
calculate_tbv(sol)
mean_tbv = list(map(lambda x: np.mean(x), sol.tbv))
sol.mean_tbv = mean_tbv
sol.max_mean_tbv = max(sol.mean_tbv)
# print("mean tbv:", mean_tbv, "max mean tbv:", max(mean_tbv))
return sol.mean_tbv
def calculate_max_mean_tbv(sol):
calculate_mean_tbv(sol)
return sol.max_mean_tbv
# return max(sol.mean_tbv)
"""def get_visit_times(sol:PathSolution):
info = sol.info
drone_path_matrix = sol.real_time_path_matrix[1:,:]
visit_times = [[] for _ in range(info.number_of_cells)]
# print(f"Path Matrix:\n{drone_path_matrix}")
for cell in range(info.number_of_cells):
# print(f"cell {cell} visit steps: {np.where(sol.real_time_path_matrix==cell)[1].tolist()}")
visit_times[cell] = np.sort(np.where(drone_path_matrix==cell)[1])[:info.min_visits] # Last bit is to exclude hovering steps
sol.visit_times = visit_times
# print(f"Visit Times: {visit_times}")
return visit_times
def calculate_tbv(sol:PathSolution):
get_visit_times(sol)
tbv = [np.diff(x) for x in sol.visit_times]
sol.tbv = tbv
# print(f"TBV: {tbv}")
return tbv
def calculate_mean_tbv(sol:PathSolution):
calculate_tbv(sol)
sol.mean_tbv = list(map(lambda x: np.mean(x), sol.tbv))
# print(f"Mean TBV: {sol.mean_tbv}")
return sol.mean_tbv
def calculate_cumulative_mean_tbv(sol:PathSolution):
calculate_mean_tbv(sol)
return np.mean(sol.mean_tbv)
def calculate_var_of_mean_tbv(sol:PathSolution):
mean_tbv = get_visit_times(sol)
"""
def get_attribute(obj, attribute_name):
# Map input names to actual attribute names
attribute_mapping = {
"Mission Time": "mission_time",
"Percentage Connectivity": "percentage_connectivity",
"Max Mean TBV": "max_mean_tbv",
"Mean Disconnected Time": "mean_disconnected_time",
"Max Disconnected Time": "max_disconnected_time",
# Add more mappings as needed
}
# Check if the input attribute exists in the mapping
if attribute_name in attribute_mapping:
# Retrieve the actual attribute value using getattr
return getattr(obj, attribute_mapping[attribute_name])
else:
raise ValueError(f"Attribute '{attribute_name}' not found.")
# sample_sol = load_pickle(f"{solutions_filepath}MOO_NSGA2_time_conn_disconn_tbv_g_8_a_50_n_4_v_2.5_r_4_minv_2_maxv_5_Nt_1_tarPos_12_ptdet_0.99_pfdet_0.01_detTh_0.9_maxIso_0-Best-Mission_Time-Solution.pkl")
# test = get_attribute(sample_sol, "Max Mean TBV")
# print(test)
def get_attr(obj:str):
if obj == "Mission Time":
return PathSolution.misson_time
elif obj == "Percentage Connectivity":
return lambda x: x.percentage_connectivity
elif obj == "Max Mean TBV":
return lambda x: x.max_mean_tbv
elif obj == "Mean Disconnected Time":
return lambda x: x.mean_disconnected_time
elif obj == "Max Disconnected Time":
return lambda x: x.max_disconnected_time
else:
raise ValueError(f"Objective {obj} is not valid")
def get_attribute(sol:PathSolution, obj:str):
if obj == "Mission Time":
return sol.mission_time
elif obj == "Percentage Connectivity":
return sol.percentage_connectivity
elif obj == "Max Mean TBV":
return sol.max_mean_tbv
elif obj == "Mean Disconnected Time":
return sol.mean_disconnected_time
elif obj == "Max Disconnected Time":
return sol.max_disconnected_time
else:
raise ValueError(f"Objective {obj} is not valid")
def plot_objective_values(models, objectives=["Mission Time","Percentage Connectivity","Max Mean TBV","Mean Disconnected Time"], number_of_drones_values=[4,8,12,16], comm_cell_range_values=[2,2*sqrt(2),4], minv_values=[1,2,3], put_model_data_on_same_plot=True, show=True, save=False):
# ASSERTIONS
if not isinstance(models, list):
models = [models]
assert len(models) <= 2, "Up to two models can be compared"
if len(models) == 2:
assert(models[0]['Alg'] == models[1]['Alg']), "Algorithms must be the same"
# OBJECTIVE VALUE - UNIT MAPPING
objective_units = {
"Mission Time": "sec",
"Percentage Connectivity": "%",
"Max Mean TBV": "sec",
"Mean Disconnected Time": "timestep",
"Max Disconnected Time": "timestep"
}
# PLOT CUSTOMIZATION
linestyles = ['--','solid'] # For different models
colors = ["black" , "blue"] # For dfferent models
markers = ["o" , "x" , "^"] # For different minv values
# Intialize model_plots if you want to put all model data on the same plot with the same values for objective name and comm__range
model_plots = []
combine_counter = -1
combine_flag = len(objectives) * len(comm_cell_range_values) - 1
test_counter = -1
axes = []
for i , model in enumerate(models):
model_plots.append([])
color = colors[i]
linestyle = linestyles[i]
for j , objective_name in enumerate(objectives):
for k , comm_cell_range in enumerate(comm_cell_range_values):
comm_cell_range = "sqrt(8)" if comm_cell_range == 2*sqrt(2) else comm_cell_range
# Create figure and axis
fig, ax = plt.subplots()
ax.grid()
title = f"Best {objective_name} Values for Transmission Range: " + "$\sqrt{8}$ Cells" if comm_cell_range == "sqrt(8)" else f"Best {objective_name} Values for Transmission Range: {comm_cell_range} Cells"
ax.set_title(title, fontsize=12)
ax.set_xlabel("Number of Drones")
ax.set_ylabel(f"{objective_name} ({objective_units[objective_name]})")
ax.set_xticks(number_of_drones_values)
for l , minv in enumerate(minv_values):
marker = markers[l]
# Initialize y at every minv iteration
y = []
for m , number_of_drones in enumerate(number_of_drones_values):
# Get scenario
scenario = f"{model['Type']}_{model['Alg']}_{model['Exp']}_g_8_a_50_n_{number_of_drones}_v_2.5_r_{comm_cell_range}_minv_{minv}_maxv_5_Nt_1_tarPos_12_ptdet_0.99_pfdet_0.01_detTh_0.9_maxIso_0"
# Get the objective values
objective_values = pd.read_pickle(f"{objective_values_filepath}{scenario}-ObjectiveValues.pkl")
if objective_name in objective_values.columns:
best_objective_solution = pd.read_pickle(f"{solutions_filepath}{scenario}-Best-{objective_name.replace(' ','_')}-Solution.pkl")
best_objective_value = get_attribute(best_objective_solution, objective_name)*100 if objective_name == "Percentage Connectivity" else get_attribute(best_objective_solution, objective_name)
else:
solution_objects = pd.read_pickle(f"{solutions_filepath}{scenario}-SolutionObjects.pkl")
# First flatten solution_objects 2D array to get a list of PathSolution objects
solution_objects = solution_objects.flatten()
# Use lambda function to get the required objective value for each PathSolution object
objective_values = np.array(list(map(lambda x: get_attribute(x, objective_name), solution_objects)))
# objective_values = get_attribute(solution_objects, objective_name)
best_objective_value = objective_values.max()*100 if objective_name == "Percentage Connectivity" else objective_values.min()
# Best objective value for the scenario has been found, now append it to y values
y.append(best_objective_value)
# Plot y vs number_of_drones lineplot after all best objective values for each number_of_drones value have been appended to y
model_underscript_alg_comma_minv = "$" + model["Exp"] + "_" + "{" + model["Alg"] + "," + str(minv) + "}" + "$"
model_underscript_alg_superscript_minv = "$" + model["Exp"] + "_" + "{" + model["Alg"] + "}^{" + str(minv) + "}" + "$"
label = "$" + model["Exp"] + "_" + "{" + model["Alg"] + "," + str(minv) + "}" + "$"
# Add objective value vs. number of drones lineplot to the figure for current (objective_name, comm_cell_range, minv) combination
ax.plot(number_of_drones_values, y, linestyle=linestyle, linewidth=2, color=color, marker=marker, label=rf"{model_underscript_alg_superscript_minv}")
model_plots[-1].append((fig, ax))
axes.append(ax)
plt.close()
# combine_counter += 1
# # Combine models if put_model_data_on_same_plot==True
# if put_model_data_on_same_plot and combine_counter > combine_flag:
# test_counter += 1
# fig_combined, ax_combined = plt.subplots()
# axes_to_combine = [axes[combine_counter-(combine_flag+1)], axes[combine_counter]] # Take two axes corresponding to the same objective and comm_range but different models
# for ax in axes_to_combine:
# lines = ax.get_lines() # Retrieves ax lines
# for line in lines:
# ax_combined.plot(line.get_xdata(), line.get_ydata(), linestyle=line.get_linestyle(), linewidth=line.get_linewidth(), color=line.get_color(), marker=line.get_marker(), label=line.get_label())
# # Add title and legend
# ax_combined.set_title(f"Combined {ax.get_title()}")
# ax_combined.grid()
# ax_combined.set_xlabel(ax.get_xlabel())
# ax_combined.set_ylabel(ax.get_ylabel())
# # ax_combined.set_xticks([int(text.get_text()) for text in ax.get_xticklabels()])
# ax_combined.set_xticks(number_of_drones_values)
# ax_combined.legend(fontsize=12)
# # Save the figure
# if save:
# fig_combined.savefig(f"Figures/Combined_test_{test_counter}.png")
if put_model_data_on_same_plot:
num_plots = len(objectives) * len(comm_cell_range_values)
for i in range(num_plots):
fig_combined, ax_combined = plt.subplots()
axes_to_combine = []
for j in range(len(models)):
axes_to_combine.append(model_plots[j][i][1])
print(axes_to_combine, len(axes_to_combine))
for ax in axes_to_combine:
lines = ax.get_lines()
for line in lines:
ax_combined.plot(line.get_xdata(), line.get_ydata(), linestyle=line.get_linestyle(), linewidth=line.get_linewidth(), color=line.get_color(), marker=line.get_marker(), label=line.get_label())
# Add title and legend
ax_combined.set_title(ax.get_title())
ax_combined.grid()
ax_combined.set_xlabel(ax.get_xlabel())
ax_combined.set_ylabel(ax.get_ylabel())
ax_combined.legend()
if show:
plt.show()
""" model_plots[-1].append((fig, ax))
print("->", len(model_plots[0]))
# ((0,0),(1,0)), ((0,1),(1,1)), ((0,2),(1,2)), ((0,3),(1,3))
if put_model_data_on_same_plot:
num_plots = len(model_plots[0])
for i in range(num_plots):
fig_combined, ax_combined = plt.subplots()
fig_ax_to_combine = []
lines = []
for j in range(len(models)):
# fig_ax_to_combine.append(model_plots[j][i])
# lines.append(fig_ax_to_combine[-1][1].get_lines())
# print("->", fig_ax_to_combine)
fig, ax = model_plots[j][i]
lines = model_plots[j][i][1].get_lines()
for line in lines:
print(line.get_linestyle())
# marker = line.get_marker()
ax_combined.plot(line.get_xdata(), line.get_ydata(), linestyle=line.get_linestyle(), linewidth=line.get_linewidth(), color=line.get_color(), marker=line.get_marker(), label=line.get_label())
# Add title and legend
ax_combined.set_title(ax.get_title())
# print(ax.get_xticklabels())
xticks = [int(text.get_text()) for text in ax.get_xticklabels()]
ax_combined.set_xticks(xticks)
ax_combined.grid()
ax_combined.set_xlabel(ax.get_xlabel())
ax_combined.set_ylabel(ax.get_ylabel())
ax_combined.legend()
plt.show()
"""
def plot_conn_tbv_for_best_mission_time(show=True, save=False):
# TODO: Get tbv best mission time solutions
# print('MOO_NSGA2_time_conn_disconn_tbv_g_8_a_50_n_4_v_2.5_r_4_minv_2_maxv_5_Nt_1_tarPos_12_ptdet_0.99_pfdet_0.01_detTh_0.9_maxIso_0-Best-Mission_Time-Solution.pkl' == "MOO_NSGA2_time_conn_disconn_tbv_g_8_a_50_n_4_v_2.5_r_4_minv_2_maxv_5_Nt_1_tarPos_12_ptdet_0.99_pfdet_0.01_detTh_0.9_maxIso_0-Best-Mission_Time-Solution.pkl")
solution_filenames = [x for x in os.listdir(solutions_filepath) if "time_conn_disconn_tbv" in x and "NSGA2" in x and "Mission_Time" in x and ("minv_2" in x or "minv_3" in x)]
# Solutions include the individual best mission time solutions for NSGA2 TCDT Models with minv=2 and minv=3
# TODO: Plot tbv and conn for each solution. In every figure, there needs to be 4 plots (2 for minv=2 and 2 for minv=3) and that means 4 (x,y) pairs
colors = ["black", "blue"]
markers = ["o", "x"]
comm_ranges = ["2", "sqrt(8)", "4"]
nvisits = [2, 3]
numbers_of_drones = [4, 8, 12, 16]
for i,r in enumerate(comm_ranges):
# Create a figure for each communication range
fig, ax = plt.subplots()
ax.grid()
ax.set_title(f"Communication Range: {r} Connectivity and TBV Values for the Best Mission Time Solution", fontsize="small")
ax.set_xlabel("Number of Drones")
ax.set_ylabel("Value")
for j,v in enumerate(nvisits):
conn_values = []
tbv_values = []
for k,n in enumerate(numbers_of_drones):
required_solution_filename = [x for x in solution_filenames if f"n_{n}" in x and f"r_{r}" in x and f"minv_{v}" in x][0]
required_solution = load_pickle(f"{solutions_filepath}{required_solution_filename}")
conn_values.append(required_solution.percentage_connectivity*100)
tbv_values.append(required_solution.max_mean_tbv)
# Plot
ax.plot(numbers_of_drones, conn_values, color="black", marker=markers[j], label=f"Min Visits: {v} - Connectivity")
ax.plot(numbers_of_drones, tbv_values, color="blue", marker=markers[j], label=f"Min Visits: {v} - TBV")
ax.legend()
if show:
plt.show()
# plot_conn_tbv_for_best_mission_time()
def plot_pareto_fronts(show=True, save=False):
"""Plot the pareto fronts of the models"""
all_objective_filenames = np.array([x for x in os.listdir(objective_values_filepath) if "ObjectiveValues" in x and "time_conn_disconn_tbv" in x and "MOO" in x])
figures = []
for filename in all_objective_filenames:
# Get plot title
full_scenario = filename.split("-")[0]
title = full_scenario[:full_scenario.find("_maxv_")]
# print(title)
objective_values = pd.read_pickle(f"{objective_values_filepath}{filename}")
# fig, ax = plt.subplots()
# fig = plt.figure()
# ax = fig.add_subplot(111, projection='3d')
if "minv_1" not in title:
# Draw 3d pareto front
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(objective_values["Mission Time"], objective_values["Percentage Connectivity"], objective_values["Max Mean TBV as Objective"])
ax.set_title(f'{title} Pareto Front', fontsize="small")
ax.set_xlabel("Mission Time")
ax.set_ylabel("Percentage Connectivity")
ax.set_zlabel("Max Mean TBV as Objective")
# Hide the outer x and y axes
# ax.set_xticks([]) # Remove x-axis ticks
# ax.set_yticks([]) # Remove y-axis ticks
# ax.xaxis.line.set_visible(False) # Hide the x-axis line
# ax.yaxis.line.set_visible(False) # Hide the y-axis line
else:
fig, ax = plt.subplots()
ax.grid()
ax.scatter(objective_values["Mission Time"], objective_values["Percentage Connectivity"])
ax.set_title(f'{title} Pareto Front', fontsize="small")
ax.set_xlabel("Mission Time")
ax.set_ylabel("Percentage Connectivity")
if show:
plt.show()
if save:
fig.savefig(f"Figures/Pareto Fronts/{title}_pf.png")
# Reset fig, ax
fig, ax = None, None
# # Create figure and axis
# conn_vs_time_fig, conn_vs_time_ax = plt.subplots()
# figures.append(conn_vs_time_fig)
# conn_vs_time_ax.set_title(title, fontsize="small")
# conn_vs_time_ax.set_xlabel("Mission Time")
# conn_vs_time_ax.set_ylabel("Percentage Connectivity")
# conn_vs_time_ax.scatter(objective_values["Mission Time"], objective_values["Percentage Connectivity"])
# conn_vs_time_fig.text(0.5, 0.95, ha='center', s=f"Non-dominated solutions = {objective_values.shape[0]}", fontsize=10, color="black")
# if save:
# conn_vs_time_fig.savefig(f"Figures/Pareto Fronts/{title}_conn_vs_time_pf.png")
# # TBV Fronts
# if "minv_1" not in title:
# conn_vs_tbv_fig, conn_vs_tbv_ax = plt.subplots()
# figures.append(conn_vs_tbv_fig)
# conn_vs_tbv_ax.set_title(title, fontsize="small")
# conn_vs_tbv_ax.set_xlabel("Max Mean TBV")
# conn_vs_tbv_ax.set_ylabel("Percentage Connectivity")
# conn_vs_tbv_ax.scatter(objective_values["Max Mean TBV as Objective"], objective_values["Percentage Connectivity"])
# conn_vs_tbv_fig.text(0.5, 0.95, ha='center', s=f"Non-dominated solutions = {objective_values.shape[0]}", fontsize=10, color="black")
# time_vs_tbv_fig, time_vs_tbv_ax = plt.subplots()
# figures.append(time_vs_tbv_fig)
# time_vs_tbv_ax.set_title(title, fontsize="small")
# time_vs_tbv_ax.set_xlabel("Max Mean TBV")
# time_vs_tbv_ax.set_ylabel("Mission Time")
# time_vs_tbv_ax.scatter(objective_values["Max Mean TBV as Objective"], objective_values["Mission Time"])
# time_vs_tbv_fig.text(0.5, 0.95, ha='center', s=f"# Non-dominated solutions = {objective_values.shape[0]}", fontsize=10, color="black")
# if save:
# conn_vs_tbv_fig.savefig(f"Figures/Pareto Fronts/{title}_conn_vs_tbv_pf.png")
# time_vs_tbv_fig.savefig(f"Figures/Pareto Fronts/{title}_time_vs_tbv_pf.png")
# if show:
# plt.show()
# if "SOO" not in title:
# objective_values = pd.read_pickle(f"{objective_values_filepath}{filename}")
# # Create figure and axis
# conn_vs_time_fig, conn_vs_time_ax = plt.subplots()
# figures.append(conn_vs_time_fig)
# conn_vs_time_ax.set_title(title, fontsize="small")
# conn_vs_time_ax.set_xlabel("Mission Time")
# conn_vs_time_ax.set_ylabel("Percentage Connectivity")
# conn_vs_time_ax.scatter(objective_values["Mission Time"], objective_values["Percentage Connectivity"])
# if save:
# conn_vs_time_fig.savefig(f"Figures/Pareto Fronts/{title}_conn_vs_time_pf.png")
# if "Max Mean TBV as Objective" in objective_values.columns and "minv_1" not in title:
# conn_vs_tbv_fig, conn_vs_tbv_ax = plt.subplots()
# figures.append(conn_vs_tbv_fig)
# conn_vs_tbv_ax.set_title(title, fontsize="small")
# conn_vs_tbv_ax.set_xlabel("Max Mean TBV")
# conn_vs_tbv_ax.set_ylabel("Percentage Connectivity")
# conn_vs_tbv_ax.scatter(objective_values["Max Mean TBV as Objective"], objective_values["Percentage Connectivity"])
# if save:
# conn_vs_tbv_fig.savefig(f"Figures/Pareto Fronts/{title}_conn_vs_tbv_pf.png")
# time_vs_tbv_fig, time_vs_tbv_ax = plt.subplots()
# figures.append(time_vs_tbv_fig)
# time_vs_tbv_ax.set_title(title, fontsize="small")
# time_vs_tbv_ax.set_xlabel("Max Mean TBV")
# time_vs_tbv_ax.set_ylabel("Mission Time")
# time_vs_tbv_ax.scatter(objective_values["Max Mean TBV as Objective"], objective_values["Mission Time"])
# if save:
# time_vs_tbv_fig.savefig(f"Figures/Pareto Fronts/{title}_time_vs_tbv_pf.png")
# if show:
# plt.show()
# plot_pareto_fronts(show=False, save=True)
def compare_tbvs_heatmap(models=[TCDT_MOO_NSGA2, TCDT_MOO_NSGA3], r=2, numbers_of_drones=[4,8,12,16], numbers_of_visits=[2,3], show=True, save=False):
"""Compare TBV performances of models with the same algorithm"""
assert len(models) == 2, "Only two models can be compared"
assert(models[0]['Alg'] == models[1]['Alg']), "Algorithms must be the same"
alg = models[0]['Alg']
all_solution_filenames = [x for x in os.listdir(solutions_filepath) if "SolutionObjects" in x]
# Replace 2*sqrt(2) with sqrt(8)
if r == sqrt(8):
r = "sqrt(8)"
# Initialize a matrix to store the differences
diff_matrix = np.zeros((len(numbers_of_visits), len(numbers_of_drones)))
for i, v in enumerate(numbers_of_visits):
for j, n in enumerate(numbers_of_drones):
model_min_mean_tbvs = []
for model in models:
# Get the solution filename
try:
solutions_filename = [x for x in all_solution_filenames if f"r_{r}" in x and f"minv_{v}" in x and f"n_{n}" in x and model["Type"] in x and model["Alg"] in x and model["Exp"] in x][0]
except:
print(f"Model: {model['Exp']}, Alg: {model['Alg']}, n={n}, v={v}")
print(f"r_{r}, minv_{v}, n_{n}")
raise
solution_objects = load_pickle(f"{solutions_filepath}{solutions_filename}")
min_max_mean_tbv = inf
for x in solution_objects:
sol = x[0]
max_mean_tbv = calculate_max_mean_tbv(sol)
# print(f"Model: {model['Exp']}, Alg: {model['Alg']}, n={n}, v={v}")
# print(f"Visit Times: {sol.visit_times}")
# print(f"Path Matrix:\n{sol.real_time_path_matrix}")
# print(f"Model: {model['Exp']}, Alg: {model['Alg']}, n={n}, v={v}, Cumulative Mean TBV: {cum_mean_tbv}")
if max_mean_tbv < min_max_mean_tbv:
min_max_mean_tbv = max_mean_tbv
# cum_mean_tbvs = [calculate_cumulative_mean_tbv(x[0]) for x in solution_objects]
# min_cum_mean_tbv = min(cum_mean_tbvs)
model_min_mean_tbvs.append(min_max_mean_tbv)
# Calculate the difference between the two models
# print(model_min_mean_tbvs)
diff_matrix[i, j] = (model_min_mean_tbvs[0] - model_min_mean_tbvs[1])/model_min_mean_tbvs[0] * 100
# Create a heatmap
fig, ax = plt.subplots()
sns.heatmap(diff_matrix, annot=True, fmt=".2f", xticklabels=numbers_of_drones, yticklabels=numbers_of_visits, ax=ax)
ax.set_xlabel("Number of Drones")
ax.set_ylabel("Number of Visits")
title = f"Max Mean TBV Performance Comparison for Alg: {alg}, r={r} ({models[0]['Exp']} - {models[1]['Exp']})"
ax.set_title(title, fontsize="small")
# Save plot
if save:
fig.savefig(f"Figures/Time Between Visits/Max Mean TBV Performance Comparison between Models/alg_{alg}_r_{r}_max_mean_tbv_performance_comparison.png")
# Show plot
if show:
plt.show()
else:
plt.close(fig)
def compare_objs_for_models_heatmap(models=[TCDT_MOO_NSGA2, TCDT_MOO_NSGA3], r=2, numbers_of_drones=[4,8,12,16], numbers_of_visits=[2,3], show=True, save=False):
"""Show the gain in performance for the models (one without tbv and one with tbv same algorithm)"""
assert len(models) == 2, "Only two models can be compared"
assert(models[0]['Alg'] == models[1]['Alg']), "Algorithms must be the same"
alg = models[0]['Alg']
all_objective_filenames = os.listdir(objective_values_filepath)
all_solution_filenames = os.listdir(solutions_filepath)
# Replace 2*sqrt(2) with sqrt(8)
if r == sqrt(8):
r = "sqrt(8)"
# Get common objectives between models
objective_names = models[0]["F"]
for objective in objective_names:
if objective not in models[1]["F"]:
objective_names.remove(objective)
for objective_name in objective_names:
# print(f"Objective: {objective_name}")
# Initialize a matrix to store the differences
# diff_matrix = np.empty((len(numbers_of_visits), len(numbers_of_drones)), dtype=str)
# print(f"Diff Matrix: {diff_matrix}")
# diff_matrix = np.empty((len(numbers_of_visits), len(numbers_of_drones)), dtype=str)
diff_matrix = np.zeros((len(numbers_of_visits), len(numbers_of_drones)))
for i, v in enumerate(numbers_of_visits):
for j, n in enumerate(numbers_of_drones):
# print(f"Number of Drones: {n}, Number of Visits: {v}")
model_best_objective_values = []
for model in models:
# print(f"Model: {model['Exp']}")
objective_filename = [x for x in all_objective_filenames if f"r_{r}" in x and f"minv_{v}" in x and f"n_{n}" in x and model["Type"] in x and model["Alg"] in x and model["Exp"] in x][0]
# print(f"Objective Filename: {objective_filename}")
objective_values = pd.read_pickle(f"{objective_values_filepath}{objective_filename}")
# print(f"Objective Values: {np.array(sorted(objective_values[objective_name]))}")
best_objective_value = min(objective_values[objective_name]) if objective_name != "Percentage Connectivity" else max(objective_values[objective_name])
# print(f"Best Objective Value for {model['Exp']}: {best_objective_value}")
model_best_objective_values.append(best_objective_value)
# Calculate the difference between the two models (percentage change)
perc_diff = (model_best_objective_values[0] - model_best_objective_values[1])/model_best_objective_values[0] * 100
diff_matrix[i, j] = perc_diff
# diff_matrix[i, j] = (model_best_objective_values[0] - model_best_objective_values[1])/model_best_objective_values[0] * 100
print(diff_matrix[i, j], perc_diff)
# print(f"Value 1: {model_best_objective_values[0]}, Value 2: {model_best_objective_values[1]}, Difference: {diff_matrix[i, j]}")
# Create a heatmap
fig, ax = plt.subplots()
if objective_name != "Percentage Connectivity":
cmap = sns.diverging_palette(150, 10, as_cmap=True) # Green to Red with white in the middle
else:
cmap = sns.diverging_palette(10, 150, as_cmap=True) # Red to Green with white in the middle
# Normalize data so that 0 corresponds to white
norm = TwoSlopeNorm(vmin=diff_matrix.min(), vcenter=0, vmax=diff_matrix.max())
sns.heatmap(diff_matrix, annot=np.vectorize(format_percentage)(diff_matrix), cmap=cmap, fmt="", annot_kws={"fontsize": 10}, cbar_kws={'label': 'Color Bar'}, xticklabels=numbers_of_drones, yticklabels=numbers_of_visits, ax=ax)
# sns.heatmap(diff_matrix, annot=True, fmt=".2f", xticklabels=numbers_of_drones, yticklabels=numbers_of_visits, ax=ax)
ax.set_xlabel("Number of Drones")
ax.set_ylabel("Number of Visits")
ax.set_title(f"Difference in {objective_name} Performance for Communication Range: {r} ({alg})", fontsize="small")
# Save plot
if save:
fig.savefig(f"Figures/Objective Values/{alg}_{objective_name.replace(' ', '_')}_performance_comparison_between_models.png")
# Show plot
if show:
plt.show()
else:
plt.close(fig)
def compare_objs_for_models_lineplot(models=[TCDT_MOO_NSGA2, TCDT_MOO_NSGA3], r=2, numbers_of_drones=[4,8,12,16], numbers_of_visits=[2,3], show=True, save=False):
"""Show the gain in performance for the models (one without tbv and one with tbv same algorithm)"""
assert len(models) == 2, "Only two models can be compared"
assert(models[0]['Alg'] == models[1]['Alg']), "Algorithms must be the same"
alg = models[0]['Alg']
all_objective_filenames = os.listdir(objective_values_filepath)
all_solution_filenames = os.listdir(solutions_filepath)
# Replace 2*sqrt(2) with sqrt(8)
if r == sqrt(8):
r = "sqrt(8)"
# Get common objectives between models
objective_names = models[0]["F"]
for objective in objective_names:
if objective not in models[1]["F"]:
objective_names.remove(objective)
# TODO: Obtain y-values for plots (best obj for r=2, v=(2,3) for each model)
for objective_name in objective_names:
fig, ax = plt.subplots()
ax.grid()
ax.set_xticks(numbers_of_drones)
ax.set_xlabel("Number of Drones")
ax.set_ylabel(objective_name)
ax.set_title(f"{objective_name} Performance Comparison for Communication Range: {r} ({alg})", fontsize="small")
for model in models:
for v in numbers_of_visits:
y = []
for n in numbers_of_drones:
objective_filename = [x for x in all_objective_filenames if f"r_{r}" in x and f"v_{v}" in x and f"n_{n}" in x and model["Type"] in x and model["Alg"] in x and model["Exp"] in x][0]
# solution_filename = [x for x in all_solution_filenames if f"r_{r}" in x and f"v_{v}" in x and f"n_{n}" in x and model["Type"] in x and model["Alg"] in x and model["Exp"] and "" in x]
objective_values = load_pickle(f"{objective_values_filepath}{objective_filename}")
best_objective_value = min(objective_values[objective_name]) if objective_name != "Percentage Connectivity" else max(objective_values[objective_name])
y.append(best_objective_value)
ax.plot(numbers_of_drones, y, linestyle="dashdot", marker="o", label=f"{model['Exp']} - {v} Visit(s)")
ax.legend()
if show:
plt.show()
if save:
pass
# fig.savefig(f"Figures/Performance Comparison/{alg}_performance_comparison.png")
# END TODO
def lineplot_for_runtimes(alg:str, model_exp:str, comm_ranges:list, numbers_of_drones:list, numbers_of_visits:list, show=True, save=False):
"""Plot average runtimes for different communication ranges, numbers of drones and visits"""
assert isinstance(comm_ranges, list) and all((isinstance(x, int) or isinstance(x, float)) and x > 0 for x in comm_ranges), "comm_ranges must be a list of numbers greater than 0"
assert isinstance(numbers_of_drones, list) and all(isinstance(x, int) and x > 0 for x in numbers_of_drones), "numbers_of_drones must be a list of integers greater than 0"
assert isinstance(numbers_of_visits, list) and all(isinstance(x, int) and x > 0 for x in numbers_of_visits), "numbers_of_visits must be a list of integers greater than 0"
runtimes_filenames = os.listdir(runtimes_filepath)
filtered_runtime_filenames = [x for x in runtimes_filenames if model_exp in x]
# Create x for plot
x = numbers_of_drones
fig, ax = plt.subplots()
ax.grid()
ax.set_xticks(numbers_of_drones)
ax.set_xlabel("Number of Drones")
ax.set_ylabel("Average Runtime")
ax.set_title(f"{alg} Average Runtime for Different Numbers of Drones and Communication Ranges", fontsize="small")
for v in numbers_of_visits:
print(f"Number of Visits: {v}", sep=" ")
# Initialize y for plot
y = []
# Create a figure and axis
for n in numbers_of_drones:
print(f"Number of Drones: {n}", sep=" ")
runtimes = []
for r in comm_ranges:
if r == 2*sqrt(2):
r = "sqrt(8)"
print(f"Comm Range: {r}", sep="\n")
# print("Filter test:", len([x for x in filtered_runtime_filenames if f"v_{v}" in x and f"n_{n}" in x and f"r_{r}" in x])==1)
filtered_runtime_filenames = [x for x in runtimes_filenames if f"v_{v}" in x and f"n_{n}" in x and f"r_{r}" in x and alg in x]
print("Filenames:\n",np.array(filtered_runtime_filenames))
runtime = load_pickle(f"{runtimes_filepath}{filtered_runtime_filenames[0]}") if len(filtered_runtime_filenames)==1 else sum([load_pickle(f"{runtimes_filepath}{x}") for x in filtered_runtime_filenames])
print(f"Runtime: {runtime}")
# runtime = load_pickle(f"{runtimes_filepath}{filtered_runtime_filenames[0] if len(filtered_runtime_filenames)==1 else filtered_runtime_filename[0]}")
# runtime = load_pickle(f"{runtimes_filepath}{runtime_filename}")
runtimes.append(runtime)
average_runtime = np.mean(runtimes)
# Update y for plot
y.append(average_runtime)
# Add lineplot for v visit(s) to the figure
ax.plot(x, y, marker='o', label=f"{v} Visit(s)")
# Add a legend to the plot
ax.legend()
# Show plot
if show:
plt.show()
# Save plot
if save:
fig.savefig(f"Figures/Average Runtimes/{model_exp}_runtimes.png")
def compare_average_runtimes_for_different_algorithms(algorithms: list, model_exp:str):
# test
# test = load_pickle("Results/Runtimes/MOO_NSGA3_time_conn_disconn_tbv_g_8_a_50_n_12_v_2.5_r_2_minv_1_maxv_5_Nt_1_tarPos_12_ptdet_0.99_pfdet_0.01_detTh_0.9_maxIso_0-Runtime.pkl")
# print(test)
# return
runtime_filenames = os.listdir(runtimes_filepath)
average_runtimes = dict()
for alg in algorithms:
# print(f"Algorithm: {alg}")
average_runtimes[alg] = []
# print(np.array(runtime_filenames))
alg_runtime_filenames = [x for x in runtime_filenames if model_exp in x and alg in x]
for filename in alg_runtime_filenames:
runtime = load_pickle(f"{runtimes_filepath}{filename}")
# print(f"Runtime: {runtime}")
average_runtimes[alg].append(runtime)
average_runtimes[alg] = np.round(np.mean(average_runtimes[alg])/60) # Convert to minutes
print(average_runtimes)
return average_runtimes
def model_comparison_heatmap_for_best_objs(models: list, r, numbers_of_drones: list, numbers_of_visits: list, show=True, save=False):
"""Plot a heatmap for comparison of the best objective values of the models for different numbers of drones and visits"""
# K.I.S.S. - Keep It Simple Stupid by Michael Scott
assert len(models) == 2, "Only two models can be compared"
assert isinstance(r, (float, int)), "Communication range should be a number"
assert isinstance(numbers_of_drones, list) and all(isinstance(x, int) and x > 0 for x in numbers_of_drones), "numbers_of_drones must be a list of integers greater than 0"
assert isinstance(numbers_of_visits, list) and all(isinstance(x, int) and x > 0 for x in numbers_of_visits), "numbers_of_visits must be a list of integers greater than 0"
objectives_folder_filenames = os.listdir(objective_values_filepath)
# First filter by communication range
filtered_objective_filenames = [x for x in objectives_folder_filenames if f"r_{r}" in x]
# filtered_objective_filenames = deepcopy(objectives_folder_filenames)
# Get common objectives between models
objective_names = models[0]["F"]
for objective_name in objective_names:
for model in models[1:]:
if objective_name not in model["F"]:
objective_names.remove(objective_name)
for objective_name in objective_names:
# Create a figure and axis
fig, ax = plt.subplots()
# ax.grid()
ax.set_xticks(numbers_of_drones)
ax.set_xlabel("Number of Drones")
ax.set_ylabel(f"Objective: {objective_name}")
ax.set_title(f"Best {objective_name} Comparison by Algorithm ({models[0]['Alg']} - {models[1]['Alg']})", fontsize=10)
best_objective_values_for_models = {model["Alg"]: [] for model in models}
for v in numbers_of_visits:
row = []
for n in numbers_of_drones:
model_best_objective_values = []
for model in models:
objective_filename = [x for x in filtered_objective_filenames if model["Type"] in x and model["Alg"] in x and model["Exp"] in x and f"v_{v}" in x and f"n_{n}" in x][0]
objective_values = pd.read_pickle(f"{objective_values_filepath}{objective_filename}")[objective_name]
best_objective_value = min(objective_values) if objective_name != "Percentage Connectivity" else max(objective_values)
model_best_objective_values.append(best_objective_value)
model_best_objective_values_diff = model_best_objective_values[0] - model_best_objective_values[1]
# print(model_best_objective_values_diff)
row.append(model_best_objective_values_diff)
best_objective_values_for_models[models[0]["Alg"]].append(row)
# Convert to numpy array for heatmap
data = np.array(best_objective_values_for_models[models[0]["Alg"]])
# Plot heatmap
sns.heatmap(data, annot=True, fmt=".2f", xticklabels=numbers_of_drones, yticklabels=numbers_of_visits, ax=ax)
# Save plot
if save:
fig.savefig(f"Figures/Heatmaps/r_{r}_comparison_{objective_name.replace(' ', '_')}.png")
# Show plot
if show:
plt.show()
else:
plt.close(fig)
"""
def model_comparison_heatmap_for_best_objs(models:list, r, numbers_of_drones:list, numbers_of_visits:list, show=True, save=False):
# K.I.S.S. - Keep It Simple Stupid by Michael Scott
assert(len(models) == 2), "Only two models can be compared"
assert(isinstance(r, float) or isinstance(r, int)), "Communication range should be a number"
assert all(isinstance(x, int) and x > 0 for x in numbers_of_visits), "All elements in the list must be integers greater than 0"
objectives_folder_filenames = os.listdir(objective_values_filepath)
# First filter by communication range
filtered_objective_filenames = [x for x in objectives_folder_filenames if f"r_{r}" in x]
# filtered_objective_filenames = deepcopy(objectives_folder_filenames)
# Get common objectives between models
objective_names = models[0]["F"]
for objective_name in objective_names:
for model in models[1:]:
if objective_name not in model["F"]:
objective_names.remove(objective_name)
for objective_name in objective_names:
# Create a figure and axis
fig, ax = plt.subplots()
ax.grid()
ax.set_xticks(numbers_of_drones)
ax.set_x_label("Number of Drones")
ax.set_y_label(f"Objective: {objective_name}")
ax.set_title(f"Best {objective_name} Comparison for by Algorithm")
best_objective_values_for_models = dict()
for v in numbers_of_visits:
filtered_objective_filenames = [x for x in filtered_objective_filenames if f"v_{v}" in x]
for n in numbers_of_drones:
filtered_objective_filenames = [x for x in filtered_objective_filenames if f"n_{n}" in x]
model_best_objective_values = []
for model in models:
objective_filename = [x for x in filtered_objective_filenames if model["Type"] in x and model["Alg"] in x and model["Exp"] in x][0]
objective_values = pd.read_pickle(f"{objective_values_filepath}{objective_filename}")[objective_name]
best_objective_value = min(objective_values) if objective_name != "Percentage Connectivity" else max(objective_values)
model_best_objective_values.append(best_objective_value)
model_best_objective_values_diff = model_best_objective_values[0] - model_best_objective_values[1]
# for model in models:
# best_objective_values_for_models[model["Alg"]] = dict()
# # Filter by model
# filtered_objective_filenames = [x for x in filtered_objective_filenames if model["Type"] in x and model["Alg"] in x and model["Exp"] in x]
# for v in numbers_of_visits:
# best_objective_values_for_models[model["Alg"]][v] = []
# y = []
# # Filter by number of visits
# filtered_objective_filenames = [x for x in filtered_objective_filenames if f"v_{v}" in x]
# for n in numbers_of_drones:
# # Filter by number of drones
# filtered_objective_filenames = [x for x in filtered_objective_filenames if f"n_{n}" in x]
# # Load the objective values
# objective_values = pd.read_pickle(f"{objective_values_filepath}{filtered_objective_filenames[0]}")
# # Get the objective values
# objective_values = objective_values[objective_name]
# # Get the best objective value
# best_objective_value = min(objective_values) if objective_name != "Percentage Connectivity" else max(objective_values)
# # Append the best objective value to the list
# best_objective_values_for_models[model["Alg"]][v].append(best_objective_value)
# best_objective_values_for_models_diff = deepcopy(best_objective_values_for_models)
"""
def plot_best_objs_for_nvisits(models, r, n, v, show=False, save=True):
assert len(models) <= 2, "Only two models can be compared"
if len(models) == 2:
assert models[0]['Alg'] == models[1]['Alg'], "Algorithms must be the same"
if not isinstance(models, list):
models = [models]
if isinstance(r, float) or isinstance(r, int):
r = [r]
if isinstance(n, float) or isinstance(n, int):
n = [n]
if isinstance(v, float) or isinstance(v, int):
n = [v]
# Define linestyles for different models
linestyles = ['--','solid']
markers = ['o', '>', 'x']
linecolors = ['black', 'blue']
markerfacecolors = ['none', 'none'] # Hollow and filled markers
markeredgecolors = ['black', 'blue'] # Edge colors for markers
unit_dict = {"Mission Time": "sec", "Percentage Connectivity": "%", "Max Mean TBV": "sec", "Max Disconnected Time":"", "Mean Disconnected Time":""}
# nvisit 1 icin o, 2 icin >, 3 icin x marker
# Get common objectives between models
if len(models) == 2:
objective_names = [x for x in models[0]["F"] if x in models[1]["F"]]
else:
objective_names = models[0]["F"]
if "Max Mean TBV" not in objective_names:
objective_names.append("Max Mean TBV")
all_objective_filenames = os.listdir(objective_values_filepath)
all_solution_filenames = [x for x in os.listdir(solutions_filepath) if "SolutionObjects" in x]
for objective in objective_names:
if objective == "Max Mean TBV as Objective":
v_new = [x for x in v if x!=1]
else:
v_new = v
print(objective, v_new)
for r_value in r:
debug_counter = 0
# Create a figure and axis
fig, ax = plt.subplots(figsize=(8, 6))
ax.set_xticks(n)
ax.grid()
ax.set_xlabel('Number of Drones')
if objective == "Max Mean TBV":
ax.set_ylabel(f"Max Mean TBV ({unit_dict[objective]})")
else:
ax.set_ylabel(f"{objective} ({unit_dict[objective]})")
# Modify r_value
if r_value == sqrt(8):
r_value = "sqrt(8)"
for i, model in enumerate(models):
linestyle = linestyles[models.index(model)]
color = linecolors[models.index(model)]
# marker = markers[models.index(model)]
markerfacecolor = markerfacecolors[models.index(model)]
markeredgecolor = markeredgecolors[models.index(model)]
for j, v_value in enumerate(v_new):
marker = markers[j]
y = []
for n_value in n:
# Get the solution filename
# print(f"Model: {model['Exp']}, Alg: {model['Alg']}, r={r_value}, n={n_value}, v={v_value}")
solutions_filename = [x for x in all_solution_filenames if f"r_{r_value}" in x and f"minv_{v_value}" in x and f"n_{n_value}" in x and f'{model["Type"]}_{model["Alg"]}_{model["Exp"]}_g' in x][0]
objective_filename = [x for x in all_objective_filenames if f"r_{r_value}" in x and f"minv_{v_value}" in x and f"n_{n_value}" in x and f'{model["Type"]}_{model["Alg"]}_{model["Exp"]}_g' in x][0]
# print(f"Objective Filename: {objective_filename}")
if objective == "Max Mean TBV":
# Find best max mean tbv through the solutions
max_mean_tbvs = []
for x in load_pickle(f"{solutions_filepath}{solutions_filename}"):
sol = x[0] if isinstance(x, np.ndarray) else x
# max_mean_tbv = calculate_max_mean_tbv(sol)
# print(f"Sol: {str(sol.info)}")
print("ERROR !!!") if sol.mission_time < sol.max_mean_tbv else None
max_mean_tbvs.append(sol.max_mean_tbv)
best_objective_value = min(max_mean_tbvs)
print(best_objective_value)
else:
objective_values = pd.read_pickle(f"{objective_values_filepath}{objective_filename}")[objective]
best_objective_value = min(objective_values) if objective != "Percentage Connectivity" else max(objective_values)*100
if best_objective_value is not None:
debug_counter += 1
# print(f"Model: {model['Exp']}, Alg: {model['Alg']}, r={r_value}, n={n_value}, v={v_value}, Objective: {objective}, Best Value: {best_objective_value}")
y.append(best_objective_value)
# if objective=="Mission Time" and n_value==16 and v_value==2 and r_value==4:
# print(objective_filename, objective_values, best_objective_value)
# print(f"Debug Counter: {debug_counter}")
if model["Exp"] == "time_conn_disconn":
ax.plot(n, y, linestyle=linestyle, color=color, linewidth=2, marker=marker, markerfacecolor=markerfacecolor, markeredgecolor=markeredgecolor, label=f'TCD - {v_value} Visit(s)')
else:
ax.plot(n, y, linestyle=linestyle, color=color, linewidth=2, marker=marker, markerfacecolor=markerfacecolor, markeredgecolor=markeredgecolor, label=f'TCDT - {v_value} Visit(s)')
# ax.plot(n, y, linestyle=linestyle, marker=marker, markerfacecolor=markerfacecolor, label=f'{model["Exp"]} - {v_value} Visit(s)')
# if save:
# fig.savefig(f"Figures/Objective Values/{model['Alg']}_r_{r_value}_{objective.replace(' ', '_')}_best_values.png")
# Set the title
if objective == "Max Mean TBV as Objective":
ax.set_title(f'Best Max Mean TBV Values for {r_value} Cell(s) Communication Range', pad=60, fontsize=12)
else:
ax.set_title(f'Best {objective} Values for {r_value} Cell(s) Communication Range', pad=60, fontsize=12)
# Adjust the plot area to make space for the legend
fig.subplots_adjust(top=0.8) # 0.8
# Add a legend to the plot
ax.legend(ncol=3, loc="upper center", bbox_to_anchor=(0.5, 1.185), fontsize=12)
# Annotate
# for j in range(len(n)):
# ax.annotate(f'{round(y[j], 2)}', (n[j], n[j]), textcoords="offset points", xytext=(0,5), ha='center')
# Save plot
if save:
# print(model['Alg'])
fig.savefig(f"Figures/Objective Values/{model['Alg']}_r_{r_value}_{objective.replace(' ', '_')}_best_values.png")
# Show plot
if show:
plt.show()
"""def plot_best_objs_for_nvisits(model, r, numbers_of_drones:list, numbers_of_visits:list, show=False, save=True):
# directions = ["Best", "Mid", "Worst"]
info_dict=PathInfo()
info_dict.model = model
info_dict.comm_cell_range = r
y_values_list = [ dict() for _ in range(len(numbers_of_visits))]
for i,v in enumerate(numbers_of_visits):
info_dict.min_visits = v
for n in numbers_of_drones:
info_dict.number_of_drones = n
scenario = str(info_dict)
v_visits_n_drones_all_objective_values = pd.read_pickle(f"{objective_values_filepath}{scenario}-ObjectiveValues.pkl")
objective_names = v_visits_n_drones_all_objective_values.columns
for objective in objective_names:
# print(y_values_list[v])
if objective not in list(y_values_list[i].keys()):
y_values_list[i][objective] = []
# print(y_values_list[v])