我试图在利用循环条件的 Pyomo 模型上定义一个约束。以下是我认为它应该如何工作(来自 GAMS 的循环语法)。
from __future__ import division
from pyomo.environ import *
model = ConcreteModel()
## define sets
model.t = Set(initialize = [i for i in range(8760)])
## define variables
model.ESS_SOC = Var(model.t, domain = NonNegativeReals) # battery state of charge
model.ESS_c = Var(model.t, domain = NonNegativeReals) # battery charging
model.ESS_d = Var(model.t, domain = NonNegativeReals) # battery discharging
## skip obj for this example
## define constraints
#SOC constraint
model.SOC_const = ConstraintList()
for i in model.t:
model.SOC_const.add( model.ESS_SOC[i] == model.ESS_SOC[i--1] + model.ESS_c[i] - model.ESS_d[i] )
但是当我运行上面的示例时,我收到以下错误消息:
KeyError: "Index '8760' is not valid for indexed component 'ESS_SOC'"
我同意给定定义的model.t
错误,但错误让我相信它几乎正在做我想要它做的事情,那就是:
model.ESS_SOC[0] == model.ESS_SOC[8759] + model.ESS_c[0] - model.ESS_d[0]
model.ESS_SOC[1] == model.ESS_SOC[0] + model.ESS_c[1] - model.ESS_d[1]
...
model.ESS_SOC[8759] == model.ESS_SOC[8758] + model.ESS_c[8759] - model.ESS_d[8759]
有没有办法定义约束,这就是我得到的?