Variable indexed by an indexed set

616 views
Skip to first unread message

dmc...@g.hmc.edu

unread,
Aug 17, 2017, 7:38:09 PM8/17/17
to Pyomo Forum
I'm having trouble figuring out how to deal with indexed sets in Pyomo. Let's say I have the following example...

from pyomo.environ import *

m = ConcreteModel()
# Different types of asset to potentially deploy
m.assets = Set(initialize=['A', 'B', 'C'])
# Different teams of each asset available
teams_dict = {'A': ['One', 'Two'], 'B': ['One'], 'C': ['One', 'Two', 'Three']}
m.teams = Set(m.assets, initialize=teams_dict)


If I want to define a variable m.x indexed over both of the above sets, how do I do that? I can't find any examples of this, and my guesses were...

# A binary variable that indicates whether a particular asset is chosen or not
m.x = Var(m.assets, m.teams, within=Binary)
which gives
TypeError: Cannot index a component with an indexed set
and

m.x = Var(m.assets, m.teams[m.assets], within=Binary)
which gives
AttributeError: '_IndexedSetData' object has no attribute '_name'

Any suggestions on how to define this variable properly? The TypeError kind of suggests that it's not possible to index over an indexed set, but that would seem to defeat the point of even having indexed sets, so I'm confused.

David Woodruff

unread,
Aug 19, 2017, 5:11:13 PM8/19/17
to pyomo...@googlegroups.com
There are multiple ways to do this. One way is described at
https://software.sandia.gov/downloads/pub/pyomo/PyomoOnlineDocs.html#_sparse_index_sets

(But depending on your browser, you might want to look at the pdf version of the document since this section does not display properly in all web browsers)

--
You received this message because you are subscribed to the Google Groups "Pyomo Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyomo-forum+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

dmc...@g.hmc.edu

unread,
Sep 7, 2017, 4:03:25 PM9/7/17
to Pyomo Forum
Thanks for getting back to me, and sorry for my own slow reply. I had read through this section before but somehow missed the example that applied directly - following that, I got this to work. Thanks again!
To unsubscribe from this group and stop receiving emails from it, send an email to pyomo-forum...@googlegroups.com.

Sebastian Jose

unread,
Sep 29, 2019, 8:58:12 PM9/29/19
to Pyomo Forum
Do you know which is the pdf version, I have the same problem
To unsubscribe from this group and stop receiving emails from it, send an email to pyomo...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages