Pyomo Cannot index a component with an indexed set

空扰寡人 提交于 2021-02-10 05:14:42

问题


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)

回答1:


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)). 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".

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)


来源:https://stackoverflow.com/questions/49950958/pyomo-cannot-index-a-component-with-an-indexed-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!