How to add multiple condition for indices on constraints

Hello everyone,

I’m having problems with my model when I try to add two conditions inside a constraint, where I try to define the constraint where an index cannot be equal to 2 values ​​from a set, and then create specific constraints for those values. This specific constraints are ok, but the “main” constraint does different things depending on how I write the condition.

I must note that I am working on a stochastic model, so this separation of the constraint is due to the uncertainty that Im applying to the “E” and “H”, so I don’t want to “duplicate” this while writing the main constraint and then the scenary specific constraints.

Here is the first try:

 @constraint(my_model,  my_constraint_name[t in TIME, s in S, l in L, p in P_M[l], p ∉ P_st, p != :E, p != :H] ... )

I suppose that these is correct, because it doesn’t throw me any error, but here I’m not using the normal way to add these " != " condition, that is used when theres only one value that should be not equal, and before the condition theres a " ; ".

So, I’ve also tried these way:

 @constraint(my_model, my_constraint_name[t in TIME, s in S, l in L, p in P_M[l], p ∉ P_st; (p != :E && p != :H)] ...)

The parenthesis is added because without it i receive an error, and also there I’ve put the " ; " before the condition. But with this formulation, I’ve noticed that the model optimize to another value of the objective, only by changing this notation.

I’ll be very grateful if anyone can tell which of this (or another) formulations is the correct way to achieve the correct results of my model.

You need

@constraint(
    my_model,
    my_constraint_name[t in TIME, s in S, l in L, p in P_M[l]; p ∉ P_st && p != :E && p != :H],
    ...
)

The condition must come after the ;, and it must be one Boolean logic statement that evaluates to true or false.

1 Like

Thank you so much @odow !

It does work indeed.

1 Like