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:
            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.

  • $\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$ – Erwin Kalvelagen Apr 9 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 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$ – Erwin Kalvelagen Apr 9 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 at 9:13
  • 1
    $\begingroup$ This will be useful pyomo.readthedocs.io/en/doc-testbuild/modeling_extensions/… $\endgroup$ – kur ag Apr 9 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.