The abs_ function is part of the Gurobi Python API and does not work in an LP file. (When your LP file is read, the solver interprets your C3 constraint as a linear constraint with four variables with names abs_(v0, v1), abs_(v1, and v2).)
Also, note that absolute value constraints can only take the form var1 = abs_(var2). So you will have to define some auxiliary variables to model the constraint above.
In the Python API, you could model the constraint C3 as follows:
a = model.addVars(4, name="a") # auxiliary variables
model.addConstr(a[0] == v0 - v1)
model.addConstr(a[1] == v1 - v2)
model.addConstr(a[2] == abs_(a[0]))
model.addConstr(a[3] == abs_(a[1]))
model.addConstr(a[2] + a[3] >= 10)
In the LP file, you can see the absolute value constraint in the General Constraints section:
General Constraints
GC0: a[2] = ABS ( a[0] )
GC1: a[3] = ABS ( a[1] )
Answer from Silke Horn on Stack OverflowGurobi
support.gurobi.com › hc › en-us › community › posts › 23001326746001-Gurobi-Absolute-Value-Constraints
Gurobi- Absolute Value Constraints – Gurobi Help Center
Can I put the bound on the precision of the variable so that it only takes the desired values after the decimal places for solving the model? e.g x= 2.9001000000000015 but if I have to allow this to take only up to 7 decimal places, like x = 2.9001001? can it be possible to set the precision of the variable in Gurobi? Unfortunately, it is currently not possible to set individual variable tolerances. ... Re-optimize and check whether constraint violations improved without degrading the optimal solution value
Gurobi
support.gurobi.com › hc › en-us › community › posts › 360054687091-How-to-handle-absolute-value-in-Gurobi-constraints
How to handle absolute value in Gurobi constraints – Gurobi Help Center
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 ) End ... Thank you so much for your help. ... 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.
Gurobi
support.gurobi.com › hc › en-us › community › posts › 360073645412-Abs-Value
Abs Value – Gurobi Help Center
The abs_() function can only be used to set one decision variable equal to the absolute value of another. E.g.: ... You unfortunately can't use abs_() in any other type of constraint, like you try to do in the first example.
Gurobi
support.gurobi.com › hc › en-us › community › posts › 360071098332-How-to-add-a-constraint-which-is-the-absolute-value-is-smaller-than-a-constant
How to add a constraint which is the absolute value is smaller than a constant? – Gurobi Help Center
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. 0 · 2 comments · Sort by Date Votes · Official comment · Simranjit Kaur · Gurobi Staff ·
Gurobi
support.gurobi.com › hc › en-us › community › posts › 360075131851-absolute-value-constraint-on-vectorised-variable-setting
absolute value constraint on vectorised variable setting – Gurobi Help Center
These Var objects can be used in general constraint functions. E.g.: import gurobipy as gp m = gp.Model() x = m.addMVar(3) y = m.addMVar(3) # MVar objects are not currently integrated with general constraints # m.addConstr(y == gp.abs_(x)) does not work # Use corresponding Var objects instead for vx, vy in zip(x.tolist(), y.tolist()): m.addConstr(vy == gp.abs_(vx))
Gurobi
support.gurobi.com › hc › en-us › community › posts › 360060405611-Abs-value-constraint-for-MLinExpr
Abs value constraint for MLinExpr – Gurobi Help Center
Depending on the values of the two matrices, the minimal value of the loss may be 0 (with x=0) or negative. I previosly tried to solve the optimization problem in Matlab using the Yalmip wrapper and a nonconvex solver (bmibnb), which works as expected. Now i have problems solving it in Python. Since the 1-Norm is not supported in the API, I try to model it as a sum of absolute values.
Gurobi
docs.gurobi.com › projects › optimizer › en › current › reference › python › func_utility.html
Python General Constraint Helper Reference - Gurobi Optimizer Reference Manual
Gurobi general constraint helper functions - used in conjunction with overloaded operators and Model.addConstr or Model.addConstrs to build general constraints. abs_(argvar)# Used to set a decision variable equal to the absolute value of another decision variable.
Gurobi
docs.gurobi.com › projects › optimizer › en › current › concepts › modeling › constraints.html
Constraints - Gurobi Optimizer Reference Manual
The L-infinity norm is equal to the maximum absolute value of any operand. The L2 norm is equal to the square root of the sum of the squares of the operands. The L0 norm counts the number of non-zero values among the operands. Regarding the L2 norm, one obvious complication comes from the fact ...
Gurobi
support.gurobi.com › hc › en-us › community › posts › 360074428532-How-to-handle-absolute-value-in-objective-function-
How to handle absolute value in objective function? – Gurobi Help Center
November 3, 2020 - A very detailed description of how to formulate the minimization of the absolute function can be found in the stackexchange post Converting absolute value program into linear program. Regarding the formulation \((a \cdot b - c) \cdot (a \cdot b - c)\). It is possible to formulate it with Gurobi, if you introduce an auxiliary variable \(z\) and the equality constraint \[ z = a \cdot b.\] Your problem would then read
Google Groups
groups.google.com › g › gurobi › c › OGc1cf3QCXQ
absolute value in objective
May 10, 2017 - You need to introduce an auxiliary variable and add a general constraint. So, instead of writing min |x - y| you would write min z z = |x - y| with the constraint being the ABS constraint, see the reference that Amal mentioned.
Gurobi
support.gurobi.com › hc › en-us › community › posts › 12626765833745-Absolute-value-modelling
Absolute value modelling – Gurobi Help Center
February 1, 2023 - for i in range(n1-1): if x[i] >= 0: constraints2 = model.addConstr(y[i] == x[i]) elif x[i] <= 0: constraints3 = model.addConstr(y[i] == -x[i]) I am trying to get the absolute value of the decision variable x, for the objective of minimizing the sum of the x values. However, I keep getting this error. How do I get over it? GurobiError: Constraint has no bool value (are you trying "lb <= expr <= ub"?)
Gurobi
support.gurobi.com › hc › en-us › community › posts › 360077266711-C-sum-absolute-value-in-objective
C/ sum absolute value in objective – Gurobi Help Center
March 5, 2021 - import gurobipy as gp model = gp.Model("test") zsize = 4 # z is MVar z = model.addMVar(zsize) y = model.addVars(zsize) # list version of z for abs function z_list = z.tolist() # construct y_i = abs(z_i) constraints model.addConstrs(y[i] == gp.abs_(z_list[i]) for i in range(zsize)) # objective = sum y_i model.setObjective(gp.quicksum(y[i] for i in range(zsize)))
Gurobi
gurobi.github.io › modeling-examples › constraint_optimization
Constraint Optimization | Gurobi Modeling Examples
Explore our modeling examples for the Gurobi Python API ... This model is an example of a constraint optimization problem. Considering a constraint of an integer programming model where all the decision variables in the constraint are binary, the goal is to find another constraint involving the same binary variables that is logically equivalent to the original constraint, but that has the smallest possible absolute value of the right-hand side.
Gurobi
support.gurobi.com › hc › en-us › community › posts › 360058259091-Help-setting-up-an-absolute-value-for-expressions
Help setting up an absolute value for expressions – Gurobi Help Center
March 9, 2020 - Any value close enough to 0 is interpreted as 0. When taking the absolute value of a variable, more things happen behind the scenes of the solver than just taking the absolute value of the value in the solution.