问题
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