-
Notifications
You must be signed in to change notification settings - Fork 18
/
02_generate_dataset.py
337 lines (288 loc) · 12.3 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
import os
import glob
import gzip
import argparse
import pickle
import queue
import shutil
import threading
import numpy as np
import ecole
from collections import namedtuple
class ExploreThenStrongBranch:
def __init__(self, expert_probability):
self.expert_probability = expert_probability
self.pseudocosts_function = ecole.observation.Pseudocosts()
self.strong_branching_function = ecole.observation.StrongBranchingScores()
def before_reset(self, model):
self.pseudocosts_function.before_reset(model)
self.strong_branching_function.before_reset(model)
def extract(self, model, done):
probabilities = [1-self.expert_probability, self.expert_probability]
expert_chosen = bool(np.random.choice(np.arange(2), p=probabilities))
if expert_chosen:
return (self.strong_branching_function.extract(model,done), True)
else:
return (self.pseudocosts_function.extract(model,done), False)
def send_orders(orders_queue, instances, seed, query_expert_prob, time_limit, out_dir, stop_flag):
"""
Continuously send sampling orders to workers (relies on limited
queue capacity).
Parameters
----------
orders_queue : queue.Queue
Queue to which to send orders.
instances : list
Instance file names from which to sample episodes.
seed : int
Random seed for reproducibility.
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.
stop_flag: threading.Event
A flag to tell the thread to stop.
"""
rng = np.random.RandomState(seed)
episode = 0
while not stop_flag.is_set():
instance = rng.choice(instances)
seed = rng.randint(2**32)
orders_queue.put([episode, instance, seed, query_expert_prob, time_limit, out_dir])
episode += 1
def make_samples(in_queue, out_queue, stop_flag):
"""
Worker loop: fetch an instance, run an episode and record samples.
Parameters
----------
in_queue : queue.Queue
Input queue from which orders are received.
out_queue : queue.Queue
Output queue in which to send samples.
stop_flag: threading.Event
A flag to tell the thread to stop.
"""
sample_counter = 0
while not stop_flag.is_set():
episode, instance, seed, query_expert_prob, time_limit, out_dir = in_queue.get()
scip_parameters = {'separating/maxrounds': 0, 'presolving/maxrestarts': 0,
'limits/time': time_limit, 'timing/clocktype': 2}
observation_function = { "scores": ExploreThenStrongBranch(expert_probability=query_expert_prob),
"node_observation": ecole.observation.NodeBipartite() }
env = ecole.environment.Branching(observation_function=observation_function,
scip_params=scip_parameters, pseudo_candidates=True)
print(f"[w {threading.current_thread().name}] episode {episode}, seed {seed}, "
f"processing instance '{instance}'...\n", end='')
out_queue.put({
'type': 'start',
'episode': episode,
'instance': instance,
'seed': seed,
})
env.seed(seed)
observation, action_set, _, done, _ = env.reset(instance)
while not done:
scores, scores_are_expert = observation["scores"]
node_observation = observation["node_observation"]
node_observation = (node_observation.row_features,
(node_observation.edge_features.indices,
node_observation.edge_features.values),
node_observation.variable_features)
action = action_set[scores[action_set].argmax()]
if scores_are_expert and not stop_flag.is_set():
data = [node_observation, action, action_set, scores]
filename = f'{out_dir}/sample_{episode}_{sample_counter}.pkl'
with gzip.open(filename, 'wb') as f:
pickle.dump({
'episode': episode,
'instance': instance,
'seed': seed,
'data': data,
}, f)
out_queue.put({
'type': 'sample',
'episode': episode,
'instance': instance,
'seed': seed,
'filename': filename,
})
sample_counter += 1
try:
observation, action_set, _, done, _ = env.step(action)
except Exception as e:
done = True
with open("error_log.txt","a") as f:
f.write(f"Error occurred solving {instance} with seed {seed}\n")
f.write(f"{e}\n")
print(f"[w {threading.current_thread().name}] episode {episode} done, {sample_counter} samples\n", end='')
out_queue.put({
'type': 'done',
'episode': episode,
'instance': instance,
'seed': seed,
})
def collect_samples(instances, out_dir, rng, n_samples, n_jobs,
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.
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 = queue.Queue(maxsize=2*n_jobs)
answers_queue = queue.SimpleQueue()
tmp_samples_dir = f'{out_dir}/tmp'
os.makedirs(tmp_samples_dir, exist_ok=True)
# start dispatcher
dispatcher_stop_flag = threading.Event()
dispatcher = threading.Thread(
target=send_orders,
args=(orders_queue, instances, rng.randint(2**32), query_expert_prob,
time_limit, tmp_samples_dir, dispatcher_stop_flag),
daemon=True)
dispatcher.start()
workers = []
workers_stop_flag = threading.Event()
for i in range(n_jobs):
p = threading.Thread(
target=make_samples,
args=(orders_queue, answers_queue, workers_stop_flag),
daemon=True)
workers.append(p)
p.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 {threading.current_thread().name}] {i} / {n_samples} samples written, "
f"ep {sample['episode']} ({in_buffer} in buffer).\n", end='')
# early stop dispatcher
if in_buffer + i >= n_samples and dispatcher.is_alive():
dispatcher_stop_flag.set()
print(f"[m {threading.current_thread().name}] dispatcher stopped...\n", end='')
# as soon as enough samples are collected, stop
if i == n_samples:
buffer = {}
break
# # stop all workers
workers_stop_flag.set()
for p in workers:
p.join()
print(f"Done collecting samples for {out_dir}")
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', 'mknapsack'],
)
parser.add_argument(
'-s', '--seed',
help='Random generator seed.',
type=int,
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
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
elif args.problem == 'mknapsack':
instances_train = glob.glob('data/instances/mknapsack/train_100_6/*.lp')
instances_valid = glob.glob('data/instances/mknapsack/valid_100_6/*.lp')
instances_test = glob.glob('data/instances/mknapsack/test_100_6/*.lp')
out_dir = 'data/samples/mknapsack/100_6'
time_limit = 60
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, exist_ok=True)
rng = np.random.RandomState(args.seed)
collect_samples(instances_train, out_dir + '/train', rng, train_size,
args.njobs, 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, 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, query_expert_prob=node_record_prob,
time_limit=time_limit)