Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug in bigm transformation with set objects #1101

Closed
wants to merge 2 commits into from
Closed

bug in bigm transformation with set objects #1101

wants to merge 2 commits into from

Conversation

michaelbynum
Copy link
Contributor

Summary/Motivation:

When creating a constraint using the index set of another constraint combined with another set, the index set of the original constraint gets modified unintentionally. See the following example:

>>> import pyomo.environ as pe
>>> m = pe.ConcreteModel()
>>> m.a = pe.Set(initialize=[1,2,3])
>>> m.b = pe.Set(initialize=['a', 'b'])
>>> m.c = pe.Constraint(m.a, m.b)
>>> m.c.index_set().pprint()
c_index : Dim=0, Dimen=2, Size=6, Domain=None, Ordered=False, Bounds=None
    Virtual
>>> for i in m.c.index_set():
...     print(i)
...
(1, 'a')
(1, 'b')
(2, 'a')
(2, 'b')
(3, 'a')
(3, 'b')
>>> m.c2 = pe.Constraint(m.c.index_set(), m.b)
>>> m.c.index_set().pprint()
c_index : Dim=0, Dimen=2, Size=12, Domain=None, Ordered=False, Bounds=None
    Virtual
>>> for i in m.c.index_set():
...     print(i)
...
(1, 'a', 'a')
(1, 'a', 'b')
(1, 'b', 'a')
(1, 'b', 'b')
(2, 'a', 'a')
(2, 'a', 'b')
(2, 'b', 'a')
(2, 'b', 'b')
(3, 'a', 'a')
(3, 'a', 'b')
(3, 'b', 'a')
(3, 'b', 'b')

This creates a bug in the BigM transformation in pyomo.gdp. This PR fixes this problem by wrapping the original index set in a list() when creating the new constraint.

This is may not be the correct fix. We may need a fix in the Constraint constructor or somewhere in Sets. However, the new Set rewrite may fix this problem. @jsiirola What is your suggestion on how to proceed? Is this the correct fix until the set rewrite is complete or should I go ahead and try to fix the underlying problem?

Legal Acknowledgement

By contributing to this software project, I agree to the following terms and conditions for my contribution:

  1. I agree my contributions are submitted under the BSD license.
  2. I represent I am authorized to make the contributions and grant the license. If my employer has rights to intellectual property that includes these contributions, I represent that I have received permission to make contributions and grant the required license on behalf of that employer.

@jsiirola
Copy link
Member

This is a duplicate of #134. The set-rewrite (#326) does indeed fix this bug. While this technically works around #134, the better solution is to fix _SetProduct - which now that I look into it, I don't think is a bad patch:

diff --git a/pyomo/core/base/sets.py b/pyomo/core/base/sets.py
index 4e27e98..1fe17af 100644
--- a/pyomo/core/base/sets.py
+++ b/pyomo/core/base/sets.py
@@ -1545,7 +1545,7 @@ class _SetProduct(_SetOperator):
         _SetOperator.__init__(self, *args, **kwd)
         # the individual index sets definining the product set.
         if isinstance(self._setA,_SetProduct):
-            self.set_tuple = self._setA.set_tuple
+            self.set_tuple = list(self._setA.set_tuple)
         else:
             self.set_tuple = [self._setA]
         if isinstance(self._setB,_SetProduct):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants