-
Notifications
You must be signed in to change notification settings - Fork 6
/
cutsel_agent_parallel.py
executable file
·364 lines (331 loc) · 13.9 KB
/
cutsel_agent_parallel.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
import torch
import torch.nn as nn
import torch.autograd as autograd
from torch.autograd import Variable
import torch.nn.functional as F
import math
import numpy as np
import time
import pyscipopt as scip
from pyscipopt import SCIP_RESULT
# from beam_search import Beam
from utils import cut_feature_generator, advanced_cut_feature_generator
# from utils_fix_isp_bug import cut_feature_generator
from logger import logger
class CutSelectAgent(scip.Cutsel):
def __init__(
self,
scip_model,
pointer_net,
value_net,
sel_cuts_percent,
device,
decode_type,
mean_std,
policy_type
):
super().__init__()
self.scip_model = scip_model
self.policy = pointer_net
self.value = value_net
self.sel_cuts_percent = sel_cuts_percent
self.device = device
self.decode_type = decode_type
self.policy_type = policy_type
self.data = {}
# self.cuts_info ={}
self.lp_info = {
"lp_solution_value": [],
"lp_solution_integer_var_value": []
}
self.mean_std = mean_std
def _normalize(self, cuts_features):
# print(f"debug log mean: {self.mean_std.mean}, std: {self.mean_std.std}")
return (cuts_features-self.mean_std.mean) / (self.mean_std.std + self.mean_std.epsilon)
def cutselselect(self, cuts, forcedcuts, root, maxnselectedcuts):
if self.policy_type == 'with_token':
cuts_dict = self._cutselselect_with_token(cuts, forcedcuts, root, maxnselectedcuts)
else:
cuts_dict = self._cutselselect(cuts, forcedcuts, root, maxnselectedcuts)
return cuts_dict
def _cutselselect(self, cuts, forcedcuts, root, maxnselectedcuts):
'''first method called in each iteration in the main solving loop. '''
# this method needs to be implemented by the user
logger.log("cut selection policy without token!!!")
logger.log(f"forcedcuts length: {len(forcedcuts)}")
logger.log(f"len cuts: {len(cuts)}")
num_cuts = len(cuts)
# cur_lp_info = self._get_lp_info()
# for k in cur_lp_info.keys():
# self.lp_info[k].append(cur_lp_info[k])
if num_cuts <= 1:
return {
'cuts': cuts, # selected sorted cuts
'nselectedcuts': 1, # num of selected cuts
'result': SCIP_RESULT.SUCCESS
}
sel_cuts_num = min(int(num_cuts * self.sel_cuts_percent), int(maxnselectedcuts))
sel_cuts_num = max(sel_cuts_num, 2)
st_before_input = time.time()
cuts_features = advanced_cut_feature_generator(self.scip_model, cuts)
et_feature_extractor = time.time()
if self.mean_std is not None:
# normalize cut features
normalize_cut_features = self._normalize(cuts_features)
input_cuts = torch.from_numpy(normalize_cut_features).to(self.device)
else:
input_cuts = torch.from_numpy(cuts_features).to(self.device)
input_cuts = input_cuts.reshape(input_cuts.shape[0], 1, input_cuts.shape[1])
st_end_input = time.time()
# 只做选择动作的功能,不做计算梯度的功能
with torch.no_grad():
_, input_idxs = self.policy(input_cuts.float(), sel_cuts_num, self.decode_type) # (list of tensor, list of tensor)
st_end_inference = time.time()
print(f"process input time: {st_end_input-st_before_input} s")
print(f"input feature extractor time: {et_feature_extractor-st_before_input} s")
print(f"input cpu data to gpu time: {st_end_input-et_feature_extractor} s")
print(f"pointer net inference time: {st_end_inference - st_end_input} s")
idxes = [input.cpu().detach().item() for input in input_idxs]
assert len(set(idxes))==len(idxes) # 保证选择的idxes 没有重复的!
all_idxes = list(range(num_cuts))
not_sel_idxes = list(set(all_idxes).difference(idxes))
sorted_cuts = [cuts[idx] for idx in idxes]
not_sel_cuts = [cuts[n_idx] for n_idx in not_sel_idxes]
sorted_cuts.extend(not_sel_cuts)
# debug
# sorted_cuts = cuts
# 只log 第一次cut 处的state 和 action
if not self.data:
self.data = {
"state": cuts_features,
"action": idxes,
"sel_cuts_num": sel_cuts_num,
}
# self.cuts_info = {
# "length_cuts": num_cuts,
# "length_forced_cuts": len(forcedcuts),
# "cut_features": cuts_features
# }
return {
'cuts': sorted_cuts, # selected sorted cuts
'nselectedcuts': sel_cuts_num, # num of selected cuts
'result': SCIP_RESULT.SUCCESS
}
def _cutselselect_with_token(self, cuts, forcedcuts, root, maxnselectedcuts):
'''first method called in each iteration in the main solving loop. '''
# this method needs to be implemented by the user
logger.log("cut selection policy with token!!!")
logger.log(f"forcedcuts length: {len(forcedcuts)}")
logger.log(f"len cuts: {len(cuts)}")
num_cuts = len(cuts)
# cur_lp_info = self._get_lp_info()
# for k in cur_lp_info.keys():
# self.lp_info[k].append(cur_lp_info[k])
if num_cuts <= 1:
return {
'cuts': cuts, # selected sorted cuts
'nselectedcuts': 1, # num of selected cuts
'result': SCIP_RESULT.SUCCESS
}
max_sel_cuts_num = len(cuts) + 1
st_before_input = time.time()
cuts_features = advanced_cut_feature_generator(self.scip_model, cuts)
et_feature_extractor = time.time()
if self.mean_std is not None:
# normalize cut features
normalize_cut_features = self._normalize(cuts_features)
input_cuts = torch.from_numpy(normalize_cut_features).to(self.device)
else:
input_cuts = torch.from_numpy(cuts_features).to(self.device)
input_cuts = input_cuts.reshape(input_cuts.shape[0], 1, input_cuts.shape[1])
st_end_input = time.time()
# 只做选择动作的功能,不做计算梯度的功能
with torch.no_grad():
_, input_idxs = self.policy(input_cuts.float(), max_sel_cuts_num, self.decode_type) # (list of tensor, list of tensor)
st_end_inference = time.time()
print(f"process input time: {st_end_input-st_before_input} s")
print(f"input feature extractor time: {et_feature_extractor-st_before_input} s")
print(f"input cpu data to gpu time: {st_end_input-et_feature_extractor} s")
print(f"pointer net inference time: {st_end_inference - st_end_input} s")
idxes = [input.cpu().detach().item() for input in input_idxs]
sel_cuts_num = len(idxes)
if not self.data:
self.data = {
"state": cuts_features,
"action": idxes,
"sel_cuts_num": sel_cuts_num,
}
# select cuts
assert idxes[-1] == num_cuts
true_idxes = idxes[:-1] # remove the last index which is end token
assert len(set(true_idxes))==len(true_idxes) # 保证选择的idxes 没有重复的!
all_idxes = list(range(num_cuts))
not_sel_idxes = list(set(all_idxes).difference(true_idxes))
sorted_cuts = [cuts[idx] for idx in true_idxes]
not_sel_cuts = [cuts[n_idx] for n_idx in not_sel_idxes]
sorted_cuts.extend(not_sel_cuts)
return {
'cuts': sorted_cuts, # selected sorted cuts
'nselectedcuts': sel_cuts_num-1, # num of selected cuts
'result': SCIP_RESULT.SUCCESS
}
def _get_lp_info(self):
lp_info = {}
lp_info['lp_solution_value'] = self.scip_model.getLPObjVal()
cols = self.scip_model.getLPColsData()
col_solution_value = [col.getPrimsol() for col in cols if col.isIntegral()]
lp_info['lp_solution_integer_var_value'] = [val for val in col_solution_value if val != 0.]
return lp_info
def get_data(self):
return self.data
def get_lp_info(self):
return self.lp_info
# def get_cuts_info(self):
# return self.cuts_info
def free_problem(self):
self.scip_model.freeProb()
class HierarchyCutSelectAgent(CutSelectAgent):
def __init__(
self,
scip_model,
pointer_net,
cutsel_percent_policy,
value_net,
sel_cuts_percent,
device,
decode_type,
mean_std,
policy_type
):
CutSelectAgent.__init__(
self,
scip_model,
pointer_net,
value_net,
sel_cuts_percent,
device,
decode_type,
mean_std,
policy_type
)
self.cutsel_percent_policy = cutsel_percent_policy
self.high_level_data = {}
def cutselselect(self, cuts, forcedcuts, root, maxnselectedcuts):
'''first method called in each iteration in the main solving loop. '''
# this method needs to be implemented by the user
logger.log(f"forcedcuts length: {len(forcedcuts)}")
logger.log(f"len cuts: {len(cuts)}")
num_cuts = len(cuts)
# cur_lp_info = self._get_lp_info()
# for k in cur_lp_info.keys():
# self.lp_info[k].append(cur_lp_info[k])
if num_cuts <= 1:
return {
'cuts': cuts, # selected sorted cuts
'nselectedcuts': 1, # num of selected cuts
'result': SCIP_RESULT.SUCCESS
}
st_before_input = time.time()
# compute states
cuts_features = advanced_cut_feature_generator(self.scip_model, cuts)
et_feature_extractor = time.time()
# normalize states
if self.mean_std is not None:
# normalize cut features
normalize_cut_features = self._normalize(cuts_features)
input_cuts = torch.from_numpy(normalize_cut_features).to(self.device)
else:
input_cuts = torch.from_numpy(cuts_features).to(self.device)
input_cuts = input_cuts.reshape(input_cuts.shape[0], 1, input_cuts.shape[1])
st_end_input = time.time()
# compute sel cuts percent
with torch.no_grad():
if self.decode_type == 'greedy':
deterministic = True
else:
deterministic = False
raw_sel_cuts_percent = self.cutsel_percent_policy.action(input_cuts.float(), deterministic=deterministic)
st_end_highlevel_policy_inference = time.time()
sel_cuts_percent = raw_sel_cuts_percent.item() * 0.5 + 0.5
sel_cuts_num = min(int(num_cuts * sel_cuts_percent), int(maxnselectedcuts))
sel_cuts_num = max(sel_cuts_num, 2)
# 只做选择动作的功能,不做计算梯度的功能
with torch.no_grad():
_, input_idxs = self.policy(input_cuts.float(), sel_cuts_num, self.decode_type) # (list of tensor, list of tensor)
st_end_pointer_net_inference = time.time()
print(f"process input time: {st_end_input-st_before_input} s")
print(f"input feature extractor time: {et_feature_extractor-st_before_input} s")
print(f"input cpu data to gpu time: {st_end_input-et_feature_extractor} s")
print(f"high level policy time: {st_end_highlevel_policy_inference-st_end_input} s")
print(f"pointer net inference time: {st_end_pointer_net_inference - st_end_highlevel_policy_inference} s")
idxes = [input.cpu().detach().item() for input in input_idxs]
assert len(set(idxes))==len(idxes) # 保证选择的idxes 没有重复的!
all_idxes = list(range(num_cuts))
not_sel_idxes = list(set(all_idxes).difference(idxes))
sorted_cuts = [cuts[idx] for idx in idxes]
not_sel_cuts = [cuts[n_idx] for n_idx in not_sel_idxes]
sorted_cuts.extend(not_sel_cuts)
# debug
# sorted_cuts = cuts
# 只log 第一次cut 处的state 和 action
if not self.data:
self.data = {
"state": cuts_features,
"action": idxes,
"sel_cuts_num": sel_cuts_num,
}
if not self.high_level_data:
self.high_level_data = {
"state": cuts_features,
"action": raw_sel_cuts_percent.item()
}
return {
'cuts': sorted_cuts, # selected sorted cuts
'nselectedcuts': sel_cuts_num, # num of selected cuts
'result': SCIP_RESULT.SUCCESS
}
def get_high_level_data(self):
return self.high_level_data
## testing code
# if __name__ == '__main__':
# from environments import SCIPCutSelEnv
# from pointer_net import PointerNetwork
# instance_file_path = "/datasets/learning_to_cut/dataset/data_nips_competition/instances/2_load_balancing/train/train_mps"
# seed = 1
# env_kwargs = {
# "scip_time_limit": 30,
# "single_instance_file": "all",
# "presolving": True,
# "separating": True,
# "conflict": True,
# "heuristics": True,
# "max_rounds_root": 1
# }
# env = SCIPCutSelEnv(
# instance_file_path,
# seed,
# **env_kwargs
# )
# device = torch.device('cuda:1')
# pointer_net = PointerNetwork(
# embedding_dim=13,
# hidden_dim=128,
# n_glimpses=1,
# tanh_exploration=5,
# use_tanh=True,
# beam_size=1,
# use_cuda=torch.cuda.is_available()
# ).to(device)
# for _ in range(10):
# env.reset()
# cutsel_agent = CutSelectAgent(
# env.m,
# pointer_net,
# None,
# 0.5,
# device,
# 'stochastic',
# None,
# 'no_token'
# )
# _ = env.step(cutsel_agent)