How to handle Abs constraints ?
I have a abs constraint in Gurobi. I have look into the manual of official guide. It provides two ways to add abs constraints.
# x5 = abs(x1)
model.addGenConstrAbs(x5, x1, "absconstr")
# overloaded form
model.addConstr(x5 == abs_(x1), name="absconstr")
I have to use the second form, because I need to draw the dual value of this constraint. Only addConstr can get the shadow price(dual value), so addGenConstrAbs should be avoided.
My code is:
def _build_constraints(self):
m = self.model
com = self.data.com
P_nm3 = self.variables.P_nm3
agents = self.data.agents
timewindow = self.data.timewindow
windowinterval = self.data.windowinterval
absP_n = self.variables.absP_n
P_n = self.variables.P_n
self.constraints.absv = {}
for t1 in timewindow:
for t2 in windowinterval:
for i, p in enumerate(agents):
if p in self.data.generators:
pass
else:
# self.constraints.absv[t1,t2,p] = m.addGenConstrAbs( absP_n[p,t1,t2], P_n[p,t1,t2])
self.constraints.absv[t1,t2,p] = m.addConstr( absP_n[p,t1,t2] == abs_(P_n[p,t1,t2]), name="absconstr" )
And I get the error like this :
AttributeError: 'gurobipy.Model' object has no attribute 'abs_'
If I use the first method,
self.constraints.absv[t1,t2,p] = m.addGenConstrAbs( absP_n[p,t1,t2], P_n[p,t1,t2])
I will get errors like :
AttributeError: 'gurobipy.GenConstr' object has no attribute 'pi'
I'm using the Python API of Gurobi 9.0.
It seems the method mentioned doesn't work at all.
How can I solve this?
Thanks a lot!
-
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 had the same problem using python gurobi. I had to used the firt option you said.
model.addGenConstrAbs (x5, x1, "absconstr" )
0 -
Note that the difference between Model.addGenConstrAbs() and Model.addConstr() + abs_() is purely syntax. In both cases, you add a general constraint to the problem. There are no dual values (Pi attributes) associated with general constraints.
For example:
>>> c = m.addConstr(y == abs_(x))
>>> type(c)
<class 'gurobipy.GenConstr'>0
Post is closed for comments.
Comments
3 comments