forked from netcontract/ncflow
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgrid_search.py
executable file
·168 lines (149 loc) · 5.55 KB
/
grid_search.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
#! /usr/bin/env python
from itertools import product
import numpy as np
import traceback
import os
import sys
sys.path.append("..")
from lib.problem import Problem
from lib.partitioning import FMPartitioning, SpectralClustering
from lib.partitioning.utils import all_partitions_contiguous
from lib.algorithms import NcfEpi
from benchmarks.benchmark_consts import HOLDOUT_PROBLEMS
OUTPUT_CSV = "grid-search.csv"
LOG_DIR = "grid-search-logs"
def print_(*args, file=None):
if file is None:
file = sys.stdout
print(*args, file=file)
file.flush()
def grid_search(
problem_name,
topo_fname,
tm_fname,
num_paths_to_sweep=[4],
edge_disjoint_to_sweep=[True, False],
dist_metrics_to_sweep=["inv-cap"],
partition_algos_to_sweep=["fm_partitioning", "spectral_clustering"],
num_parts_scale_factors_to_sweep=[1, 2, 3, 4],
):
problem = Problem.from_file(topo_fname, tm_fname)
assert problem_name == problem.name
print_(problem.name, tm_fname)
traffic_seed = problem.traffic_matrix.seed
total_demand = np.sum(problem.traffic_matrix.tm)
print_("traffic seed: {}".format(traffic_seed))
print_("traffic matrix model: {}".format(problem.traffic_matrix.model))
print_("traffic scale factor: {}".format(problem.traffic_matrix.scale_factor))
print_("total demand: {}".format(total_demand))
num_parts_to_sweep = [
sf * int(np.sqrt(len(problem.G.nodes)))
for sf in num_parts_scale_factors_to_sweep
]
for (
partition_algo,
num_partitions_to_set,
num_paths,
edge_disjoint,
dist_metric,
) in product(
partition_algos_to_sweep,
num_parts_to_sweep,
num_paths_to_sweep,
edge_disjoint_to_sweep,
dist_metrics_to_sweep,
):
if partition_algo == "fm_partitioning":
partitioner = FMPartitioning(num_partitions_to_set)
elif partition_algo == "spectral_clustering":
partitioner = SpectralClustering(num_partitions_to_set)
print_(
"\nNCFlow, {} partitioner, {} partitions, {} paths, edge disjoint {}, dist metric {}".format(
partition_algo,
num_partitions_to_set,
num_paths,
edge_disjoint,
dist_metric,
)
)
run_nc_dir = os.path.join(
LOG_DIR,
"ncflow",
partition_algo,
"{}-partitions".format(num_partitions_to_set),
"{}-paths".format(num_paths),
"edge-disjoint-{}".format(edge_disjoint),
"{}-dist-metric".format(dist_metric),
)
if not os.path.exists(run_nc_dir):
os.makedirs(run_nc_dir)
with open(
os.path.join(
run_nc_dir,
"{}-ncflow-{}_partitioner-{}_partitions-{}_paths-{}_edge_disjoint-{}_dist_metric.txt".format(
problem.name,
partition_algo,
num_partitions_to_set,
num_paths,
edge_disjoint,
dist_metric,
),
),
"w",
) as log:
partition_vector = partitioner.partition(problem)
if not all_partitions_contiguous(problem, partition_vector):
print_(
"Topology {}, partitioner {}, num_partitions_to_set {} did not find a valid partition".format(
topo_fname, partition_algo, num_partitions_to_set
)
)
continue
try:
ncflow = NcfEpi.new_total_flow(
num_paths,
edge_disjoint=edge_disjoint,
dist_metric=dist_metric,
out=log,
)
ncflow.solve(problem, partitioner)
num_partitions = len(np.unique(ncflow._partition_vector))
size_of_largest_partition = partitioner.size_of_largest_partition
runtime = ncflow.runtime_est(14)
total_flow = ncflow.obj_val
with open(OUTPUT_CSV, "a") as w:
print_(
"{},{},{},{},{},{},{},{},{},{}".format(
problem.name,
os.path.basename(tm_fname),
partition_algo,
num_partitions,
size_of_largest_partition,
num_paths,
edge_disjoint,
dist_metric,
total_flow,
runtime,
),
file=w,
)
except:
print_(
"TM {}, {} partitioner, {} partitions, {} paths, edge disjoint {}, dist metric {} failed".format(
tm_fname,
partition_algo,
num_partitions_to_set,
num_paths,
edge_disjoint,
dist_metric,
)
)
traceback.print_exc(file=sys.stdout)
if __name__ == "__main__":
with open(OUTPUT_CSV, "a") as w:
print_(
"problem,tm_fname,partition_algo,num_partitions,size_of_largest_partition,num_paths,edge_disjoint,dist_metric,total_flow,runtime",
file=w,
)
for problem_name, topo_fname, tm_fname in HOLDOUT_PROBLEMS:
grid_search(problem_name, topo_fname, tm_fname)