Repository navigation
Expand file tree
/
Copy pathdo_algorithms_plot_with_replicates.py
More file actions
139 lines (119 loc) · 4.54 KB
/
Copy pathdo_algorithms_plot_with_replicates.py
File metadata and controls
139 lines (119 loc) · 4.54 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
import argparse
import matplotlib.pyplot as plt
import pandas as pd
from experiment_config import load_experiment_config
ALGORITHM_COLORS = {
"NSGA2": "#ff8500",
"NSGA3": "#FF595E",
"UNSGA3": "#1982C4",
"SMSEMOA": "#6A4C93",
"HSMOEA": "#ff8500",
"MOCPO": "#1982C4",
"MOEATS": "#6A4C93",
"Hybrid": "#8AC926",
"Hybrids": "#8AC926",
}
DEFAULT_COLORS = ["#198754", "#D7263D", "#4C72B0", "#55A868", "#C44E52", "#8172B2"]
METRICS = ["GD", "IGD", "HV", "S", "STE"]
def _save_plot(config, fig, filename):
config.plots_dir.mkdir(parents=True, exist_ok=True)
fig.savefig(config.plots_dir / f"{filename}.png", dpi=300, bbox_inches="tight")
fig.savefig(config.plots_dir / f"{filename}.pdf", bbox_inches="tight")
plt.close(fig)
def plot_metrics_time_series(config):
df = pd.read_csv(config.standard_metrics_file)
df = df.loc[df["Generation"] <= config.cut_generation].copy()
df_hybrids = pd.read_csv(config.hybrid_metrics_file) if config.hybrid_metrics_file.exists() else None
if df_hybrids is not None:
df_hybrids = df_hybrids.loc[df_hybrids["Generation"] <= config.cut_generation].copy()
fig, axes = plt.subplots(len(METRICS), 1, figsize=(12, 15))
handles = []
labels = []
for metric_index, metric in enumerate(METRICS):
ax = axes[metric_index]
for algorithm_index, algorithm in enumerate(config.standard_algorithms):
algorithm_data = df[df["Algorithm"] == algorithm]
grouped = algorithm_data.groupby("Generation")[metric].agg(["mean", "std"])
color = ALGORITHM_COLORS.get(
algorithm,
DEFAULT_COLORS[algorithm_index % len(DEFAULT_COLORS)],
)
line = ax.plot(
grouped.index,
grouped["mean"],
label=algorithm,
color=color,
linewidth=2,
marker="o",
markersize=3,
markevery=10,
)
ax.fill_between(
grouped.index,
grouped["mean"] - grouped["std"],
grouped["mean"] + grouped["std"],
color=color,
alpha=0.2,
)
if metric_index == 0:
handles.extend(line)
labels.append(algorithm)
if df_hybrids is not None:
grouped_hybrids = df_hybrids.groupby("Generation")[metric].agg(["mean", "std"])
hybrid_line = ax.plot(
grouped_hybrids.index,
grouped_hybrids["mean"],
label="Hybrids",
color=ALGORITHM_COLORS["Hybrids"],
linewidth=2,
marker="v",
markersize=4,
markevery=10,
)
ax.fill_between(
grouped_hybrids.index,
grouped_hybrids["mean"] - grouped_hybrids["std"],
grouped_hybrids["mean"] + grouped_hybrids["std"],
color=ALGORITHM_COLORS["Hybrids"],
alpha=0.2,
)
if metric_index == 0:
handles.extend(hybrid_line)
labels.append("Hybrids")
ax.set_xlabel("Generation", fontsize=10)
ax.set_ylabel(metric, fontsize=10)
ax.set_xlim(left=0, right=config.cut_generation)
ax.grid(True, linestyle="--", alpha=0.7)
if metric in ["GD", "IGD", "S"]:
ax.set_ylim(bottom=0)
ax.minorticks_on()
ax.grid(True, which="minor", linestyle=":", alpha=0.4)
ax.tick_params(axis="both", which="major", labelsize=9)
ax.text(
0.98,
0.95,
metric,
transform=ax.transAxes,
fontsize=12,
fontweight="bold",
verticalalignment="top",
horizontalalignment="right",
)
fig.legend(handles, labels, loc="upper center", bbox_to_anchor=(0.5, 0.95), fontsize=12, ncol=len(labels))
plt.tight_layout()
plt.subplots_adjust(top=0.92)
_save_plot(config, fig, "metrics_time_series")
def build_parser():
parser = argparse.ArgumentParser(description="Plot metrics with replicates for the configured experiment")
parser.add_argument(
"--config",
default="configs/config_NSGA2MOCPOMOEATS_codeA.ini",
help="Path to the experiment configuration INI file.",
)
return parser
def main(argv=None):
args = build_parser().parse_args(argv)
config = load_experiment_config(args.config)
plot_metrics_time_series(config)
if __name__ == "__main__":
main()