# Call back with matrix

**URL:** <https://discourse.julialang.org/t/call-back-with-matrix/87584>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [September 21, 2022, 4:12pm UTC](https://discourse.julialang.org/t/call-back-with-matrix/87584 "2022-09-21T16:12:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fabrizio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fabrizio/32/30587_2.png) [@Fabrizio](https://discourse.julialang.org/u/Fabrizio)\
**Post date:** [September 21, 2022, 4:12pm UTC](https://discourse.julialang.org/t/call-back-with-matrix/87584/1 "2022-09-21T16:12:08Z")

</div>

Hi, the following code fom Jump manual works a scalar value, x and x\_val

> ```
> x_val = callback_value(cb_data, x)
> status = MOI.submit(
> model, MOI.HeuristicSolution(cb_data), [x], [floor(Int, x_val)]
> )
> 
> ```

how about submit a intere matrix without having to make a for loop?

I tried with coomplhension like

[u[i,j] for i in 1:gmax, j in 1:hmax]… but does not work  
any hint or pointer to understand the behaviour?

Additionally how to get the current value of the OF? Before i was using MathProgBase.cbgetobj(cb)

Thank very much  
Fabrizio

---

<div class="post-metadata">

**Author:** ![mike\_k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike_k/32/211864_2.png) [@mike\_k](https://discourse.julialang.org/u/mike_k)\
**Post date:** [September 21, 2022, 5:05pm UTC](https://discourse.julialang.org/t/call-back-with-matrix/87584/2 "2022-09-21T17:05:33Z")

</div>

I think you should use `vcat()` for that. For instance, something like

```julia
vars = vcat( [model[:u][i,j] for i in 1:gmax for j in 1:hmax] )
vals = vcat( u_vals[i,j] for i in 1:gmax for j in 1:hmax] )
status = MOI.submit( model, MOI.HeuristicSolution(cb_data), vars, vals )
println("$status")

```

but I am not sure. Probably also reshaping helps (see [here](https://discourse.julialang.org/t/setting-heuristic-solution-involving-symmetric-matrix-in-jump-callback/69621/3) ). Regarding the objective function value: I always computed it manually with the current variable values.

---

<div class="post-metadata">

**Author:** ![Fabrizio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fabrizio/32/30587_2.png) [@Fabrizio](https://discourse.julialang.org/u/Fabrizio)\
**Post date:** [September 22, 2022, 10:49am UTC](https://discourse.julialang.org/t/call-back-with-matrix/87584/3 "2022-09-22T10:49:56Z")

</div>

Thanks @mike_k, indeed the only issue was the that u\_vars must be of type “sub::AbstractSubmittable”

so with the syntiax you suggested, and not the simple way i used before, the following code works fine:

> ```
> u_vars = [MyMod[:u][i,h] for i in 1:gmax for h in 1:hmax]
> u_vals = [u_vals[i,h] for i in 1:gmax for h in 1:hmax]
> status = MOI.submit(MyMod, MOI.HeuristicSolution(cb), u_vars, u_vals)
> 
> ```

moreover it must be a Vector{VariableRef} if you pass a Matrix{VariableRef} again you get an error.

Again i did not find an equivalent for getting the Obj value in the callback with MOI, i do need it since i want to run the heur callback only until the solver (gurobi) did non find a primal solution

---

<div class="post-metadata">

**Author:** ![mike\_k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike_k/32/211864_2.png) [@mike\_k](https://discourse.julialang.org/u/mike_k)\
**Post date:** [September 22, 2022, 12:50pm UTC](https://discourse.julialang.org/t/call-back-with-matrix/87584/4 "2022-09-22T12:50:01Z")

</div>

You could try to use a Gurobi-specific callback as given in the [example](https://github.com/jump-dev/Gurobi.jl) of Gurobi.jl, and use the [callback codes](https://www.gurobi.com/documentation/9.0/refman/cb_codes.html) you need.  
I neither found a MOI function for that.

---

<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:** [September 22, 2022, 4:28pm UTC](https://discourse.julialang.org/t/call-back-with-matrix/87584/5 "2022-09-22T16:28:05Z")

</div>

> [@Fabrizio](#):
>
> Again i did not find an equivalent for getting the Obj value in the callback with MOI,

The functionality in the generic callbacks is intentionally limited. Use a solver dependant callback instead
