1

我试图在利用循环条件的 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] 

有没有办法定义约束,这就是我得到的?

4

1 回答 1

3

我建议使用索引约束而不是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)

请注意,您需要在和方法ordered=True上设置选项才能工作。Setfirst()last()

于 2019-12-18T20:05:12.930 回答