-
Notifications
You must be signed in to change notification settings - Fork 0
/
mass_benchmark.py
181 lines (149 loc) · 6.02 KB
/
mass_benchmark.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
"""
Driver code for mass benchmark
"""
import json
import random
from typing import Any, DefaultDict
import matplotlib.pyplot as plt # type: ignore
import benchmark
from graph import Graph
class Bcolors:
"""
Helper class for adding colors to prints
https://svn.blender.org/svnroot/bf-blender/trunk/blender/build_files/scons/tools/Bcolors.py
"""
HEADER = "\033[95m"
OKBLUE = "\033[94m"
OKCYAN = "\033[96m"
OKGREEN = "\033[92m"
WARNING = "\033[93m"
FAIL = "\033[91m"
ENDC = "\033[0m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
CLEAR_LAST_LINE = (
"\033[A \033[A"
)
def main() -> None:
############################################################################
########################### Mass Benchmarking ##############################
############################################################################
# Parameters for Graphs and Partitions
num_graphs: int = 100
num_nodes: int = 201 # 20 agents * 10 nodes per agent + start
metric = True
upper: float = 1.0 # Travel time between 0.5-1 hour
node_w: tuple[int, int] = (1, 1500)
num_agents: int = 20
print("Generating graphs")
graph_bank: list[Graph] = benchmark.generate_graph_bank(
count=num_graphs, n=num_nodes, metric=metric, upper=upper, node_w=node_w
)
print("Seeing start location weight to 0")
for g in graph_bank:
g.node_weight[0] = 0
print("Adding repair times")
for g in graph_bank:
# Ranges from "Predicting Outage Restoration ..."
for v in range(num_nodes):
pop: int = g.node_weight[v]
if pop <= 10:
repair_time: float = random.uniform(2, 4)
elif pop <= 100:
repair_time = random.uniform(2, 6)
elif pop <= 1000:
repair_time = random.uniform(3, 8)
else:
repair_time = random.uniform(5, 10)
for u in range(num_nodes):
if u != v:
g.edge_weight[u][v] += repair_time
print("Generating initial partitions")
partition_bank: list[list[set[int]]] = benchmark.generate_agent_partitions(
graph_bank, num_agents
)
# Mass benchmark of graphs given bank
# Need to edit the ranges
# If metric: do (upper / 2, upper)
benchmark_results: list[DefaultDict[Any, Any]] = benchmark.mass_benchmark(
graph_bank, partition_bank, (0.5, 1.0)
)
# Write to files
names: list[str] = [
"maximums",
"wait_times",
"times",
"minimums",
"sums",
"ranges",
"averages",
"bests",
]
for res, name in zip(benchmark_results, names):
with open(
f"results/mass_benchmark/{name}.json", "w", encoding="utf-8"
) as outfile:
json.dump(res, outfile)
# Box Plot for sum of weighted latencies
with open("results/mass_benchmark/sums.json", encoding="utf-8") as file:
sums: dict[str, list[float]] = json.load(file)
results: list[str] = [
"Greedy Assignment",
"Nearest Neighbor Assignment",
"Greedy + Random (25%) Assignment",
"Transfers and Swaps Greedy",
"Transfers and Swaps Nearest Neighbor",
]
boxes: list[list[float]] = [sums[name] for name in results]
colors: list[str] = ["royalblue", "aqua", "blue", "limegreen", "darkgreen"]
fig, ax = plt.subplots(figsize=(6, 6))
bp = ax.boxplot(boxes, patch_artist=True)
for patch, color in zip(bp["boxes"], colors):
patch.set_facecolor(color)
for median in bp["medians"]:
median.set(color="black", linewidth=3)
frame1 = plt.gca()
frame1.axes.xaxis.set_ticklabels(["GA", "NNA", "GRA", "TSG", "TSNN"])
ax.tick_params(axis="both", which="major", labelsize=20)
ax.tick_params(axis="both", which="minor", labelsize=20)
plt.suptitle("Sum of Weighted Latencies", fontsize=20)
plt.gcf().axes[0].yaxis.get_major_formatter().set_scientific(False)
fig.savefig("results/mass_benchmark/total_work", bbox_inches="tight")
# Bar Plot for average wait times
with open("results/mass_benchmark/wait_times.json", encoding="utf-8") as file:
wait: dict[str, list[float]] = json.load(file)
boxes = [wait[name] for name in results]
colors = ["royalblue", "aqua", "blue", "limegreen", "darkgreen"]
fig, ax = plt.subplots(figsize=(6, 6))
bp = ax.boxplot(boxes, patch_artist=True)
for patch, color in zip(bp["boxes"], colors):
patch.set_facecolor(color)
for median in bp["medians"]:
median.set(color="black", linewidth=3)
frame1 = plt.gca()
frame1.axes.xaxis.set_ticklabels(["GA", "NNA", "GRA", "TSG", "TSNN"])
ax.tick_params(axis="both", which="major", labelsize=20)
ax.tick_params(axis="both", which="minor", labelsize=20)
plt.suptitle("Average Wait Time (Hours)", fontsize=20)
plt.gcf().axes[0].yaxis.get_major_formatter().set_scientific(False)
fig.savefig("results/mass_benchmark/wait_time", bbox_inches="tight")
# Bar Plot for ranges
with open("results/mass_benchmark/ranges.json", encoding="utf-8") as file:
ranges: dict[str, list[float]] = json.load(file)
boxes = [ranges[name] for name in results]
colors = ["royalblue", "aqua", "blue", "limegreen", "darkgreen"]
fig, ax = plt.subplots(figsize=(6, 6))
bp = ax.boxplot(boxes, patch_artist=True)
for patch, color in zip(bp["boxes"], colors):
patch.set_facecolor(color)
for median in bp["medians"]:
median.set(color="black", linewidth=3)
frame1 = plt.gca()
frame1.axes.xaxis.set_ticklabels(["GA", "NNA", "GRA", "TSG", "TSNN"])
ax.tick_params(axis="both", which="major", labelsize=20)
ax.tick_params(axis="both", which="minor", labelsize=20)
plt.suptitle("Range of Weighted Latencies", fontsize=20)
plt.gcf().axes[0].yaxis.get_major_formatter().set_scientific(False)
fig.savefig("results/mass_benchmark/ranges", bbox_inches="tight")
if __name__ == "__main__":
main()