How to use callback to save the objvalue output of each iteration of gurobi to an array or txt file
AnsweredHello sir, I am a scientific researcher. I have designed a warm_up algorithm for a certain type of problem. I want to get the value and time of each iteration of gurobi. I know that I can output them to the console or log file. But there is too much redundant information, can we just save the objective function value and iteration time of each iteration directly?
0
-
Hi Bin,
You can query the current best objective, current best bound, and runtime using the OBJBST, OBJBND and RUNTIME Callback Codes.
Here is an example Python code of how to query these attributes and save them into an array.
import gurobipy as gp
from gurobipy import GRB
def data_cb(model, where):
if where == gp.GRB.Callback.MIP:
cur_time = model.cbGet(gp.GRB.Callback.RUNTIME)
cur_obj = model.cbGet(gp.GRB.Callback.MIP_OBJBST)
cur_bd = model.cbGet(gp.GRB.Callback.MIP_OBJBND)
model._data.append([cur_time, cur_obj, cur_bd])
if where == gp.GRB.Callback.MIPSOL:
cur_time = model.cbGet(gp.GRB.Callback.RUNTIME)
cur_obj = model.cbGet(gp.GRB.Callback.MIPSOL_OBJBST)
cur_bd = model.cbGet(gp.GRB.Callback.MIPSOL_OBJBND)
model._data.append([cur_time, cur_obj, cur_bd])
if where == gp.GRB.Callback.MIPNODE:
cur_time = model.cbGet(gp.GRB.Callback.RUNTIME)
cur_obj = model.cbGet(gp.GRB.Callback.MIPNODE_OBJBST)
cur_bd = model.cbGet(gp.GRB.Callback.MIPNODE_OBJBND)
model._data.append([cur_time, cur_obj, cur_bd])
model = gp.read("/Library/gurobi1000/macos_universal2/examples/data/glass4.mps")
model._data = []
model.optimize(data_cb)Best regards,
Elisabeth
1
Please sign in to leave a comment.
Comments
1 comment