How to retrieve the dual variable value for a linear constraint in a SOCP problem
AnsweredHi,
I have a second order cone problem with some linear constraints, I am trying to get the values for the dual variables corresponding to the linear constraints, my parameters for the models are
model = gp.Model()
model.Params.LogToConsole = 0
model.setParam('QCPDual', 1)
for c in CC:
print(c.constrName)
print(c.Pi)
File "src\gurobipy\constr.pxi", line 67, in gurobipy.Constr.__getattr__
File "src\gurobipy\constr.pxi", line 95, in gurobipy.Constr.getAttr
File "src\gurobipy\attrutil.pxi", line 100, in gurobipy.__getattr
AttributeError: Unable to retrieve attribute 'Pi'

Hi Fei Wang,
The attributes Pi and QCPi can only be queried for convex, continuous models. Could you please confirm that your model is convex and continuous?
Could you please verify that your model is feasible? In the case of infeasibility, attempting to access Pi values can result in the error message "AttributeError: Unable to retrieve attribute 'Pi'. Please check the status of the model at the end of optimization by querying the value of model.status. The status values are described in the Status Code section of our reference manual.
Regards,
Simran
0 
Hello Simranjit,
Yes, my model is convex and continuous. In fact it is a second order cone optimization problem. I am solving this problem in a branch and bound algorithm, and I can always solve this problem in the branch and bound algorithm
I checked my model, it is feasible and the objective value is 2.8614659442815887e08.
model.status shows 2I print my model, and here are some of the constraints:
T6: yt[0] + yt[1] + yt[2] + yt[3] + yt[4] = 1
T7: yt[5] + yt[6] + yt[7] + yt[8] + yt[9] = 1
T8: yt[10] + yt[11] + yt[12] + yt[13] + yt[14] = 1
T9: yt[15] + yt[16] + yt[17] + yt[18] + yt[19] = 1
T10: yt[20] + yt[21] + yt[22] + yt[23] + yt[24] = 1
T11: yt[25] + yt[26] + yt[27] + yt[28] + yt[29] = 1
T12: yt[30] + yt[31] + yt[32] + yt[33] + yt[34] = 1The second order cone constraints look like this:
General Constraints
Then I run the following code
GC0: de[0] = NORM ( 2 ) ( w[0,0] , w[0,1] )
GC1: de[1] = NORM ( 2 ) ( w[1,0] , w[1,1] )
GC2: de[2] = NORM ( 2 ) ( w[2,0] , w[2,1] )
GC3: de[3] = NORM ( 2 ) ( w[3,0] , w[3,1] )L = model.getConstrByName('T6')
and it produces the error
L.Pi
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Community Edition 2022.3.1\plugins\pythonce\helpers\pydev\_pydevd_bundle\pydevd_exec2.py", line 3, in Exec
exec(exp, global_vars, local_vars)
File "<input>", line 1, in <module>
File "src\gurobipy\constr.pxi", line 67, in gurobipy.Constr.__getattr__
File "src\gurobipy\constr.pxi", line 95, in gurobipy.Constr.getAttr
File "src\gurobipy\attrutil.pxi", line 100, in gurobipy.__getattr
AttributeError: Unable to retrieve attribute 'Pi'1 
Hi Fei Wang,
Thanks for the further information about your model.
As mentioned in our documentation, adding a simple general constraint like the norm to an otherwise continuous model will transform it into a MIP. To retrieve the dual values for a MIP model, please refer to our KB article "How do I retrieve the (dual) Pi values for a MIP problem?".
Best regards,
Simran
0 
Thank you for the answer. But I don't really understand, in my model all variables are continuous, even I added some norm constraints, they are still continuous (no integer variables in my model). So why does my model become a mixed integer programming model?
I tried the fixed method, here is the code, but it still produces the same error
fixed = model.fixed()
CC = model.getConstrs()
for c in CC:
print(c.constrName)
print(model.getRow(c))
print(c.RHS)
print(c.Pi)0
Please sign in to leave a comment.
Comments
4 comments