-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
1041 lines (1021 loc) · 34.2 KB
/
run.py
File metadata and controls
1041 lines (1021 loc) · 34.2 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
from logs_postprocessing import parse_log_file, get_faasmacro_runtime
from run_centralized_model import load_configuration
from run_centralized_model import run as run_centralized
from run_faasmacro import run as run_iterations
from run_faasmadea import run as run_auction
from postprocessing import load_models_results
from utilities import reconcile_paths
import matplotlib.colors as mcolors
import matplotlib.pyplot as plt
from copy import deepcopy
from parse import parse
from tqdm import tqdm
import pandas as pd
import numpy as np
import argparse
import json
import os
import logging
logging.getLogger('pyomo.core').setLevel(logging.ERROR)
def parse_arguments() -> argparse.Namespace:
"""
Parse input arguments
"""
parser: argparse.ArgumentParser = argparse.ArgumentParser(
description="Run LMM, FaaS-MACrO and/or FaaS-MADeA on multiple experiments",
formatter_class = argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
"-c", "--config",
help = "Configuration file",
type = str,
default = "config_files/config.json"
)
parser.add_argument(
"--n_experiments",
help = "Number of experiments to run for each configuration",
type = int,
default = 3
)
parser.add_argument(
"--methods",
type = str,
nargs = "+",
choices = [
"centralized",
"faas-macro-v0",
"faas-macro",
"faas-madea",
"generate_only"
],
required = True
)
parser.add_argument(
"--postprocessing_only",
default = False,
action = "store_true"
)
parser.add_argument(
"--postprocessing_list",
help = "To be used in conjunction with postprocessing_only. True if the "
"base_solution_folder includes multiple subfolders to post-process",
default = False,
action = "store_true"
)
parser.add_argument(
"--fix_r",
help = "True to fix the number of replicas in FaaS-MACrO and/or "
"FaaS-MADeA according to the optimal centralized solution",
default = False,
action = "store_true"
)
parser.add_argument(
"-j", "--sp_parallelism",
help = "Number of parallel processes to start (-1: auto, 0: sequential)",
type = int,
default = -1
)
parser.add_argument(
"--enable_plotting",
default = False,
action = "store_true"
)
parser.add_argument(
"--loop_over",
help = "Key to loop over",
type = str,
default = "Nn"
)
# Parse the arguments
args: argparse.Namespace = parser.parse_known_args()[0]
return args
def generate_experiments_list(exp_values, seed, n_experiments):
rng = np.random.default_rng(seed)
# list of exp values
exp_list = exp_values.get("values", [])
if len(exp_list) == 0:
step = exp_values.get("step", 1)
exp_list = list(range(exp_values["min"], exp_values["max"] + step, step))
# seed(s)
seed_list = [seed] + rng.integers(
1000, 10000, endpoint = True, size = (n_experiments - 1,)
).tolist()
# list of experiments
return [[exp_value, int(s)] for s in seed_list for exp_value in exp_list]
def load_obj_value(solution_folder: str) -> pd.DataFrame:
obj = pd.DataFrame()
if os.path.exists(os.path.join(solution_folder, "obj.csv")):
obj = pd.read_csv(
os.path.join(solution_folder, "obj.csv")
)
obj = obj.loc[:,~obj.columns.str.startswith("Unnamed")]
for key in ["FaaS-MACrO", "SP/coord"]:
if key in obj:
obj.rename(columns = {key: "FaaS-MACrO"}, inplace = True)
return obj
def load_termination_condition(
solution_folder: str, centralized: bool = False
) -> pd.DataFrame:
tc = pd.DataFrame()
if os.path.exists(
os.path.join(solution_folder, "termination_condition.csv")
):
tc = pd.read_csv(
os.path.join(solution_folder, "termination_condition.csv")
)
if not centralized:
tc.rename(columns = {"Unnamed: 0": "time"}, inplace = True)
criterion = []
iteration = []
deviation = []
best_it = []
for s in tc["0"]:
c, i, d, b, bc, trt = [None] * 6
if "total runtime" in s:
if "centralized" in s:
c, i, d, b, bc, trt = parse(
"{} (it: {}; obj. deviation: {}; best it: {}; best centralized it: {}; total runtime: {})",
s
)
else:
c, i, d, b, trt = parse(
"{} (it: {}; obj. deviation: {}; best it: {}; total runtime: {})",
s
)
elif "best it" in s:
c, i, d, b = parse(
"{} (it: {}; obj. deviation: {}; best it: {})",
s
)
else:
c, i, d = parse(
"{} (it: {}; obj. deviation: {})",
s
)
if c.startswith("reached time limit"):
c1, c2 = parse("reached time limit: {} >= {}", c)
c = f"reached time limit ({c2})"
criterion.append(c)
iteration.append(int(i))
deviation.append(float(d) if d != "None" else d)
best_it.append(int(b) if b is not None else b)
tc.drop("0", axis = "columns", inplace = True)
tc["criterion"] = criterion
tc["iteration"] = iteration
tc["deviation"] = deviation
tc["best_iteration"] = best_it
else:
tc["time"] = tc.index
return tc
def merge_sol_dict(results_list: list, methods_names: list) -> pd.DataFrame:
res = results_list[0]["tot"].rename(
columns = {"tot": f"tot_{methods_names[0]}"}
)
for r,m in zip(results_list[1:], methods_names[1:]):
res = res.join(r["tot"], rsuffix = f"_{m}")
res["time"] = "tot"
for key in results_list[0]:
if key != "tot":
time = int(key.split(" ")[-1])
df = results_list[0][key].rename(
columns = {key: f"{key}_{methods_names[0]}"}
)
for r,m in zip(results_list[1:], methods_names[1:]):
df = df.join(r[key], rsuffix = f"_{m}")
df["time"] = time
res = pd.concat([res, df])
return res
def plot_total_count(df: pd.DataFrame, plot_filename: str):
df[df["time"]=="tot"].drop(columns = "time").plot.bar(
rot = 0,
logy = True
)
plt.grid(True, which = "both")
plt.savefig(plot_filename, dpi = 300, format = "png", bbox_inches = "tight")
plt.close()
def results_postprocessing(
solution_folders: dict,
base_folder: str,
loop_over: str,
methods: list
):
# prepare folder to store plots
plot_folder = os.path.join(base_folder, "postprocessing")
os.makedirs(plot_folder, exist_ok = True)
all_obj_values = pd.DataFrame()
all_rej_values = pd.DataFrame()
all_runtime_values = pd.DataFrame()
all_tc = pd.DataFrame()
ping_pong_list = []
# loop over experiments
for tokens in zip(
solution_folders["experiments_list"],
*[solution_folders[m] for m in methods]
):
exp_description_tuple = tokens[0]
print(f"Postprocessing exp: {exp_description_tuple}")
# prepare folder to store results
exp_description = "_".join([str(s) for s in exp_description_tuple])
exp_plot_folder = os.path.join(plot_folder, exp_description)
os.makedirs(exp_plot_folder, exist_ok = True)
# convert relative to absolute paths and load results
abs_folders = []
results = []
found_methods = []
method_colors = [
mcolors.TABLEAU_COLORS["tab:blue"],
mcolors.TABLEAU_COLORS["tab:orange"],
mcolors.TABLEAU_COLORS["tab:red"],
mcolors.TABLEAU_COLORS["tab:green"],
mcolors.TABLEAU_COLORS["tab:pink"]
]
for method, method_folder in zip(methods, tokens[1:]):
if method_folder is not None:
abs_folders.append(
reconcile_paths(base_solution_folder, method_folder)
)
# -- load results
# ---- local_count, fwd_count, rej_count, replicas, ping_pong
mkey = "LoadManagementModel" if method == "centralized" else (
"LSP" if method.startswith("faas-macro") else "LSPc"
)
mname = "LoadManagementModel" if method == "centralized" else (
"FaaS-MACrO" if method == "faas-macro" else (
"FaaS-MACrO(v0)" if method == "faas-macro-v0" else "FaaS-MADeA"
)
)
results.append(load_models_results(abs_folders[-1], [mkey], [mname]))
# -- check ping-pong problems
if len(results[-1][-1][mname]) > 0:
ping_pong_list.append([exp_description, method, method_folder])
found_methods.append(mname)
# merge solutions
if len(results) > 0:
local_count = merge_sol_dict(
[res[0]["by_function"] for res in results], found_methods
)
fwd_count = merge_sol_dict(
[res[1]["by_function"] for res in results], found_methods
)
rej_count = merge_sol_dict(
[res[2]["by_function"] for res in results], found_methods
)
# plot
plot_total_count(
local_count, os.path.join(exp_plot_folder, "loc_by_function.png")
)
plot_total_count(
fwd_count, os.path.join(exp_plot_folder, "fwd_by_function.png")
)
plot_total_count(
rej_count, os.path.join(exp_plot_folder, "rej_by_function.png")
)
# save
local_count.to_csv(os.path.join(exp_plot_folder, "loc_by_function.csv"))
fwd_count.to_csv(os.path.join(exp_plot_folder, "fwd_by_function.csv"))
rej_count.to_csv(os.path.join(exp_plot_folder, "rej_by_function.csv"))
# total rejections
all_rej = rej_count.groupby("time").sum()
all_req = (
local_count.groupby("time").sum() + fwd_count.groupby("time").sum()
) + all_rej
all_rej = all_rej / all_req * 100
# objective function value
obj = load_obj_value(abs_folders[0])
obj.columns = [found_methods[0]]
for af, mname in zip(abs_folders[1:], found_methods[1:]):
temp = load_obj_value(af)
temp.columns = [mname]
obj = obj.join(temp)
# plot
_, axs = plt.subplots(nrows = 1, ncols = 2, figsize = (16,6))
obj.plot(marker = ".", grid = True, ax = axs[0])
all_rej.drop("tot").plot(marker = ".", grid = True, ax = axs[1])
axs[0].set_xlabel("Control time period $t$")
axs[1].set_xlabel("Control time period $t$")
axs[0].set_ylabel("Objective function value")
axs[1].set_ylabel("Total percentage of rejections")
plt.savefig(
os.path.join(exp_plot_folder, "obj.png"),
dpi = 300,
format = "png",
bbox_inches = "tight"
)
plt.close()
# compute deviation
if "LoadManagementModel" in found_methods and len(found_methods) > 1:
for mname in found_methods:
if mname != "LoadManagementModel":
obj[f"dev_{mname}"] = (
obj[mname] - obj["LoadManagementModel"]
) / obj["LoadManagementModel"] * 100
all_rej[f"dev_{mname}"] = (
all_rej[mname] - all_rej["LoadManagementModel"]
)
# -- plot deviation
_, axs = plt.subplots(nrows = 1, ncols = 2, figsize = (16,6))
obj.loc[:,obj.columns.str.startswith("dev")].plot(
marker = ".", grid = True, ax = axs[0], color = method_colors[1:]
)
all_rej.loc[:,all_rej.columns.str.startswith("dev")].drop("tot").plot(
marker = ".", grid = True, ax = axs[1], color = method_colors[1:]
)
# -- add average deviation line(s)
for mname, method_color in zip(found_methods, method_colors):
if mname != "LoadManagementModel":
axs[0].axhline(
y = obj[f"dev_{mname}"].mean(),
color = method_color,
linewidth = 2
)
axs[1].axhline(
y = all_rej[f"dev_{mname}"].drop("tot").mean(),
color = method_color,
linewidth = 2
)
axs[0].set_xlabel("Control time period $t$")
axs[1].set_xlabel("Control time period $t$")
axs[0].set_ylabel(
"Objective function deviation ((other - LMM) / LMM )[%]"
)
axs[1].set_ylabel(
"Percentage rejections deviation (other - LMM) [%]"
)
plt.savefig(
os.path.join(exp_plot_folder, "obj_deviation.png"),
dpi = 300,
format = "png",
bbox_inches = "tight"
)
plt.close()
# save
obj.to_csv(os.path.join(exp_plot_folder, "obj.csv"), index = False)
# merge
obj["time"] = obj.index
obj[loop_over] = exp_description_tuple[0]
obj["seed"] = exp_description_tuple[1]
all_obj_values = pd.concat([all_obj_values, obj], ignore_index = True)
#
all_rej.drop("tot", inplace = True)
all_rej["time"] = all_rej.index
all_rej[loop_over] = exp_description_tuple[0]
all_rej["seed"] = exp_description_tuple[1]
all_rej_values = pd.concat(
[all_rej_values, all_rej], ignore_index = True
)
# termination condition
for mname, af in zip(found_methods, abs_folders):
if mname != "LoadManagementModel":
tc = load_termination_condition(af)
_, axs = plt.subplots(nrows = 1, ncols = 2, figsize = (20,5))
tc.plot(
x = "time",
y = "iteration",
marker = ".",
grid = True,
ax = axs[0]
)
if not tc["best_iteration"].isnull().all():
tc.plot(
x = "time",
y = "best_iteration",
marker = ".",
color = mcolors.TABLEAU_COLORS["tab:orange"],
grid = True,
ax = axs[0]
)
tc["criterion"].value_counts().plot.bar(
rot = 0,
grid = True,
ax = axs[1]
)
axs[0].set_xlabel("Control time period $t$")
axs[0].set_ylabel("Number of iterations")
axs[1].set_xlabel(None)
plt.savefig(
os.path.join(exp_plot_folder, f"iterations_{mname}.png"),
dpi = 300,
format = "png",
bbox_inches = "tight"
)
plt.close()
# -- merge
tc["method"] = mname
tc[loop_over] = exp_description_tuple[0]
tc["seed"] = exp_description_tuple[1]
all_tc = pd.concat([all_tc, tc], ignore_index = True)
# runtime
runtimes = {}
for mname, af in zip(found_methods, abs_folders):
if os.path.exists(os.path.join(af, "runtime.csv")):
runtimes[mname] = pd.read_csv(os.path.join(af, "runtime.csv"))
else:
if mname != "LoadManagementModel":
logs_df, _ = parse_log_file(
af,
exp_description,
pd.DataFrame(),
{},
int(exp_description_tuple[0]),
mname
)
runtimes[mname] = get_faasmacro_runtime(
logs_df, exp_plot_folder, mname
)
# plot runtime comparison
if "LoadManagementModel" in found_methods and len(runtimes) > 1:
runtime_comparison = {
"LoadManagementModel": runtimes[
"LoadManagementModel"
]["LoadManagementModel"].tolist()
}
for mname in found_methods:
if mname != "LoadManagementModel":
runtime_comparison[mname] = runtimes[mname]["tot"].tolist()
runtime_comparison[f"iteration_{mname}"] = all_tc[
(
all_tc["method"] == mname
) & (
all_tc[loop_over] == exp_description_tuple[0]
) & (
all_tc["seed"] == exp_description_tuple[1]
)
]["iteration"].tolist()
runtime_comparison[f"best_iteration_{mname}"] = all_tc[
(
all_tc["method"] == mname
) & (
all_tc[loop_over] == exp_description_tuple[0]
) & (
all_tc["seed"] == exp_description_tuple[1]
)
]["best_iteration"].tolist()
runtime_comparison = pd.DataFrame(runtime_comparison)
# -- compute deviation
for mname in found_methods:
if mname != "LoadManagementModel":
runtime_comparison[f"dev_{mname}"] = (
runtime_comparison[mname] / runtime_comparison["LoadManagementModel"]
)
# -- plot
_, axs = plt.subplots(nrows = 1, ncols = 2, figsize = (12,8))
runtime_comparison.loc[
:,~runtime_comparison.columns.str.contains("iteration")
].plot(grid = True, marker = ".", ax = axs[0])
runtime_comparison.loc[
:,runtime_comparison.columns.str.startswith("dev")
].plot(
grid = True, marker = ".", ax = axs[1], color = method_colors[1:]
)
for mname, method_color in zip(found_methods, method_colors):
if mname != "LoadManagementModel":
axs[1].axhline(
y = runtime_comparison[f"dev_{mname}"].mean(),
color = method_color
)
axs[0].set_ylabel("Runtime [s]", fontsize = 14)
axs[1].set_ylabel("Runtime deviation [x]", fontsize = 14)
plt.savefig(
os.path.join(exp_plot_folder, "runtime_comparison.png"),
dpi = 300,
format = "png",
bbox_inches = "tight"
)
plt.close()
# merge
runtime_comparison["time"] = runtime_comparison.index
runtime_comparison[loop_over] = exp_description_tuple[0]
runtime_comparison["seed"] = exp_description_tuple[1]
all_runtime_values = pd.concat(
[all_runtime_values, runtime_comparison], ignore_index = True
)
# cumulative plot
if len(all_obj_values) > 0 and len(all_runtime_values) > 0:
# -- save
all_obj_values.to_csv(
os.path.join(plot_folder, "obj.csv"), index = False
)
all_rej_values.to_csv(
os.path.join(plot_folder, "rejections.csv"), index = False
)
all_runtime_values.to_csv(
os.path.join(plot_folder, "runtime.csv"), index = False
)
all_tc.to_csv(
os.path.join(plot_folder, "i_termination_condition.csv"), index = False
)
# -- plot
for exp_value, objs in all_obj_values.groupby(loop_over):
rejs = all_rej_values[all_rej_values[loop_over] == exp_value]
rtvs = all_runtime_values[
all_runtime_values[loop_over] == exp_value
].copy(deep = True)
i_tc = all_tc[all_tc[loop_over] == exp_value]
fig, axs = plt.subplots(
nrows = 2, ncols = 2, figsize = (12, 8), sharex = True,
gridspec_kw = {"hspace": 0.02}
)
fig2, axs2 = plt.subplots(
nrows = 1, ncols = 3, figsize = (18, 4), sharex = True,
gridspec_kw = {"hspace": 0.02}
)
for seed, obj in objs.groupby("seed"):
rej = rejs[rejs["seed"] == seed]
rtv = rtvs[rtvs["seed"] == seed]
# deviation
for mname, method_color in zip(found_methods, method_colors):
if mname != "LoadManagementModel":
obj.plot(
x = "time",
y = f"dev_{mname}",
ax = axs[0,1],
color = method_color,
linewidth = 1,
marker = ".",
grid = True,
legend = False
)
rej.plot(
x = "time",
y = f"dev_{mname}",
ax = axs[1,1],
color = method_color,
linewidth = 1,
marker = ".",
grid = True,
legend = False
)
rtv.plot(
x = "time",
y = f"dev_{mname}",
ax = axs2[1],
color = method_color,
linewidth = 1,
marker = ".",
grid = True,
legend = False
)
if not rtv[f"best_iteration_{mname}"].isnull().all():
rtv.plot(
x = "time",
y = f"best_iteration_{mname}",
ax = axs2[2],
color = method_color,
linewidth = 1,
marker = ".",
grid = True,
legend = False
)
rtv.plot(
x = "time",
y = f"iteration_{mname}",
ax = axs2[2],
color = method_color,
linewidth = 1,
linestyle = "dashed",
marker = ".",
grid = True,
legend = False
)
# method
obj.plot(
x = "time",
y = mname,
ax = axs[0,0],
color = method_color,
linewidth = 1,
grid = True,
legend = False
)
rej.plot(
x = "time",
y = mname,
ax = axs[1,0],
color = method_color,
linewidth = 1,
grid = True,
legend = False
)
rtv.plot(
x = "time",
y = mname,
ax = axs2[0],
color = method_color,
linewidth = 1,
grid = True,
legend = False
)
fig3, axs3 = plt.subplots(
nrows = 2, ncols = 1, figsize = (12, 6), sharex = True,
gridspec_kw = {"hspace": 0.02}
)
rtvs["idx"] = rtvs.index
for mname, method_color in zip(found_methods, method_colors):
rtvs.plot.scatter(
x = "idx",
y = mname,
ax = axs3[0],
c = method_color,
grid = True,
label = mname
)
if f"dev_{mname}" in rtvs:
rtvs.plot.scatter(
x = "idx",
y = f"dev_{mname}",
ax = axs3[1],
c = method_color,
grid = True
)
# average
avg = objs.groupby("time").mean(numeric_only = True)
avg_rej = rejs.groupby("time").mean(numeric_only = True)
avg_rtv = rtvs.groupby("time").mean(numeric_only = True)
# -- deviation
for mname, method_color in zip(found_methods, method_colors):
if mname != "LoadManagementModel":
avg.plot(
y = f"dev_{mname}",
ax = axs[0,1],
color = method_color,
linewidth = 2,
marker = ".",
grid = True,
label = f"Average deviation (({mname} - LMM) / LMM) [%]"
)
avg_rej.plot(
y = f"dev_{mname}",
ax = axs[1,1],
color = method_color,
linewidth = 2,
marker = ".",
grid = True,
label = f"Average deviation ({mname} - LMM) [%]"
)
if "dev" in avg_rtv:
avg_rtv.plot(
y = f"dev_{mname}",
ax = axs2[1],
color = method_color,
linewidth = 2,
marker = ".",
grid = True,
label = f"Average deviation ({mname} / LMM) [x]"
)
if f"best_iteration_{mname}" in avg_rtv:
avg_rtv.plot(
y = f"best_iteration_{mname}",
ax = axs2[2],
color = method_color,
linewidth = 2,
marker = ".",
grid = True,
label = "Best iteration"
)
avg_rtv.plot(
y = f"iteration_{mname}",
ax = axs2[2],
color = method_color,
linewidth = 1,
linestyle = "dashed",
marker = ".",
grid = True,
label = "# iterations"
)
# -- method
avg.plot(
y = mname,
ax = axs[0,0],
color = method_color,
linewidth = 2,
grid = True,
label = f"Average {mname}"
)
avg_rej.plot(
y = mname,
ax = axs[1,0],
color = method_color,
linewidth = 2,
grid = True,
label = f"Average {mname}"
)
if mname in avg_rtv:
avg_rtv.plot(
y = mname,
ax = axs2[0],
color = method_color,
linewidth = 2,
grid = True,
label = f"Average {mname}"
)
axs3[0].axhline(
y = rtvs[mname].mean(),
color = method_color,
linewidth = 2
)
if f"dev_{mname}" in rtvs:
axs3[1].axhline(
y = rtvs[f"dev_{mname}"].mean(),
color = method_color,
linewidth = 2
)
axs[1,0].set_xlabel("Control time period $t$")
axs[1,1].set_xlabel("Control time period $t$")
axs[0,0].set_ylabel("Objective function value")
axs[0,1].set_ylabel("Objective function deviation [%]")
axs[1,0].set_ylabel("Total percentage of rejections [%]")
axs[1,1].set_ylabel("Percentage rejections deviation [%]")
axs2[0].set_xlabel("Control time period $t$")
axs2[1].set_xlabel("Control time period $t$")
axs2[2].set_xlabel("Control time period $t$")
axs2[0].set_ylabel("Runtime [s]")
axs2[1].set_ylabel("Runtime deviation [x]")
axs2[2].set_ylabel("Number of iterations")
axs3[-1].set_xlabel("Experiment")
axs3[0].set_ylabel("Runtime [s]")
axs3[1].set_ylabel("Runtime deviation [x]")
fig.savefig(
os.path.join(plot_folder, f"obj-{loop_over}_{exp_value}.png"),
dpi = 300,
format = "png",
bbox_inches = "tight"
)
plt.close(fig)
fig2.savefig(
os.path.join(plot_folder, f"runtime-{loop_over}_{exp_value}.png"),
dpi = 300,
format = "png",
bbox_inches = "tight"
)
plt.close(fig2)
fig3.savefig(
os.path.join(
plot_folder, f"linear_runtime-{loop_over}_{exp_value}.png"
),
dpi = 300,
format = "png",
bbox_inches = "tight"
)
plt.close(fig3)
# termination condition
i_tc["criterion"].value_counts().plot.bar(
rot = 0,
grid = True
)
plt.savefig(
os.path.join(plot_folder, f"i_tc-{loop_over}_{exp_value}.png"),
dpi = 300,
format = "png",
bbox_inches = "tight"
)
plt.close()
# termination condition
if "criterion" in all_tc:
_, ax = plt.subplots(figsize = (20,6))
all_tc["criterion"].value_counts(normalize = True).plot.bar(
rot = 0,
grid = True,
ax = ax,
fontsize = 21
)
ax.set_xlabel("Stopping criterion", fontsize = 21)
ax.set_ylabel("Frequency", fontsize = 21)
plt.savefig(
os.path.join(plot_folder, "i_termination_condition.png"),
dpi = 300,
format = "png",
bbox_inches = "tight"
)
plt.close()
# save ping-pong problems info
with open(os.path.join(plot_folder, "ping_pong_problems.txt"), "w") as ost:
for el in ping_pong_list:
ost.write(f"{el}\n")
def run(
base_config: dict,
base_solution_folder: str,
n_experiments: int,
methods: list,
fix_r: bool,
sp_parallelism: int,
enable_plotting: bool,
loop_over: str
):
seed = base_config["seed"]
log_on_file = True if base_config["verbose"] > 0 else False
exp_values = base_config["limits"].get(loop_over)
if exp_values is None:
exp_values = base_config["limits"]["neighborhood"][loop_over]
disable_plotting = not enable_plotting
from_instances = base_config["limits"].get("path", None)
generate_only = "generate_only" in methods
# generate list of experiments
experiments_list = generate_experiments_list(exp_values, seed, n_experiments)
# load list of already-run experiments (if any)
solution_folders = {
"experiments_list": [],
"centralized": [],
"faas-macro": [],
"faas-macro-v0": [],
"faas-madea": []
}
if os.path.exists(os.path.join(base_solution_folder, "experiments.json")):
with open(
os.path.join(base_solution_folder, "experiments.json"), "r"
) as ist:
solution_folders = json.load(ist)
# load list of previous instances (if required)
old_instance_paths = {}
if from_instances is not None:
with open(os.path.join(from_instances, "experiments.json"), "r") as ist:
old_instance_paths = json.load(ist)
# loop over the experiments list
for exp_value, seed in tqdm(experiments_list):
# check if the experiment is still to run
run_c = False # -- centralized
run_i = False # -- faasmacro
run_i_v0 = False # -- faasmacro (v0)
run_a = False # -- faasmadea
experiment_idx = None
try:
experiment_idx = solution_folders["experiments_list"].index(
[exp_value, seed]
)
if (not generate_only and "centralized" in methods) and ((
len(solution_folders["centralized"]) <= experiment_idx
) or (
solution_folders["centralized"][experiment_idx] is None
)):
run_c = True
if (not generate_only and "faas-macro" in methods) and ((
len(solution_folders["faas-macro"]) <= experiment_idx
) or (
solution_folders["faas-macro"][experiment_idx] is None
)):
run_i = True
if (not generate_only and "faas-macro-v0" in methods) and ((
len(solution_folders["faas-macro-v0"]) <= experiment_idx
) or (
solution_folders["faas-macro-v0"][experiment_idx] is None
)):
run_i_v0 = True
if (not generate_only and "faas-madea" in methods) and ((
len(solution_folders["faas-madea"]) <= experiment_idx
) or (
solution_folders["faas-madea"][experiment_idx] is None
)):
run_a = True
except ValueError:
run_c = "centralized" in methods
run_i = "faas-macro" in methods
run_i_v0 = "faas-macro-v0" in methods
run_a = "faas-madea" in methods
# if the experiment is still to run...
if run_c or run_i or run_i_v0 or run_a or generate_only:
# -- update configuration
config = deepcopy(base_config)
if loop_over in config["limits"]:
config["limits"][loop_over].pop("values", None)
config["limits"][loop_over]["min"] = exp_value
config["limits"][loop_over]["max"] = exp_value
else:
config["limits"]["neighborhood"][loop_over].pop("values", None)
config["limits"]["neighborhood"][loop_over] = exp_value
config["seed"] = seed
# -- look for old instance path (if required)
if "experiments_list" in old_instance_paths:
try:
old_exp_idx = old_instance_paths["experiments_list"].index(
[exp_value, seed]
)
old_exp_path = None
if "centralized" in old_instance_paths:
old_exp_path = old_instance_paths["centralized"][
old_exp_idx
]
elif "faas-macro" in old_instance_paths:
old_exp_path = old_instance_paths["faas-macro"][
old_exp_idx
]
elif "faas-macro-v0" in old_instance_paths:
old_exp_path = old_instance_paths["faas-macro-v0"][
old_exp_idx
]
elif "faas-madea" in old_instance_paths:
old_exp_path = old_instance_paths["faas-madea"][
old_exp_idx
]
config["limits"]["path"] = old_exp_path
if config["limits"]["load"]["trace_type"] == "load_existing":
config["limits"]["load"]["path"] = old_exp_path
except Exception:
pass
# -- solve centralized model
c_folder = None
if run_c or generate_only:
c_folder = run_centralized(
config,
log_on_file = log_on_file,
disable_plotting = disable_plotting,
generate_only = generate_only
)
solution_folders["centralized"].append(c_folder)
else:
if experiment_idx is not None:
c_folder = solution_folders["centralized"][experiment_idx]
# -- solve iterative model (v0)
if fix_r:
config["opt_solution_folder"] = c_folder
if run_i_v0:
i_folder_v0 = run_iterations(
config,
sp_parallelism,
log_on_file = log_on_file,
disable_plotting = disable_plotting,
v0 = True
)
solution_folders["faas-macro-v0"].append(i_folder_v0)
# -- solve iterative model
if run_i:
i_folder = run_iterations(
config,
sp_parallelism,
log_on_file = log_on_file,
disable_plotting = disable_plotting
)
solution_folders["faas-macro"].append(i_folder)
# -- solve auction
if run_a:
a_folder = run_auction(
config,
sp_parallelism,
log_on_file = log_on_file,
disable_plotting = disable_plotting
)
solution_folders["faas-madea"].append(a_folder)
# -- save info
if experiment_idx is None:
solution_folders["experiments_list"].append([exp_value, seed])
# -- save
with open(
os.path.join(base_solution_folder, "experiments.json"), "w"
) as ost:
ost.write(json.dumps(solution_folders, indent = 2))
# immediate postprocessing
results_postprocessing(
solution_folders, base_solution_folder, loop_over, methods
)
if __name__ == "__main__":
args = parse_arguments()
config_file = args.config
n_experiments = args.n_experiments
methods = args.methods
postprocessing_only = args.postprocessing_only
postprocessing_list = args.postprocessing_list
fix_r = args.fix_r
sp_parallelism = args.sp_parallelism
enable_plotting = args.enable_plotting
loop_over = args.loop_over
# load configuration file
base_config = load_configuration(config_file)