getting names of the indexing sets for a component

90 views
Skip to first unread message

Danny Kaufman

unread,
Oct 28, 2019, 11:46:50 AM10/28/19
to Pyomo Forum
Hi,
I am wondering if there is a way to get the names (and order) of the indexing sets for a component?  This would really help with model inspection after solving.

For example, if I define two sets, A and B:
model.A = pyo.Set(initialize=[0, 1, 2])
model.B = pyo.Set(initialize=['left', 'right', 'center'])
and also an expression:
model.my_expression = pyo.Expression(model.A, model.B, rule=some_rule)

Is there some way to programmatically retrieve the names and order of the indexing sets (specifically, the list ['A', 'B']) from the my_expression object?

Danny Kaufman

unread,
Oct 28, 2019, 12:07:15 PM10/28/19
to Pyomo Forum
Oh!  With some more experimenting, I have found a way to get the list of indexing set names:
[s.name for s in model.my_expression._implicit_subsets]

However, if there is a better way to do this, I'd be grateful to hear it before using this too much in my code.

Siirola, John D

unread,
Oct 30, 2019, 12:53:35 PM10/30/19
to pyomo...@googlegroups.com

Right now, _implicit_subsets is probably the best thing to use.  I believe that is what Bethany is using in Pyomo.dae as well.

 

That said, we are in the middle of an overhaul of the Set system, and one of the goals is to make the APIs more consistent and to provide new public APIs that better meet user’s needs.  One of the approaches I am looking at is a “product_subsets” method, so you could do:

 

model.my_expression.index_set().subsets()

 

That said, it will expand the arguments regardless of how you created the product set.  That is:

 

                m.A = Set()

                m.B = Set()

                m.ex1 = Expression(m.A, m.B, rule=some_rule)

                m.ex1.index_set().subsets() # == [ m.A, m.B ]

                m.C = m.A * m.B

                m.ex2 = Expression(m.C, rule=some_rule)

                m.ex2.index_set().subsets() # == [ m.A, m.B ]

 

Also, because of other changes, it is *possible* that _implicit_subsets may go away in the relatively near future (although probably no sooner than January)

 

john

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyomo-forum/995e9899-52f6-4953-9d3e-1e8a059ccb4d%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages