Search code examples
optimizationplotjuliajulia-jump

Is it possible to create or access the gap history


In Julia JuMP, I am wondering whether it is possible to access an array, or similar, of the GAP history, the GAP improvements.

I know that at the end of the optimization, I can access the final gap with relative_gap(m) with m of type Model and I want the history of improvements over the solving process.

It could be for instance a vector of Float64 in percent: gaps=[20.2, 16.7, 13.8, 5.1, 0]. In such case my problem is solved to optimality as the final gap is 0.

At the end, I would like to plot the gap improvement in function of time of solving. So maybe all values of gaps could be a couple of two elements, the gap and the solving time until this new gap improvement?

Maybe this is not possible in JuMP so you could answer for Gurobi ?

Thanks!


Solution

  • The Gurobi callback solution has been answered by Oscar at another post - see Julia JUMP Gurobi MIP - query and store best objective and bound at runtime.

    However, since Gurobi is very serious about its TimeLimit and has low times to continue computations there is also a simpler approach (this code assumes mo is your JuMP Gurobi model):

    set_optimizer_attribute(mo, "TimeLimit", 2.0)
    gap_history = Float64[]
    max_steps = 1800
    for t in 1:max_steps
        optimize!(mo)
        gap = MOI.get(mo, MOI.RelativeGap())
        status=termination_status(mo)
        push!(gap_history, gap)
        gap > 0.01 || status == MOI.TIME_LIMIT || break  
    end