Conditional constraint if else in JuMP

Hello,
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 GitHub - rdeits/ConditionalJuMP.jl: Automatic transformation of implications and complementarity into mixed-integer models in Julia.

4 Likes

@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 Conditional Expressions, Assignments and Equations. 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]
(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.

Take a read of the first post in Please read: make it easier to help you - #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.

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])
    end
end
3 Likes

Perfect this is what I wanted. Thanks so much.

1 Like

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?

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))