# Easier Solver Callback for JuMP.lj + Gurobi

**URL:** <https://discourse.julialang.org/t/easier-solver-callback-for-jump-lj-gurobi/84889>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump, gurobi\
**Created:** [July 27, 2022, 7:39pm UTC](https://discourse.julialang.org/t/easier-solver-callback-for-jump-lj-gurobi/84889 "2022-07-27T19:39:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chelseas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chelseas/32/12148_2.png) [@chelseas](https://discourse.julialang.org/u/chelseas)\
**Post date:** [July 27, 2022, 7:39pm UTC](https://discourse.julialang.org/t/easier-solver-callback-for-jump-lj-gurobi/84889/1 "2022-07-27T19:39:04Z")

</div>

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

> **[How do I use callbacks to terminate the solver?](https://support.gurobi.com/hc/en-us/articles/360047717291)**
>
> Custom termination criteria can be set within callbacks in order to avoid calling optimize() several times. The following example uses the Python API to implement a "soft" time limit that is only v...

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

> **[GitHub - jump-dev/Gurobi.jl: Julia interface for Gurobi Optimizer](https://github.com/jump-dev/Gurobi.jl#callbacks)**
>
> Julia interface for Gurobi Optimizer. Contribute to jump-dev/Gurobi.jl development by creating an account on GitHub.

and here  
[[ANN] Upcoming breaking changes to CPLEX.jl and Gurobi.jl](https://discourse.julialang.org/t/ann-upcoming-breaking-changes-to-cplex-jl-and-gurobi-jl/47814)

Any suggestions?

---

<div class="post-metadata">

**Author:** ![chelseas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chelseas/32/12148_2.png) [@chelseas](https://discourse.julialang.org/u/chelseas)\
**Post date:** [July 27, 2022, 11:20pm UTC](https://discourse.julialang.org/t/easier-solver-callback-for-jump-lj-gurobi/84889/2 "2022-07-27T23:20:16Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [July 28, 2022, 5:45am UTC](https://discourse.julialang.org/t/easier-solver-callback-for-jump-lj-gurobi/84889/3 "2022-07-28T05:45:49Z")

</div>

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

```julia
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](https://www.gurobi.com/documentation/9.5/refman/cb_codes.html)
