-1

I try add a constraint expressions to a python/pyomo model: the code I tried is this:

for r_k in VAR1: 
    for p_v in VAR2:
        for b_n in VAR3:
            IF mix[b_n][p_v]() >= 0:
                model.cons.add(model.func2[r_k,b_n,p_v]*q1[b_n][p_v] - model.func1[r_k,b_n,p_v] ==0)

If I leave the IF loop away it runs fine. The code above produces a Syntax error. Only downside is it creates 1000s of extra mathematically trivial constraints that are not needed.
"mix" is only a sparsely populated "binary" dictionary (i.e. mostly zeros, a few ones in between). A constraint is only needed where there is a "1". It seems there is an issue with evaluating/calling the dictionary values within the for loops. Any idea how to resolve this would be much appreciated. Thanks.

Perry Hall
  • 11
  • 5

4 Answers4

0

Remove calling mix:

for r_k in VAR1: 
    for p_v in VAR2:
        for b_n in VAR3:
            IF mix[b_n][p_v] >= 0:
                model.cons.add(model.func2[r_k,b_n,p_v]*q1[b_n][p_v] - model.func1[r_k,b_n,p_v] ==0)
n1tr0xs
  • 408
  • 2
  • 9
0

You've got two errors in your code:

  • if is lowercase in Python. IF does NOT exist in any of the standard modules
  • assuming mix is a dicitonary, you can't call it. By adding braces () behind a variable or anything in the scope, you tell python to treat it as a callable and try to execute it.

Assuming the rest of your code works fine, this should solve your problem:

for r_k in VAR1: 
    for p_v in VAR2:
        for b_n in VAR3:
            if mix[b_n][p_v] >= 0:
                model.cons.add(model.func2[r_k, b_n, p_v] * q1[b_n][p_v] - model.func1[r_k, b_n, p_v] == 0)

Further remarks:

  • if (and elif, else) is NOT a loop, it is a conditional statement! for or while are loops.
  • you check for mix[b_n][p_v] >= 0, but state that a constraint is only needed when "there is a "1"". So shouldn't your condition be mix[b_n][p_v] > 0 or even more explicitly mix[b_n][p_v] == 1? (Explicit is better than implicit, see The Zen of Python, second statement)
JE_Muc
  • 5,403
  • 2
  • 26
  • 41
  • thanks for the quick reply. I will definitely try the```==1```. re loop, sorry i am still pretty new to programming, so not that good yet with all the terms. – Perry Hall Feb 01 '20 at 10:17
  • You are welcome and no problem at all! I'm glad I was able to help. :) – JE_Muc Feb 03 '20 at 10:27
0

If i correctly understood the problem , the constraint is only added to the model when the value of mix dictionary for some key evaluates to 1.

if so

in your code IF mix[b_n][p_v]() >= 0:

here in if statement you are adding constraint for every value be it 0 or 1. try

for r_k in VAR1: for p_v in VAR2: for b_n in VAR3: if mix[b_n][p_v] == 1: model.cons.add(model.func2[r_k,b_n,p_v]*q1[b_n][p_v] -model.func1[r_k,b_n,p_v] ==0)

0

IF should be if. Lowercase not uppercase.

verma314
  • 56
  • 1
  • thanks a lot, this worked right away. sometimes one does not see the forest because of too many trees. – Perry Hall Feb 01 '20 at 10:18
  • thanks to all who answered so quick. i tried to upvote. but i am still quite new, so do not have enough reputation yet. hopefully get there one day. :-) – Perry Hall Feb 01 '20 at 10:20