# Submitted arrays of different types in JuMP Heuristic Callbacks

**URL:** <https://discourse.julialang.org/t/submitted-arrays-of-different-types-in-jump-heuristic-callbacks/83950>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, gurobi\
**Created:** [July 8, 2022, 2:15pm UTC](https://discourse.julialang.org/t/submitted-arrays-of-different-types-in-jump-heuristic-callbacks/83950 "2022-07-08T14:15:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![K5Julien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/k5julien/32/210626_2.png) [@K5Julien](https://discourse.julialang.org/u/K5Julien)\
**Post date:** [July 8, 2022, 2:15pm UTC](https://discourse.julialang.org/t/submitted-arrays-of-different-types-in-jump-heuristic-callbacks/83950/1 "2022-07-08T14:15:51Z")

</div>

Hi!  
I am sorry, I can not produce a MWE but I think you can still answer my question.

I need to use [JuMP heuristic callbacks](https://jump.dev/JuMP.jl/stable/manual/callbacks/#Heuristic-solutions). However, the documentation only gives an exemple for a single variable. I need to submit a heuristic solution for different matrices, vectors and `Float64` values.

```julia
function call_back_benders_heuristic(cb_data)
    vars_submit = JuMP.VariableRef[]
    vars_improve_two_opt_submit = Bool[]
    for i in V
        for j in i+1:n
            push!(vars_submit, x[i,j])
            push!(vars_improve_two_opt_submit, x_improve_two_opt[i,j])
        end
    end
    status = MOI.submit(
    m, MOI.HeuristicSolution(cb_data), 
    [vars_submit; y; JuMP.VariableRef[offset, λ]] , 
    [vars_improve_two_opt_submit; y_improve_two_opt; Float64[bar_offset_improve_two_opt, λ_improve_two_opt]])
    println("Submitted a heuristic solution with status $status")
end
MOI.set(m, MOI.HeuristicCallback(), call_back_benders_heuristic)

```

```julia
# Earlier in my JuMP model
    @variable(m, x[i=V, j=i+1:n], Bin)
    @variable(m, y[i=V], Bin)
    @variable(m, offset >= 0)
    @variable(m, λ >= 0)

```

As you can see, I have, a matrix of `Bool`, `x`, a vector of `Bool` `x` and two `Float64` variables, `offset` and `λ`. My problem is that I get  
`Submitted a heuristic solution with status HEURISTIC_SOLUTION_REJECTED`

My guess is that `[vars_improve_two_opt_submit; y_improve_two_opt; Float64[bar_offset_improve_two_opt, λ_improve_two_opt]]` becomes an array of `{Any}` because there are both arrays of `Bool` and `Float64`

Please note that `MOI.submit`:

```julia
status = MOI.submit(
    m, MOI.HeuristicSolution(cb_data), 
    [vars_submit; y], 
    [vars_improve_two_opt_submit; y_improve_two_opt])

```

Works and give `HEURISTIC_SOLUTION_ACCEPTED` with the major flaw that my two `Float64` variables `offset` and `λ` do not get heuristically submitted!

Thanks a lot!

---

<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 9, 2022, 8:01pm UTC](https://discourse.julialang.org/t/submitted-arrays-of-different-types-in-jump-heuristic-callbacks/83950/2 "2022-07-09T20:01:44Z")

</div>

> [@K5Julien](#):
>
> a matrix of `Bool`, `x`, a vector of `Bool` `x`

Even though the variable are `Bin`, you need to pass the `Float64` values `0.0` or `1.0`. The solver doesn’t use `true` and `false` internally.

I don’t have your full code, so I couldn’t test to check this works, but you probably want something like:

```julia
function call_back_benders_heuristic(cb_data)
    vars_x = JuMP.VariableRef[]
    solution_x = Bool[]
    for i in V
        for j in i+1:n
            push!(vars_x, x[i,j])
            push!(solution_x, x_improve_two_opt[i,j])
        end
    end
    status = MOI.submit(
        m, 
        MOI.HeuristicSolution(cb_data), 
        vcat(vars_x, y, offset, λ),
        vcat(
            convert(Vector{Float64}, solution_x),
            convert(Vector{Float64}, y_improve_two_opt),
            bar_offset_improve_two_opt,
            λ_improve_two_opt,
        )
    println("Submitted a heuristic solution with status $status")
end

```

---

<div class="post-metadata">

**Author:** ![K5Julien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/k5julien/32/210626_2.png) [@K5Julien](https://discourse.julialang.org/u/K5Julien)\
**Post date:** [July 19, 2022, 1:31pm UTC](https://discourse.julialang.org/t/submitted-arrays-of-different-types-in-jump-heuristic-callbacks/83950/3 "2022-07-19T13:31:33Z")

</div>

That worked! Thanks 🙂
