[英]Defining cyclic/periodic boundary conditions in Pyomo constraints
I am trying to define a constraint on a Pyomo model that utilizes a cyclic condition.我正在尝试在使用循环条件的 Pyomo 模型上定义约束。 Below is how I think it should work (cyclic syntax from GAMS).
下面是我认为它应该如何工作(来自 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] )
But when I run the above example, I get the following error message:但是当我运行上面的示例时,我收到以下错误消息:
KeyError: "Index '8760' is not valid for indexed component 'ESS_SOC'"
I agree with the error given the definition of model.t
, but the error has me believing that it is almost doing what I want it to do, which would be to have:鉴于
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]
Is there a way to define the constraint so that is what I get?有没有办法定义约束,这就是我得到的?
I would recommend doing this using an indexed constraint instead of a ConstraintList
:我建议使用索引约束而不是
ConstraintList
来执行此操作:
from __future__ import division
from pyomo.environ import *
model = ConcreteModel()
## define sets
model.t = Set(initialize = [i for i in range(8760)], ordered=True)
## 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
def _SOC_const(m, i):
if i == m.t.first():
return model.ESS_SOC[i] == model.ESS_SOC[m.t.last()] + model.ESS_c[i] - model.ESS_d[i]
return model.ESS_SOC[i] == model.ESS_SOC[i-1] + model.ESS_c[i] - model.ESS_d[i]
model.SOC_const = Constraint(model.t, rule=_SOC_const)
Notice that you need to set the ordered=True
option on the Set
for the first()
and last()
methods to work.请注意,您需要在
Set
上Set
ordered=True
选项才能使first()
和last()
方法工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.