How to use callback to save the objvalue output of each iteration of gurobi to an array or txt file
回答済みHello 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
-
正式なコメント
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 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
投稿コメントは受け付けていません。
コメント
2件のコメント