Easier Solver Callback for JuMP.lj + Gurobi

I would like to write a callback function that does something similar to this:

but it seems much more complicated to do this in JuMP :frowning_face:.

and here
[ANN] Upcoming breaking changes to CPLEX.jl and Gurobi.jl

Any suggestions?

Ideally I would like something like:

  • If can solve to optimality within 10 seconds, do so
  • If not, settle for 10% optimality gap
  • (and maybe as more time goes on, settle for a larger optimality gap)

The literal translation of the example in the Gurobi documentation is:

softlimit = 5
hardlimit = 100
function my_callback_function(cb_data, cb_where::Cint)
    if cb_where == GRB_CB_MIP
        runtimeP = Ref{Cdouble}()
        objbstP = Ref{Cdouble}()
        objbndP = Ref{Cdouble}()
        GRBcbget(cb_data, cb_where, GRB_CB_RUNTIME, runtimeP)
        GRBcbget(cb_data, cb_where, GRB_CB_MIP_OBJBST, objbstP)
        GRBcbget(cb_data, cb_where, GRB_CB_MIP_OBJBND, objbndP)
        gap = abs((objbstP[] - objbndP[]) / objbstP[])
        if runtimeP[] > softlimit && gap < 0.5
MOI.set(model, Gurobi.CallbackFunction(), my_callback_function)

You should be able to modify it as needed.

Here’s the list of other things you can query: Callback Codes

1 Like