How to use abs() function with decision variables

3,034 views
Skip to first unread message

156265...@gmail.com

unread,
Jan 1, 2016, 12:48:31 PM1/1/16
to Gurobi Optimization
Hi, I am trying to use abs function with decision variables but Gurobi gives this output :  TypeError: bad operand type for abs(): 'gurobipy.LinExpr'

Here the code that gurobi directs me for the error;

Penalty = quicksum((1 / (1+Real_Discount_Rate)**y)*abs(W2G[p]+S2G[p]-EPD[p])*AAA*BBB
          for y in range(1,CCC+1) for p in range(1,DDD+1))

**W2G and S2G are decision variables while EPD is a parameter.


I tried Numpy.absolute to solve the problem but it seems Gurobi does not like to process abs function in loops constructed with decision variables.

I would be happy if anyone has an idea?

Thanks,
Enes

Kostja Siefen

unread,
Jan 5, 2016, 3:35:28 AM1/5/16
to Gurobi Optimization
Hi Enes,

the absolute value is a non-linear function. However, abs(x) is a piecewise-linear function and you can use a modeling 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 have a value other than zero at any time.

1) Add variables p, n >= 0
2) Add binary variable y
3) Add constraint x = p - n.
4) Add constraints p <= M*y and n <= M * (1-y) with suitable values for M
5) Add constraint z = p + n

Then you have z = abs(x). Make sure that you set proper bounds for x in your model (i.e. allow negative values). 

Kostja
Reply all
Reply to author
Forward
0 new messages