# Conditional constraint if else in JuMP

**URL:** <https://discourse.julialang.org/t/conditional-constraint-if-else-in-jump/6795>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [October 31, 2017, 8:29am UTC](https://discourse.julialang.org/t/conditional-constraint-if-else-in-jump/6795 "2017-10-31T08:29:34Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![UserMorocco](https://avatars.discourse-cdn.com/v4/letter/u/ee59a6/32.png) [@UserMorocco](https://discourse.julialang.org/u/UserMorocco)\
**Post date:** [October 31, 2017, 8:29am UTC](https://discourse.julialang.org/t/conditional-constraint-if-else-in-jump/6795/1 "2017-10-31T08:29:34Z")

</div>

Hello,  
How I can model the conditional constraint if else with JuMP in Julia ?

Thank you

---

<div class="post-metadata">

**Author:** ![joaquimg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joaquimg/32/223_2.png) [@joaquimg](https://discourse.julialang.org/u/joaquimg)\
**Post date:** [October 31, 2017, 11:13am UTC](https://discourse.julialang.org/t/conditional-constraint-if-else-in-jump/6795/2 "2017-10-31T11:13:58Z")

</div>

You might be able to do that using Integer Programming techniques. Can you give an example of what you want to do?

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [October 31, 2017, 12:35pm UTC](https://discourse.julialang.org/t/conditional-constraint-if-else-in-jump/6795/3 "2017-10-31T12:35:53Z")

</div>

You might be interested in [GitHub - rdeits/ConditionalJuMP.jl: Automatic transformation of implications and complementarity into mixed-integer models in Julia](https://github.com/rdeits/ConditionalJuMP.jl).

---

<div class="post-metadata">

**Author:** ![bbrunaud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bbrunaud/32/1737_2.png) [@bbrunaud](https://discourse.julialang.org/u/bbrunaud)\
**Post date:** [November 5, 2017, 9:38pm UTC](https://discourse.julialang.org/t/conditional-constraint-if-else-in-jump/6795/4 "2017-11-05T21:38:57Z")

</div>

@tkoolen great work!. I was implementing indicator constraints myself which use native CPLEX and Gurobi functions. I just need to finish the @indicatorconstraint macro but everything else is done. Also check the Pyomo.gdp plugin which also implements some cool stuff like nested disjunctions and convex hull reformulations.

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [November 5, 2017, 9:47pm UTC](https://discourse.julialang.org/t/conditional-constraint-if-else-in-jump/6795/5 "2017-11-05T21:47:51Z")

</div>

Woah, that’s not my package; it’s all @rdeits’ work. But yeah, he’s doing great work!

---

<div class="post-metadata">

**Author:** ![jmatovu](https://avatars.discourse-cdn.com/v4/letter/j/838e76/32.png) [@jmatovu](https://discourse.julialang.org/u/jmatovu)\
**Post date:** [May 4, 2020, 9:22am UTC](https://discourse.julialang.org/t/conditional-constraint-if-else-in-jump/6795/6 "2020-05-04T09:22:57Z")

</div>

I also want to to the same something like this:

@NLconstraint(cgecam, eq[it in IT] , pm[it] == pwm[it]_er_(1 + tm[it]) if pwm[it]\>=0.0 )

in otherwords I create a contraint only if pwm[it] is greater than zero. In GAMS it works well with $ operator. For Julia/Jump I have failed to do this.

---

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [May 4, 2020, 9:34am UTC](https://discourse.julialang.org/t/conditional-constraint-if-else-in-jump/6795/7 "2020-05-04T09:34:25Z")

</div>

You can do this with [complementarity constraints](https://www.juliaopt.org/JuMP.jl/v0.21.1/constraints/#Complementarity-constraints-1).  
For instance, the following constrains `y = 1/2` if `x >= 0` but it also constrain `x` to be zero if `y` is not `1/2` which may or may not be what you want depending on your application.

```julia
@variable(model, x >= 0)
@constraint(model, 2y - 1 ⟂ x)
@constraint(model, 2y - 1 ⟂ x)

```

Can you give a reference to the GAMS doc for this `$` operator ?

---

<div class="post-metadata">

**Author:** ![jmatovu](https://avatars.discourse-cdn.com/v4/letter/j/838e76/32.png) [@jmatovu](https://discourse.julialang.org/u/jmatovu)\
**Post date:** [May 4, 2020, 9:51am UTC](https://discourse.julialang.org/t/conditional-constraint-if-else-in-jump/6795/8 "2020-05-04T09:51:16Z")

</div>

Thanks for the quick response. Reference for the dollar operator is [Conditional Expressions, Assignments and Equations](https://www.gams.com/latest/docs/UG_CondExpr.html). It is used a lot especially for computable general equilibrium models to ignore equations.

Now to put my question different–suppose x is not a variable in the model and its a parameter. How can I implement this  
@constraint(model, eq[i in I], 2y[i] - 1, if x[i]\>=0)

---

<div class="post-metadata">

**Author:** ![jmatovu](https://avatars.discourse-cdn.com/v4/letter/j/838e76/32.png) [@jmatovu](https://discourse.julialang.org/u/jmatovu)\
**Post date:** [May 4, 2020, 10:09am UTC](https://discourse.julialang.org/t/conditional-constraint-if-else-in-jump/6795/9 "2020-05-04T10:09:11Z")

</div>

Am trying to figure out how to write this in short form because they are so many equations to use loops everywhere. The LDCO[j], KDCO[j]! are not variables but rather parameters computed from elsewhere.  
for j in J  
if LDCO[j]!=0 && KDCO[j]!=0  
@NLconstraint(CGEModel,VA[j] == B\_VA[j]\*((beta\_VA[j]\*LDC[j]^(-rho\_VA[j]))+((1-beta\_VA[j])_KDC[j]^(-rho\_VA[j])))^(-1/rho\_VA[j]))  
elseif LDCO[j]!=0  
 @NLconstraint(CGEModel,VA[j] == B\_VA[j]_(beta\_VA[j]_LDC[j]^(-rho\_VA[j]))^(-1/rho\_VA[j]))  
elseif KDCO[j]!=0  
 @NLconstraint(CGEModel,VA[j] == B\_VA[j]_((1-beta\_VA[j])_KDC[j]^(-rho\_VA[j]))^(-1/rho\_VA[j]))  
end  
end  
If this was GAMS I would just write this using the dollar operators:  
EQ3(j)… VA(j) =e= B\_VA(j)_{  
[beta\_VA(j)\*LDC(j)**(-rho\_VA(j))]$LDCO(j)  
+[(1-beta\_VA(j))\*KDC(j)**(-rho\_VA(j))]$KDCO(j)  
}\*\*(-1/rho\_VA(j));  
How do i do something similar in Julia/JuMP.

---

<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:** [May 4, 2020, 4:47pm UTC](https://discourse.julialang.org/t/conditional-constraint-if-else-in-jump/6795/10 "2020-05-04T16:47:17Z")

</div>

Take a read of the first post in [Please read: make it easier to help you - #11](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757/11).

It’s easier to help if you do the following:

- format your code using the triple backticks: `````
- provide a minimal working example of what you are trying to accomplish.
  - Simplify it down to one constraint, and two or three variables. Remove any redundant information.
  - It may help to provide the math formulation of your simple problem as well.

- What solver are you using? If you are trying to solve MPEC-type models, JuMP is a little behind compared to GAMS. You either have to do the reformulations manually, or using a solver like KNITRO.jl.
- You could also use a mixed-integer reformulation.

---

<div class="post-metadata">

**Author:** ![jmatovu](https://avatars.discourse-cdn.com/v4/letter/j/838e76/32.png) [@jmatovu](https://discourse.julialang.org/u/jmatovu)\
**Post date:** [May 4, 2020, 5:14pm UTC](https://discourse.julialang.org/t/conditional-constraint-if-else-in-jump/6795/11 "2020-05-04T17:14:21Z")

</div>

The simpler version of my problem is as follows.  
@NLconstraint(model, eq[i in I], 2y[i]^3 - 1, if x0[i]\>=0  
where x0 is a parameter. In otherwords how does NLconstraint accomodate if statements

---

<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:** [May 4, 2020, 5:59pm UTC](https://discourse.julialang.org/t/conditional-constraint-if-else-in-jump/6795/12 "2020-05-04T17:59:13Z")

</div>

> [@jmatovu](#):
>
> @NLconstraint(model, eq[i in I], 2y[i]^3 - 1, if x0[i]\>=0

This is not a constraint. Do you mean

```Julia
@NLconstraint(model, eq[i in I; x0[i] >= 0], 2y[i]^3 - 1 == x0[i])

```

which is equivalent to

```Julia
for i in I
    if x0[i] >= 0
        @NLconstraint(model, 2y[i]^3 - 1 == x0[i])
    end
end

```

---

<div class="post-metadata">

**Author:** ![jmatovu](https://avatars.discourse-cdn.com/v4/letter/j/838e76/32.png) [@jmatovu](https://discourse.julialang.org/u/jmatovu)\
**Post date:** [May 4, 2020, 6:51pm UTC](https://discourse.julialang.org/t/conditional-constraint-if-else-in-jump/6795/13 "2020-05-04T18:51:58Z")

</div>

Perfect this is what I wanted. Thanks so much.

---

<div class="post-metadata">

**Author:** ![Katti](https://avatars.discourse-cdn.com/v4/letter/k/6de8d8/32.png) [@Katti](https://discourse.julialang.org/u/Katti)\
**Post date:** [November 9, 2022, 5:22pm UTC](https://discourse.julialang.org/t/conditional-constraint-if-else-in-jump/6795/14 "2022-11-09T17:22:13Z")

</div>

Hello, I would like to come back to this discussion:

Is it possible to build if else constraints in a non-linear program while using JuMP and Ipopt?

I have tried what you suggested

> @NLconstraint(model, constraint[q in Q, c in COMP; PRICE\_B[q, c] - Price[q] \>= 0], Exp\_Rev[q, c] == Price[q]\*Prob[q, elec\_gen])  
> @NLconstraint(model, constraint[q in Q, c in COMP; PRICE\_B[q, c] - Price[q] \< 0], Exp\_Rev[q, c] == 0)

However Julia returns the following error message:

> MethodError: no method matching isless(::Int64, ::AffExpr)

What am I doing wrong? Is there any other way to implement an if else constraint in NLP using Ipopt?

---

<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 9, 2022, 6:43pm UTC](https://discourse.julialang.org/t/conditional-constraint-if-else-in-jump/6795/15 "2022-11-09T18:43:33Z")

</div>

Please start a new post, rather than posting on an old one, and please include a reproducible example.

You can use `ifelse` inside the macros: `@NLobjective(model, Min, ifelse(x >= 0, x^2, -x^3))`
