For example, I want to create a dictionnary of Pyomo Sets from a Python dictionnary of lists named dictOfList
. dictOfList
is built as follow:
dictOfList= {}
dictOfList[1] = [10,11,12]
dictOfList[2] = [20,21]
dictOfList[3] = [31,32,33,34]
Now, I would like the values [10,11,12]
, [20,21]
, and [31,32,33,34]
to be put in a Python dictionnary of Pyomo Sets named dictOfSets
as follow:
model.dictOfSets = {}
for el in dictOfList:
model.dictOfSets[el] = Set(initialize=dictOfList[el])
Then, if I display the values of the Sets using another simple for loop:
for el in dictOfList:
model.dictOfSets[el].display()
It seems like the Sets are not properly built, since it shows this in the running console:
_unknown_ : Dim=0, Dimen=1, Size=0, Domain=None, Ordered=False, Bounds=None
Not constructed
_unknown_ : Dim=0, Dimen=1, Size=0, Domain=None, Ordered=False, Bounds=None
Not constructed
_unknown_ : Dim=0, Dimen=1, Size=0, Domain=None, Ordered=False, Bounds=None
Not constructed
Is there a way to build a Pyomo Set inside a Python dictionnary? Right now, I have used normal Python lists, but it caused me very long processing time in larger models, and I know that it is better to use Pyomo Sets for faster performances in building constraints.
The
Set
s are not being fully constructed because they are never being assigned to aBlock
or model object. While not exactly what you are asking for (you don't actually end up with adict
ofSet
s on your model), the easiest thing to do to get the effect you want is to actually create an indexedSet
:If you print the model (
model.pprint()
), you will get:To actually create a Python
dict
ofSet
s you would also need to assign each individualSet
to the model so that it gets properly constructed. Something like this could work:gives
EDIT: You can have more complicated indexing if you explicitly create the indexed
Set
's index. That is:By explicitly creating the set, you can have arbitrarily complicated keys. Note that in some cases you may have to explicitly set the "dimension" of the index Set using the
dimen=
keyword:This is especially true if you want the tuples to have different lengths:
Where
model.pprint()
gives: