Pyomo 无法使用索引集索引组件

Pyomo Cannot index a component with an indexed set

我有一个 Pyomo 模型,它有一组稀疏值,但是当我尝试根据这个稀疏集索引一个二元变量时,我得到了错误 Cannot index a component with an indexed set。举个简单的例子:

model = ConcreteModel()

model.S = Set([1, 4, 6])
model.V = Var(model.S, within=Binary)

model.S = Set([1, 4, 6])

创建一个索引集:这是一个由 3 个集合组成的集合,每个集合都是空的(Pyomo 将位置参数视为索引集 - 就像您对 Var([1,3,5], within-Binary) 的评论一样)。由于通过一组集合来索引某些内容没有意义,因此您会得到异常“Cannot index a component with an indexed set”。

在您的例子中,您似乎想要一个具有三个值的 单个 集合 S。正确的语法是:

model.S = Set(initialize=[1, 4, 6])
model.V = Var(model.S, within=Binary)