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.
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$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$if(exogenous condition)
which is ok. There isif(endogenous condition)
which is bad. It is important to understand the difference between a set/parameter and a decision variable. $\endgroup$