Sunday, February 21, 2016

xor as linear inequalities

I came across a question about using an xor condition in a MIP model. Here is a summary of the answer.

Expressing z=xyz=xy (or z=x and y ) where x,y,z{0,1} as a set of linear constraints is well-known:

and:     zxzyzx+y1
zxy

000
001
010
111
The relation z=x xor y is slightly more complicated. The xor (exclusive-or) condition can also be written as z=x<>y, i.e. z is 1 if x and y are different (and z is zero if they are the same). This we can write as the following set of linear inequalities:
xor:     zx+yzxyzyxz2xy
zxy

000
101
110
011
To be complete z=x or y can be written as:
or:     zx+yzxzy
zxy

000
101
110
111
In all of this we assumed x,y and z are binary variables.

3 comments:

  1. Thanks! You help me a lot!

    ReplyDelete
  2. Dear colleagues,
    recently we understood how to represent any boolean function f(x) as system of linear inequalities in the following sense.

    Let B^n={(x_1,x_2, ..., x_n): x_k={0,1}} be is a set of n-dimensional unit cube's vertices, i.e. the set of all 0-1 arguments of some boolean function f:B^n -> {0,1}. Then there exist a system of a number of linear inequalities of n+1 variables (x,y):
    S(x,y)={a_i + l_i(x) + b_i*y >= 0: i={1:m}} such that for any x\in B^n, f(x)=y if and only if S(x,y) holds.
    It is important that we do not need to assume that y is a discrete, 0-1, variable! For any 0-1 vector x, corresponding y value will be either 0, or 1.

    And I can explicitly produce the system S(x,y) for any function f(x) given either by formula or in tabular form.
    Can any body tell me any reference on this subject?
    I can not believe that it a "fresh result" in math. programming ... But my rather intensive search over literature did not bring any similar assertion...

    ReplyDelete
  3. what about detecting equality or not (xor) between 2 consecutive integer variables? not only binary. thanks

    ReplyDelete