I get an error in Pyomo that I do not understand. I habe 288 timeslots t and a binary variable y (which I call here
model.variable_HPswitchedOff_Individual_SpaceHeating ) and another binary variable x (which I call
model.variable_HPswitchedOff_HelpAssociatedBinary_SpaceHeating). Now I want to have an equation that sets:
y(t) <= x(t-1) for all t >=2
y(t) =0 for t = 1
I only do this if the boolean from another file
Run_Simulations.considerMaxiumNumberOfStartsHP_Individual ==True: Basically I am trying to implement the first suggested equations from hereHow to set a limit for a switch to 0 of a variable (but in the equations in the link there is a small error because there the t begins by 1 but it should begin by 2 for the difference equation). Here is a part of my code:
model.set_timeslots = pyo.RangeSet(1, 288) model.variable_HPswitchedOff_Individual_SpaceHeating = pyo.Var(model.set_timeslots, within =pyo.Binary) model.variable_HPswitchedOff_HelpAssociatedBinary_SpaceHeating = pyo.Var(model.set_timeslots, within =pyo.Binary) def maximumNumberOfStarts_Individual_SpaceHeating_EQ1_Rule (model, t): if Run_Simulations.considerMaxiumNumberOfStartsHP_Individual ==True: if t == model.set_timeslots.first(): return model.variable_HPswitchedOff_Individual_SpaceHeating [t] == 0 return model.variable_HPswitchedOff_Individual_SpaceHeating [t] <= model.variable_HPswitchedOff_HelpAssociatedBinary_SpaceHeating [t-1] model.constraint_maximumNumberOfStarts_Individual_SpaceHeating_EQ1 = pyo.Constraint(model.set_timeslots, rule =maximumNumberOfStarts_Individual_SpaceHeating_EQ1_Rule)
However, when I execute it, I get an error message that I do not understand
ValueError: Constraint 'constraint_maximumNumberOfStarts_Individual_SpaceHeating_EQ1': rule returned None. Constraint rules must return either a valid expression, a 2- or 3-member tuple, or one of Constraint.Skip, Constraint.Feasible, or Constraint.Infeasible. The most common cause of this error is forgetting to include the "return" statement at the end of your rule.
Do you have an idea what the problem might be and how I can get rid of it? As indicated the problem is caused by the first timeslot for the equation but I do not know how to solve it because in another equation in my model I have exactly the same structure and it works. I'd appreciate every comment.
Update: I think the problem is that the boolean variable
Run_Simulations.considerMaxiumNumberOfStartsHP_Individual ==True was False such that there was no instruction for the constraint. But this brings me to another follow-up question. How can I exclude constraints from the model based on the boolean value of a external if-statement (external means that is does not come from the model itself)?