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

Incorrect guess for Set.dimen for nested initialize kwarg + proposed fix #637

Closed
mlangiu opened this issue Jul 31, 2018 · 1 comment
Closed

Comments

@mlangiu
Copy link

mlangiu commented Jul 31, 2018

When guessing the dimension in sets.py: Set.init from the initialize kwarg, nesting is ignored.
However when accessing keys they are flattened. This results in incorrect guesses for cases such as this:

constraints = {c for c in itertools.product(['constrA', 'constrB'], range(5))}
vars = {v for v in itertools.product(['var1', 'var2', 'var3'], range(5))}
matrix_coefficients = {m for m in itertools.product(constraints, vars)}
m = ConcreteModel()
m.Matrix = Param(matrix_coefficients, default=0)

>>> ValueError: The value=('constrB', 4, 'var1', 1) does not have dimension=2, which is needed for set=Matrix_index

The code that is causing this sits in sets.py lines 691-695:

if type(self.initialize) is tuple:
    tmp = len(self.initialize)
elif type(self.initialize) is list and len(self.initialize) > 0 \
         and type(self.initialize[0]) is tuple:
    tmp = len(self.initialize[0])

One way to amended this is to flattening the entire self.initialize before checking with the current method or flattening the single element used for guessing, e.g. with pyutilib.misc.flatten.
If we're considering that nested containers other than tuple might occur, getattr seems more adequate than a direct len call.
Finally looking over init it seems like lines 691 and 692 are dead code, as tuples are already converted to lists in lines 682-685!

In summary it seems that changeing lines 691-695 to:

if type(self.initialize) is list and len(self.initialize) > 0:
    tmp = getattr(flatten(self.initialize[0]), 'len', 0)

should fix this behaviour.

@blnicho
Copy link
Member

blnicho commented Jul 31, 2018

I'm going to add this to the list of known Set issues in #326

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

No branches or pull requests

2 participants