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

LpVariable dicts issue parsing indices of the form [(a1, b1, c1), (a2, b2, c2), ..., (an, bn, cn)] #722

Open
hdelrio opened this issue Jan 12, 2024 · 2 comments

Comments

@hdelrio
Copy link

hdelrio commented Jan 12, 2024

Details for the issue

When creating a collection of LpVariables using the class method dicts, using an index that is a list of tuples in order to emulate a matrix it only work when the name does not contain a '%'

What did you do?

i1 = list(range(10))
i2 = list('abcdefghij')
i3 = 'jan feb mar apr may jun jul aug sep oct'.split()
indices = [(a, b, c) for a in i1 for b in i2 for c in i3]

P = pl.LpVariable.dicts('P', indices=indices)
P[0, 'a', 'jan']

What did you expect to see?

I was expecting to see: P_(0, 'a', 'jan')

What did you see instead?

P_(0,'a','jan')

Which is a weird looking name

@pchtsp
Copy link
Collaborator

pchtsp commented Jan 13, 2024

I'm not sure if the formatting from github is changing the output. Because I see the same variable name...

@urbanophile
Copy link
Contributor

urbanophile commented Sep 8, 2024

I think their example is an issue with the github markdown formatting. The variable name is

In [7]: P[9,'j','oct'].name
Out[7]: "P_(9,_'j',_'oct')"

from this

In [1]: from pulp import *

In [2]: i1 = list(range(10))
   ...: i2 = list('abcdefghij')
   ...: i3 = 'jan feb mar apr may jun jul aug sep oct'.split()
   ...: indices = [(a, b, c) for a in i1 for b in i2 for c in i3]
   ...: 
   ...: P = LpVariable.dicts('P', indices=indices)

In [3]: P
Out[3]: 
{(0, 'a', 'jan'): P_(0,_'a',_'jan'),
 (0, 'a', 'feb'): P_(0,_'a',_'feb'),
 (0, 'a', 'mar'): P_(0,_'a',_'mar'),
 (0, 'a', 'apr'): P_(0,_'a',_'apr'),
 (0, 'a', 'may'): P_(0,_'a',_'may'),

Either a little more logic could be added to handle the construction of the variable names here :

name += "_%s" * len(indices)

Or the docstring could amend with more info. Happy to make a pull request.

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

No branches or pull requests

3 participants