# How to determine which variables are fixed to 0 or 1 in a node of a branch and bound tree using CPLEX.jl?

**URL:** <https://discourse.julialang.org/t/how-to-determine-which-variables-are-fixed-to-0-or-1-in-a-node-of-a-branch-and-bound-tree-using-cplex-jl/115028>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [May 31, 2024, 7:00pm UTC](https://discourse.julialang.org/t/how-to-determine-which-variables-are-fixed-to-0-or-1-in-a-node-of-a-branch-and-bound-tree-using-cplex-jl/115028 "2024-05-31T19:00:52Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ASUKaiwenFang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asukaiwenfang/32/209648_2.png) [@ASUKaiwenFang](https://discourse.julialang.org/u/ASUKaiwenFang)\
**Post date:** [May 31, 2024, 7:00pm UTC](https://discourse.julialang.org/t/how-to-determine-which-variables-are-fixed-to-0-or-1-in-a-node-of-a-branch-and-bound-tree-using-cplex-jl/115028/1 "2024-05-31T19:00:52Z")

</div>

For example, at node A, x\_1 = 0, x\_2 = 1, x\_3 = 1, which API should I use to query this information?

---

<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:** [May 31, 2024, 7:34pm UTC](https://discourse.julialang.org/t/how-to-determine-which-variables-are-fixed-to-0-or-1-in-a-node-of-a-branch-and-bound-tree-using-cplex-jl/115028/2 "2024-05-31T19:34:20Z")

</div>

For an example of a cplex callback, see

> **[jump-dev/CPLEX.jl · JuMP](https://jump.dev/JuMP.jl/stable/packages/CPLEX/#Callbacks)**
>
> Documentation for JuMP.

---

<div class="post-metadata">

**Author:** ![ASUKaiwenFang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asukaiwenfang/32/209648_2.png) [@ASUKaiwenFang](https://discourse.julialang.org/u/ASUKaiwenFang)\
**Post date:** [May 31, 2024, 11:22pm UTC](https://discourse.julialang.org/t/how-to-determine-which-variables-are-fixed-to-0-or-1-in-a-node-of-a-branch-and-bound-tree-using-cplex-jl/115028/3 "2024-05-31T23:22:59Z")

</div>

Thank you! I checked the C API library of CPLEX but couldn’t find a function to query which variables are fixed at the current node. Do they provide such an interface?

---

<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:** [June 1, 2024, 12:42am UTC](https://discourse.julialang.org/t/how-to-determine-which-variables-are-fixed-to-0-or-1-in-a-node-of-a-branch-and-bound-tree-using-cplex-jl/115028/4 "2024-06-01T00:42:20Z")

</div>

Im away from my computer so i cant test, but i assume

> **[CPXXcallbackgetlocallb and CPXcallbackgetlocallb](https://www.ibm.com/docs/en/cofz/12.10.0?topic=c-cpxxcallbackgetlocallb-cpxcallbackgetlocallb)**
>
> The routine CPXXcallbackgetlocallb/CPXcallbackgetlocallb retrieves the lower bound values for the subproblem at the current node during MIP optimization from within a user-written callback.

> **[CPXXcallbackgetlocalub and CPXcallbackgetlocalub](https://www.ibm.com/docs/en/cofz/12.10.0?topic=c-cpxxcallbackgetlocalub-cpxcallbackgetlocalub)**
>
> The routine CPXXcallbackgetlocalub/CPXcallbackgetlocalub retrieves the upper bound values for the subproblem at the current node during MIP optimization from within a user-written callback.

---

<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:** [June 4, 2024, 1:49am UTC](https://discourse.julialang.org/t/how-to-determine-which-variables-are-fixed-to-0-or-1-in-a-node-of-a-branch-and-bound-tree-using-cplex-jl/115028/5 "2024-06-04T01:49:12Z")

</div>

Here you go:

```julia
julia> using JuMP, CPLEX, Random

julia> function main(seed)
           Random.seed!(seed)
           N = 30
           model = Model(CPLEX.Optimizer)
           set_silent(model)
           set_attribute(model, MOI.NumberOfThreads(), 1)
           @variable(model, x[1:N], Bin, start = 0)
           @constraint(model, rand(N)' * x <= 10)
           @objective(model, Max, rand(N)' * x)
           function my_callback_function(cb_data::CPLEX.CallbackContext, context_id::Clong)
               if context_id == CPX_CALLBACKCONTEXT_RELAXATION
                   lb, ub = fill(NaN, N), fill(NaN, N)
                   @assert CPXcallbackgetlocallb(cb_data, lb, 0, length(lb) - 1) == 0
                   @assert CPXcallbackgetlocalub(cb_data, ub, 0, length(ub) - 1) == 0
                   println("There are $(count(lb .≈ ub)) fixed variables")
               end
               return
           end
           MOI.set(model, CPLEX.CallbackFunction(), my_callback_function)
           optimize!(model)
           return
       end
main (generic function with 1 method)

julia> main(1)
There are 21 fixed variables

julia> main(2)
There are 24 fixed variables
There are 24 fixed variables
There are 25 fixed variables

julia> main(3)

julia> main(4)

julia> main(5)

julia> main(6)
There are 20 fixed variables
There are 22 fixed variables

```

---

<div class="post-metadata">

**Author:** ![ASUKaiwenFang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asukaiwenfang/32/209648_2.png) [@ASUKaiwenFang](https://discourse.julialang.org/u/ASUKaiwenFang)\
**Post date:** [June 4, 2024, 11:56pm UTC](https://discourse.julialang.org/t/how-to-determine-which-variables-are-fixed-to-0-or-1-in-a-node-of-a-branch-and-bound-tree-using-cplex-jl/115028/6 "2024-06-04T23:56:27Z")

</div>

Thank you! It works perfectly for my situation.

---

<div class="post-metadata">

**Author:** ![ASUKaiwenFang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asukaiwenfang/32/209648_2.png) [@ASUKaiwenFang](https://discourse.julialang.org/u/ASUKaiwenFang)\
**Post date:** [June 6, 2024, 11:57pm UTC](https://discourse.julialang.org/t/how-to-determine-which-variables-are-fixed-to-0-or-1-in-a-node-of-a-branch-and-bound-tree-using-cplex-jl/115028/7 "2024-06-06T23:57:24Z")

</div>

I’m currently testing my model using Gurobi. However, I couldn’t find the corresponding function `CPXcallbackgetlocallb` and `CPXcallbackgetlocalub` in Gurobi. Do you know which function I should use instead? Alternatively, does MOI provide a similar API?

---

<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:** [June 7, 2024, 12:09am UTC](https://discourse.julialang.org/t/how-to-determine-which-variables-are-fixed-to-0-or-1-in-a-node-of-a-branch-and-bound-tree-using-cplex-jl/115028/8 "2024-06-07T00:09:49Z")

</div>

The functions `CPXcallbackgetlocallb` are specific to CPLEX.

Gurobi does not provide this functionality, and there is nothing in MOI.

In general, callbacks are solver-specific; you cannot easily take a callback written for one solver and apply it to a different one.

---

<div class="post-metadata">

**Author:** ![ASUKaiwenFang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asukaiwenfang/32/209648_2.png) [@ASUKaiwenFang](https://discourse.julialang.org/u/ASUKaiwenFang)\
**Post date:** [June 7, 2024, 12:31am UTC](https://discourse.julialang.org/t/how-to-determine-which-variables-are-fixed-to-0-or-1-in-a-node-of-a-branch-and-bound-tree-using-cplex-jl/115028/9 "2024-06-07T00:31:09Z")

</div>

Got it! It’s really not easy to employ the callback method.

---

<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:** [June 7, 2024, 12:36am UTC](https://discourse.julialang.org/t/how-to-determine-which-variables-are-fixed-to-0-or-1-in-a-node-of-a-branch-and-bound-tree-using-cplex-jl/115028/10 "2024-06-07T00:36:52Z")

</div>

> [@ASUKaiwenFang](#):
>
> It’s really not easy to employ the callback method.

Nope 😄

JuMP provides some solver-independent callbacks for common use cases, see [Solver-independent Callbacks · JuMP](https://jump.dev/JuMP.jl/stable/manual/callbacks/), but your request is not common.

---

<div class="post-metadata">

**Author:** ![ASUKaiwenFang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asukaiwenfang/32/209648_2.png) [@ASUKaiwenFang](https://discourse.julialang.org/u/ASUKaiwenFang)\
**Post date:** [June 7, 2024, 12:56am UTC](https://discourse.julialang.org/t/how-to-determine-which-variables-are-fixed-to-0-or-1-in-a-node-of-a-branch-and-bound-tree-using-cplex-jl/115028/11 "2024-06-07T00:56:04Z")

</div>

Indeed, these solver-independent callbacks are quite helpful. I’ve utilized them previously when incorporating Benders cuts. However, at present, I require access to information about fixed variables, as I intend to use it for generating user-cuts. Then the issue is forthcoming. 🤔
