# How to output the first integer solution found in B&B process in Mosek

**URL:** https://discourse.julialang.org/t/how-to-output-the-first-integer-solution-found-in-b-b-process-in-mosek/116462
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [July 1, 2024, 8:32am UTC](https://discourse.julialang.org/t/how-to-output-the-first-integer-solution-found-in-b-b-process-in-mosek/116462 "2024-07-01T08:32:05Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![izng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/izng/32/210367_2.png) [@izng](https://discourse.julialang.org/u/izng)
#### Post date: [July 1, 2024, 8:32am UTC](https://discourse.julialang.org/t/how-to-output-the-first-integer-solution-found-in-b-b-process-in-mosek/116462/1 "2024-07-01T08:32:05Z")

</div>

Hi,

I want to know how to output the first integer solution and its objective value found within B&B process in Mosek.

---

<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: [July 1, 2024, 2:02pm UTC](https://discourse.julialang.org/t/how-to-output-the-first-integer-solution-found-in-b-b-process-in-mosek/116462/2 "2024-07-01T14:02:58Z")

</div>

Welcome!  
This should work with a solver-specific callback (which is called each time an integer solution is found). According to the readme in the [Mosek.jl](https://github.com/MOSEK/Mosek.jl/tree/master) this functionality is wrapped. Unfortunately, I did not find an example how to do that with JuMP.

---

<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 4, 2024, 7:34am UTC](https://discourse.julialang.org/t/how-to-output-the-first-integer-solution-found-in-b-b-process-in-mosek/116462/3 "2024-07-04T07:34:18Z")

</div>

Yes, apparently you can call `putcallbackfunc(t::MSKtask, f::Function)`

> <https://github.com/MOSEK/Mosek.jl/blob/91f62efd746fec35b391ae585bf1830aa169dcd5/src/msk_callback.jl#L92-L107>

The Mosek MOI wrapper in `MosekTools.jl` does not have any special support for it so you’ll have to use [`JuMP.unsafe_backend`](https://jump.dev/JuMP.jl/stable/manual/models/#Unsafe-backend):

```julia
optimizer = unsafe_backend(model) # Gives you a `MosekTools.Optimizer`
task = unsafe_backend(model).task # Gives you a `Mosek.MSKtask`
function my_callback(where::Callbackcode, dinf::Vector{Float64}, iinf::Vector{Int32}, liinf::Vector{Int64})
    # ...
end
putcallbackfunc(task, my_callback)

```

By careful that the mapping between the JuMP variables and the indices in the Mosek task may be a bit nontrivial. [`JuMP.optimizer_index`](https://jump.dev/JuMP.jl/stable/api/JuMP/#optimizer_index) gives you the `MOI.VariableIndex` associated to the `MosekTools.Optimizer`. So given a JuMP variable `x`, do

```julia
index = JuMP.optimizer_index(x) # Gives a `MOI.VariableIndex` corresponding to the index in `optimizer`
col = MosekTools.mosek_index(optimizer, index) # Gives a `MosekTools.ColumnIndex`
col.value # Gives a `Int32` corresponding to the column in `task`

```

Let us know how it goes, we could add MOI attributes in MosekTools to make this easier
