Search code examples
pythonoptimizationconstraintspyomo

PYOMO: Constraint does not have a proper value


I'm pretty new to pyomo and python, so this might be a pretty dumb mistake. The gist of what I'm trying to do:
I have a demand array, one demand value for each time step. The power bought plus the power provided by a CHP should equal the demand in each time step. (That's what I'm trying to do with the constraint). Running it leads to the following error:

ValueError: Constraint 'ElPowerBalanceEq' does not have a proper value. Found '<generator object ElPowerBalance.<locals>.<genexpr> at 0x000001BBF81DC040>'
Expecting a tuple or equation. Examples:
   sum(model.costs) == model.income
   (0, model.price[item], 50)

Here's the relevant code. Thanks in advance :-)

from pyomo.environ import*
import numpy as np

t = np.linspace(0,24,97) #time variable, one day in 0.25 steps
model.i=range(t.size) #index

model.Pel_buy = Var(within=PositiveReals) #electrical power bought
model.Pel_chp = Var(within=PositiveReals) #electrical power of chp

Del = 2+2*np.exp(-(t-12)**2/8**2) #demand electrical

#Define constraints

#Power Balance
def ElPowerBalance(model) :
    return (model.Pel_chp[i] + model.Pel_buy[i] == Del[i] for i in model.i)
model.ElPowerBalanceEq = Constraint(rule = ElPowerBalance)

Solution

  • Your ElPowerBalance() function is returning a generator object because you have the return value wrapped in parantheses (which python interprets as a generator). The simplest solution would be to use the * operator to unpack your generator, like so:

    def ElPowerBalance(model) :
        return *(model.Pel_chp[i] + model.Pel_buy[i] == Del[i] for i in model.i)