Abs Value
AnsweredI am trying to solve a problem with absolute value. I used the following sintax https://www.gurobi.com/documentation/9.0/refman/py_abs_.html
m.addConstr(y == abs_(x))
but, It gives me an error. NameError: name 'abs_' is not defined.
I would like to know what is the problem and how can I solve this.
Best regars.

Hi Juan, I suppose you talk spanish if you want to keep talking. I was trying to do the next thing.
y = model.addVars(Amigos, vtype= GRB.CONTINUOUS, lb= GRB.INFINITY, ub=GRB.INFINITY, name = "y")
model.update()
model.addConstr(quicksum(abs_(y[a]) for a in Amigos) <= rho)
It does not work, so I had to do the following code. I created an auxiliar var called y_abs
from gorobipy import GRB, quicksum, Model, abs_
model = Model()
y = model.addVars(Amigos,vtype=GRB.CONTINUOUS,lb= GRB.INFINITY, ub= GRB.INFINITY, name="y")
y_abs = model.addVars(Amigos,vtype=GRB.CONTINUOUS, name="y_abs")
model.update()
for a in Amigos:
model.addConstr(y_abs[a] == abs_(y[a]), name="absconstr")
model.addConstr(quicksum(y_abs[a] for a in Amigos) <= rho)
......I would like to know what is the problem with the first option and why doesn work.
Best regards and thanks for your answer
Cristian.
1 
Did you load the package guobipy into your script? Also, if you did import the package using the standard import gurobipy as gp, make sure you use gp.abs_() instead of just abs_().
To illustrate how to deal with absolute values using general constraints, I'll use the optimization model shown in this stackoverflow question:
import gurobipy as gp
from gurobipy import GRB
model = gp.Model("myModel")
v1 = model.addVar(vtype=GRB.INTEGER, name="v1")
v2 = model.addVar(vtype=GRB.INTEGER, name="v2")
v3 = model.addVar(vtype=GRB.INTEGER, name="v3")
diff1 = model.addVar(vtype=GRB.CONTINUOUS, name="diff1")
diff2 = model.addVar(vtype=GRB.CONTINUOUS, name="diff2")
abs1 = model.addVar(lb=0, vtype=GRB.CONTINUOUS, name="abs1")
abs2 = model.addVar(lb=0, vtype=GRB.CONTINUOUS, name="abs2")
model.addConstr(3*v1 + v2 + v3 <= 72, name="constr1")
model.addConstr(2*v1 + 3*v2 + 2*v3 <= 80, name="constr2")
model.addConstr(diff1 == v1  v2, name="diffConstr1")
model.addConstr(diff2 == v2  v3, name="diffConstr2")
model.addGenConstrAbs(abs1, diff1, "absConstr1")
model.addGenConstrAbs(abs2, diff2, "absConstr1")
model.addConstr(abs1 + abs2 >= 10, name="constr3")
model.setObjective(v1 + v2 + v3, GRB.MAXIMIZE)
model.optimize()After optimizing this model, Gurobi should output v_1 = 18; v_2 = 9; v_3 = 8, with constr3 as binding constraint.
0 
The abs_() function can only be used to set one decision variable equal to the absolute value of another. E.g.:
model.addConstr(y == abs_(x), name='abs_x')
You unfortunately can't use abs_() in any other type of constraint, like you try to do in the first example. Creating auxiliary variables is the right way to model this.
0 
Thanks Eli. I will keep this on mind.
Best regards
0 
Hi Gurobi team
I have alike situation, I´m trying to use the abs value of a variable in the objective function. but in the restrictions I cannot change the variable because the sign is important there. please help
and thanks in advance
"
#Objective functionm1.setObjective( gb.quicksum(Pr* Dn[(t,n)] for t in T for n in N )# Cost of gas supply
+gb.quicksum(nodos_info_[t][k]['precio_oferta']*Ok[(t,k)]for t in T for k in K)+gb.quicksum(tramos_info_[t][l]["c_trans"] *gb.abs_((fl[(t,l)]))for t in T for l in L)
, gb.GRB.MINIMIZE)#m1.optimize()"
0 
Hi Carlos,
Please reread Eli's answer above.
 Riley
0 
Hi Eli,
Hi Riley,
Is it not possible to have multiple constraints with abs_() functions? I am working on a multi objective problem. I want to minimize the absolute values of 3 different decision variables. The abs_() function works for one variable and doesn't do the task for the other 2.
Will you please explain?
Thanks a lot!
 Vivek
0 
A model can include multiple absolute value constraints. Can you please post a minimal reproducible example that shows the issue?
0 
So these are some of the constraints:
opt_model.addConstrs((energy_bess1[t]/capacity1  energy_bess2[t]/capacity2 == delta_SOE[t] for t in time), 'deltaSOE')#Absolute Delta SOEopt_model.addConstrs((abs_delta[t] == abs_(delta_SOE[t]) for t in time), 'absDelta')opt_model.addConstrs((energy_heat2[t]  energy_heat1[t] == delta_energy_heat[t] for t in time), 'delta_energy_heat')opt_model.addConstrs((abs_delta_energy_heat[t] == abs_(delta_energy_heat[t]) for t in time), 'abs_Delta_heatContent')The abs_(delta_SOE) works as intended but the second absolute function gives an error if the energy_heat1 is bigger than energy_heat2.Any tips on this?Thanks,Vivek0 
Without knowing what the error message is, I would guess the variable bounds might be the problem. By default, variables have a lower bound of \(0\). Did you remove this default lower bound from the \(\texttt{delta_energy_heat}\) variables when you defined them? E.g.:
delta_energy_heat = opt_model.addVars(
time, lb=gp.GRB.INFINITY, name="delta_energy_heat"
)0 
Hi Eli,
Thanks a lot for your help, it worked.
Best regards,
Vivek
0
Please sign in to leave a comment.
Comments
11 comments