0
$\begingroup$

I would like to define the value of a variable model.variable_costsPerTimeSlot [t] in pyomo and I would like to use if-statements. Depending on the value of the if-statements, another expression should be used for assigning the value. However, I get an error message when doing this

Evaluating Pyomo variables in a Boolean context, e.g.
            if expression <= 5:
        is generally invalid.  If you want to obtain the Boolean value of the
        expression based on the current variable values, explicitly evaluate the
        expression using the value() function:
            if value(expression) <= 5:
        or
            if value(expression <= 5):

My question is how can I avoid this error? I do not really understand what the problem is. I also tried to use the value() function but I got an error telling me `NameError: name 'value' is not defined

So here you can see this constraint:

def costsPerTimeSlotRule (model, t):
    if (model.variable_pvGeneration [t] <= model.variable_electricalPowerTotal [t]):
        return model.variable_costsPerTimeSlot [t] == (model.variable_electricalPowerTotal [t] - model.variable_pvGeneration [t]) * SetUpScenarios.timeResolution * 60 * (model.param_electricityPrice_In_Cents[t]/3600000)
    if (model.variable_pvGeneration [t] > model.variable_electricalPowerTotal [t]):
        return model.variable_costsPerTimeSlot [t] == (model.variable_pvGeneration [t] - model.variable_electricalPowerTotal [t]) * SetUpScenarios.timeResolution * 60 * (SetUpScenarios.revenueForFeedingBackElecticityIntoTheGrid_CentsPerKWH/3600000)
model.constraint_costsPerTimeSlots = pyo.Constraint(model.set_timeslots, rule =costsPerTimeSlotRule )`

I'd appreciate every comment and would be thankful for your help.

$\endgroup$
11
  • $\begingroup$ In almost all cases you should not use an if statement. Typically binary variables are used. If is for programming, not for use in mathematical programming equations. Final advice: first write down the mathematical model on a piece of paper before starting to code. $\endgroup$ Apr 9, 2021 at 7:20
  • $\begingroup$ Thanks Erwin for your answer. Basically I use if many time when writing optimizaiton problems and in some cases it was also recommended to use if. For example when I want to specify a specific value in an constraint like this: def temperatureConstraintRule(model, t): if t == model.set_timeslots.first(): return model.variable_temperature[t] ==10 return model.variable_temperature[t] == model.variable_temperature[t-1] + ... model.constraint_temperature = pyo.Constraint (model.set_timeslots, rule=temperatureConstraintRule) $\endgroup$
    – PeterBe
    Apr 9, 2021 at 8:32
  • $\begingroup$ There is if(exogenous condition) which is ok. There is if(endogenous condition) which is bad. It is important to understand the difference between a set/parameter and a decision variable. $\endgroup$ Apr 9, 2021 at 8:36
  • $\begingroup$ Thanks Erwin for your answer and effort. So I have to use a binary variable instead of the if? There is no way how to use the if with endogenous condition, right? $\endgroup$
    – PeterBe
    Apr 9, 2021 at 9:13
  • 1
    $\begingroup$ This will be useful pyomo.readthedocs.io/en/doc-testbuild/modeling_extensions/… $\endgroup$
    – kur ag
    Apr 9, 2021 at 15:16

1 Answer 1

2
$\begingroup$

My question is how can I avoid this error? I do not really understand what the problem is. I also tried to use the value() function but I got an error telling me `NameError: name 'value' is not defined

As stated in several comments, it seems like you're making use of endogenous if statements. On the other hand, if you need an exegenous if statement reliying on the value of an expression (after having solved a model for example), don't forget to

from pyomo.environ import value

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.