メインコンテンツへスキップ

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

回答済み

コメント

1件のコメント

  • 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

サインインしてコメントを残してください。