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

For example, at node A, x_1 = 0, x_2 = 1, x_3 = 1, which API should I use to query this information?

For an example of a cplex callback, see

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?

1 Like

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

Here you go:

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
1 Like

Thank you! It works perfectly for my situation.

1 Like

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?

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.

1 Like

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

1 Like

Nope :smile:

JuMP provides some solver-independent callbacks for common use cases, see Solver-independent Callbacks · JuMP, but your request is not common.

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. :thinking:

1 Like