4

I have two variables: x>= 0 and y binary (either 0 or 1), and I have a constant z >= 0. How can I use linear constraints to describe the following condition:

If x = z then y = 1 else y = 0.

I tried to solve this problem by defining another binary variable i and a large-enough positive constant U and adding constraints

y - U * i = 0;
x - U * (1 - i) = z;

Is this correct?

2 Answers 2

3

Really there are two classes of constraints that you are asking about:

  1. If y=1, then x=z. For some large constant M, you could add the following two constraints to achieve this:
x-z <= M*(1-y)
z-x <= M*(1-y)

If y=1 then these constraints are equivalent to x-z <= 0 and z-x <= 0, meaning x=z, and if y=0, then these constraints are x-z <= M and z-x <= M, which should not be binding if we selected a sufficiently large M value.

  1. If y=0 then x != z. Technically you could enforce this constraint by adding another binary variable q that controls whether x > z (q=1) or x < z (q=0). Then you could add the following constraints, where m is some small positive value and M is some large positive value:

x-z >= mq - M(1-q)
x-z <= Mq - m(1-q)

If q=1 then these constraints bound x-z to the range [m, M], and if q=0 then these constraints bound x-z to the range [-M, -m].

In practice when using a solver this usually will not actually ensure x != z, because small bounds violations are typically allowed. Therefore, instead of using these constraints I would suggest not adding any constraints to your model to enforce this rule. Then, if you get a final solution with y=0 and x=z, you could adjust x to take value x+epsilon or x-epsilon for some infinitesimally small value of epsilon.

Sign up to request clarification or add additional context in comments.

6 Comments

@Bosco sorry, I don't understand. x=z is easy to model (we did it in part 1). x != z is hard to model (as I describe in part 2).
Hi @josilber, so the condition is x= z, result is y =1, or x != z, result is y =0; By your constraints, when x = z, 0 <= M*(1 - y), then y can be 1 or 0, thanks a lot.
@Bosco basically I have enforced the logic "If y=1 then x=z". I haven't added any logic for the case when y=0, so you could either have x=z or x != z in that case.
Hi @josilber, so I think I have to change the problem to if y= 0 then x = z, if y = 1, then x != z, then I get the answer :)
@Bosco As I state in my answer, I don't think you can easily model the x != z part using linear programming (please see my comment on your answer for why the approach you describe is more restrictive than x != z).
|
0

So I change the conditional constraints to

if x = z then y = 0 else y = 1

Then the answer will be

x - z <= M * y;
x - z >= m * y;

where M is a large enough positive number, m is a small enough positive number.

2 Comments

If y=0 then these constraints state that x=z (just as my answer does). However if y=1 then you are forcing x-z to take a value between m and M, meaning you have disallowed the case where x != z and x < z.
Hi @josilber, so what I can only do is to compare the range of x and z, my answer above is the case that x will never be smaller than z.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.