Search code examples
optimizationmathematical-optimizationlinear-programmingscientific-computinginteger-programming

Integer programming: assignment of absolute value (depending on variable value)


I am relatively new to integer programming and (again) got stuck with the formulation of a constraint.

In my simplified model I have a (continous) variable with a lower bound LB below zero and an upper bound UB above zero. Now I want to assign the variable value to other variables depending on the value that the variable has taken.

The logic I want to express is the following:

LB > 0
UB > 0
-LB <= Variable1 <= UB

if Variable1 => 0:
    Variable2 = Variable1
    Variable3 = 0
else:
    Variable2 = 0
    Variable3 = abs(Variable1)

How can I describe this using linear (in)equalities?

I guess am a bit slow on the uptake..

Thanks in advance!

** Edit: For the modeling I am using Python, Pyomo and the newest Gurobi solver.

*** Edit: I have now formulated it the following way by the use of a binary variable. (I know it is quadratic but this can be linearized later):

LB > 0
UB > 0

-LB <= Variable1 <= UB
0 <= Variable2 <= UB
0 <= Variable3 <= LB
Variable4 = Variable2 * BinaryVariable - Variable3 * (1-BinaryVariable)

But now I still have to make sure that Variable3 is 0 if Variable2 is > 0 and vice versa.

Any ideas?


Solution

  • First create a binary variable that equals 1 if Variable1 > 0 and 0 if Variable1 < 0:

    Variable1 <= UB * BinaryVar
    LB * (1 - BinaryVar) <= Variable1
    

    (If Variable1 > 0, then BinaryVar must equal 1. If Variable1 < 0, then BinaryVar must equal 0. Note that if Variable1 = 0, then BinaryVar could equal 0 or 1, but that doesn't matter for your problem because if Variable1 = 0 then Variable2 = Variable3 = 0 and the constraints below work out OK.)

    Now add constraints enforcing the values for Variable2 and Variable3:

    Variable2 = Variable1 * BinaryVar
    Variable3 = -Variable1 * (1 - BinaryVar)
    

    These are quadratic constraints, which you can then linearize.

    Linearization:

    Variable2 <= UB * BinaryVar
    Variable2 >= -LB * BinaryVar
    Variable2 <= Variable1 + LB * (1 - BinaryVar)
    Variable2 >= Variable1 - UB * (1 - BinaryVar)
    Variable3 = Variable2 - Variable1