What should I do abs in objectfunction

269 views Asked by At

I want to find an expression with an absolute value as an objective function in gurobi. Specifically, it is an expression such as ∑_j ∈ J | ∑_i ∈ P x_ij-d_i *t_i |.

The variable is x_ij, and the others are constants. I tried and errored this formula using abs (), but I couldn't figure it out in the end. I would appreciate it if you could tell me.

model.setObjective(quicksum(quicksum((x[i,j] for i in P)-d[i]*t[i]) for j in W),GRB.MINIMIZE) 
#objectfunction

what should I change this?

1

There are 1 answers

0
joni On

You could add additional helper variables and then use Gurobi's general abs constraints:

import gurobipy as gp
from gurobipy import quicksum, GRB

# ...your model and sets W, P, J...

helper1 = model.addVars(W, lb=GRB.INFINITY, vtype="C")
helper2 = model.addVars(W, vtype="C")

for j in W:
    model.addConstr(helper1[j] == quicksum(x[i,j]-d[i]*t[i] for i in P))
    model.addConstr(helper2[j] == gp.abs_(helper1[j]))

model.setObjective(quicksum(helper2[j] for j in W), GRB.MINIMIZE)