Why I can't get the shadow price (dual value) from Gurobipy while the model is feasible?
Awaiting user inputI'm trying to solve peer to peer market optimization problem. Since the price of each trade between different participants can be different, I want to draw the price of each trade. And the problem should be a continuous model, because the trade amount can be at any level.
The model already works fine without problem, I can get the trade amount of each trade using like model.variables.trade[t,k,y].x
.
But the problem is when I try to get the shadow price from the related constraints, it shows : GurobiError: Unable to retrieve attribute 'Pi'
.
I can't find out how comes this error since the model is already feasible, and what I do is get an attribute which already exists.
I guess it's not a LP because the obj is a quadratic one like min (a*x^2 + b * x + c)
, but all the constraints are in LP form. The variables used in constraints are continuous and can be at any level.
The related part of my code shows as follows:
def _build_constraints(self):
m = self.model
P_nm3 = self.variables.P_nm3
agents = self.data.agents
timewindow = self.data.timewindow
windowinterval = self.data.windowinterval
budget = self.data.budget
P_n = self.variables.P_n
self.constraints.pnmmatch = {}
......
for t in self.data.interval:
for i in range(len(P_nm3[t])):
for j in range(len(P_nm3[t][0])):
self.constraints.pnmmatch[t,i,j] = m.addConstr(
P_nm3[t][i][j],
gb.GRB.EQUAL,
-P_nm3[t][j][i]
)
.......
poolmarket = marketclear()
poolmarket.optimize()
print(poolmarket.constraints.pnmmatch[0,1,6])
print(type(poolmarket.constraints.pnmmatch))
print(poolmarket.constraints.pnmmatch[0,1,6].pi)
shadow_price = poolmarket.model.getAttr(gb.GRB.Attr.Pi)
The result is:
<gurobi.Constr R1001>
<class 'dict'>
AttributeError: Unable to retrieve attribute 'pi'
both poolmarket.constraints.pnmmatch[0,1,6].pi
or poolmarket.model.getAttr(gb.GRB.Attr.Pi)
doesn't work. If I comment on the last two lines about pi, the models works pretty fine.
But in a simpler similar model where the constraint is:
for t in self.data.interval:
for i in range(len(P_nm3[t])):
for j in range(len(P_nm3[t][0])):
self.constraints.pnmmatch[t,i,j] = m.addConstr(
P_nm3[t][i][j],
gb.GRB.EQUAL,
-P_nm3[t][j][i]
)
Then I'm able to draw the pi value with poolmarket.constraints.pnmmatch[0].pi
command.
How can I solve this?
Thanks in advance!
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
I've fixed this problem but come up with a new one now. I post it in the nest post.
0 -
Could you tell me how did you fixed this problem?
0 -
Tianyang, I am happy to hear you were able to resolve your issue.
Ying,
It sounds like you may be having the same issue. Here are a couple of things that may help you:
- Pi values are only available for convex, continuous problems. If you are solving a MIP, Gurobi does not provide dual values for an integer program because shadow prices are not well-defined for these types of problems. However, if you would like to understand the sensitivity of your problem to bound/constraint limits, please check out How do I retrieve the (dual) Pi values for a MIP problem?
- If you are solving a QCP, there are a couple of extra steps to get your duals. To get the duals of linear constraints, set the QCPDual parameter is set to 1. To get the duals for quadratic constraints, you will want to use QCPi attribute rather than the Pi attribute.
If this doesn't help, can you tell me a bit more about your model and how you are trying to access Pi values?
0
Post is closed for comments.
Comments
4 comments