# IloIfThen constraints for CPLEX in Julia

**URL:** https://discourse.julialang.org/t/iloifthen-constraints-for-cplex-in-julia/85619
**Category:** Optimization (Mathematical)
**Created:** [August 11, 2022, 1:44pm UTC](https://discourse.julialang.org/t/iloifthen-constraints-for-cplex-in-julia/85619 "2022-08-11T13:44:54Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Deoch](https://avatars.discourse-cdn.com/v4/letter/d/e274bd/32.png) [@Deoch](https://discourse.julialang.org/u/Deoch)
#### Post date: [August 11, 2022, 1:44pm UTC](https://discourse.julialang.org/t/iloifthen-constraints-for-cplex-in-julia/85619/1 "2022-08-11T13:44:55Z")

</div>

Hello,

I need to use the conditional constraints for a JuMP model with CPLEX, that would be IloIfThen in C++. Like if I have three variables x, y and z, a constraint like:

If (x \>= 1) && (y \<= 2) then z == 1

I am using JuMP, and have tried to use the indicator constraints, with something like this:  
@constraint(m, name, ((x \>= 1) && (y \<= 2)) =\> { z == 1})

However it does not work, it seems that I can’t use a JuMP decision variable in a logical expression, therefore I can only use a binary decision variable to trigger the constraint instead of a more complex logical expression.

Of course I could linearize the conditional constraints, but that is not possible in my case: I need to implement this model (found in the literature) to benchmark an algorithm I developped. The model uses a lot of conditional constraints, and I fear that the workaround of linearizing everything might affect the performances.

So, is there a way for JuMP to model this constraint ? Otherwise, is it possible to do that using the low-level CPLEX API ? What would be the syntax in this case ?

Thanks!

---

<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: [August 11, 2022, 11:39pm UTC](https://discourse.julialang.org/t/iloifthen-constraints-for-cplex-in-julia/85619/2 "2022-08-11T23:39:11Z")

</div>

> Of course I could linearize the conditional constraints

Yes, this is what you will need to do.

> is there a way for JuMP to model this constraint ?

No. JuMP supports only indicator constraints of the form `z => { ... }` where `z` is a binary variable.

> Otherwise, is it possible to do that using the low-level CPLEX API ? What would be the syntax in this case ?

you can call any functions in the CPLEX C API: [https://github.com/jump-dev/CPLEX.jl/blob/master/src/gen2210/libcpx\_api.jl](https://github.com/jump-dev/CPLEX.jl/blob/master/src/gen2210/libcpx_api.jl)

But this is usually non-trivial, and requires a detailed understanding of the C API.

---

<div class="post-metadata">

### Author: ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)
#### Post date: [August 11, 2022, 11:42pm UTC](https://discourse.julialang.org/t/iloifthen-constraints-for-cplex-in-julia/85619/3 "2022-08-11T23:42:30Z")

</div>

It sounds like you’re looking to do constraint programming. There is a nice blog post about the relationship between JuMP and this topic here:

> **[An update on constraint programming in JuMP](https://jump.dev/blog/constraint-programming-update/)**
>
> Author: Thibaut Cuvelier (@dourouc05) JuMP and MathOptInterface are oriented towards traditional mathematical optimization, encompassing problem classes such as mixed-integer linear programs and conic optimization. However, the MathOptInterface API...

In particular, the package [ConstraintProgrammingExtensions.jl](https://github.com/JuliaConstraints/ConstraintProgrammingExtensions.jl) can interact with **[CPLEXCP.jl](https://github.com/JuliaConstraints/CPLEXCP.jl)**, which wraps the Java interface rather than the C++ version; see, for example, this list of exported functions [here](https://github.com/JuliaConstraints/CPLEXCP.jl/blob/cadc6577e4a076f1a763c904a4bc022e88472986/src/CPLEXCP.jl#L259).

There is probably some more work to do with introductory tutorials and docs for getting started.

---

<div class="post-metadata">

### Author: ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)
#### Post date: [August 11, 2022, 11:58pm UTC](https://discourse.julialang.org/t/iloifthen-constraints-for-cplex-in-julia/85619/4 "2022-08-11T23:58:19Z")

</div>

Or have a look here on reformulation ideas:  
[https://docs.mosek.com/modeling-cookbook/mio.html](https://docs.mosek.com/modeling-cookbook/mio.html)
