User-cut with CPLEX

Greetings.

This thread is related to this one. I am trying to use user-cut with JuMP 0.21.4, Julia 1.6.2, and CPLEX 12.10. Below follows the code I am running.

using JuMP, CPLEX

model = direct_model(CPLEX.Optimizer())
MOI.set(model, MOI.NumberOfThreads(), 1)

@variable(model, 0 <= x <= 2.5)
@variable(model, 0 <= y <= 2.5, Int)
@objective(model, Max, x + y)

function my_callback_function(cb_data::CPLEX.CallbackContext, context_id::Clong)
  # preliminary checkings
  context_id != CPX_CALLBACKCONTEXT_CANDIDATE && return
  ispoint_p = Ref{CPXINT}()
  (CPXcallbackcandidateispoint(cb_data, ispoint_p) != 0 || ispoint_p[] == 0) && return
  # get values
  CPLEX.load_callback_variable_primal(cb_data, context_id)
  x_val, y_val = callback_value(cb_data, x), callback_value(cb_data, y)
  # check if the values are integers
  (abs(x_val - floor(x_val + 0.5)) <= 1e-3 && abs(y_val - floor(y_val + 0.5)) <= 1e-3) && return
  # add user cut
  MOI.submit(model, MOI.UserCut(cb_data), @build_constraint(x + y <= 2))
end
MOI.set(model, CPLEX.CallbackFunction(), my_callback_function)
optimize!(model)

However, I am facing the following error:

CPLEX Error 1811: Attempt to invoke unsupported operation

Specifically, when x = 2.5 and y = 2, i.e. when the command MOI.submit(model, MOI.UserCut(cb_data), @build_constraint(x + y <= 2)) is executed. I also tried commenting the command context_id != CPX_CALLBACKCONTEXT_CANDIDATE && return, which led to the same result. I would like to know if anyone already used user-cuts with CPLEX.

Thank you for your time.

User cuts cannot be added at CPX_CALLBACKCONTEXT_CANDIDATE in CPLEX. In addition, they cannot exclude any feasible integer point. For example, your initial constraints have the feasible point (2, 2), which your cut removes.

Writing a solver specific callback is difficult, and requires detailed knowledge of how particular callbacks work in the solver.

I suggest you re-read the documentation: IBM Documentation

You might be better off writing a solver-independent callback: Solver-independent Callbacks · JuMP

1 Like

Thank you.

1 Like