# User-cut with CPLEX

**URL:** <https://discourse.julialang.org/t/user-cut-with-cplex/71490>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, cplex\
**Created:** [November 15, 2021, 12:06pm UTC](https://discourse.julialang.org/t/user-cut-with-cplex/71490 "2021-11-15T12:06:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matheus\_Diogenes\_And](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matheus_diogenes_and/32/27925_2.png) [@Matheus\_Diogenes\_And](https://discourse.julialang.org/u/Matheus_Diogenes_And)\
**Post date:** [November 15, 2021, 12:06pm UTC](https://discourse.julialang.org/t/user-cut-with-cplex/71490/1 "2021-11-15T12:06:33Z")

</div>

Greetings.

This thread is related to this [one](https://discourse.julialang.org/t/get-branch-and-bound-node-depth-with-jump/71128/11). 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.

```julia
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.

---

<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:** [November 15, 2021, 7:14pm UTC](https://discourse.julialang.org/t/user-cut-with-cplex/71490/2 "2021-11-15T19:14:40Z")

</div>

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](https://www.ibm.com/docs/en/cofz/12.10.0?topic=callbacks-what-is-generic-callback)

You might be better off writing a solver-independent callback: [Solver-independent Callbacks · JuMP](https://jump.dev/JuMP.jl/stable/manual/callbacks/)

---

<div class="post-metadata">

**Author:** ![Matheus\_Diogenes\_And](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matheus_diogenes_and/32/27925_2.png) [@Matheus\_Diogenes\_And](https://discourse.julialang.org/u/Matheus_Diogenes_And)\
**Post date:** [November 19, 2021, 4:23am UTC](https://discourse.julialang.org/t/user-cut-with-cplex/71490/3 "2021-11-19T04:23:28Z")

</div>

Thank you.
