Skip to main content

Gurobi Python API Callback

Answered

Comments

5 comments

  • Official comment
    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

    The objective function of model \( \texttt{bm23} \) is integral. The objective bound reported by Gurobi through \( \texttt{GRB.Callback.MIP_OBJBND} \) exploits the integrality of the objective function by rounding up to the nearest integer. This results in a tighter (and completely valid) bound. Is there a reason you want to query the weaker objective bound?

    0
  • Ambareesh Vaidya
    • Gurobi-versary
    • First Comment
    • First Question

    Hello,

    I am trying to query the objective function at the root node before cuts are applied. The value for instance "bm23" at the root node is 20.57092 but using the MIP callback function the value is rounded to 21. Is there a way to get the value without rounding?

    Thank you 

    0
  • Eli Towle
    • Gurobi Staff

    The Gurobi C API includes the constants necessary to query the "continuous" objective bound (ObjBoundC) in a callback. The continuous objective bound does not include the additional integrality strengthening. In \( \texttt{gurobi_c.h} \), we have:

    #define GRB_CB_MIP_OBJBNDC 3007
    #define GRB_CB_MIPSOL_OBJBNDC 4007
    #define GRB_CB_MIPNODE_OBJBNDC 5008

    So, to query the continuous objective bound in a \( \texttt{MIP} \) callback through the Python API, pass in the integer code associated with the C API's \( \texttt{GRB_CB_MIP_OBJBNDC} \) constant to Model.cbGet():

    MIP_OBJBNDC = 3007

    def cb(model, where):
        if where == GRB.Callback.MIP:
            objbndc = model.cbGet(MIP_OBJBNDC)
            print(objbndc)

    Note that these callback codes are intentionally undocumented, because (i) it's rare to want to query the continuous bound when a tighter bound is readily available, and (ii) the results may not be as expected on problems other than MILPs.

    0
  • Ambareesh Vaidya
    • Gurobi-versary
    • First Comment
    • First Question

    It worked! 

    Thank you!

    0

Post is closed for comments.