How to add a constraint which is the absolute value is smaller than a constant?
AnsweredHi,
I have a question about the gp.abs_() function. I am now adding a constraint which contains a absolute value and a float constant. Here is an example:
suppose that I have two variables x and y, which are added through m.addVar()
- variables: x, y
- constraint: |x - y| < 0.7
I tried to do
- model.addConstr(gp.abs_(x - y) < 0.7)
However, I got the error due to the mismatch of the types for comparison. How can I solve this problem? Thank you.
-
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?. -
Hi Hanxiong,
The abs_() function is used to set one variable equal to the absolute value of another. You should first define an auxiliary variable and set it equal to \( x - y \). Then, introduce another auxiliary variable and set it equal to \( | x - y| \) using \( \texttt{abs_()} \). E.g.:
x = model.addVar(name="x")
y = model.addVar(name="y")
u = model.addVar(lb=-gp.GRB.INFINITY, name="u")
v = model.addVar(ub=0.7, name="v")
model.addConstr(u == x - y)
model.addConstr(v == gp.abs_(u))This is discussed more in this forum post.
Also, note that strict inequalities like \( < \) are not supported by Gurobi.
Thanks,
Eli
0
Post is closed for comments.
Comments
2 comments