如何在Pyomo中指定离散化的特定点?

时间:2016-06-11 23:31:09

标签: python optimization pyomo

我有一个连续时间的Pyomo模型:

from pyomo.environ import *
from pyomo.dae import *

m      = ConcreteModel()
m.t    = ContinuousSet(bounds=(0,150)) 
m.T    = Param(default=150)
m.S    = Var(m.t, bounds=(0,None))
m.Sdot = DerivativeVar(m.S)

discretizer = TransformationFactory('dae.collocation')
discretizer.apply_to(m,nfe=100,ncp=3,scheme='LAGRANGE-RADAU')

m.obj  = Objective(expr=m.S[122],sense=maximize)

但是,当我运行上面的代码时,我收到以下错误:

KeyError: "Error accessing indexed component: Index '120' is not valid for array component 'S'"

查看list(m.t),我看到,确定不包括122作为离散化点:

..., 121.73257700000001, 122.467423, ...

如何指定要包含在离散化中的点?

1 个答案:

答案 0 :(得分:1)

您可以强制离散器在离散模型之前将其添加到ContinuousSet 中以包含特定点(请参阅在线documentation)。对于您的特定情况,最简单的方法可能是在初始化列表中包含点:

m.t = ContinuousSet(bounds=(0,150), intialize=[122])
print list(m.t)
# [0, 122, 150]

m.S    = Var(m.t, bounds=(0,None))
m.Sdot = DerivativeVar(m.S)
discretizer = TransformationFactory('dae.collocation')
discretizer.apply_to(m,nfe=100,ncp=3,scheme='LAGRANGE-RADAU')
print list(m.t)
# ..., 121.323184, 122, 122.271339, ...