简体   繁体   English

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

[英]Pyomo Cannot index a component with an indexed set

I have a Pyomo model that has a sparse set of values but I get the error Cannot index a component with an indexed set when I try to index a binary variable according to this sparse set. 我有一个Pyomo模型,其中包含一组稀疏值但我得到错误当我尝试根据此稀疏集索引二进制变量时, Cannot index a component with an indexed set For a simplified example: 有关简化示例:

model = ConcreteModel()

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

The line 这条线

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

creates an Indexed Set: that is a Set of 3 Sets, each one of which is empty (Pyomo treats positional arguments as indexing sets - just like in your comment about Var([1,3,5], within-Binary) ). 创建一个索引集:这是一组3个集合,每个集合都是空的(Pyomo将位置参数视为索引集 - 就像你关于Var([1,3,5], within-Binary)的评论一样Var([1,3,5], within-Binary) )。 As it does not make sense to index something by a set-of-sets, you get the exception " Cannot index a component with an indexed set ". 由于通过一组集合来索引某些东西没有意义,因此您将获得异常“ Cannot index a component with an indexed set ”。

In your case, it looks like you want a single set S that has three values. 在您的情况下,看起来您想要一个具有三个值的单个集合S The correct syntax is: 正确的语法是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM