Skip to content

Make component index sets initialized using dictionary views ordered #3627

Open
@shermanjasonaf

Description

@shermanjasonaf

Summary

When a Python dictionary view object (i.e., dict_keys, dict_values, or dict_items) is passed as an index set argument to a Pyomo component constructor, the index set of the constructed component is unordered. This may be unexpected, since: (1) dictionary view objects are (insertion) ordered as of Python 3.7, (2) Pyomo is currently tested with Python >= 3.9, and (3) component index sets derived from instances of other builtin ordered iterable types (e.g., list, str, tuple) are ordered.

Working with models that contain components with unordered index sets may result in nondeterminstic outcomes later on, particularly when solvers are used.

Steps to reproduce the issue

>>> import pyomo.environ as pyo
>>> m = pyo.ConcreteModel()
>>> # index set derived from list
>>> m.v1 = pyo.Var([1, 2, 3])
>>> print(m.v1.index_set().isordered())
True
>>> # index set derived from dict_keys
>>> m.v2 = pyo.Var({"a": 1, "b": 2, "c": 3}.keys())
>>> print(m.v2.index_set().isordered())
False

Information on your system

Pyomo version: 6.9.3dev0 (main @ a8d0bfa)
Python version: 3.12.7
Operating system: Ubuntu Linux 20.04
How Pyomo was installed (PyPI, conda, source): source
Solver (if applicable): N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions