Question on obtaining dual solution of LP

5,131 views
Skip to first unread message

Yonghan Feng

unread,
Aug 6, 2014, 1:34:42 AM8/6/14
to gur...@googlegroups.com
Hi All,

I have a question about getting dual solutions of LP using Python and Gurobi.


I tried

solution = Constr.getAttr('Pi')
or 
solution = Var.getAttr('Pi')

to get dual solution of LP, but they do not work. 

Error message said "File "constr.pxi", line 58, in gurobipy.Constr.getAttr (../../src/python/gurobipy.c:11348)
TypeError: getAttr() takes exactly %Id positional argument%s (%Id given)".

Is there anyone who can teach me on how to get dual variables of LP? If you can give me an simple example, that will be very helpful.

Thank you so much!

Best regards,

Yonghan Feng


Sonja Mars

unread,
Aug 6, 2014, 2:54:03 AM8/6/14
to gur...@googlegroups.com
Hi,

> Following http://www.gurobi.com/documentation/5.6/reference-manual/attributes,
This page states:

Python: var.getAttr(GRB.attr.lb), or just var.lb

So Constr.getAttr('Pi’) will not work. However,
constr.Pi
or
constr.getAttr(GRB.attr.Pi)
will return the dual value (if constr is a gurobi constraint).

Additionally, please note there is no attribute Pi for variables.

Best regards,
Sonja

-----------------------------------------------------------------
Dr. Sonja Mars
Gurobi Optimization

Sonja Mars

unread,
Aug 6, 2014, 10:58:28 AM8/6/14
to gur...@googlegroups.com
Hi,

Sorry, my post earlier was not correct:

First note: Pi is only available for continuous models after optimize() has been called.

> solution = Constr.getAttr('Pi’)
This should work fine. If Constr is one Gurobi constraint object (and not a list of Gurobi constraints).

> or
> solution = Var.getAttr('Pi’)
This should give you the following error:
gurobipy.GurobiError: Attribute pi is not a Var attribute

> Error message said "File "constr.pxi", line 58, in gurobipy.Constr.getAttr (../../src/python/gurobipy.c:11348)
> TypeError: getAttr() takes exactly %Id positional argument%s (%Id given)".

This looks strange, which Gurobi version are you using? What is your OS? And what kind of Python are you running?

Thanks,
Sonja

Yonghan Feng

unread,
Aug 6, 2014, 11:50:17 AM8/6/14
to gur...@googlegroups.com
Hi Sonja,

Thank you! I am running Python 3.2 with Gurobi 5.6.2 on Windows 7. 

在 2014年8月6日星期三UTC-5上午9时58分28秒,Sonja Mars写道:

Sonja Mars

unread,
Aug 7, 2014, 2:41:42 AM8/7/14
to gur...@googlegroups.com
Hi,

Try updating to Gurobi 5.6.3.

I just tried Python 3.2.5 on a Win7 machine with Gurobi 5.6.2 and everything works fine.

Here is what I did:
opened a Python 3.2.5 Shell
entered the following:

from gurobipy import *
m=read(“model.mps”)
m.optimize()
cons=m.getConstrs()
cons[0].getAttr(“Pi")

Please try this, if it is still not working, please send a screenshot of what you did and what happened.

Sonja

Yonghan Feng

unread,
Aug 7, 2014, 4:36:57 PM8/7/14
to gur...@googlegroups.com
Hi Sonja,

I tried another constr.Pi again, and then it works, where constr represents a constraint.
Thank you so much!

Sincerely, 

Yonghan Feng

在 2014年8月7日星期四UTC-5上午1时41分42秒,Sonja Mars写道:
Reply all
Reply to author
Forward
0 new messages