KeyError for dict

890 views
Skip to first unread message

Ahmet Emre Aladağ

unread,
Nov 25, 2009, 4:35:14 PM11/25/09
to pulp-or...@googlegroups.com
Hi,

I am trying to solve a problem but facing some errors. I don't have much knowledge about LP so I couldn't figure out where my mistake is.

Here's a simple case where I face this error: http://pastebin.com/m5dcc26fd
You can see both the problematic and working prob set.

# Problematic
prob += lpSum( [ vars[u] - vars[v] for (u,v) in ((1,2), (2,3), (3,4)) ] )

Traceback (most recent call last):
  File "whis.py", line 16, in <module>
    print prob.variables
  File "/usr/lib/python2.6/site-packages/pulp/pulp.py", line 919, in __repr__
    string += repr(self.objective) +"\n"
  File "/usr/lib/python2.6/site-packages/pulp/pulp.py", line 551, in __repr__
    l = [str(self[v]) + "*" + str(v) for v in self]
  File "/usr/lib/python2.6/site-packages/pulp/odict.py", line 426, in __getitem__
    return dict.__getitem__(self, key)
KeyError: Var3

 
# Works Fine:
prob += lpSum( [ vars[u] - vars[v] for (u,v) in ((1,2), (3,5), (3,4)) ] )


P.S. How can I take square of LP variables? I get TypeError: unsupported operand type(s) for ** or pow(): 'LpAffineExpression' and 'int'
 
when I say 

prob += lpSum( [ (vars[u] - vars[v])**2 for (u,v) in ((1,2), (2,3), (3,4)) ] )

Thank you very much,
Sorry for the novice questions.

--
Ahmet Emre Aladağ
http://www.emrealadag.com

Stu

unread,
Nov 25, 2009, 5:39:37 PM11/25/09
to pulp-or-discuss
Hmm are you trying to fit a least squared model here?

First up there is a bug in pulp you have discovered because
>>> [ vars[u] - vars[v] for (u,v) in ((1,2), (2,3), (3,4)) ]
[1*Var1 + -1*Var2 + 0, 1*Var2 + -1*Var3 + 0, 1*Var3 + -1*Var4 + 0]

And when you sum this Var3 disappears therefore the error :-(
I will put in a fix to this soon

However, as pulp currently does not solve non-linear problems the
following will not work
>>> prob += lpSum( [ (vars[u] - vars[v])**2 for (u,v) in ((1,2), (2,3), (3,4)) ]

as the (vars[u] - vars[v])**2 term is non-linear

however you can use some extra variables to achieve a similar result
to the following (this code will not work but is for illustrative
purposes) This is a 1-norm
>>> prob += lpSum( [ abs(vars[u] - vars[v]) for (u,v) in ((1,2), (2,3), (3,4)) ]

Please note in the code below I would prefer to use LpVariable.matrix
() and LpVariable.dicts() to create the variables rather then the loop

------------------------------

from pulp import *

prob = LpProblem("Minimize absolute difference 1-norm", LpMinimize)

vars = []
for i in range(10):
vars.append(LpVariable("Var%d" % i))
print vars[i]

pairs = ((1,2), (2,3), (3,4))
pairvars = {}
for pair in pairs:
pairvars[pair] = (LpVariable("PairVar(%s,%s)"%pair))
print pairvars[pair]

# objective
prob += lpSum( [ pairvars[pair] for pair in pairs] )

# constraints:
for (u,v) in pairs:
prob += vars[u] - vars[v] <= pairvars[(u,v)]
prob += vars[v] - vars[u] <= pairvars[(u,v)]

print prob
prob.solve()
print "Status:", LpStatus[prob.status]

Ahmet Emre Aladağ

unread,
Nov 26, 2009, 5:24:14 PM11/26/09
to pulp-or...@googlegroups.com
Thank you very much for your quick response.

What I'm trying to do is a horizontal coordinate assignment of layered graph nodes. Two objective function alternatives were given and I was trying to perform the simplest one, where I want the nodes in the adjacent layers to have same (or close) x coordinates so that they look vertically aligned. So keeping sum [ square or abs of (x[u] - x[v]) ] minimum would give the best result.

Thanks for the tips and this great utility, I'm looking forward for the fix.

Stu

unread,
Nov 26, 2009, 7:32:22 PM11/26/09
to pulp-or-discuss
Please have a look at the documentation for graphviz as they detail
the optimisation model that they use to solve this problem.

http://www.graphviz.org/Documentation/TSE93.pdf

Stu


On Nov 27, 11:24 am, Ahmet Emre Aladağ <aladage...@gmail.com> wrote:
> Thank you very much for your quick response.
>
> What I'm trying to do is a horizontal coordinate assignment of layered graph
> nodes. Two objective function alternatives were given and I was trying to
> perform the simplest one, where I want the nodes in the adjacent layers to
> have same (or close) x coordinates so that they look vertically aligned. So
> keeping sum [ square or abs of (x[u] - x[v]) ] minimum would give the best
> result.
>
> Thanks for the tips and this great utility, I'm looking forward for the fix.
>

suhail....@gmail.com

unread,
Nov 26, 2014, 5:35:11 AM11/26/14
to pulp-or...@googlegroups.com
i am getting error at this following line
 prob += lpDot(d[j], x) + s[j] - w[j] == lpSum(d[j])/2

Stuart Mitchell

unread,
Nov 26, 2014, 6:35:33 PM11/26/14
to pulp-or...@googlegroups.com
What error is it?

if you are using python 3 there is a bug with lpAffineExpression and division

so use this
 prob += lpDot(d[j], x) + s[j] - w[j] == lpSum(d[j]/2.0)

instead


--
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 post to this group, send email to pulp-or...@googlegroups.com.
Visit this group at http://groups.google.com/group/pulp-or-discuss.
For more options, visit https://groups.google.com/d/optout.



--
Stuart Mitchell
PhD Engineering Science
Extraordinary Freelance Programmer and Optimisation Guru

suhail....@gmail.com

unread,
Nov 27, 2014, 12:06:45 AM11/27/14
to pulp-or...@googlegroups.com
# Constraints

d = [[random.randint(0,D) for i in range(n)] for j in range(m)]

for j in range(m):

prob += pulp.lpDot(d[j], x) + s[j] - w[j] == pulp.lpSum (d[j]/2)


for this i m getting error.....TypeError: unsupported operand type(s) for /: 'list' and 'int'

Stuart Mitchell

unread,
Nov 27, 2014, 5:10:18 PM11/27/14
to pulp-or...@googlegroups.com
right sorry I didn't realize that d[j] is a list

try

 prob += lpDot(d[j], x) + s[j] - w[j] == lpSum(d[j]) * 0.5

the fix for this will be in the next release

Stu

suhail....@gmail.com

unread,
Nov 28, 2014, 7:01:07 AM11/28/14
to pulp-or...@googlegroups.com
thanks it worked..
Reply all
Reply to author
Forward
0 new messages