Conditional constraint if else in JuMP

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

Thank you

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

1 Like

You might be interested in


@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.

1 Like

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

1 Like

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.

You can do this with complementarity constraints.
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.

@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 ?

Thanks for the quick response. Reference for the dollar operator is 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)

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]
elseif KDCO[j]!=0
@NLconstraint(CGEModel,VA[j] == B_VA[j]
If this was GAMS I would just write this using the dollar operators:
EQ3(j)… VA(j) =e= B_VA(j)
How do i do something similar in Julia/JuMP.

Take a read of the first post in PSA: make it easier to help you.

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.

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

This is not a constraint. Do you mean

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

which is equivalent to

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

Perfect this is what I wanted. Thanks so much.

1 Like