Reputation: 1
I'm modeling a PV + BESS system to find the optimum size that will meet a certain demand profile. I'm trying to model a condition for the charging of the BESS whereby charging occur when the output of the pv is greater than the demand. I'm getting the error below:
ERROR: Rule failed when generating expression for Constraint charge_binary_constraint_1 with index 0: PyomoException: Cannot convert non- constant Pyomo expression (0.0 < PV_output[0]) to bool. This error is usually caused by using a Var, unit, or mutable Param in a Boolean context such as an "if" statement, or when checking container membership or equality.
I'm looking for a method to implement the logic. Here is the constraint I tried to formulate
def charge_binary_rule_1(model, t):
if model.PV_output[t] > model.demand[t]:
return model.charge_binary_1[t] == 1
else:
return model.charge_binary_1[t] == 0
model.charge_binary_constraint_1 = pyo.Constraint(model.T, rule=charge_binary_rule_1)
Upvotes: 0
Views: 51
Reputation: 71
You get this error because you cannot use variables inside logical expressions in pyomo.
One way to implement the logic you want would be something like this:
def charge_binary_rule_1(model, t):
return model.PV_output[t] <= model.demand[t] + M * model.charge_binary_1[t]
model.charge_binary_constraint_1 = pyo.Constraint(model.T, rule=charge_binary_rule_1)
Where M
is a big number that allows the right hand side to be always bigger than left hand side when model.charge_binary_1[t]
is equal to 1. Then to force the binary to be equal to zero when PV output is smaller than demand you need to introduce this binary in the objective function by penalizing it when binary is 1. In that way you are going to ensure that binary takes value of 1 only when strictly necessary
Upvotes: 0