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