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[1]': 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)?

  • $\begingroup$ Your approach in defining a constraint rule is correct. You need to check whether the binary variable that comes from outside of the model for any t can be TRUE… $\endgroup$ – Oguz Toragay May 13 at 6:18
  • $\begingroup$ Thanks Oguz for your answer. Yes, the problem is that - depending on the external boolean variable - the constraint should be activated or not. If the exernal boolean variable is TRUE then the constraint should be activated and it is activated. However, when the external variable is FALSE then the constraint should not be activated (not considered in the model). Here I get the mentioned error message. So the question is how can I deactivate the constraints in this case? This is a general question about Pyomo modells. Do you have any advice? $\endgroup$ – PeterBe May 14 at 7:20
  • $\begingroup$ I just found out that is such case you can use return pyo.Constraint.Feasible and then the constraint is automatically always feasbile meaning that it is deactivated. $\endgroup$ – PeterBe May 14 at 7:57
  • $\begingroup$ I believe it's better to use "Constraint.Skip" when you don't want to have a constraint for that specific binary value from outside. $\endgroup$ – Oguz Toragay May 14 at 17:19
  • 1
    $\begingroup$ Looking at this link pyomo.readthedocs.io/en/stable/_modules/pyomo/core/base/…, Constraint.Skip is used when you don't want to generate a constraint for a specific index of variables. If you use the Constraint.Feasible, you actually generate the constraint for all indices and for some consider the constraint if feasible. It in turn changes the structure of your model (may even make it more complicated). $\endgroup$ – Oguz Toragay May 17 at 15:34

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.