Easier Solver Callback for JuMP.lj + Gurobi

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

(gurobipy)
but it seems much more complicated to do this in JuMP :frowning_face:.
see

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
            GRBterminate(backend(model))
        end
    end
    return
end
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

2 Likes