Print intermediate solutions (Objective value and variables) while running MIP using gurobi in python.
回答済みWhen running my gurobi optimization for 60 seconds. I see my objective function value below, is there a way for me to see what variables were used (I have 8) during each of these intermdediate steps aswell.
Nodes | Current Node | Objective Bounds | Work
Expl Unexpl | Obj Depth IntInf | Incumbent BestBd Gap | It/Node Time
0 0 1.1957e+09 0 477 3.5443e+09 1.1957e+09 66.3% - 0s
H 0 0 2.477577e+09 1.1957e+09 51.7% - 0s
H 0 0 1.989941e+09 1.1957e+09 39.9% - 0s
H 0 0 1.805903e+09 1.1957e+09 33.8% - 0s
H 0 0 1.780393e+09 1.1957e+09 32.8% - 0s
0 0 1.2321e+09 0 529 1.7804e+09 1.2321e+09 30.8% - 0s
H 0 0 1.701134e+09 1.2321e+09 27.6% - 0s
0 0 1.2321e+09 0 523 1.7011e+09 1.2321e+09 27.6% - 0s
0 0 1.2321e+09 0 453 1.7011e+09 1.2321e+09 27.6% - 1s
0 0 1.2321e+09 0 453 1.7011e+09 1.2321e+09 27.6% - 1s
0 2 1.2321e+09 0 453 1.7011e+09 1.2321e+09 27.6% - 1s
H 31 36 1.699992e+09 1.3234e+09 22.2% 416 1s
H 34 36 1.699210e+09 1.3234e+09 22.1% 395 1s
H 35 36 1.697900e+09 1.3234e+09 22.1% 384 1s
H 62 76 1.697222e+09 1.3234e+09 22.0% 232 1s
H 66 76 1.697109e+09 1.3234e+09 22.0% 220 1s
H 70 76 1.696988e+09 1.3234e+09 22.0% 208 1s
H 77 84 1.695236e+09 1.3234e+09 21.9% 190 2s
H 83 84 1.693595e+09 1.3234e+09 21.9% 177 2s
H 109 132 1.693386e+09 1.3234e+09 21.9% 138 2s
H 112 132 1.693081e+09 1.3234e+09 21.8% 135 2s
H 130 132 1.692910e+09 1.3234e+09 21.8% 121 2s
H 165 186 1.692692e+09 1.3234e+09 21.8% 99.4 2s
H 169 186 1.692642e+09 1.3234e+09 21.8% 97.1 2s
H 172 186 1.692554e+09 1.3234e+09 21.8% 95.6 2s
H 438 439 1.692534e+09 1.3234e+09 21.8% 83.8 2s
H 439 439 1.692480e+09 1.3234e+09 21.8% 85.0 2s
H 472 482 1.686467e+09 1.3234e+09 21.5% 82.0 2s
H 497 482 1.686150e+09 1.3234e+09 21.5% 80.3 2s
H 1508 1457 1.685521e+09 1.3234e+09 21.5% 34.4 3s
H 1801 1709 1.684923e+09 1.3234e+09 21.5% 30.8 3s
H 2367 2131 1.682392e+09 1.3234e+09 21.3% 26.4 3s
H 2372 2112 1.680475e+09 1.3234e+09 21.3% 26.4 3s
H 2377 2000 1.678343e+09 1.3234e+09 21.2% 26.4 4s
2379 2001 1.6106e+09 169 491 1.6783e+09 1.3234e+09 21.2% 26.3 5s
H 2556 2036 1.669077e+09 1.3234e+09 20.7% 28.2 7s
H 2561 1939 1.664964e+09 1.3234e+09 20.5% 28.5 7s
3617 2530 1.3234e+09 49 542 1.6650e+09 1.3234e+09 20.5% 24.2 10s
H 3624 2437 1.663481e+09 1.3234e+09 20.4% 24.2 10s
7307 4984 1.5220e+09 138 604 1.6635e+09 1.3234e+09 20.4% 20.5 15s
19236 15554 1.4508e+09 81 684 1.6635e+09 1.3234e+09 20.4% 19.3 20s
35012 29523 1.5993e+09 405 450 1.6635e+09 1.3234e+09 20.4% 20.2 25s
43252 35214 1.6122e+09 469 1784 1.6635e+09 1.3234e+09 20.4% 19.6 34s
43255 35216 1.3698e+09 26 479 1.6635e+09 1.3234e+09 20.4% 19.6 35s
43347 35289 1.6222e+09 30 671 1.6635e+09 1.6057e+09 3.47% 20.0 40s
H43359 33523 1.663148e+09 1.6057e+09 3.46% 20.0 40s
H43465 31924 1.662385e+09 1.6057e+09 3.41% 20.0 42s
44481 32303 1.6400e+09 58 670 1.6624e+09 1.6057e+09 3.41% 20.2 45s
49153 33472 1.6460e+09 66 893 1.6624e+09 1.6057e+09 3.41% 20.4 50s
53305 35088 1.6057e+09 37 713 1.6624e+09 1.6057e+09 3.41% 21.1 55s
57290 36547 1.6168e+09 83 679 1.6624e+09 1.6057e+09 3.41% 22.0 60s
-
正式なコメント
This post is more than two years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Bhargav Gorantla Sridhar,
Thank you for reaching us.
I would like to introduce two approaches that handle intermediate solutions.
The simple one is to write .sol file by SolFiles parameter and I found a post asking the similar question to yours.
Another approach is to use a callback function(by requesting theMIPSOL_SOLin aMIPSOLcallback).
Please check the linked Python example if you want to know how to implement a callback function.Here is an example python code dealing with a callback function and SOLFiles parameter.
#Print intermediate solutions - two ways 1) callback 2) parameter SolFiles
import gurobipy as gp
from gurobipy import GRB
def mycallback(model, where):
if where == GRB.Callback.MIPSOL:
# Access solution values using the custom attribute model._vars
sol = model.cbGetSolution(model._vars)
obj = model.cbGet(GRB.Callback.MIPSOL_OBJ)
# Print the solution with variable names
print('**** New solution of obj %g '%obj)
for i in range(len(model._vars)):
print(f"{model._vars[i].VarName}: {sol[i]}")
model = gp.Model()
'''
Build your own model
'''
# Create custom attribute model._vars
model._vars = model.getVars()
#Alternative - you can also use the SolFiles parameter to write new solutions to disk.
model.params.SolFiles = "solution"
model.optimize(mycallback)If you have further questions, feel free to contact us again.
Best regards,
Chung-Kyun Han0
投稿コメントは受け付けていません。
コメント
2件のコメント