简体   繁体   English

制定约束时的 Pyomo 错误

[英]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.似乎该错误是由我在 cons4 中使用的 IF 语句引起的。 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.您的“示例”代码会产生错误,因为您使用的条件if语句取决于变量的值,这是不允许的。 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.如果您遇到困难,请使用完全可重现的示例编辑您的帖子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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