 |
XOR gate sold by Amazon [1] |
In [2] a question was asked:
How can we solve xTAy=b for xi,yi∈{−1,+1}?
In this post I will discuss three different formulations:
- A simple quadratic formulation yielding a non-convex MIQCP model
- A MIP model based on a standard linearization
- A MIP model based on a linearization using an xor construct
Test Data
To help with some test models we need some data for A and b. Here is an instance that actually has a solution (printed with 3 decimals):
---- 13 PARAMETER a
i1 i2 i3 i4 i5
i1 0.998 0.579 0.991 0.762 0.131
i2 0.640 0.160 0.250 0.669 0.435
i3 0.360 0.351 0.131 0.150 0.589
i4 0.831 0.231 0.666 0.776 0.304
i5 0.110 0.502 0.160 0.872 0.265
---- 13 PARAMETER b = 2.222
Notice that solutions are not unique: if we have a solution
(x,y) then another solution is
(−x,−y).
MIQCP Model
The simplest is to use a Mixed-Integer Quadratically Constrained (MIQCP) model that handles the quadratic equation directly. Of course, in optimization we prefer binary variables
z∈{0,1} instead of
z∈{−1,+1}. That is easily fixed: introduce binary variables
pi,qi and write:
xi=2pi−1yi=2qi−1pi,qi∈{0,1} This maps a binary variable to
{−1,+1}. So a MIQCP model can look like:
MIQCP Formulation |
∑i,jxiyjai,j=bxi=2pi−1yi=2qi−1pi,qi∈{0,1} |
This is a non-convex problem, so you need a global solver like Baron, Couenne or Antigone. Most quadratic solvers only support the much easier, convex case. When expressing this feasibility problem as an optimization problem, we need to add a dummy objective. The solution can look like:
---- 55 VARIABLE p.L
i1 1.000, i4 1.000, i5 1.000
---- 55 VARIABLE q.L
i1 1.000, i2 1.000, i4 1.000
---- 55 VARIABLE x.L
i1 1.000, i2 -1.000, i3 -1.000, i4 1.000, i5 1.000
---- 55 VARIABLE y.L
i1 1.000, i2 1.000, i3 -1.000, i4 1.000, i5 -1.000
---- 59 PARAMETER b2 = 2.222 x'Ay using solution values
We find a solution that when plugged into
xTAy, reproduces our right-hand side.
Linearization 1
This problem can be linearized. This will allow us to use a linear MIP solver. One way is to write:∑i,jxiyjai,j=∑i,j(2pi−1)(2qj−1)ai,j=∑i,j(4piqj−2pi−2qj+1)ai,j
The binary product vi,j=piqj can be linearized using a standard formulation [3]: vi,j≤pivi,j≤qjvi,j≥pi+qj−1pi,qj,vi,j∈{0,1} Combining this leads to:
MIP Formulation I |
∑i,j(4vi,j−2pi−2qj+1)ai,j=bvi,j≤pivi,j≤qjvi,j≥pi+qj−1xi=2pi−1yi=2qi−1pi,qi,vi,j∈{0,1} |
It is noted that
v can be relaxed to be continuous between 0 and 1:
vi,j∈[0,1]. The solution can look like:
---- 62 VARIABLE p.L
i1 1.000, i4 1.000, i5 1.000
---- 62 VARIABLE q.L
i1 1.000, i2 1.000, i4 1.000
---- 62 VARIABLE v.L v(i,j)=p(i)*q(j)
i1 i2 i4
i1 1.000 1.000 1.000
i4 1.000 1.000 1.000
i5 1.000 1.000 1.000
---- 62 VARIABLE x.L
i1 1.000, i2 -1.000, i3 -1.000, i4 1.000, i5 1.000
---- 62 VARIABLE y.L
i1 1.000, i2 1.000, i3 -1.000, i4 1.000, i5 -1.000
---- 66 PARAMETER b2 = 2.222 x'Ay using solution values
Note that zeroes are not printed here (so the matrix
v may look a bit strange).
Linearization 2
There is another linearization which is more interesting. For this we will use the binary
xor operator.
There are different ways
xor is denoted. I believe the most common ones are
z=x xor yz=xor(x,y)z=x⊕yz=x⊻y The
xor truth table is:
x y z 000011101110I.e.
z=x xor y is 1 if and only if
x and
y are different.
Now consider the following thruth-table:
---- 15 PARAMETER t truth-table
x y x*y p q p xor q 1 - 2 xor
i1 -1 -1 1 1
i2 -1 1 -1 1 1 -1
i3 1 -1 -1 1 1 -1
i4 1 1 1 1 1 1
From this we can see that
xiyj=1−2(pi xor qj) So our quadratic constraint becomes
∑i,jxiyjai,j=∑i,j(1−2(pi xor qj))ai,j=b The operation
wi,j=pi xor qj can be linearized [4]:
wi,j≤pi+qjwi,j≥pi−qjwi,j≥qj−piwi,j≤2−pi−qjpi,qj,wi,j∈{0,1} With these tools we can formulate a different linearization:
MIP Formulation II |
∑i,j(1−2wi,j)ai,j=bwi,j≤pi+qjwi,j≥pi−qjwi,j≥qj−piwi,j≤2−pi−qjxi=2pi−1yi=2qi−1pi,qi,wi,j∈{0,1} |
It is noted that
w can be relaxed to be continuous between 0 and 1:
wi,j∈[0,1]. A solution can look like:
---- 62 VARIABLE p.L
i1 1.000, i4 1.000, i5 1.000
---- 62 VARIABLE q.L
i1 1.000, i2 1.000, i4 1.000
---- 62 VARIABLE w.L w(i,j) = p(i) xor q(j)
i1 i2 i3 i4 i5
i1 1.000 1.000
i2 1.000 1.000 1.000
i3 1.000 1.000 1.000
i4 1.000 1.000
i5 1.000 1.000
---- 62 VARIABLE x.L
i1 1.000, i2 -1.000, i3 -1.000, i4 1.000, i5 1.000
---- 62 VARIABLE y.L
i1 1.000, i2 1.000, i3 -1.000, i4 1.000, i5 -1.000
---- 66 PARAMETER b2 = 2.222 x'Ay using solution values
Conclusion
I am somewhat surprised how different the two linearizations look. You would not easily recognize that the two different linear formulations are essentially the same. I don't remember many times I could use an
xor operation in an optimization (I used it to solve Takuzu puzzles in [5]). The second linearization earns extra points for using
xor!
This question was more interesting than I anticipated.
References
- A Fairchild/ON Semiconductor XOR gate sold by Amazon for $1.69. https://www.amazon.com/Logic-Gates-2-Input-XOR-Gate/dp/B00MEKUZV8
- Matrix Equation & Integer Programming, https://math.stackexchange.com/questions/3047086/matrix-equation-integer-programming/3047835
- Multiplication of Binary Variables, https://yetanothermathprogrammingconsultant.blogspot.com/2008/05/multiplication-of-binary-variables.html
- XOR as linear inequalities, https://yetanothermathprogrammingconsultant.blogspot.com/2016/02/xor-as-linear-inequalities.html
- Solving Takuzu puzzles as a MIP using xor, https://yetanothermathprogrammingconsultant.blogspot.com/2017/01/solving-takuzu-puzzles-as-mip-using-xor.html