-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_solve2opt_rootnode_ecole.py
143 lines (120 loc) · 5.18 KB
/
test_solve2opt_rootnode_ecole.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
import pyscipopt
from pyscipopt import Model
import ecole
import numpy
import pathlib
import matplotlib.pyplot as plt
# from improvelp import improvelp
import gzip
import pickle
from utility import instancetypes, generator_switcher, instancesizes, binary_support
# instancetype = instancetypes[2]
instance_type = instancetypes[3]
instance_size = instancesizes[0]
dataset = instance_type + instance_size
directory_opt = './result/generated_instances/' + instance_type + '/' + instance_size + '/'
pathlib.Path(directory_opt).mkdir(parents=True, exist_ok=True)
generator = generator_switcher(dataset)
generator.seed(100)
opt_mode = 'rootsol-valid100-199'
objs = []
times = []
i = 0
while i > -1 and i < 200:
instance = next(generator)
MIP_model = instance.as_pyscipopt()
MIP_model.setProbName(instance_type + '-' + str(i))
instance_name = MIP_model.getProbName()
print(instance_name)
n_vars = MIP_model.getNVars()
n_binvars = MIP_model.getNBinVars()
print("N of variables: {}".format(n_vars))
print("N of binary vars: {}".format(n_binvars))
# MIP_model.setHeuristics(pyscipopt.SCIP_PARAMSETTING.OFF)
# MIP_model.setSeparating(pyscipopt.SCIP_PARAMSETTING.OFF)
print("Solving first solution ...")
MIP_model.setParam('presolving/maxrounds', 0)
MIP_model.setParam('presolving/maxrestarts', 0)
MIP_model.setParam("display/verblevel", 0)
MIP_model.setParam("limits/solutions", 1)
MIP_model.optimize()
status = MIP_model.getStatus()
stage = MIP_model.getStage()
print("* Solve status: %s" % status)
print("* Solve stage: %s" % stage)
n_sols = MIP_model.getNSols()
print('* number of solutions : ', n_sols)
obj = MIP_model.getObjVal()
print('* first sol obj : ', obj)
print("first solution solving time: ", MIP_model.getSolvingTime())
MIP_model_copy, MIP_copy_vars, success = MIP_model.createCopy(problemName='Copy',
origcopy=True)
print("Solving root node ...")
MIP_model_copy.resetParams()
MIP_model_copy.setParam('presolving/maxrounds', 0)
MIP_model_copy.setParam('presolving/maxrestarts', 0)
MIP_model_copy.setParam("display/verblevel", 0)
# MIP_model_copy.setSeparating(pyscipopt.SCIP_PARAMSETTING.FAST)
# MIP_model_copy.setPresolve(pyscipopt.SCIP_PARAMSETTING.FAST)
# MIP_model_copy.setHeuristics(pyscipopt.SCIP_PARAMSETTING.OFF)
# MIP_model_copy.setSeparating(pyscipopt.SCIP_PARAMSETTING.OFF)
MIP_model_copy.setParam("limits/nodes", 1)
MIP_model_copy.optimize()
status = MIP_model_copy.getStatus()
stage = MIP_model_copy.getStage()
print("* Solve status: %s" % status)
print("* Solve stage: %s" % stage)
n_sols = MIP_model_copy.getNSols()
print('* number of solutions : ', n_sols)
obj_root = MIP_model_copy.getObjVal()
print('* root node obj : ', obj_root)
print("root node solving time: ", MIP_model_copy.getSolvingTime())
sol_MIP_copy = MIP_model_copy.getBestSol()
n_supportbinvars = binary_support(MIP_model_copy, sol_MIP_copy)
print('Binary support: ', n_supportbinvars)
lp_status = MIP_model_copy.getLPSolstat()
print("* LP status: %s" % lp_status) # 1:optimal
if lp_status:
print('LP of root node is solved!')
lp_obj = MIP_model_copy.getLPObjVal()
print("LP objective: ", lp_obj)
incumbent_solution_first = MIP_model.getBestSol()
incumbent_solution_root = MIP_model_copy.getBestSol()
first_sol_check = MIP_model.checkSol(solution=incumbent_solution_first)
if first_sol_check:
print('first solution is valid')
else:
print('Warning: first solution is not valid!')
root_sol_check = MIP_model.checkSol(solution=incumbent_solution_root)
if root_sol_check:
print('root node solution is valid')
else:
print('Warning: root node solution is not valid!')
if (not status == 'optimal') and first_sol_check and root_sol_check:
if i > -1:
MIP_model_copy, MIP_copy_vars, success = MIP_model.createCopy(problemName='Copy2',
origcopy=True)
print("Solving to optimal ...")
MIP_model_copy.resetParams()
MIP_model_copy.setParam('presolving/maxrounds', 0)
MIP_model_copy.setParam('presolving/maxrestarts', 0)
MIP_model_copy.setParam("display/verblevel", 0)
MIP_model_copy.setParam('limits/time', 600)
MIP_model_copy.optimize()
status = MIP_model_copy.getStatus()
if status == 'optimal':
print('instance is solved to optimal!')
objs.append(MIP_model_copy.getObjVal())
times.append(MIP_model_copy.getSolvingTime())
print("instance:", MIP_model_copy.getProbName(),
"status:", MIP_model_copy.getStatus(),
"best obj: ", MIP_model_copy.getObjVal(),
"solving time: ", MIP_model_copy.getSolvingTime())
i += 1
else:
"no solution"
print("\n")
# data = [objs, times]
# filename = f'{directory_opt}root-obj-time-' + opt_mode + '.pkl'
# with gzip.open(filename, 'wb') as f:
# pickle.dump(data, f)