Skip to content

Commit

Permalink
_component_data_iter should sort only by indices; add test for #1845
Browse files Browse the repository at this point in the history
  • Loading branch information
jsiirola committed Mar 9, 2021
1 parent c3946a0 commit 577ec55
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pyomo/core/base/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,7 @@ def _component_data_iter(self, ctype=None, active=None, sort=False):
_items = ((None, comp),)

if _sort_indices:
_items = sorted_robust(_items)
_items = sorted_robust(_items, key=itemgetter(0))
if active is None or not isinstance(comp, ActiveIndexedComponent):
for idx, compData in _items:
yield (name, idx), compData
Expand Down
9 changes: 9 additions & 0 deletions pyomo/core/tests/unit/test_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,15 @@ def test_Block(self):
self.assertEqual(sorted([id(comp) for comp in model.block_data_objects(sort=False)]),
sorted([id(comp) for comp in [model,]+model.component_data_lists[Block]]))

def test_mixed_index_type(self):
m = ConcreteModel()
m.I = Set(initialize=[1,'1',3.5,4])
m.x = Var(m.I)
v = list(m.component_data_objects(Var, sort=True))
self.assertEqual(len(v), 4)
for a,b in zip([m.x[1], m.x[3.5], m.x[4], m.x['1']], v):
self.assertIs(a, b)


class HierarchicalModel(object):
def __init__(self):
Expand Down

0 comments on commit 577ec55

Please sign in to comment.