Site logoTungTT

Modeling Binox puzzle

5 minutes
958 words

1. Introduction#

Binox is a logic puzzle described as follows: given a grid of squares with some cells already filled with XX or OO. Player needs to complete the grid by filling in the empty cells with XX or OO, satisfying the following conditions:

  • The grid is completely covered by XX and OO
  • In each row/column, there is no sequence of more than 2 consecutive cells filled with the same symbol
  • In each row/column, the number of XX is equal to the number of OO
  • Each row/column is unique (No two rows are the same, and no two columns are the same)

Below is an example of a 6×6 Binox puzzle and its solution.

binox_example

2. Modeling the puzzle#

  • nRn_R is the number of rows in the grid (nRn_R is an even number)
  • nCn_C is the number of columns in the grid (nCn_C is an even number)
  • FXF_X is the set of all cells (i,j)(i, j) already filled with XX
  • FOF_O is the set of all cells (i,j)(i, j) already filled with OO

2.1. Decision variables#

For each cell (i,j)(i, j) in the grid, we define a variable x(i,j)x(i, j) such that

x(i,j)={1if cell (i,j) filled with X 0if cell (i,j) filled with O x(i, j) = \begin{cases} 1 & \text{if cell } (i, j) \text{ filled with X } \\ 0 & \text{if cell } (i, j) \text{ filled with O } \end{cases}

2.2. Constraints#

  • Cells already filled
{x(i,j)=1,(i,j)FXx(i,j)=0,(i,j)FO\begin{cases} x(i, j) = 1, \forall (i, j) \in F_X\\ x(i, j) = 0, \forall (i, j) \in F_O \end{cases}
  • In each row, there is no sequence of more than 2 consecutive cells filled with the same symbol
{x(i,j)+x(i,j+1)+x(i,j+2)2x(i,j)+x(i,j+1)+x(i,j+2)10i<nR,0j<nC2\begin{cases} x(i, j) + x(i, j + 1) + x(i, j + 2) \leq 2 \\ x(i, j) + x(i, j + 1) + x(i, j + 2) \geq 1 \end{cases} \\ \forall 0 \leq i < n_R, 0 \leq j < n_C - 2
  • In each column, there is no sequence of more than 2 consecutive cells filled with the same symbol
{x(i,j)+x(i+1,j)+x(i+2,j)2x(i,j)+x(i+1,j)+x(i+2,j)10i<nR2,0j<nC\begin{cases} x(i, j) + x(i + 1, j) + x(i + 2, j) \leq 2 \\ x(i, j) + x(i + 1, j) + x(i + 2, j) \geq 1 \end{cases} \\ \forall 0 \leq i < n_R - 2, 0 \leq j < n_C
  • In each row, the number of XX is equal to the number of OO
j=0nC1x(i,j)=nC2,0i<nR\sum\limits_{j = 0}^{n_C - 1}{x(i, j)} = {n_C \over 2}, \\ \forall 0 \leq i < n_R
  • In each column, the number of XX is equal to the number of OO
i=0nR1x(i,j)=nR2,0j<nC\sum\limits_{i = 0}^{n_R - 1}{x(i, j)} = {n_R \over 2}, \\ \forall 0 \leq j < n_C
  • Each row is unique. To linearize this constraint, we can rephrase it as follows: for any two rows ii and jj, there exists at least one position kk such that x(i,k)x(j,k)x(i, k) \ne x(j, k).

    For any two distinct rows iji \ne j, and for each column k, we define an intermediate variable y(i, j, k) such that

    y(i,j,k)={1if x(i,k)=x(j,k)0otherwisey(i, j, k) = \begin{cases} 1 & \text{if } x(i, k) = x(j, k) \\ 0 & \text{otherwise} \end{cases}

    Using geometric method we can represent the variables yy through the variables xx by the following constraints:

    {y(i,j,k)+x(i,k)+x(j,k)1y(i,j,k)+x(i,k)x(j,k)+1y(i,j,k)+x(j,k)x(i,k)+1x(i,k)+x(j,k)y(i,j,k)+10i,j<nR,ij,0k<nC\begin{cases} y(i, j, k) + x(i, k) + x(j, k) \geq 1 \\ y(i, j, k) + x(i, k) \leq x(j, k) + 1 \\ y(i, j, k) + x(j, k) \leq x(i, k) + 1 \\ x(i, k) + x(j, k) \leq y(i, j, k) + 1 \end{cases} \\ \forall 0 \leq i, j < n_R, i \ne j, 0 \leq k < n_C

    We linearize the original constraint using the variables yy as follows

    k=0nC1y(i,j,k)nC1,0i,j<nR,ij\sum\limits_{k = 0}^{n_C - 1}{y(i, j, k)} \leq n_C - 1, \\ \forall 0 \leq i, j < n_R, i \ne j
  • Each column is unique. Similarly to the above condition, for any two distinct columns ii and jj, and for each row kk, we construct an intermediate variable z(i,j,k)z(i, j, k) such that

    z(i,j,k)={1if x(k,i)=x(k,j)0otherwisez(i, j, k) = \begin{cases} 1 & \text{if } x(k, i) = x(k, j) \\ 0 & \text{otherwise} \end{cases}

    Represent the variables zz through the variables xx by the following constraints:

    {z(i,j,k)+x(k,i)+x(k,j)1z(i,j,k)+x(k,i)x(k,j)+1z(i,j,k)+x(k,j)x(k,i)+1x(k,i)+x(k,j)z(i,j,k)+10k<nR,0i,j<nC,ij\begin{cases} z(i, j, k) + x(k, i) + x(k, j) \geq 1 \\ z(i, j, k) + x(k, i) \leq x(k, j) + 1 \\ z(i, j, k) + x(k, j) \leq x(k, i) + 1 \\ x(k, i) + x(k, j) \leq z(i, j, k) + 1 \end{cases} \\ \forall 0 \leq k < n_R, 0 \leq i, j < n_C, i \ne j

    Linearize the original constraint as follows

    k=0nR1z(i,j,k)nR1,0i,j<nC,ij\sum\limits_{k = 0}^{n_R - 1}{z(i, j, k)} \leq n_R - 1, \\ \forall 0 \leq i, j < n_C, i \ne j

2.3. Objective function#

This problem does not have an objective function, as we are not aiming to minimize or maximize any value. Our goal is simply to find a feasible solution. Technically, when programming, we can set the objective function to a constant (I often choose 0)

3. Conclusion#

The highlight of this puzzle is the unique row/column constraint. In reality, such constraints are often very difficult to model efficiently. Imagine if we had thousands of rows or columns; the number of constraints would be enormous. In such cases, we must find alternative ways to solve the problem effectively

For more Binox puzzles and variations, please refer to Krazydad. The Python code for Binox modeling is available at Tung-hehe

Happy modeling!