-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathenvironments.py
More file actions
349 lines (292 loc) · 13.3 KB
/
Copy pathenvironments.py
File metadata and controls
349 lines (292 loc) · 13.3 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
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
import os
import io
import sys
import ipdb
import numpy as np
import pandas as pd
import pyscipopt as scip
from logger import logger
from contextlib import redirect_stdout
SEPA_LIST = [ # 缺gomorymi, knapsackcover
'closecuts',
'disjunctive',
'minor',
'mixing',
'rlt',
'interminor',
# '#SM',
# '#CS',
'convexproj',
'gauge',
'impliedbounds',
'intobj',
'gomory',
'cgmip',
'strongcg',
'aggregation',
'clique',
'zerohalf',
'mcf',
'eccuts',
'oddcycle',
'flowcover',
'cmir',
'rapidlearning'
]
class SCIPSepaSelEnv():
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.sepa_list = SEPA_LIST
# 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', 10)
model.setParam('separating/clique/maxsepacuts', max_cuts)
# The close-cuts separator
model.setParam('separating/closecuts/freq', 10)
# 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', 10)
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', 10)
# 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', 10)
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', 10)
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)
# The mixing separator
model.setParam('separating/mixing/freq', frequency)
# The minor separator
model.setParam('separating/minor/freq', frequency)
# The rlt separator
model.setParam('separating/rlt/freq', frequency)
# The interminor separator
model.setParam('separating/interminor/freq', frequency)
# 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'], init_scip_kwargs['max_rounds'], 10000, 100, 10)
# separation only at root node
# self.m.setIntParam('separating/maxrounds', 0)
self.m.setIntParam('separating/maxrounds', init_scip_kwargs['max_rounds'])
# 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'
# instance_file = 'supportcase37__trans__seed__1.mps'
logger.log(f"instance_file: {instance_file}")
instance_file = os.path.join(self.instance_file_path, instance_file)
self.m.setIntParam('display/verblevel', 1) # disable statistic timing inside sub SCIP and output to console
# 0: only error and warning messages are displayed
# 1: only interactive dialogs, errors, and warnings are displayed
# 2: only important messages are displayed
# 3: standard messages are displayed
# 4: a lot of information is displayed
# 5: all messages are displayed
self.m.readProblem(instance_file)
self.m.setRealParam('limits/time', self.scip_time_limit) # self.init_scip_kwargs['scip_time_limit']
self.m.setIntParam('timing/clocktype', 2)
self._init_scip_params(**self.init_scip_kwargs)
return instance_file
def step(self, SepaSel=None, CutSel=None, Event=None):
# include cutsel
if SepaSel is not None:
self.m.includeSepa(
SepaSel,
SepaSel.name,
SepaSel.desc,
SepaSel.priority,
SepaSel.freq,
SepaSel.maxbounddist,
SepaSel.usessubscip,
SepaSel.delay)
if CutSel is not None:
self.m.includeCutsel(
cutsel=CutSel,
name=CutSel.name,
desc=CutSel.desc,
priority=CutSel.priority
)
if Event is not None:
for event in Event:
self.m.includeEventhdlr(
eventhdlr=event,
name=event.name,
desc=event.desc
)
# optimize the scip model
self.m.optimize()
# self.m.printStatistics()
# get statstics which can be chosen as reward signal
stats={}
stats['solving_time'] = self.m.getSolvingTime() # not include reading time
stats['ntotal_nodes'] = self.m.getNTotalNodes()
stats['primal_dual_gap'] = self.m.getGap()
stats['primaldualintegral'] = self.m.getPrimalDualIntegral()
stats['last_neg_reward'] = SepaSel._get_reward_info()
stats['config_time'] = self.m.getSepaFeatures(SepaSel.name)['time']
# stats['sepa_neg_reward'] = SepaSel._getSepaNegReward()
# stats['lp_solution'] = self.m.getLPObjVal()
# stats['milp_solution'] = self.m.getObjVal()
# stats['dual_bound'] = self.m.getDualbound()
# stats['separator_statistics'] = sepa_stats
stats['separator_priority'] = self.m.sepaGetPriority()
stats['separator_freq'] = self.m.sepaGetFreq()
stats['separator_cutoffs'] = self.m.sepaGetNCutoffs()
stats['separator_domreds'] = self.m.sepaGetNDomredsFound()
stats['separator_appliedcuts'] = self.m.sepaGetNCutsApplied()
stats['separator_foundcuts'] = self.m.sepaGetNCutsFound()
stats['separator_num'] = self.m.sepaGetNExecute()
# 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 SCIPSepaSelEnv
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 = SCIPSepaSelEnv(
instance_file_path,
seed,
scip_time_limit=3600,
**init_scip_kwargs
)
instance_file = env.reset()
print(f"instance_file: {instance_file}")