0

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.

sierra_papa
  • 368
  • 3
  • 9

1 Answers1

1

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
AirSquid
  • 10,214
  • 2
  • 7
  • 31
  • Yes I am summing a binary variable. Not sure how this can solve the problem though. I would need an indicator_var to take the value 1 if lpSum(…) > 0 and 0 if lpSum(…) = 0. In your example, If lpSum(…) = 3, indicator_var[…] can still be 0. Thanks for input! – sierra_papa Sep 05 '21 at 16:11
  • I wasn't clear in my answer. I was assuming you would still implement the constraint you already had... the pair is needed. :) – AirSquid Sep 05 '21 at 16:29