Skip to main content

How to use callback to save the objvalue output of each iteration of gurobi to an array or txt file

Answered

Comments

2 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    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.
  • Elisabeth Rodríguez Heck
    • Gurobi Staff

    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

Post is closed for comments.