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 › 360054687091-How-to-handle-absolute-value-in-Gurobi-constraints
How to handle absolute value in Gurobi constraints – Gurobi Help Center
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.
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
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| ...
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 - Unfortunately, this is currently not possible and the introduction of auxiliary variable is required. You can still work with MVars by using the list version of them, e.g., 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
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 - 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
Gurobi
support.gurobi.com › hc › en-us › community › posts › 23001326746001-Gurobi-Absolute-Value-Constraints
Gurobi- Absolute Value Constraints – Gurobi Help Center
I use several parameters including FeasibilityTol=1e-6, Presolve=0, and NumericFocus=3 but the violation remains... ... Moreover, when given a fixed bound to 'x^k', e.g. -1e6 to 1e6, (26) constraint is violated which absolute value constraint and performs this strange behavior: t_2_1 should be |xp_2_1| but it gives a strange value, how to fix it? ... A constraint violation of ~2e-11 is nothing to be worried about. Note that Gurobi uses numerical algorithm so it is basically impossible to achieve a 0 violation for models with continuous variables.
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.
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))
Google Groups
groups.google.com › g › gurobi › c › OGc1cf3QCXQ
absolute value in objective
May 10, 2017 - To avoid confusion, please note that the general constraints (like abs) in Gurobi do not lead to non-linear models. They will be linearized (similar to what you propose for abs) automatically during presolve.
Gurobi
support.gurobi.com › hc › en-us › community › posts › 360060405611-Abs-value-constraint-for-MLinExpr
Abs value constraint for MLinExpr – Gurobi Help Center
Or why not try our AI Gurobot?. ... You can simply change your variable declaration from using MVar to regular Gurobi variables; the abs-value should work there without any issues.
Gurobi
docs.gurobi.com › projects › optimizer › en › current › reference › parameters.html
Parameter Reference - Gurobi Optimizer Reference Manual
This parameter limits the bounds on the variables that participate in function constraints approximated by a piecewise-linear function. Specifically, any bound larger than FuncMaxVal (in absolute value) on the variables participating in such a function constraint will be truncated.
Gurobi
support.gurobi.com › hc › en-us › community › posts › 360051079331-Setting-objective-for-absolute-error-minimization
Setting objective for absolute error minimization – Gurobi Help Center
September 21, 2019 - Similarly, the abs_() function accepts a single Var object as its argument. So, the first step is to introduce auxiliary variables (say, z[t]) that represent the expressions inside the absolute value functions. E.g.: model.addConstr(z[t] == quicksum(Unit[r, t] for v in Readings) - set_point)
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.
Google Groups
groups.google.com › g › gurobi › c › YUWqUfk3PSc
How to find absolute of a linear expression?
July 12, 2016 - The absolute value is a non-linear function. However, abs(x) is a piecewise-linear function and you can use a linearization technique. Let x be your decision variable and z = abs(x). You can use two separate variables p, n >= 0 and use an indicator variable y to ensure that only p or n can ...
Gurobi
support.gurobi.com › hc › en-us › community › posts › 4407810309265-How-to-find-absolute-difference-of-list-and-use-those-values-in-maximization-optimization-model-guobipy-
How to find absolute difference of list and use those values in maximization optimization model guobipy? – Gurobi Help Center
#Following is my code where I try absolute operations of two lists based on some conditions and then maximize the summation of those. m=[5,3,2] cm=[sum(m[0:x:1]) for x in range(1, len(m)+1)] P=len(m) p = range(len(m)) N=sum(m) n = range(N) sett=[0 for i in p] for i in p: if(i==0): sett[i]=range(0,cm[i]) else: sett[i]= range(cm[i-1],cm[i]) #model for optimization with grb.Env() as env, grb.Model(env=env) as o: o = grb.Model() o.Params.LogToConsole =0 o.Params.OutputFlag =0 x = {} for i in n: for j in n: x[i,j] = o.addVar(vtype=grb.GRB.BINARY, name='x'+str(i)+'_'+str(j)) c = {} for j in n: c[j]