-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathenvironments.py
executable file
·258 lines (211 loc) · 10.1 KB
/
environments.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
import os
import numpy as np
import pyscipopt as scip
from logger import logger
class SCIPCutSelEnv():
def __init__(
self,
instance_file_path,
scip_seed,
seed,
scip_time_limit=3600,
single_instance_file=None,
**init_scip_kwargs
):
self.instance_file_path = instance_file_path
self.instances = os.listdir(instance_file_path)
self.single_instance_file = single_instance_file
self.scip_seed = scip_seed
self.seed = seed
self.scip_time_limit = scip_time_limit
self.init_scip_kwargs = init_scip_kwargs
# self.reset()
self.set_seed()
def _set_scip_separator_params(self, max_rounds_root=-1, max_rounds=-1, max_cuts_root=10000, max_cuts=10000,
frequency=10):
"""
Function for setting the separator params in SCIP. It goes through all separators, enables them at all points
in the solving process,
Args:
scip: The SCIP Model object
max_rounds_root: The max number of separation rounds that can be performed at the root node
max_rounds: The max number of separation rounds that can be performed at any non-root node
max_cuts_root: The max number of cuts that can be added per round in the root node
max_cuts: The max number of cuts that can be added per node at any non-root node
frequency: The separators will be called each time the tree hits a new multiple of this depth
Returns:
The SCIP Model with all the appropriate parameters now set
"""
assert type(max_cuts) == int and type(max_rounds) == int
assert type(max_cuts_root) == int and type(max_rounds_root) == int
model = self.m
# First for the aggregation heuristic separator
model.setParam('separating/aggregation/freq', frequency)
model.setParam('separating/aggregation/maxrounds', max_rounds)
model.setParam('separating/aggregation/maxroundsroot', max_rounds_root)
model.setParam('separating/aggregation/maxsepacuts', max_cuts)
model.setParam('separating/aggregation/maxsepacutsroot', max_cuts_root)
# Now the Chvatal-Gomory w/ MIP separator
# model.setParam('separating/cgmip/freq', frequency)
# model.setParam('separating/cgmip/maxrounds', max_rounds)
# model.setParam('separating/cgmip/maxroundsroot', max_rounds_root)
# The clique separator
model.setParam('separating/clique/freq', frequency)
model.setParam('separating/clique/maxsepacuts', max_cuts)
# The close-cuts separator
model.setParam('separating/closecuts/freq', frequency)
# The CMIR separator
model.setParam('separating/cmir/freq', frequency)
# The Convex Projection separator
model.setParam('separating/convexproj/freq', frequency)
model.setParam('separating/convexproj/maxdepth', -1)
# The disjunctive cut separator
model.setParam('separating/disjunctive/freq', frequency)
model.setParam('separating/disjunctive/maxrounds', max_rounds)
model.setParam('separating/disjunctive/maxroundsroot', max_rounds_root)
model.setParam('separating/disjunctive/maxinvcuts', max_cuts)
model.setParam('separating/disjunctive/maxinvcutsroot', max_cuts_root)
model.setParam('separating/disjunctive/maxdepth', -1)
# The separator for edge-concave function
model.setParam('separating/eccuts/freq', frequency)
model.setParam('separating/eccuts/maxrounds', max_rounds)
model.setParam('separating/eccuts/maxroundsroot', max_rounds_root)
model.setParam('separating/eccuts/maxsepacuts', max_cuts)
model.setParam('separating/eccuts/maxsepacutsroot', max_cuts_root)
model.setParam('separating/eccuts/maxdepth', -1)
# The flow cover cut separator
model.setParam('separating/flowcover/freq', frequency)
# The gauge separator
model.setParam('separating/gauge/freq', frequency)
# Gomory MIR cuts
model.setParam('separating/gomory/freq', frequency)
model.setParam('separating/gomory/maxrounds', max_rounds)
model.setParam('separating/gomory/maxroundsroot', max_rounds_root)
model.setParam('separating/gomory/maxsepacuts', max_cuts)
model.setParam('separating/gomory/maxsepacutsroot', max_cuts_root)
# The implied bounds separator
model.setParam('separating/impliedbounds/freq', frequency)
# The integer objective value separator
model.setParam('separating/intobj/freq', frequency)
# The knapsack cover separator
model.setParam('separating/knapsackcover/freq', frequency)
# The multi-commodity-flow network cut separator
model.setParam('separating/mcf/freq', frequency)
model.setParam('separating/mcf/maxsepacuts', max_cuts)
model.setParam('separating/mcf/maxsepacutsroot', max_cuts_root)
# The odd cycle separator
model.setParam('separating/oddcycle/freq', frequency)
model.setParam('separating/oddcycle/maxrounds', max_rounds)
model.setParam('separating/oddcycle/maxroundsroot', max_rounds_root)
model.setParam('separating/oddcycle/maxsepacuts', max_cuts)
model.setParam('separating/oddcycle/maxsepacutsroot', max_cuts_root)
# The rapid learning separator
model.setParam('separating/rapidlearning/freq', frequency)
# The strong CG separator
# model.setParam('separating/strongcg/freq', frequency)
# model.setParam('separating/strongcg/maxrounds', max_rounds)
# model.setParam('separating/strongcg/maxroundsroot', max_rounds_root)
# model.setParam('separating/strongcg/maxsepacuts', max_cuts)
# model.setParam('separating/strongcg/maxsepacutsroot', max_cuts_root)
# The zero-half separator
model.setParam('separating/zerohalf/freq', frequency)
model.setParam('separating/zerohalf/maxcutcands', max(max_cuts, max_cuts_root))
model.setParam('separating/zerohalf/maxrounds', max_rounds)
model.setParam('separating/zerohalf/maxroundsroot', max_rounds_root)
model.setParam('separating/zerohalf/maxsepacuts', max_cuts)
model.setParam('separating/zerohalf/maxsepacutsroot', max_cuts_root)
# Now the general cut and round parameters
model.setParam("separating/maxroundsroot", max_rounds_root)
model.setParam("separating/maxstallroundsroot", max_rounds_root)
model.setParam("separating/maxcutsroot", max_cuts_root)
model.setParam("separating/maxrounds", max_rounds)
model.setParam("separating/maxstallrounds", 1)
model.setParam("separating/maxcuts", max_cuts)
def _init_scip_params(self, **init_scip_kwargs):
seed = self.scip_seed % 2147483648 # SCIP seed range
# set up randomization
self.m.setBoolParam('randomization/permutevars', True)
self.m.setIntParam('randomization/permutationseed', seed)
self.m.setIntParam('randomization/randomseedshift', seed)
# separators
self._set_scip_separator_params(init_scip_kwargs['max_rounds_root'], 1, 10000, 1000, 10)
# separation only at root node
self.m.setIntParam('separating/maxrounds', 0)
# no restart
self.m.setIntParam('presolving/maxrestarts', 0)
# if asked, disable presolving
if not init_scip_kwargs['presolving']:
self.m.setIntParam('presolving/maxrounds', 0)
self.m.setIntParam('presolving/maxrestarts', 0)
# if asked, disable separating (cuts)
if not init_scip_kwargs['separating']:
self.m.setIntParam('separating/maxroundsroot', 0)
# if asked, disable conflict analysis (more cuts)
if not init_scip_kwargs['conflict']:
self.m.setBoolParam('conflict/enable', False)
# if asked, disable primal heuristics
if not init_scip_kwargs['heuristics']:
self.m.setHeuristics(scip.SCIP_PARAMSETTING.OFF)
def set_seed(self, seed=None):
if seed:
self.seed = seed
self.rng = np.random.RandomState(seed)
else:
self.rng = np.random.RandomState(self.seed)
def reset(self):
# create scip model
self.m = scip.Model()
if self.single_instance_file == 'all':
instance_file = self.rng.choice(self.instances)
else:
instance_file = self.single_instance_file
# instance_file = 'instance_9575.lp'
logger.log(f"instance_file: {instance_file}")
instance_file = os.path.join(self.instance_file_path, instance_file)
self.m.setIntParam('display/verblevel', 0)
self.m.readProblem(instance_file)
self.m.setRealParam('limits/time', self.scip_time_limit)
self.m.setIntParam('timing/clocktype', 2)
self._init_scip_params(**self.init_scip_kwargs)
return instance_file
def step(self, CutSel):
# include cutsel
self.m.includeCutsel(
cutsel=CutSel,
name="RL trained cutsel",
desc="",
priority=666666
)
# optimize the scip model
self.m.optimize()
# get statstics
stats={}
stats['solving_time'] = self.m.getSolvingTime()
stats['ntotal_nodes'] = self.m.getNTotalNodes()
stats['primal_dual_gap'] = self.m.getGap()
stats['primaldualintegral'] = self.m.getPrimalDualIntegral()
# stats['primal_dual_integral'] = self.m.getPrimalDualInte()
# free problem
self.m.freeProb()
return stats
def set_random_seed(self, seed):
self.rng = np.random.RandomState(seed)
# test
if __name__ == '__main__':
# test SCIPCutSelEnv
instance_file_path = "../dataset/data/instances/setcover/train_500r_1000c_0.05d"
seed = 0
init_scip_kwargs = {
'presolving': True,
'separating': True,
'conflict': True,
'heuristics': True
}
env = SCIPCutSelEnv(
instance_file_path,
seed,
scip_time_limit=3600,
**init_scip_kwargs
)
instance_file = env.reset()
print(f"instance_file: {instance_file}")