Converting constraint with absolute value equation into linear programming constraint

118 Views Asked by At

I have following constraints,

w1 = | XR1 - 20 |

where w1 is a binary variable and XR1 is a nonnegative variable. How can I convert this into a linear programming constraint?

For instance, if XR1 == 25, I want w1 to be 5, similarly if XR1 == 15, I want w1 to be 5 again.

What I tried is the following:

(S1 is a binary variable)

XR1 >= 20 + 1000 * (1 - S1)
XR1 <= 20 + 1000 * S1

(XR1 - 20) - 1000 * (1 - S1) <= W1
W1 <= (XR1 - 20) + 1000 * (1 - S1)

(20 - XR1) - 1000 * S1 <= W1
W1 <= (20 - XR1) + 1000 * S1

I believe these constraints work when XR1 >= 20 correctly, however, they are not when XR1 < 20, do you agree? If yes, how can I convert it to what I want correctly?