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

MIP best objective and bound output with runtime

回答済み

コメント

3件のコメント

  • 正式なコメント
    Simranjit Kaur
    • Gurobi Staff
    This post is more than three 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 why not try our AI Gurobot?.
  • Eli Towle
    • 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

投稿コメントは受け付けていません。