-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccp_bench_eval.py
226 lines (203 loc) · 6.46 KB
/
ccp_bench_eval.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
from copy import deepcopy
import os
import numpy as np
from lib.problems import ProblemDataset
from baselines.utils import eval_method
from baselines.CPP import methods_registry
from baselines.CPP.methods_registry import CUDA_METHODS
from lib.ltr.ccp.method import CKMeans
from lib.ltr.utils import load_model
SIZE = None
SEED = 1
NSEEDS = 1
CUDA = True
K_NOT_KNOWN = False
CORES = 4
T_LIM = 360 # 6min
SAVE_DIR = f"./outputs_eval/ccp_bench/"
BL_DIR = os.path.join(SAVE_DIR, "baselines")
M_DIR = os.path.join(SAVE_DIR, "model")
smp_cfg = {"sample_size": SIZE}
INF = float("inf")
DS_PTH = f"data/CCP/benchmark/stefanello/ccp_bench.npz"
CKPT = "outputs/final/shanghai_tel_ccp_200/gnn_pool_pointwise/2023-02-08_15-59-27_166847/checkpoints/epoch=189_val_acc=0.9791.ckpt"
NUM_INIT = 8
#MTHD = "ccp_mh"
#MTHD = "ncc_greedy"
MTHD = "ncc_samp"
result = None
metrics = {}
RESULTS = {}
seeds = [SEED+i for i in range(NSEEDS)]
ds = ProblemDataset(problem="CCP", seed=SEED, data_pth=DS_PTH)
ds = ds.sample(**smp_cfg, allow_pickle=True)
if MTHD == "ccp_mh":
result, smry = eval_method(
method=getattr(methods_registry, MTHD),
dataset=ds,
seeds=seeds,
save_dir=BL_DIR,
cuda=CUDA,
k_not_known=K_NOT_KNOWN,
sample_cfg=smp_cfg,
num_init=NUM_INIT,
num_cores=CORES,
t_total=T_LIM,
t_local=T_LIM//10,
g_initial=10,
l=10,
init_method="capacity-based",
raise_error=True
)
m_id = f"{MTHD}{'_cuda' if CUDA and MTHD in CUDA_METHODS else ''}"
RESULTS[m_id] = result
# replace infeasible runs with mean cost of random method
res = deepcopy(result)
costs = np.array([r['tot_center_dist'] for r in res])
costs = costs.reshape(NSEEDS, -1)
for i in range(len(costs)):
inst_cost = costs[:, i]
inf_msk = inst_cost == INF
if np.any(inf_msk):
print(f"inf: {inf_msk.sum()}")
inst_cost[inf_msk] = 100
costs[:, i] = inst_cost
smry['center_dist_mean'] = np.mean(costs)
smry['center_dist_std'] = np.mean(np.std(costs, axis=0))
print(f"adapted summary: {smry}")
metrics[m_id] = smry
#print(RESULTS[m_id])
elif MTHD == "rpack":
if not CUDA or CUDA and MTHD in CUDA_METHODS:
result, smry = eval_method(
method=getattr(methods_registry, MTHD),
dataset=ds,
seeds=seeds,
save_dir=BL_DIR,
cuda=CUDA,
k_not_known=K_NOT_KNOWN,
sample_cfg=smp_cfg,
num_init=NUM_INIT,
num_cores=CORES,
gurobi_timeout=T_LIM//4,
timeout=T_LIM,
timeout_kill=(T_LIM*2)+1,
verbose=False,
)
m_id = f"{MTHD}{'_cuda' if CUDA and MTHD in CUDA_METHODS else ''}"
RESULTS[m_id] = result
# replace infeasible runs with mean cost of random method
res = deepcopy(result)
costs = np.array([r['tot_center_dist'] for r in res])
costs = costs.reshape(NSEEDS, -1)
for i in range(len(costs)):
inst_cost = costs[:, i]
inf_msk = inst_cost == INF
if np.any(inf_msk):
print(f"inf: {inf_msk.sum()}")
inst_cost[inf_msk] = 1000
costs[:, i] = inst_cost
smry['center_dist_mean'] = np.mean(costs)
smry['center_dist_std'] = np.mean(np.std(costs, axis=0))
print(f"adapted summary: {smry}")
metrics[m_id] = smry
elif MTHD == "ncc_greedy":
# greedily assigns the last 'opt_last_frac' fraction of total nodes
# ordered by their absolute priority to the closest center
model = load_model("ccp", CKPT)
ckmeans = CKMeans(
max_iter=50,
num_init=NUM_INIT,
model=model,
seed=SEED,
nbh_knn=25,
init_method="ckm++",
permute_k=False,
tol=0.001,
pre_iter=0,
verbose=False,
opt_last_frac=0.7,
opt_last_samples=1,
opt_last_prio=True
)
result, smry = eval_method(
method=ckmeans.inference,
dataset=ds,
seeds=seeds,
save_dir=M_DIR,
cuda=CUDA,
k_not_known=K_NOT_KNOWN,
sample_cfg=smp_cfg,
method_str=MTHD,
time_limit=T_LIM,
)
m_id = f"{MTHD}{'_cuda' if CUDA and MTHD in CUDA_METHODS else ''}"
RESULTS[m_id] = result
# replace infeasible runs with mean cost of random method
res = deepcopy(result)
costs = np.array([r['tot_center_dist'] for r in res])
costs = costs.reshape(NSEEDS, -1)
for i in range(len(costs)):
inst_cost = costs[:, i]
inf_msk = inst_cost == INF
if np.any(inf_msk):
print(f"inf: {inf_msk.sum()}")
inst_cost[inf_msk] = 100
costs[:, i] = inst_cost
smry['center_dist_mean'] = np.mean(costs)
smry['center_dist_std'] = np.mean(np.std(costs, axis=0))
print(f"adapted summary: {smry}")
metrics[m_id] = smry
#print(RESULTS[m_id])
elif MTHD == "ncc_samp":
# samples multiple assignments for the last 'opt_last_frac' fraction of total nodes
# and selects the best one
model = load_model("ccp", CKPT)
ckmeans = CKMeans(
max_iter=50,
num_init=NUM_INIT,
model=model,
seed=SEED,
nbh_knn=25,
init_method="ckm++",
permute_k=False,
tol=0.0001,
pre_iter=0,
verbose=False,
opt_last_frac=0.7,
opt_last_samples=64,
opt_last_prio=True
)
result, smry = eval_method(
method=ckmeans.inference,
dataset=ds,
seeds=seeds,
save_dir=M_DIR,
cuda=CUDA,
k_not_known=K_NOT_KNOWN,
sample_cfg=smp_cfg,
method_str=MTHD,
)
m_id = f"{MTHD}{'_cuda' if CUDA and MTHD in CUDA_METHODS else ''}"
RESULTS[m_id] = result
# replace infeasible runs with mean cost of random method
res = deepcopy(result)
costs = np.array([r['tot_center_dist'] for r in res])
costs = costs.reshape(NSEEDS, -1)
for i in range(len(costs)):
inst_cost = costs[:, i]
inf_msk = inst_cost == INF
if np.any(inf_msk):
print(f"inf: {inf_msk.sum()}")
inst_cost[inf_msk] = 100
costs[:, i] = inst_cost
smry['center_dist_mean'] = np.mean(costs)
smry['center_dist_std'] = np.mean(np.std(costs, axis=0))
print(f"adapted summary: {smry}")
metrics[m_id] = smry
#print(RESULTS[m_id])
else:
raise RuntimeError
for i, r in zip(ds.data, result):
print(i)
print(r)