How to handle absolute value in Gurobi constraints
OngoingI want to handle absolute value in Gurobi constraints. This is my code abc.lp. However I am not getting solution.
Maximize
v0 + v1 + v2
SubjeCt To
C1: 3 v0 + v1 + v2 <= 72
C2: 2 v0 + 3 v1 + 2 v2 <= 80
C3: abs_(v0 - v1) + abs_(v1 - v2) >= 10
Integers
v0 v1 v2
End
-
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 Santanu,
Did you write this LP file from scratch? If so, I don't recommend doing this; it's very error-prone, and the LP format isn't standardized. If you want to use algebraic modeling syntax to interact with Gurobi, you may want to look at AMPL or GAMS. Gurobi also supports APIs in several programming languages, so you can build this model in Python, C++, MATLAB, etc.
One LP file that models this problem is below. We introduce auxiliary variables \( y_0 = v_0 - v_1 \) and \( y_1 = v_1 - v_2 \). Then, we use variables \( z_0 = |y_0| \) and \( z_1 = |y_1| \) to represent the corresponding absolute value terms.
\ abc.lp
Maximize
v0 + v1 + v2
Subject To
R0: 3 v0 + v1 + v2 <= 72
R1: 2 v0 + 3 v1 + 2 v2 <= 80
R2: - v0 + v1 + y0 = 0
R3: - v1 + v2 + y1 = 0
R4: z0 + z1 >= 10
Bounds
Generals
v0 v1 v2
General Constraints
GC0: z0 = ABS ( y0 )
GC1: z1 = ABS ( y1 )
EndThanks!
Eli
0 -
Thank you so much for your help.
0 -
Hi,
The absolute function in constraints works well in MATLAB +YALMIP. However, when I am trying to use the same constraint in PYOMO, Gurobi is not able to solve it. It shows non-linearity in the constraint. Has anyone encountered the same problem?
0
Post is closed for comments.
Comments
4 comments