Defining a nested dict of LP variables with different "depths" along one direction

361 views
Skip to first unread message

Camille Hamon

unread,
May 6, 2020, 2:18:33 AM5/6/20
to pulp-or-discuss
Hi,

I am using a LP variable dict which should have 2 dimensions. The second dimension does not have the same depth for all elements. For example, if I call X that variable, X[0][] may have four elements X[0][0], ... , X[0][3], while X[1][] only has 2 X[1][0] and X[1][1]. The reason for this is due to my problem structure where the second dimension represents a number that is intrinsic to what the first dimension represents.

I have modelled this by using tuple indices so my variable X has the following structure: X_(0,_0), X_(0,_1),X_(0,_2), X_(0,_3), X_(1,_0) and X_(1,_1).
I would like instead to use a nested dict for X so I can use the notation X[0][0], X[1][0], ... to access the elements in X. But I haven't figured out how to define such an X in PuLP.
Does anyone know how to do this?

Sean Grogan

unread,
May 6, 2020, 9:25:05 AM5/6/20
to pulp-or-discuss


One way would be to assign a LpVariable to each dictionary index, e.g.

#Given your 2d dict "X_input"
for i, J in X_input.items():
   
for j in J:
        X
[i][j] = LpVariable(name, lowBound=None, upBound=None, cat='Continuous', e=None)


And you'll be able to access the variables as X[i][j]

Or if you have a list of 'arcs' that are a tuple (i, j) 

# Given a list of "arcs", Variable name

X = LpVariable.dicts(name, arcs, lowBound=None, upBound=None, cat='Continuous', indexStart=[])


And then you'll access the varaibles as X[(i, j)]

Stuart Mitchell

unread,
May 6, 2020, 5:51:53 PM5/6/20
to pulp-or...@googlegroups.com
I think the 'arcs' formulation is the way to go.

However I prefer x[i,j] which can be used in stead of x[(i,j)]

This is because in python the , makes a tuple not the ()

--
New posters to this group are moderated which can take up to 48 hours, so please be patient if your first post takes a while to turn up.
---
You received this message because you are subscribed to the Google Groups "pulp-or-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pulp-or-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pulp-or-discuss/addb2211-d729-48f7-83a5-565e65fd948f%40googlegroups.com.

Camille Hamon

unread,
May 10, 2020, 2:16:11 PM5/10/20
to pulp-or-discuss
Thanks both for your answers. I will try it next week.
To unsubscribe from this group and stop receiving emails from it, send an email to pulp-or...@googlegroups.com.

Camille Hamon

unread,
May 13, 2020, 3:57:16 AM5/13/20
to pulp-or-discuss
Just a quick update that your solution works very well. Thanks again!
Reply all
Reply to author
Forward
0 new messages