-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path05_evaluate.py
More file actions
298 lines (257 loc) · 9.28 KB
/
05_evaluate.py
File metadata and controls
298 lines (257 loc) · 9.28 KB
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
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #
# Evaluate all models (il, rl, untrained, and random), B*FS and SCIP's default #
# rule on 2 benchmark sets (test and transfer). Each instance-model pair is #
# solved with 5 different seeds. Results are written into csv files. #
# Usage: python 05_evaluate.py <problem> -s <seed> -g <cudaId> -j <njobs> #
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #
import os
import csv
import json
import time
import glob
import queue
import argparse
import utilities
import model as ml
import numpy as np
import torch as th
import pyscipopt as scip
import multiprocessing as mp
from tqdm import trange
from nodesels.nodesel_policy import NodeselPolicy
from scipy.stats import gmean, gstd
class NodeselBFS(scip.Nodesel):
def __init__(self):
super().__init__()
def __str__(self):
return "BFS"
def nodeselect(self):
selnode = self.model.getBestboundNode()
return {'selnode': selnode}
class NodeselRandom(NodeselPolicy):
def __init__(self, seed):
super().__init__(name="Random")
self.random = np.random.default_rng(seed)
def nodecomp(self, node1, node2):
return -1 if self.random.random() < 0.5 else 1
def evaluate(in_queue, out_queue, nodesel, static):
"""
Worker loop: fetch an instance, run an episode and record samples.
Parameters
----------
in_queue : queue.Queue
Input queue from which instances are received.
out_queue : queue.Queue
Output queue in which to put solutions.
"""
while not in_queue.empty():
num_nodes = []
solvetime = []
instance, base_seed, opt_sol = in_queue.get()
instance_name = os.path.basename(instance)
for seed in range(base_seed, base_seed + 5):
th.manual_seed(seed)
# Initialize SCIP model
m = scip.Model()
m.hideOutput()
m.readProblem(instance)
# 1: CPU user seconds, 2: wall clock time
m.setIntParam('timing/clocktype', 1)
m.setRealParam('limits/time', 150)
utilities.init_scip_params(m, seed, static)
m.setRealParam('limits/objectivestop', opt_sol)
if nodesel is not None:
m.includeNodesel(nodesel=nodesel,
name=f"nodesel_{nodesel}",
desc="nodesel to be evaluated",
stdpriority=300000,
memsavepriority=300000)
# Solve and retrieve solutions
wall_time = time.perf_counter()
proc_time = time.process_time()
m.optimize()
wall_time = time.perf_counter() - wall_time
proc_time = time.process_time() - proc_time
num_nodes.append(m.getNNodes())
solvetime.append(m.getSolvingTime())
out_queue.put({
'type': "row",
'seed': seed,
'instance': instance_name,
'status': m.getStatus(),
'nnodes': m.getNNodes(),
'nlps': m.getNLPs(),
'gap': m.getGap(),
'nsols': m.getNBestSolsFound(),
'stime': m.getSolvingTime(),
'walltime': wall_time,
'proctime': proc_time,
})
m.freeProb()
out_queue.put({
'type': "stats",
'instance': instance_name,
'nnodes': np.mean(num_nodes),
'solve_time': np.mean(solvetime),
})
def collect_evaluation(instances, opt_sols, seed, n_jobs, nodesel, static, result_file):
"""
Runs branch-and-bound episodes on the given set of instances
with the provided node selector and settings.
Parameters
----------
instances : list
Instances to process
opt_sols : dict
Optimal solution values for all instances
seed : int
Base seed for all evaluations
n_jobs : int
Number of jobs for parallel sampling.
nodesel : object
Nodesel for which to evaluate
static : bool
Environment type to evaluate in
result_file : str
File to output results to
"""
in_queue = mp.Queue()
out_queue = mp.Queue()
for instance in instances:
in_queue.put([instance, seed, opt_sols[instance]])
workers = []
for i in range(n_jobs):
p = mp.Process(
target=evaluate,
args=(in_queue, out_queue, nodesel, static),
daemon=True)
workers.append(p)
p.start()
nnodes = []
stimes = []
with open(result_file, 'w', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for _ in trange(6 * len(instances)):
try: answer = out_queue.get(timeout=160)
# if no response is given in time_limit seconds,
# the solver has crashed and the worker is dead:
# start a new worker to pick up the pieces.
except queue.Empty:
print("starting new worker")
p = mp.Process(
target=evaluate,
args=(in_queue, out_queue, nodesel, static),
daemon=True)
workers.append(p)
p.start()
continue
if answer['type'] == "row":
del answer['type']
writer.writerow(answer)
csvfile.flush()
else: # answer['type'] == "stats"
nnodes.append(answer['nnodes'])
stimes.append(answer['solve_time'])
for p in workers:
p.join()
return {'nnodes_g': gmean(nnodes),
'nnodes_std': gstd(nnodes),
'stimes_g': gmean(stimes),
'stimes_std': gstd(stimes),
}
if __name__ == "__main__":
# read default config file
with open('config.json') as f:
config = json.load(f)
# read command-line arguments
parser = argparse.ArgumentParser()
parser.add_argument(
'problem',
help='MILP instance type to process.',
choices=config['problems'],
)
parser.add_argument(
'-s', '--seed',
help='Random generator seed.',
default=config['seed'],
type=int,
)
parser.add_argument(
'-g', '--gpu',
help='CUDA GPU id (-1 for CPU).',
default=config['gpu'],
type=int,
)
parser.add_argument(
'-j', '--njobs',
help='Number of parallel jobs.',
default=1,
type=int,
)
args = parser.parse_args()
### PYTORCH SETUP ###
if args.gpu == -1:
os.environ['CUDA_VISIBLE_DEVICES'] = ''
device = 'cpu'
else:
os.environ['CUDA_VISIBLE_DEVICES'] = f'{args.gpu}'
device = f"cuda:0"
timestamp = time.strftime('%Y-%m-%d--%H.%M.%S')
experiment_dir = f'experiments/{args.problem}/05_evaluate'
running_dir = experiment_dir + f'/{args.seed}_{timestamp}'
os.makedirs(running_dir, exist_ok=True)
# Default: BestEstimate, BFS, Random, Untrained Policy
model = ml.MLPPolicy().to(device)
nodesels = ( # [None, NodeselBFS(), NodeselRandom(args.seed),
[NodeselPolicy(model, device, "policy")])
# nodesels = []
# Learned models
for model_id in ["il_k=10_Children"]: # , "rl_mdp", "il_k=1_Nodes", "rl_lb-obj_active"
model_path = f'actor/{args.problem}/{model_id}.pkl'
if os.path.exists(model_path):
model = ml.MLPPolicy(16).to(device)
model.load_state_dict(th.load(model_path))
nodesel = NodeselPolicy(model, device, model_id)
nodesels.append(nodesel)
print(f"problem: {args.problem}")
print(f"gpu: {args.gpu}")
fieldnames = [
'seed',
'instance',
'status',
'nnodes',
'nlps',
'gap',
'nsols',
'stime',
'walltime',
'proctime',
]
transfer_difficulty = {
'indset': "1000_4",
'gisp': "80_0.5",
'mkp': "100_8",
'cflp': "60_25_3",
'fcmcnf': "30_45_100",
'setcover': "500_1000_0.05",
'cauctions': "200_1000"
}[args.problem]
results = {}
for instance_type in ["test", "transfer"]:
difficulty = transfer_difficulty if instance_type == "transfer" else config['difficulty'][args.problem]
instance_dir = f'data/{args.problem}/instances/{instance_type}_{difficulty}'
instances = [str(file).replace(os.sep, '/') for file in glob.glob(instance_dir + f'/*.lp')]
instance_dir = f'data/{args.problem}/instances'
with open(instance_dir + f'/obj_values.json') as f:
opt_sols = json.load(f)
for nodesel in nodesels:
for static in [True]: # , False
env = "static" if static else "active"
experiment_id = f"{instance_type}_{env}_{nodesel}"
utilities.log(f"Starting experiment {experiment_id}")
result_file = os.path.join(running_dir, f'{experiment_id}_results.csv')
stats = collect_evaluation(instances, opt_sols, args.seed, args.njobs, nodesel, static, result_file)
results[experiment_id] = stats
print(stats)
print(results)