-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.py
752 lines (619 loc) · 20.9 KB
/
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
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
"""
Benchmark Functions
"""
import json
import time
from collections import defaultdict
from typing import Any, Callable, DefaultDict, no_type_check
import matplotlib.pyplot as plt # type: ignore
import mplcursors # type: ignore
import networkx as nx # type: ignore
import numpy as np
import algos
from graph import Graph, graph_dict
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 solve_partition(
g: Graph, part: list[set[int]], f: Callable[..., list[int]] = algos.brute_force_mwlp
) -> list[list[int]]:
"""
Determine optimal orders for each subset in the partition
according to a passed heuristic
Parameters
----------
g: Graph
Input graph
Assertions:
g must be a complete graph
part: list[set[int]]
Starting unordered assignment of nodes for each agent
Assertions:
Must be an agent partition
f: Callable[..., list[int]]
Passed heuristic
Default: brute force mwlp
Returns
-------
list[list[int]]
Solved orders of each agent
"""
if not Graph.is_complete(g):
raise ValueError("Passed graph is not complete")
if Graph.is_agent_partition(g, part) is False:
raise ValueError("Passed partition is invalid")
# creating a deep copy to be safe
partition: list[set[int]] = [set(s) for s in part]
res: list[list[int]] = []
for p in partition:
sub_g, sto, _ = Graph.subgraph(g, list(p))
sub_res: list[int] = f(sub_g)
remapped_res: list[int] = [sto[node] for node in sub_res]
res.append(remapped_res)
return res
def benchmark_partition(
g: Graph, assignment: list[list[int]]
) -> tuple[float, float, float, float, float, float]:
"""
Takes in a partition and path order of agents and benchmarks it
Parameters
----------
g: Graph
Input graph
Assertions:
g must be a complete graph
assignment: list[set[int]]
Assignment of nodes for each agent
Assertions:
Must be an agent assignment
Returns
-------
tuple[float, float, float, float, float, float]
maximum, average wait, minimum, range, sum, average
"""
if not Graph.is_complete(g):
raise ValueError("Passed graph is not complete")
if Graph.is_agent_partition(g, [set(s) for s in assignment]) is False:
raise ValueError("Passed partition is invalid")
# creating a deep copy to be safe
assign: list[list[int]] = [list(p) for p in assignment]
vals: list[float] = [algos.wlp(g, p) for p in assign]
# Calculate average wait times
wait_times: list[float] = []
for val, p in zip(vals, assign):
wait_times.append(val / algos.num_visited_along_path(g, p)[-1])
res: tuple[float, float, float, float, float, float] = (
max(vals),
sum(wait_times) / len(wait_times),
min(vals),
max(vals) - min(vals),
sum(vals),
sum(vals) / len(vals),
)
return res
def generate_graph_bank(
count: int,
n: int,
edge_w: tuple[float, float] = (0.0, 1.0),
metric: bool = True,
upper: float = 1.0,
node_w: tuple[int, int] = (0, 100),
) -> list[Graph]:
"""
Generate a list of graphs based on passed parameters
Parameters
----------
count: int
The number of graphs to benchmark
n: int
The number of nodes per graph
edge_w: tuple[float, float]
The range of edge weights allowed
Default: (0.0, 1.0)
metric: bool
Determine whether to test on metric or non-metric graphs
Default: True
upper: float
Upper bound of edge weights for a metric graph
Default: 1.0
node_w: tuple[int, int]
The range of node weights allowed
Default: (0, 100)
Returns
-------
list[Graph]:
List of randomly generated graphs based on passed data
"""
graph_bank: list[Graph] = []
for _ in range(count):
if metric:
g = Graph.random_complete_metric(n, upper, node_w)
else:
g = Graph.random_complete(n, edge_w, node_w)
graph_bank.append(g)
return graph_bank
def graph_bank_from_file(loc: str) -> list[Graph]:
"""
Generate a list of graphs based on a json file
Parameters
----------
loc: str
location of json file of graph_dicts
Returns
-------
list[Graph]:
List of graphs based on json file
"""
graph_dict_bank: dict[str, graph_dict] = {}
with open(loc, encoding="utf-8") as gd_json:
graph_dict_bank = json.load(gd_json)
n: int = len(graph_dict_bank)
graph_bank: list[Graph] = []
for i in range(n):
gd: graph_dict = graph_dict_bank[str(i)]
graph_bank.append(Graph.from_dict(gd))
return graph_bank
def generate_agent_partitions(graph_bank: list[Graph], k: int) -> list[list[set[int]]]:
"""
Generate agent partitions based on passed graphs
Parameters
----------
graph_bank: list[Graph]
Passed list of graphs
Assertions:
Each graph must be complete
k: int
Number of agents
Returns
-------
list[list[set[int]]]
Generated agent partitions
"""
partition_bank: list[list[set[int]]] = []
for g in graph_bank:
assert Graph.is_complete(g)
partition: list[set[int]] = Graph.create_agent_partition(g, k)
partition_bank.append(partition)
return partition_bank
def agent_partitions_from_file(loc: str) -> list[list[set[int]]]:
"""
Generate a list of agent partitions based on a json file
Parameters
----------
loc: str
location of json file of partitions
Returns
-------
list[list[set[int]]]:
List of agent partitions based on json file
"""
serialized_partition_bank: dict[str, list[list[int]]] = {}
with open(loc, encoding="utf8") as part_file:
serialized_partition_bank = json.load(part_file)
n: int = len(serialized_partition_bank)
partition_bank: list[list[set[int]]] = []
for i in range(n):
serialized_part: list[list[int]] = serialized_partition_bank[str(i)]
part: list[set[int]] = [set(s) for s in serialized_part]
partition_bank.append(part)
return partition_bank
def mass_benchmark(
graph_bank: list[Graph],
partition_bank: list[list[set[int]]],
rand_dist_range: tuple[float, float],
) -> list[DefaultDict[Any, Any]]:
"""
Benchmarks a large number of graphs randomly generated accord to the parameters
Parameters
----------
graph_bank: list[Graph]
List of graphs to test over
Assertions:
complete graphs
partition_bank: list[list[set[int]]]
List of partitions associated with graphs in graph_bank
Assertions:
partition_bank[i] is an agent partition of graph_bank[i]
rand_dist_range: tuple[float, float]
Range of allowed distances for random aspect of prior strategies
"""
assert len(graph_bank) == len(partition_bank)
for g, p in zip(graph_bank, partition_bank):
assert Graph.is_complete(g)
assert Graph.is_agent_partition(g, p)
maximums: DefaultDict[str, list[float]] = defaultdict(list)
# WLP is a weighted average of wait times of sorts
wait_times: DefaultDict[str, list[float]] = defaultdict(list)
times: DefaultDict[str, list[float]] = defaultdict(list)
minimums: DefaultDict[str, list[float]] = defaultdict(list)
sums: DefaultDict[str, list[float]] = defaultdict(list)
ranges: DefaultDict[str, list[float]] = defaultdict(list)
averages: DefaultDict[str, list[float]] = defaultdict(list)
bests: DefaultDict[str, int] = defaultdict(int)
count: int = len(graph_bank)
for i, (g, partition) in enumerate(zip(graph_bank, partition_bank)):
print(i)
k: int = len(partition)
# Put all desired heuristics here
best, curr_best = "", float("inf")
curr = "Greedy Assignment"
print(curr)
start: float = time.perf_counter_ns()
res = algos.greedy_assignment(g, k)
end: float = time.perf_counter_ns()
(
curr_max,
curr_wait,
curr_min,
curr_range,
curr_sum,
curr_avg,
) = benchmark_partition(g, res)
if curr_sum < curr_best:
curr_best = curr_sum
best = curr
maximums[curr].append(curr_max)
wait_times[curr].append(curr_wait)
times[curr].append(end - start)
minimums[curr].append(curr_min)
ranges[curr].append(curr_range)
sums[curr].append(curr_sum)
averages[curr].append(curr_avg)
print(Bcolors.CLEAR_LAST_LINE)
curr = "Nearest Neighbor Assignment"
print(curr)
start = time.perf_counter_ns()
res = algos.nearest_neighbor_assignment(g, k)
end = time.perf_counter_ns()
(
curr_max,
curr_wait,
curr_min,
curr_range,
curr_sum,
curr_avg,
) = benchmark_partition(g, res)
if curr_sum < curr_best:
curr_best = curr_sum
best = curr
maximums[curr].append(curr_max)
wait_times[curr].append(curr_wait)
times[curr].append(end - start)
minimums[curr].append(curr_min)
ranges[curr].append(curr_range)
sums[curr].append(curr_sum)
averages[curr].append(curr_avg)
print(Bcolors.CLEAR_LAST_LINE)
lo, hi = rand_dist_range
dist_range: float = hi - lo
curr = "Greedy + Random (25%) Assignment"
print(curr)
start = time.perf_counter_ns()
res = algos.greedy_random_assignment(g, k, lo + (dist_range * 0.25))
end = time.perf_counter_ns()
(
curr_max,
curr_wait,
curr_min,
curr_range,
curr_sum,
curr_avg,
) = benchmark_partition(g, res)
if curr_sum < curr_best:
curr_best = curr_sum
best = curr
maximums[curr].append(curr_max)
wait_times[curr].append(curr_wait)
times[curr].append(end - start)
minimums[curr].append(curr_min)
ranges[curr].append(curr_range)
sums[curr].append(curr_sum)
averages[curr].append(curr_avg)
print(Bcolors.CLEAR_LAST_LINE)
curr = "Greedy + Random (50%) Assignment"
print(curr)
start = time.perf_counter_ns()
res = algos.greedy_random_assignment(g, k, lo + (dist_range * 0.50))
end = time.perf_counter_ns()
(
curr_max,
curr_wait,
curr_min,
curr_range,
curr_sum,
curr_avg,
) = benchmark_partition(g, res)
if curr_sum < curr_best:
curr_best = curr_sum
best = curr
maximums[curr].append(curr_max)
wait_times[curr].append(curr_wait)
times[curr].append(end - start)
minimums[curr].append(curr_min)
ranges[curr].append(curr_range)
sums[curr].append(curr_sum)
averages[curr].append(curr_avg)
print(Bcolors.CLEAR_LAST_LINE)
curr = "Greedy + Random (75%) Assignment"
print(curr)
start = time.perf_counter_ns()
res = algos.greedy_random_assignment(g, k, lo + (dist_range * 0.75))
end = time.perf_counter_ns()
(
curr_max,
curr_wait,
curr_min,
curr_range,
curr_sum,
curr_avg,
) = benchmark_partition(g, res)
if curr_sum < curr_best:
curr_best = curr_sum
best = curr
maximums[curr].append(curr_max)
wait_times[curr].append(curr_wait)
times[curr].append(end - start)
minimums[curr].append(curr_min)
ranges[curr].append(curr_range)
sums[curr].append(curr_sum)
averages[curr].append(curr_avg)
print(Bcolors.CLEAR_LAST_LINE)
curr = "Transfers and Swaps Greedy"
print(curr)
print("Finding partition")
start = time.perf_counter_ns()
output = algos.find_partition_with_heuristic(g, partition, algos.greedy, 0.13)
end = time.perf_counter_ns()
print(Bcolors.CLEAR_LAST_LINE)
print("Solving partition")
res = solve_partition(g, output, algos.greedy)
print(Bcolors.CLEAR_LAST_LINE)
(
curr_max,
curr_wait,
curr_min,
curr_range,
curr_sum,
curr_avg,
) = benchmark_partition(g, res)
if curr_sum < curr_best:
curr_best = curr_sum
best = curr
maximums[curr].append(curr_max)
wait_times[curr].append(curr_wait)
times[curr].append(end - start)
minimums[curr].append(curr_min)
ranges[curr].append(curr_range)
sums[curr].append(curr_sum)
averages[curr].append(curr_avg)
print(Bcolors.CLEAR_LAST_LINE)
curr = "Transfers and Swaps Nearest Neighbor"
print(curr)
print("Finding partition")
start = time.perf_counter_ns()
output = algos.find_partition_with_heuristic(
g, partition, algos.nearest_neighbor, 0.13
)
end = time.perf_counter_ns()
print(Bcolors.CLEAR_LAST_LINE)
print("Solving partition")
res = solve_partition(g, output, algos.nearest_neighbor)
print(Bcolors.CLEAR_LAST_LINE)
(
curr_max,
curr_wait,
curr_min,
curr_range,
curr_sum,
curr_avg,
) = benchmark_partition(g, res)
if curr_sum < curr_best:
curr_best = curr_sum
best = curr
maximums[curr].append(curr_max)
wait_times[curr].append(curr_wait)
times[curr].append(end - start)
minimums[curr].append(curr_min)
ranges[curr].append(curr_range)
sums[curr].append(curr_sum)
averages[curr].append(curr_avg)
print(Bcolors.CLEAR_LAST_LINE)
bests[best] += 1
print(Bcolors.CLEAR_LAST_LINE)
print(f"{Bcolors.OKBLUE}Sums: {Bcolors.ENDC}")
for key, vals in sums.items():
print(f"\t{key:40}{sum(vals) / count}")
print()
print(f"{Bcolors.OKBLUE}Maximums: {Bcolors.ENDC}")
for key, vals in maximums.items():
print(f"\t{key:40}{sum(vals) / count}")
print()
print(f"{Bcolors.OKBLUE}Wait Times: {Bcolors.ENDC}")
for key, vals in wait_times.items():
print(f"\t{key:40}{sum(vals) / count}")
print()
print(f"{Bcolors.OKBLUE}Runtime in seconds: {Bcolors.ENDC}")
for key, vals in times.items():
print(f"\t{key:40}{sum(vals) / (count * (10 ** 9))}")
print()
print(f"{Bcolors.OKBLUE}Bests: {Bcolors.ENDC}")
for key, val in bests.items():
print(f"\t{key:40}{val}")
print()
print(f"{Bcolors.OKBLUE}Minimums: {Bcolors.ENDC}")
for key, vals in minimums.items():
print(f"\t{key:40}{sum(vals) / count}")
print()
print(f"{Bcolors.OKBLUE}Ranges: {Bcolors.ENDC}")
for key, vals in ranges.items():
print(f"\t{key:40}{sum(vals) / count}")
print()
print(f"{Bcolors.OKBLUE}Averages: {Bcolors.ENDC}")
for key, vals in averages.items():
print(f"\t{key:40}{sum(vals) / count}")
print()
return [maximums, wait_times, times, minimums, sums, ranges, averages, bests]
def alpha_heuristic_given(
f: Callable[..., list[int]],
graph_bank: list[Graph],
partition_bank: list[list[set[int]]],
) -> dict[float, float]:
"""
Benchmark function to help determine ideal alpha values for transfers and swaps
Creates a list of average resulting sums of weighted latencies for each alpha
Parameters
----------
f: Callable[..., list[int]]
Passed heuristic
graph_bank: list[Graph]
List of graphs to test over
Assertions:
complete graphs
partition_bank: list[list[set[int]]]
List of partitions associated with graphs in graph_bank
Assertions:
partition_bank[i] is an agent partition of graph_bank[i]
Returns
-------
dict[float, float]
Averages of sums of weighted latencies
One for each alpha value from 0.0 to 1.0 in increments of 0.01
"""
assert len(graph_bank) == len(partition_bank)
for g, p in zip(graph_bank, partition_bank):
assert Graph.is_complete(g)
assert Graph.is_agent_partition(g, p)
count: int = len(graph_bank)
averages: dict[float, float] = {}
alpha: float = 0.0
while alpha <= 1.0:
print(f"Current Alpha Val = {alpha}")
sums: list[float] = []
for i, (g, partition) in enumerate(zip(graph_bank, partition_bank)):
print(f"Current Graph = {i}")
output = algos.find_partition_with_heuristic(g, partition, f, alpha)
res = solve_partition(g, output)
_, _, _, _, curr_sum, _ = benchmark_partition(g, res)
sums.append(curr_sum)
print(Bcolors.CLEAR_LAST_LINE)
averages[alpha] = sum(sums) / count
alpha = round(alpha + 0.01, 2)
print(Bcolors.CLEAR_LAST_LINE)
return averages
def line_plot(
g: Graph,
assignments: list[list[list[int]]],
names: list[str],
colors: list[str],
x_range: tuple[int, int] = (0, 10),
loc: str = "most_recent_line_plot.png",
) -> None:
"""
Generate a plot of visited nodes for the given assignments
Parameters
----------
g: Graph
Input graph
Assertions:
Must be complete
assignments: list[list[list[int]]]
List of agent assignments
Assertions:
Each assignment be an agent assignment
names: list[str]
Names for each plot
colors: list[str]
Colors for each plot
Assertions:
len(colors) == len(names) == len(assignments)
x_range: tuple[int, int]
x-axis bounds for plotting
Default: (0, 10)
loc: str
Location to save graph to
"""
if Graph.is_complete(g) is False:
raise ValueError("Passed graph is not complete")
if not len(assignments) == len(names) == len(colors):
raise ValueError("Lengths of arguments do not match")
for assignment in assignments:
part: list[set[int]] = [set(s) for s in assignment]
if Graph.is_agent_partition(g, part) is False:
raise ValueError("Some passed assignment is invalid")
n: int = g.num_nodes
low, high = x_range
x = np.linspace(low, high, high * 10)
_, ax = plt.subplots()
total = sum(g.node_weight[x] for x in range(n))
lines = []
# List of matplotlib colors
# https://matplotlib.org/3.5.0/_images/sphx_glr_named_colors_003.png
for paths, name, color in zip(assignments, names, colors):
f = algos.generate_partition_path_function(g, paths)
y = [total - f(i) for i in x]
(line,) = ax.plot(x, y, label=f"{name}", linewidth=2.0, color=color)
lines.append(line)
mplcursors.cursor(lines, highlight=True)
plt.legend(prop={"size": 30})
figure = plt.gcf()
figure.set_size_inches(11, 7) # horizontal x vertical
plt.suptitle("Population without Service over Time", fontsize=20)
plt.xlabel("Time (Minutes)", fontsize=20)
plt.ylabel("Population Count", fontsize=20)
ax.tick_params(axis="both", which="major", labelsize=20)
ax.tick_params(axis="both", which="minor", labelsize=20)
plt.savefig(loc)
# plt.show()
@no_type_check
def draw_graph_with_partitions(nx_g, assignments: list[list[int]], name=None) -> None:
"""
Draws a graph using networkx
Not all edges are drawn. Just the ones pertaining to the passed edges
Parameters
----------
nx_g: nx.DiGraph()
Input Networkx Graph
assignments: list[list[int]]
Input agent assignment
Assertions:
Is agent assignment
name: str
Used to name output plot
Default: None
"""
# Color the nodes
idx: int = 0
color_list = plt.cm.get_cmap("tab20", 20)
color_map = [None] * len(nx_g.nodes)
for assignment in assignments:
for node in assignment:
color_map[node] = color_list.colors[idx]
idx = (idx + 1) % 20
# Choose edges
edges = []
for assignment in assignments:
for (u, v) in zip(assignment, assignment[1:]):
edges.append((u, v))
plt.figure(name)
nx.draw(
nx_g,
pos=nx.spring_layout(nx_g),
edgelist=edges,
with_labels=True,
node_color=color_map,
)
# plt.show()