简体   繁体   中英

Pyomo error in formulating the constraint

I am trying to formulate a battery optimization problem to charge/discharge the battery optimally. The formulation is the following:

model = pyo.ConcreteModel()

#Set time period
model.timesteps = pyo.Set(initialize=pyo.RangeSet(len(pv)),ordered=True)

#Parameters
model.b_efficiency  = pyo.Param(initialize=etta)
model.b_cap = pyo.Param(initialize=battery_capacity)
model.b_min_soc = pyo.Param(initialize=battery_soc_min)
model.b_max_soc = pyo.Param(initialize=battery_soc_max)
model.b_charging_rate = pyo.Param(initialize=battery_charge_rate)

model.Ppv = pyo.Param(model.timesteps,initialize=dict(enumerate(pv,1)),within=pyo.Any)
model.Pdemand = pyo.Param(model.timesteps, initialize=dict(enumerate(demand,1)),within=pyo.Any)

#Variables
model.Pbat_ch = pyo.Var(model.timesteps, within = pyo.NonNegativeReals)
model.Pbat_dis = pyo.Var(model.timesteps, within = pyo.NonNegativeReals)
model.Ebat = pyo.Var(model.timesteps, within = pyo.NonNegativeReals)
model.Pgrid = pyo.Var(model.timesteps, within = pyo.NonNegativeReals)

# Define the constraints of the model

def BatEnergyBounds(model, t):
    return model.b_min_soc * model.b_cap <= model.Ebat[t] <= model.b_max_soc * model.b_cap

model.cons1 = pyo.Constraint(model.timesteps, rule = BatEnergyBounds)

def BatChargingBounds(model, t):
    return 0 <= model.Pbat_ch[t] <= model.b_charging_rate

model.cons2 = pyo.Constraint(model.timesteps, rule = BatChargingBounds)

def BatDischargingBounds(model, t):
    return 0 <= model.Pbat_dis[t] <= model.b_charging_rate

model.cons3 = pyo.Constraint(model.timesteps, rule = BatDischargingBounds)

def BatEnergyRule(model, t):
    if t == model.timesteps.first():
        return model.Ebat[t] == model.b_cap/2
    else:  
        return model.Ebat[t] == model.Ebat[t-1] + (model.b_efficiency * model.Pbat_ch[t] - model.Pbat_dis[t]/model.b_efficiency)

model.cons4 = pyo.Constraint(model.timesteps, rule = BatEnergyRule)

def PowerBalanceRule(model, t):
    return model.Pgrid[t] == model.Ppv[t] - model.Pdemand[t] + model.Pbat_dis[t] - model.Pbat_ch[t] 

model.cons5 = pyo.Constraint(model.timesteps, rule = PowerBalanceRule)

# Define the objective function
def ObjRule(model):
    return sum(model.Pgrid[t] for t in model.timesteps)

model.obj = pyo.Objective(rule = ObjRule, sense = pyo.minimize)

However, I am getting the following error:

PyomoException: Cannot convert non-constant Pyomo expression (1.0  <=  Ebat[1]) 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. For example,
    >>> m.x = Var()
    >>> if m.x >= 1:
    ...     pass
and
    >>> m.y = Var()
    >>> if m.y in [m.x, m.y]:
    ...     pass
would both cause this exception.

It seems like the error is caused by the IF statement that I used in cons4. Any idea of what is causing the error?

It isn't clear what is causing the error. If you are looking for help with an error, it is best to provide an example that produces the error by itself with the exact stack trace, etc.

Your "example" code produces the error because you are using a conditional if statement that depends on the value of a variable, which isn't allowed. Your code in the main program does not do that, so it looks fine.

If you are stuck, edit your post with a completely reproducible example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM