Reputation: 394
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
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