sierra_papa
sierra_papa

Reputation: 394

How to formulate a constraint in pulp involving indicator variable?

I looked here, here and here

but couldn't generalize solutions to my problem or there were no correct answers.

If I want a binary variable to turn off and on based on another variable selection so that when lpSum(some_var) = 0 then indicator_var must be 0 and when lpSum(some_var) > 0 then indicator_var must be 1 where lpSum(some_var) will never be grater than 5, then if I write:

for j in some_list:
    prob += lpSum(some_var[i, j] for i in some_other_list) <= indicator_var[j] * 5

this ensures that indicator_var is 1 if lpSum > 0 which is fine, but it does not guarantee that indicator_var is 0 if lpSum = 0. Hopefully it's clear what I want to achieve, if not please let me know so I can clarify further with more concrete example.

Upvotes: 0

Views: 306

Answers (1)

AirSquid
AirSquid

Reputation: 11938

You didn’t say what type of variable you are summing, but assuming non-negativity, this should work:

prob += lpSum(...) >= indicator_var[...]

Edit: The above should be used in conjunction with the constraint you already have above (2 constraints needed to enforce the inference you want). So also:

prob += lpSum(...) <= indicator_var[...] * 5

Upvotes: 1

Related Questions