Skip to main content

MIP best objective and bound output with runtime

Answered

Comments

2 comments

  • Eli Towle
    Gurobi Staff Gurobi Staff

    Hi Wu,

    Yes, you can do this with a callback. A similar question was answered here.

    I'll re-post the code below. It generates a list of times, incumbent objective values, and best bounds using a callback. Then, these values are saved to a CSV file.

    import gurobipy as gp
    import csv
    import time


    def data_cb(model, where):
        if where == gp.GRB.Callback.MIP:
            cur_obj = model.cbGet(gp.GRB.Callback.MIP_OBJBST)
            cur_bd = model.cbGet(gp.GRB.Callback.MIP_OBJBND)

    # Did objective value or best bound change?
            if model._obj != cur_obj or model._bd != cur_bd:
                model._obj = cur_obj
                model._bd = cur_bd
                model._data.append([time.time() - m._start, cur_obj, cur_bd])

    # Build model m here

    m._obj = None
    m._bd = None
    m._data = []
    m._start = time.time()
    m.optimize(callback=data_cb)

    with open('data.csv', 'w') as f:
        writer = csv.writer(f)
        writer.writerows(m._data)

    Thanks,

    Eli

    0
  • Wu Hao
    Gurobi-versary
    First Comment
    First Question

    Thank you very much!

    0

Please sign in to leave a comment.