Pyomo无法为具有索引集的组件建立索引 [英] Pyomo Cannot index a component with an indexed set
问题描述
我有一个Pyomo模型,该模型具有一组稀疏值,但是当我尝试根据此稀疏集为二进制变量建立索引时,出现错误无法为具有索引集的组件建立索引
.举一个简单的例子:
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. For a simplified example:
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]内部二进制)
).由于按组索引某些内容没有意义,因此会出现异常"无法对具有索引集的组件进行索引
".
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)
). 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
".
在您的情况下,您似乎想要具有三个值的单个集 S
.正确的语法是:
In your case, it looks like you want a single set S
that has three values. The correct syntax is:
model.S = Set(initialize=[1, 4, 6])
model.V = Var(model.S, within=Binary)
这篇关于Pyomo无法为具有索引集的组件建立索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!