# How to define a heuristic callback using JuMP?

**URL:** <https://discourse.julialang.org/t/how-to-define-a-heuristic-callback-using-jump/43322>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [July 19, 2020, 5:53am UTC](https://discourse.julialang.org/t/how-to-define-a-heuristic-callback-using-jump/43322 "2020-07-19T05:53:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gaiato](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gaiato/32/16510_2.png) [@Gaiato](https://discourse.julialang.org/u/Gaiato)\
**Post date:** [July 19, 2020, 5:53am UTC](https://discourse.julialang.org/t/how-to-define-a-heuristic-callback-using-jump/43322/1 "2020-07-19T05:53:36Z")

</div>

I’m trying to implement integer programming callbacks using JuMP. Basically, my heuristic callback is defined like this:

> function my\_heuristic\_callback(cb\_data)  
> x\_val = [callback\_value(cb\_data, x\_i) for x\_i in x]  
> x\_sol = [v \< 0.5 ? 0 : 1 for v in x\_val]  
> status = MOI.submit(  
> m, MOI.HeuristicSolution(cb\_data), x, x\_sol  
> )  
> println("I submitted a heuristic solution, and the status was: ", status)  
> end
> 
> m = Model(CPLEX.Optimizer)  
> @variable(m, x[1:n], Bin)  
> @objective(m, Max, sum(x))  
> MOI.set(m, MOI.LazyConstraintCallback(), my\_lazy\_callback)  
> MOI.set(m, MOI.UserCutCallback(), my\_user\_callback)  
> MOI.set(m, MOI.HeuristicCallback(), my\_heuristic\_callback)  
> optimize!(m)

The following error appeared:

```julia
ERROR: LoadError: MethodError: no method matching submit(::Model, ::MathOptInterface.HeuristicSolution{CPLEX.CallbackContext}, ::Array{VariableRef,1}, ::Array{Int64,1})
Closest candidates are:
  submit(::MathOptInterface.Utilities.MockOptimizer, ::MathOptInterface.AbstractSubmittable, ::Any...) at /Users/user/.julia/packages/MathOptInterface/bygN7/src/Utilities/mockoptimizer.jl:529
  submit(::MathOptInterface.Utilities.CachingOptimizer, ::MathOptInterface.AbstractSubmittable, ::Any...) at /Users/user/.julia/packages/MathOptInterface/bygN7/src/Utilities/cachingoptimizer.jl:727
  submit(::MathOptInterface.Bridges.AbstractBridgeOptimizer, ::MathOptInterface.AbstractSubmittable, ::Any...) at /Users/user/.julia/packages/MathOptInterface/bygN7/src/Bridges/bridge_optimizer.jl:1195

```

It seems that parameters type are wrong, but the code is a copy-paste from the documentation of JuMP v0.21.3. I don’t know what can possibly be wrong.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [July 19, 2020, 1:11pm UTC](https://discourse.julialang.org/t/how-to-define-a-heuristic-callback-using-jump/43322/2 "2020-07-19T13:11:21Z")

</div>

The first question is, the, are you using the the version 0.21.3?

If you are not using an environment/project (i.e, have `Project.toml` and `Manifest.toml` in your folder) you can just:

```julia
julia> import Pkg
julia> Pkg.status()

```

To check the installed version.

---

<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 19, 2020, 1:12pm UTC](https://discourse.julialang.org/t/how-to-define-a-heuristic-callback-using-jump/43322/3 "2020-07-19T13:12:49Z")

</div>

We probably need to relax the signature in CPLEX.jl to accept any real number. Currently, it requires `Float64`: [https://github.com/jump-dev/CPLEX.jl/blob/02d4ef57d143e49c158e59ea14095e84f7916dcd/src/MOI/MOI\_callbacks.jl#L207](https://github.com/jump-dev/CPLEX.jl/blob/02d4ef57d143e49c158e59ea14095e84f7916dcd/src/MOI/MOI_callbacks.jl#L207)

Just use

```julia
x_sol = Float64[v < 0.5 ? 0 : 1 for v in x_val]

```

---

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [July 19, 2020, 1:14pm UTC](https://discourse.julialang.org/t/how-to-define-a-heuristic-callback-using-jump/43322/4 "2020-07-19T13:14:42Z")

</div>

The vector of values should be `Vector{Float64}` but you provided `Vector{Int}`, do

```julia
x_sol = [v < 0.5 ? 0.0 : 1.0 for v in x_val]

```

---

<div class="post-metadata">

**Author:** ![Gaiato](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gaiato/32/16510_2.png) [@Gaiato](https://discourse.julialang.org/u/Gaiato)\
**Post date:** [July 19, 2020, 5:13pm UTC](https://discourse.julialang.org/t/how-to-define-a-heuristic-callback-using-jump/43322/5 "2020-07-19T17:13:06Z")

</div>

REPL output:

julia\> import Pkg  
julia\> Pkg.status(“JuMP”)  
Status `~/.julia/environments/v1.4/Project.toml`  
[4076af6c] JuMP v0.21.3

---

<div class="post-metadata">

**Author:** ![Gaiato](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gaiato/32/16510_2.png) [@Gaiato](https://discourse.julialang.org/u/Gaiato)\
**Post date:** [July 19, 2020, 5:13pm UTC](https://discourse.julialang.org/t/how-to-define-a-heuristic-callback-using-jump/43322/6 "2020-07-19T17:13:58Z")

</div>

Gurobi also gives the same error

---

<div class="post-metadata">

**Author:** ![Gaiato](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gaiato/32/16510_2.png) [@Gaiato](https://discourse.julialang.org/u/Gaiato)\
**Post date:** [July 19, 2020, 5:26pm UTC](https://discourse.julialang.org/t/how-to-define-a-heuristic-callback-using-jump/43322/7 "2020-07-19T17:26:45Z")

</div>

Indeed, with gurobi this solution worked. But, with CPLEX shows the following: ERROR: LoadError: CPLEX.CplexError(1003, “CPLEX Error 1003: Bad argument to Callable Library routine.\n”). But it may ocur because i’m sending infeasible integer points.

---

<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 19, 2020, 5:48pm UTC](https://discourse.julialang.org/t/how-to-define-a-heuristic-callback-using-jump/43322/9 "2020-07-19T17:48:03Z")

</div>

Upgrade CPLEX to 12.9 or 12.10. I think there was a bug in 12.8 with the callbacks.

---

<div class="post-metadata">

**Author:** ![Gaiato](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gaiato/32/16510_2.png) [@Gaiato](https://discourse.julialang.org/u/Gaiato)\
**Post date:** [July 23, 2020, 9:10pm UTC](https://discourse.julialang.org/t/how-to-define-a-heuristic-callback-using-jump/43322/11 "2020-07-23T21:10:01Z")

</div>

TY Sir 😃 cplex 12.10 works like a charm
