forked from ds4dm/learn2branch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
02_generate_dataset.py
376 lines (310 loc) · 13.6 KB
/
02_generate_dataset.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
import os
import argparse
import multiprocessing as mp
import pickle
import glob
import numpy as np
import shutil
import gzip
import pyscipopt as scip
import utilities
class SamplingAgent(scip.Branchrule):
def __init__(self, episode, instance, seed, out_queue, exploration_policy, query_expert_prob, out_dir, follow_expert=True):
self.episode = episode
self.instance = instance
self.seed = seed
self.out_queue = out_queue
self.exploration_policy = exploration_policy
self.query_expert_prob = query_expert_prob
self.out_dir = out_dir
self.follow_expert = follow_expert
self.rng = np.random.RandomState(seed)
self.new_node = True
self.sample_counter = 0
def branchinit(self):
self.khalil_root_buffer = {}
def branchexeclp(self, allowaddcons):
if self.model.getNNodes() == 1:
# initialize root buffer for Khalil features extraction
utilities.extract_khalil_variable_features(self.model, [], self.khalil_root_buffer)
# once in a while, also run the expert policy and record the (state, action) pair
query_expert = self.rng.rand() < self.query_expert_prob
if query_expert:
state = utilities.extract_state(self.model)
cands, *_ = self.model.getPseudoBranchCands()
state_khalil = utilities.extract_khalil_variable_features(self.model, cands, self.khalil_root_buffer)
result = self.model.executeBranchRule('vanillafullstrong', allowaddcons)
cands_, scores, npriocands, bestcand = self.model.getVanillafullstrongData()
assert result == scip.SCIP_RESULT.DIDNOTRUN
assert all([c1.getCol().getLPPos() == c2.getCol().getLPPos() for c1, c2 in zip(cands, cands_)])
action_set = [c.getCol().getLPPos() for c in cands]
expert_action = action_set[bestcand]
data = [state, state_khalil, expert_action, action_set, scores]
# Do not record inconsistent scores. May happen if SCIP was early stopped (time limit).
if not any([s < 0 for s in scores]):
filename = f'{self.out_dir}/sample_{self.episode}_{self.sample_counter}.pkl'
with gzip.open(filename, 'wb') as f:
pickle.dump({
'episode': self.episode,
'instance': self.instance,
'seed': self.seed,
'node_number': self.model.getCurrentNode().getNumber(),
'node_depth': self.model.getCurrentNode().getDepth(),
'data': data,
}, f)
self.out_queue.put({
'type': 'sample',
'episode': self.episode,
'instance': self.instance,
'seed': self.seed,
'node_number': self.model.getCurrentNode().getNumber(),
'node_depth': self.model.getCurrentNode().getDepth(),
'filename': filename,
})
self.sample_counter += 1
# if exploration and expert policies are the same, prevent running it twice
if not query_expert or (not self.follow_expert and self.exploration_policy != 'vanillafullstrong'):
result = self.model.executeBranchRule(self.exploration_policy, allowaddcons)
# apply 'vanillafullstrong' branching decision if needed
if query_expert and self.follow_expert or self.exploration_policy == 'vanillafullstrong':
assert result == scip.SCIP_RESULT.DIDNOTRUN
cands, scores, npriocands, bestcand = self.model.getVanillafullstrongData()
self.model.branchVar(cands[bestcand])
result = scip.SCIP_RESULT.BRANCHED
return {"result": result}
def make_samples(in_queue, out_queue):
"""
Worker loop: fetch an instance, run an episode and record samples.
Parameters
----------
in_queue : multiprocessing.Queue
Input queue from which orders are received.
out_queue : multiprocessing.Queue
Output queue in which to send samples.
"""
while True:
episode, instance, seed, exploration_policy, query_expert_prob, time_limit, out_dir = in_queue.get()
print(f'[w {os.getpid()}] episode {episode}, seed {seed}, processing instance \'{instance}\'...')
m = scip.Model()
m.setIntParam('display/verblevel', 0)
m.readProblem(f'{instance}')
utilities.init_scip_params(m, seed=seed)
m.setIntParam('timing/clocktype', 2)
m.setRealParam('limits/time', time_limit)
branchrule = SamplingAgent(
episode=episode,
instance=instance,
seed=seed,
out_queue=out_queue,
exploration_policy=exploration_policy,
query_expert_prob=query_expert_prob,
out_dir=out_dir)
m.includeBranchrule(
branchrule=branchrule,
name="Sampling branching rule", desc="",
priority=666666, maxdepth=-1, maxbounddist=1)
m.setBoolParam('branching/vanillafullstrong/integralcands', True)
m.setBoolParam('branching/vanillafullstrong/scoreall', True)
m.setBoolParam('branching/vanillafullstrong/collectscores', True)
m.setBoolParam('branching/vanillafullstrong/donotbranch', True)
m.setBoolParam('branching/vanillafullstrong/idempotent', True)
out_queue.put({
'type': 'start',
'episode': episode,
'instance': instance,
'seed': seed,
})
m.optimize()
m.freeProb()
print(f"[w {os.getpid()}] episode {episode} done, {branchrule.sample_counter} samples")
out_queue.put({
'type': 'done',
'episode': episode,
'instance': instance,
'seed': seed,
})
def send_orders(orders_queue, instances, seed, exploration_policy, query_expert_prob, time_limit, out_dir):
"""
Continuously send sampling orders to workers (relies on limited
queue capacity).
Parameters
----------
orders_queue : multiprocessing.Queue
Queue to which to send orders.
instances : list
Instance file names from which to sample episodes.
seed : int
Random seed for reproducibility.
exploration_policy : str
Branching strategy for exploration.
query_expert_prob : float in [0, 1]
Probability of running the expert strategy and collecting samples.
time_limit : float in [0, 1e+20]
Maximum running time for an episode, in seconds.
out_dir: str
Output directory in which to write samples.
"""
rng = np.random.RandomState(seed)
episode = 0
while True:
instance = rng.choice(instances)
seed = rng.randint(2**32)
orders_queue.put([episode, instance, seed, exploration_policy, query_expert_prob, time_limit, out_dir])
episode += 1
def collect_samples(instances, out_dir, rng, n_samples, n_jobs,
exploration_policy, query_expert_prob, time_limit):
"""
Runs branch-and-bound episodes on the given set of instances, and collects
randomly (state, action) pairs from the 'vanilla-fullstrong' expert
brancher.
Parameters
----------
instances : list
Instance files from which to collect samples.
out_dir : str
Directory in which to write samples.
rng : numpy.random.RandomState
A random number generator for reproducibility.
n_samples : int
Number of samples to collect.
n_jobs : int
Number of jobs for parallel sampling.
exploration_policy : str
Exploration policy (branching rule) for sampling.
query_expert_prob : float in [0, 1]
Probability of using the expert policy and recording a (state, action)
pair.
time_limit : float in [0, 1e+20]
Maximum running time for an episode, in seconds.
"""
os.makedirs(out_dir, exist_ok=True)
# start workers
orders_queue = mp.Queue(maxsize=2*n_jobs)
answers_queue = mp.SimpleQueue()
workers = []
for i in range(n_jobs):
p = mp.Process(
target=make_samples,
args=(orders_queue, answers_queue),
daemon=True)
workers.append(p)
p.start()
tmp_samples_dir = f'{out_dir}/tmp'
os.makedirs(tmp_samples_dir, exist_ok=True)
# start dispatcher
dispatcher = mp.Process(
target=send_orders,
args=(orders_queue, instances, rng.randint(2**32), exploration_policy, query_expert_prob, time_limit, tmp_samples_dir),
daemon=True)
dispatcher.start()
# record answers and write samples
buffer = {}
current_episode = 0
i = 0
in_buffer = 0
while i < n_samples:
sample = answers_queue.get()
# add received sample to buffer
if sample['type'] == 'start':
buffer[sample['episode']] = []
else:
buffer[sample['episode']].append(sample)
if sample['type'] == 'sample':
in_buffer += 1
# if any, write samples from current episode
while current_episode in buffer and buffer[current_episode]:
samples_to_write = buffer[current_episode]
buffer[current_episode] = []
for sample in samples_to_write:
# if no more samples here, move to next episode
if sample['type'] == 'done':
del buffer[current_episode]
current_episode += 1
# else write sample
else:
os.rename(sample['filename'], f'{out_dir}/sample_{i+1}.pkl')
in_buffer -= 1
i += 1
print(f"[m {os.getpid()}] {i} / {n_samples} samples written, ep {sample['episode']} ({in_buffer} in buffer).")
# early stop dispatcher (hard)
if in_buffer + i >= n_samples and dispatcher.is_alive():
dispatcher.terminate()
print(f"[m {os.getpid()}] dispatcher stopped...")
# as soon as enough samples are collected, stop
if i == n_samples:
buffer = {}
break
# stop all workers (hard)
for p in workers:
p.terminate()
shutil.rmtree(tmp_samples_dir, ignore_errors=True)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'problem',
help='MILP instance type to process.',
choices=['setcover', 'cauctions', 'facilities', 'indset'],
)
parser.add_argument(
'-s', '--seed',
help='Random generator seed.',
type=utilities.valid_seed,
default=0,
)
parser.add_argument(
'-j', '--njobs',
help='Number of parallel jobs.',
type=int,
default=1,
)
args = parser.parse_args()
print(f"seed {args.seed}")
train_size = 100000
valid_size = 20000
test_size = 20000
exploration_strategy = 'pscost'
node_record_prob = 0.05
time_limit = 3600
if args.problem == 'setcover':
instances_train = glob.glob('data/instances/setcover/train_500r_1000c_0.05d/*.lp')
instances_valid = glob.glob('data/instances/setcover/valid_500r_1000c_0.05d/*.lp')
instances_test = glob.glob('data/instances/setcover/test_500r_1000c_0.05d/*.lp')
out_dir = 'data/samples/setcover/500r_1000c_0.05d'
elif args.problem == 'cauctions':
instances_train = glob.glob('data/instances/cauctions/train_100_500/*.lp')
instances_valid = glob.glob('data/instances/cauctions/valid_100_500/*.lp')
instances_test = glob.glob('data/instances/cauctions/test_100_500/*.lp')
out_dir = 'data/samples/cauctions/100_500'
elif args.problem == 'indset':
instances_train = glob.glob('data/instances/indset/train_500_4/*.lp')
instances_valid = glob.glob('data/instances/indset/valid_500_4/*.lp')
instances_test = glob.glob('data/instances/indset/test_500_4/*.lp')
out_dir = 'data/samples/indset/500_4'
elif args.problem == 'facilities':
instances_train = glob.glob('data/instances/facilities/train_100_100_5/*.lp')
instances_valid = glob.glob('data/instances/facilities/valid_100_100_5/*.lp')
instances_test = glob.glob('data/instances/facilities/test_100_100_5/*.lp')
out_dir = 'data/samples/facilities/100_100_5'
time_limit = 600
else:
raise NotImplementedError
print(f"{len(instances_train)} train instances for {train_size} samples")
print(f"{len(instances_valid)} validation instances for {valid_size} samples")
print(f"{len(instances_test)} test instances for {test_size} samples")
# create output directory, throws an error if it already exists
os.makedirs(out_dir)
rng = np.random.RandomState(args.seed)
collect_samples(instances_train, out_dir + '/train', rng, train_size,
args.njobs, exploration_policy=exploration_strategy,
query_expert_prob=node_record_prob,
time_limit=time_limit)
rng = np.random.RandomState(args.seed + 1)
collect_samples(instances_valid, out_dir + '/valid', rng, test_size,
args.njobs, exploration_policy=exploration_strategy,
query_expert_prob=node_record_prob,
time_limit=time_limit)
rng = np.random.RandomState(args.seed + 2)
collect_samples(instances_test, out_dir + '/test', rng, test_size,
args.njobs, exploration_policy=exploration_strategy,
query_expert_prob=node_record_prob,
time_limit=time_limit)