forked from ds4dm/learn2branch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
05_evaluate.py
325 lines (263 loc) · 12.2 KB
/
05_evaluate.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
import os
import sys
import importlib
import argparse
import csv
import numpy as np
import time
import pickle
import pyscipopt as scip
import tensorflow as tf
import tensorflow.contrib.eager as tfe
import svmrank
import utilities
class PolicyBranching(scip.Branchrule):
def __init__(self, policy):
super().__init__()
self.policy_type = policy['type']
self.policy_name = policy['name']
if self.policy_type == 'gcnn':
model = policy['model']
model.restore_state(policy['parameters'])
self.policy = tfe.defun(model.call, input_signature=model.input_signature)
elif self.policy_type == 'internal':
self.policy = policy['name']
elif self.policy_type == 'ml-competitor':
self.policy = policy['model']
# feature parameterization
self.feat_shift = policy['feat_shift']
self.feat_scale = policy['feat_scale']
self.feat_specs = policy['feat_specs']
else:
raise NotImplementedError
def branchinitsol(self):
self.ndomchgs = 0
self.ncutoffs = 0
self.state_buffer = {}
self.khalil_root_buffer = {}
def branchexeclp(self, allowaddcons):
# SCIP internal branching rule
if self.policy_type == 'internal':
result = self.model.executeBranchRule(self.policy, allowaddcons)
# custom policy branching
else:
candidate_vars, *_ = self.model.getPseudoBranchCands()
candidate_mask = [var.getCol().getLPPos() for var in candidate_vars]
# initialize root buffer for Khalil features extraction
if self.model.getNNodes() == 1 \
and self.policy_type == 'ml-competitor' \
and self.feat_specs['type'] in ('khalil', 'all'):
utilities.extract_khalil_variable_features(self.model, [], self.khalil_root_buffer)
if len(candidate_vars) == 1:
best_var = candidate_vars[0]
elif self.policy_type == 'gcnn':
state = utilities.extract_state(self.model, self.state_buffer)
# convert state to tensors
c, e, v = state
state = (
tf.convert_to_tensor(c['values'], dtype=tf.float32),
tf.convert_to_tensor(e['indices'], dtype=tf.int32),
tf.convert_to_tensor(e['values'], dtype=tf.float32),
tf.convert_to_tensor(v['values'], dtype=tf.float32),
tf.convert_to_tensor([c['values'].shape[0]], dtype=tf.int32),
tf.convert_to_tensor([v['values'].shape[0]], dtype=tf.int32),
)
var_logits = self.policy(state, tf.convert_to_tensor(False)).numpy().squeeze(0)
candidate_scores = var_logits[candidate_mask]
best_var = candidate_vars[candidate_scores.argmax()]
elif self.policy_type == 'ml-competitor':
# build candidate features
candidate_states = []
if self.feat_specs['type'] in ('all', 'gcnn_agg'):
state = utilities.extract_state(self.model, self.state_buffer)
candidate_states.append(utilities.compute_extended_variable_features(state, candidate_mask))
if self.feat_specs['type'] in ('all', 'khalil'):
candidate_states.append(utilities.extract_khalil_variable_features(self.model, candidate_vars, self.khalil_root_buffer))
candidate_states = np.concatenate(candidate_states, axis=1)
# feature preprocessing
candidate_states = utilities.preprocess_variable_features(candidate_states, self.feat_specs['augment'], self.feat_specs['qbnorm'])
# feature normalization
candidate_states = (candidate_states - self.feat_shift) / self.feat_scale
candidate_scores = self.policy.predict(candidate_states)
best_var = candidate_vars[candidate_scores.argmax()]
else:
raise NotImplementedError
self.model.branchVar(best_var)
result = scip.SCIP_RESULT.BRANCHED
# fair node counting
if result == scip.SCIP_RESULT.REDUCEDDOM:
self.ndomchgs += 1
elif result == scip.SCIP_RESULT.CUTOFF:
self.ncutoffs += 1
return {'result': result}
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'problem',
help='MILP instance type to process.',
choices=['setcover', 'cauctions', 'facilities', 'indset'],
)
parser.add_argument(
'-g', '--gpu',
help='CUDA GPU id (-1 for CPU).',
type=int,
default=0,
)
args = parser.parse_args()
result_file = f"{args.problem}_{time.strftime('%Y%m%d-%H%M%S')}.csv"
instances = []
seeds = [0, 1, 2, 3, 4]
gcnn_models = ['baseline']
other_models = ['extratrees_gcnn_agg', 'lambdamart_khalil', 'svmrank_khalil']
internal_branchers = ['relpscost']
time_limit = 3600
if args.problem == 'setcover':
instances += [{'type': 'small', 'path': f"data/instances/setcover/transfer_500r_1000c_0.05d/instance_{i+1}.lp"} for i in range(20)]
instances += [{'type': 'medium', 'path': f"data/instances/setcover/transfer_1000r_1000c_0.05d/instance_{i+1}.lp"} for i in range(20)]
instances += [{'type': 'big', 'path': f"data/instances/setcover/transfer_2000r_1000c_0.05d/instance_{i+1}.lp"} for i in range(20)]
gcnn_models += ['mean_convolution', 'no_prenorm']
elif args.problem == 'cauctions':
instances += [{'type': 'small', 'path': f"data/instances/cauctions/transfer_100_500/instance_{i+1}.lp"} for i in range(20)]
instances += [{'type': 'medium', 'path': f"data/instances/cauctions/transfer_200_1000/instance_{i+1}.lp"} for i in range(20)]
instances += [{'type': 'big', 'path': f"data/instances/cauctions/transfer_300_1500/instance_{i+1}.lp"} for i in range(20)]
elif args.problem == 'facilities':
instances += [{'type': 'small', 'path': f"data/instances/facilities/transfer_100_100_5/instance_{i+1}.lp"} for i in range(20)]
instances += [{'type': 'medium', 'path': f"data/instances/facilities/transfer_200_100_5/instance_{i+1}.lp"} for i in range(20)]
instances += [{'type': 'big', 'path': f"data/instances/facilities/transfer_400_100_5/instance_{i+1}.lp"} for i in range(20)]
elif args.problem == 'indset':
instances += [{'type': 'small', 'path': f"data/instances/indset/transfer_500_4/instance_{i+1}.lp"} for i in range(20)]
instances += [{'type': 'medium', 'path': f"data/instances/indset/transfer_1000_4/instance_{i+1}.lp"} for i in range(20)]
instances += [{'type': 'big', 'path': f"data/instances/indset/transfer_1500_4/instance_{i+1}.lp"} for i in range(20)]
else:
raise NotImplementedError
branching_policies = []
# SCIP internal brancher baselines
for brancher in internal_branchers:
for seed in seeds:
branching_policies.append({
'type': 'internal',
'name': brancher,
'seed': seed,
})
# ML baselines
for model in other_models:
for seed in seeds:
branching_policies.append({
'type': 'ml-competitor',
'name': model,
'seed': seed,
'model': f'trained_models/{args.problem}/{model}/{seed}',
})
# GCNN models
for model in gcnn_models:
for seed in seeds:
branching_policies.append({
'type': 'gcnn',
'name': model,
'seed': seed,
'parameters': f'trained_models/{args.problem}/{model}/{seed}/best_params.pkl'
})
print(f"problem: {args.problem}")
print(f"gpu: {args.gpu}")
print(f"time limit: {time_limit} s")
### TENSORFLOW SETUP ###
if args.gpu == -1:
os.environ['CUDA_VISIBLE_DEVICES'] = ''
else:
os.environ['CUDA_VISIBLE_DEVICES'] = f'{args.gpu}'
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
tf.enable_eager_execution(config)
tf.executing_eagerly()
# load and assign tensorflow models to policies (share models and update parameters)
loaded_models = {}
for policy in branching_policies:
if policy['type'] == 'gcnn':
if policy['name'] not in loaded_models:
sys.path.insert(0, os.path.abspath(f"models/{policy['name']}"))
import model
importlib.reload(model)
loaded_models[policy['name']] = model.GCNPolicy()
del sys.path[0]
policy['model'] = loaded_models[policy['name']]
# load ml-competitor models
for policy in branching_policies:
if policy['type'] == 'ml-competitor':
try:
with open(f"{policy['model']}/normalization.pkl", 'rb') as f:
policy['feat_shift'], policy['feat_scale'] = pickle.load(f)
except:
policy['feat_shift'], policy['feat_scale'] = 0, 1
with open(f"{policy['model']}/feat_specs.pkl", 'rb') as f:
policy['feat_specs'] = pickle.load(f)
if policy['name'].startswith('svmrank'):
policy['model'] = svmrank.Model().read(f"{policy['model']}/model.txt")
else:
with open(f"{policy['model']}/model.pkl", 'rb') as f:
policy['model'] = pickle.load(f)
print("running SCIP...")
fieldnames = [
'policy',
'seed',
'type',
'instance',
'nnodes',
'nlps',
'stime',
'gap',
'status',
'ndomchgs',
'ncutoffs',
'walltime',
'proctime',
]
os.makedirs('results', exist_ok=True)
with open(f"results/{result_file}", 'w', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for instance in instances:
print(f"{instance['type']}: {instance['path']}...")
for policy in branching_policies:
tf.set_random_seed(policy['seed'])
m = scip.Model()
m.setIntParam('display/verblevel', 0)
m.readProblem(f"{instance['path']}")
utilities.init_scip_params(m, seed=policy['seed'])
m.setIntParam('timing/clocktype', 1) # 1: CPU user seconds, 2: wall clock time
m.setRealParam('limits/time', time_limit)
brancher = PolicyBranching(policy)
m.includeBranchrule(
branchrule=brancher,
name=f"{policy['type']}:{policy['name']}",
desc=f"Custom PySCIPOpt branching policy.",
priority=666666, maxdepth=-1, maxbounddist=1)
walltime = time.perf_counter()
proctime = time.process_time()
m.optimize()
walltime = time.perf_counter() - walltime
proctime = time.process_time() - proctime
stime = m.getSolvingTime()
nnodes = m.getNNodes()
nlps = m.getNLPs()
gap = m.getGap()
status = m.getStatus()
ndomchgs = brancher.ndomchgs
ncutoffs = brancher.ncutoffs
writer.writerow({
'policy': f"{policy['type']}:{policy['name']}",
'seed': policy['seed'],
'type': instance['type'],
'instance': instance['path'],
'nnodes': nnodes,
'nlps': nlps,
'stime': stime,
'gap': gap,
'status': status,
'ndomchgs': ndomchgs,
'ncutoffs': ncutoffs,
'walltime': walltime,
'proctime': proctime,
})
csvfile.flush()
m.freeProb()
print(f" {policy['type']}:{policy['name']} {policy['seed']} - {nnodes} ({nnodes+2*(ndomchgs+ncutoffs)}) nodes {nlps} lps {stime:.2f} ({walltime:.2f} wall {proctime:.2f} proc) s. {status}")