Search code examples
callbackjuliagurobimixed-integer-programmingjulia-jump

Julia JUMP Gurobi MIP - query and store best objective and bound at runtime


I am using Gurobi through the JuMP package in Julia to solve a mixed-integer program.

I would like to obtain a graph like this one, where also a solution based on Python is provided (which was also addressed on Gurobi community form).

However, I have not found working solutions for Julia calling Gurobi through JuMP. I understand that callback functions have to be used (such as this suggestion or even the main documentation here), but I do not fully understand how they work and what is essential to achieve my goal.

Any help is much appreciated, as well as a possible description of what the callback function is doing at each step.

If it helps, I am using Gurobi (v.9.0.0), JuMP (v0.20.1), MathOptInterface (v0.9.22) and Julia (v.1.3.0).


Solution

  • You need to use the C API. Here is a translation of Eli's answer on the Gurobi forum:

    using JuMP, Gurobi
    model = direct_model(Gurobi.Optimizer())
    N = 30
    @variable(model, x[1:N], Bin)
    @constraint(model, rand(N)' * x  <= 10)
    @objective(model, Max, rand(N)' * x)
    data = Any[]
    start_time = 0.0
    function my_callback_function(cb_data, cb_where::Cint)
        @show cb_where
        if cb_where == GRB_CB_MIP
            objbst = Ref{Cdouble}()
            GRBcbget(cb_data, cb_where, GRB_CB_MIP_OBJBST, objbst)
            objbnd = Ref{Cdouble}()
            GRBcbget(cb_data, cb_where, GRB_CB_MIP_OBJBND, objbnd)    
            push!(data, (time() - start_time, objbst[], objbnd[]))
        end
        return
    end
    MOI.set(model, Gurobi.CallbackFunction(), my_callback_function)
    start_time = time()
    optimize!(model)
    open("data.csv", "w") do io
        for x in data
            println(io, join(x, ", "))
        end
    end
    

    p.s. please update to Julia 1.6 and JuMP 0.22. I have not tested whether this works on the older version.