-
Notifications
You must be signed in to change notification settings - Fork 4
/
collect.py
161 lines (136 loc) · 4.31 KB
/
collect.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
""" Collect MILP data for Imitation Learning (IL), by performing SCIP roll-outs. """
import os
import argparse
import pickle
import pyscipopt as scip
from src.environments import SCIPCollectEnv
from utils import STATE_DIMS
import multiprocessing as mp
import faulthandler
faulthandler.enable()
# solver parametric setting, key ('sandbox' or 'default') to be specified in argparse --setting
SETTINGS = {
'sandbox': {
'heuristics': False, # enable primal heuristics
'cutoff': True, # provide cutoff (value needs to be passed to the environment)
'conflict_usesb': False, # use SB conflict analysis
'probing_bounds': False, # use probing bounds identified during SB
'checksol': False, # check LP solutions found during strong branching with propagation
'reevalage': 0, # number of intermediate LPs solved to trigger reevaluation of SB value
},
'default': {
'heuristics': True,
'cutoff': False,
'conflict_usesb': True,
'probing_bounds': True,
'checksol': True,
'reevalage': 10,
},
}
# limits in solvers
LIMITS = {
'node_limit': -1,
'time_limit': 3600.,
}
# collection branching rules
COLLECTORS = {
'explorer': 'random',
'expert': 'relpscost'
}
def make_samples(args, instance_name):
# setup output directory and path to instance
outfile_dir = os.path.join(args.out_dir, 'SCIPCollect_{}_{}_{}'.format(
args.setting, args.seed, args.k_node
))
os.makedirs(outfile_dir, exist_ok=True)
instance_file_path = os.path.join(args.instances_dir, instance_name) # name contains extension mps.gz
name = instance_name.split('.')[0]
# get cutoff
cutoff_dict = pickle.load(open(args.cutoff_dict, 'rb'))
assert name in cutoff_dict
# setup the environment and collect data
env = SCIPCollectEnv()
exp_dict, collect_dict = env.run_episode(
instance=instance_file_path,
name=name,
explorer=COLLECTORS['explorer'],
expert=COLLECTORS['expert'],
k=args.k_node,
state_dims=STATE_DIMS,
scip_seed=args.seed,
cutoff_value=cutoff_dict[name],
scip_limits=LIMITS,
scip_params=SETTINGS[args.setting],
verbose=args.verbose,
)
# dump the dictionaries
f = open(os.path.join(outfile_dir, '{}_{}_{}_info.pkl'.format(name, args.seed, args.k_node)), 'wb')
pickle.dump(exp_dict, f)
f.close()
ff = open(os.path.join(outfile_dir, '{}_{}_{}_data.pkl'.format(name, args.seed, args.k_node)), 'wb')
pickle.dump(collect_dict, ff)
ff.close()
if __name__ == '__main__':
# parser definition
parser = argparse.ArgumentParser(description='Parser for SCIP data collection.')
parser.add_argument(
'-s',
'--seeds',
type=int,
nargs='+',
default=[0, 1, 2, 3, 4],
help='Random seed for SCIP solver.'
)
parser.add_argument(
'-k',
'--k_nodes',
type=int,
nargs='+',
default=[0,1,5,10,15],
help='Number of initial nodes to be explored randomly, before starting data collection.'
)
parser.add_argument(
'--setting',
type=str,
default='sandbox',
help='Solver parameters setting.'
)
parser.add_argument(
'-v',
'--verbose',
action='store_true',
default=False,
help='Flag on verbosity.'
)
parser.add_argument(
'--out_dir',
type=str,
help='Path to output directory',
)
parser.add_argument(
'--instances_dir',
type=str,
help='Path to MILP instances',
)
parser.add_argument(
'--cutoff_dict',
type=str,
help='Path to pickled dictionary containing cutoff values'
)
parser.add_argument(
'-j', '--njobs',
type=int,
help='Number of parallel jobs.',
)
args = parser.parse_args()
instance_names = os.listdir(args.instances_dir)
pool = mp.Pool(processes=args.njobs)
for k_node in args.k_nodes:
args.k_node = k_node
for seed in args.seeds:
args.seed = seed
for instance_name in instance_names:
pool.apply_async(make_samples, (args, instance_name))
pool.close()
pool.join()
pool.terminate()