0
$\begingroup$

Is there any way to convert $y=x_1~ \text{XOR} ~x_2$ to linear constraints? It means we write some linear relations with:

if $x_1=x_2 =0$ or $x_1=x_2=1$ $\to$ $y=0$, else, $y=1$?

$\endgroup$
3
  • 1
    $\begingroup$ Are you sure you don't mean XOR? By the truth table you should have something like: y=x1+x2-2*t where t is additional variable. $\endgroup$
    – joro
    Commented Aug 22, 2022 at 11:07
  • $\begingroup$ Thanks, I corrected it. $\endgroup$
    – A.R.S
    Commented Aug 22, 2022 at 11:21
  • $\begingroup$ In fact, $t=x_1 \times x_2$. It seems this nonlinear relation must also be considered and converted to a linear relation. Is it necessary? $\endgroup$
    – A.R.S
    Commented Aug 22, 2022 at 11:24

3 Answers 3

2
$\begingroup$

You can derive the desired constraints somewhat automatically via conjunctive normal form as follows: $$(\lnot x_1 \land \lnot x_2) \implies \lnot y \\ \lnot (\lnot x_1 \land \lnot x_2) \lor \lnot y \\ (x_1 \lor x_2) \lor \lnot y \\ x_1 + x_2 + (1-y)\ge 1\\ x_1 + x_2 -y \ge 0 $$ The other three constraints are similar.

$\endgroup$
2
$\begingroup$

Under the constraints $x_1,x_2,y\in\{0,1\}$, the equality $y=x_1\oplus x_2$ is equivalent to $$\begin{cases} y \geq x_1 - x_2, \\ y \geq x_2 - x_1, \\ y \leq x_1 + x_2, \\ y \leq 2 - x_1 - x_2. \end{cases}$$

$\endgroup$
4
  • 1
    $\begingroup$ A more symmetric form: $x_3=x_1\oplus x_2$ is equivalent to $x_1+x_2+x_3\le2$ and $x_i\le x_j+x_k$ for $\{i,j,k\}=\{1,2,3\}$. $\endgroup$ Commented Aug 22, 2022 at 20:01
  • $\begingroup$ @EmilJeřábek: Yes, and more symmetric initial equation is $x_1\oplus x_2\oplus x_3 = 0$. $\endgroup$ Commented Aug 22, 2022 at 20:28
  • $\begingroup$ Isn't it more elementary to observe that XOR is addition modulo 2 and introduce a temporary variable and the constraint $y=x_1+x_2-2 t,0 \le y \le 1$? $\endgroup$
    – joro
    Commented Aug 23, 2022 at 5:49
  • $\begingroup$ @joro: It depends on relative cost of variables vs constraints. It is often believed that introducing new variables results in a more computationally costly ILP problem, and from this perspective 4 constraints are more preferable than 1 constraint and 1 variable. $\endgroup$ Commented Aug 23, 2022 at 12:09
1
$\begingroup$

If we consider the cube $0 \le x_1, x_2, y \le 1$ then the eight possible Boolean assignments to $x_1, x_2, y$ are the vertices of the cube. We can mark an assignment $x_1 = a, x_2 = b, y = c$ as illegal with the constraint $$\begin{pmatrix}x_1 - \tfrac12 \\ x_2 - \tfrac12 \\ y - \tfrac12\end{pmatrix} \cdot \begin{pmatrix}a - \tfrac12 \\ b - \tfrac12 \\ c - \tfrac12\end{pmatrix} < \begin{pmatrix}a - \tfrac12 \\ b - \tfrac12 \\ c - \tfrac12\end{pmatrix} \cdot \begin{pmatrix}a - \tfrac12 \\ b - \tfrac12 \\ c - \tfrac12\end{pmatrix}$$

which can be expressed as a linear constraint with integer coefficients via suitable scaling. The xor constraint can be written as four of these linear constraints.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged .