This is part of our series on Alternatives to Backprop for Reasoning Models:
- Part 1: Belief Propagation : Obscure Backprop Alternative That Solves Sudoku.
- Part 2: Target Propagation : Bengio’s Biologically Plausible Backprop Alternative.
- Part 3: Forward Forward : Hinton’s New Alternative to Backpropagation.
- Part 4 (we are here): Grobner Constraint Solvers for Sudoku.
Grobner Basis Representations of Sudoku (Arnold et al., 2009)1 demonstrates practical ways to encode Sudoku tables as polynomial constraints for a Grobner basis solver.
If you know nothing about Grobner bases then we recommend this super friendly intro:
We introduced Sudoku in Part 1 when we coded the belief propagation paper.
Belief propagation algorithm is a gradient-descent alternative that uses sinkhorn-knopp to solve sudoku with machine learning.
This paper demonstrates 3 different ways to encode constraints for a polynomial Sudoku solver. (Arnold et al., 2009) however works with Shidoku boards.
1.1 Shidoku Primer #
Shidoku is a Sudoku variant where regions each contain the integers 1-4 exactly once.
Shidoku has 4 rows, 4 columns and 4 [2*2] boxes, so 12 units. Typically one is provided 6 known cells and must fill 10 unknown cells.
The rules are exactly similar to Sudoku and we went into extreme detail in part 1.
The remainder of this article solves the left-most board in Figure 2 above.
Code is available on Google Colab and GitHub.
(Arnold et al., 2009) introduces three different polynomial representation of Shidoku: the sum-product representation, the roots-of-unity representation and the boolean shidoku system.
Each representation has its advantages and we’ll cover each. If you know nothing about Grobner bases then we recommend this super friendly intro:
2.1 Sum-Product Representation #
The Sum-Product representation encodes the 4 possible cell values as a polynomial product:
Furthermore, one encodes the fact that one must choose 4 numbers that sum to 10 and multiply to 24 exactly once as the polynomial pair:
Note that the equation 2 pair above can be replaced by the single equation:
The sum-product form encodes Shidoku rules as 40 polynomial equations. Then to encode provided cells one simply includes more equations. For instance, the left Figure 2 board needs these equations:
Let’s break down the 40 equations among the cells and units (we showed why there are 12 units in Section 1.1):
- 10 unknown cells. So 10 equations representing (xi−1)(xi−2)(xi−3)(xi−4)=0.
- 6 known cells. So 6 equations where k is a provided constant in x-k = 0.
- 12 units. So 24 eqs since each unit has x1+x2+x3+x4−10=0 and x1x2x3x4−24=0.
10+6+24= 40 equations.
In Python, this resembles:
import sympy as sp
board = [[0,0,0,4],
[4,0,2,0],
[0,3,0,1],
[1,0,0,0]]
x = sp.Matrix(4,4,lambda i,j: sp.Symbol(f"x{i+1}{j+1}"))
equations = []
for i in range(4):
for j in range(4):
v = x[i,j]
equations.append((v-1)*(v-2)*(v-3)*(v-4) if board[i][j] == 0 else v-board[i][j])
units = [[x[i,j] for j in range(4)] for i in range(4)]
units += [[x[i,j] for i in range(4)] for j in range(4)]
units += [[x[i,j] for i in range(bi,bi+2) for j in range(bj,bj+2)] for bi in range(0,4,2) for bj in range(0,4,2)]
for unit in units:
equations += [sum(unit)-10, sp.prod(unit)-24]
G = sp.groebner(equations, *list(x), order="lex")
print("Variables:", len(list(x)))
print("Equations:", len(equations))
solutions = sp.solve(equations, list(x), dict=True)
print("\nSolutions:")
for sol in solutions:
for i in range(4):
print([sol[x[i,j]] for j in range(4)])
Running this code yields our expected solution!
2.2 Roots-of-Unity Representation #
The Roots-of-Unity representation encodes the 4 possible cell values as a complex polynomial product:
For each cell pair, say w and x, one forces w and x to admit different values using the polynomial product:
This form need 72 equations to encode Shidoku’s structure and the entries (1,2,3,4) are replaced by the fourth roots of unity {±1,±i}.
Let’s break down the 72 equations:
- 10 unknown cells. So 10 equations representing (x <sup>4</sup><sub>i</sub> − 1) = 0.
- 6 known cells. So 6 equations where k is a provided constant in x-k = 0.
- 56 unique pairs because:
- Each row has (4 choose 2 = 6) ways to pick pairs. There are 4 rows so 4*6 = 24 unique pairs in the rows.
- Each col has (4 choose 2 = 6) ways to pick pairs. There are 4 cols so 4*6 = 24 unique pairs in the columns.
- These 8 uncounted pairs in the diagonal:
10+6+56= 72 equations.
In Python, this resembles:
import sympy as sp
board = [[0,0,0,4],
[4,0,2,0],
[0,3,0,1],
[1,0,0,0]]
x = sp.Matrix(4,4,lambda i,j: sp.Symbol(f"x{i+1}{j+1}"))
roots = {1:1, 2:sp.I, 3:-1, 4:-sp.I}
equations = []
#16 cell equations
for i in range(4):
for j in range(4):
v = x[i,j]
equations.append(v**4-1 if board[i][j] == 0 else v-roots[board[i][j]])
#56 unique row/column/block pair equations
pairs = set()
for i in range(4):
for j in range(4):
for k in range(4):
for l in range(4):
if (i,j) != (k,l):
if i == k or j == l or (i//2 == k//2 and j//2 == l//2):
pairs.add(tuple(sorted([(i,j),(k,l)])))
for (i,j),(k,l) in pairs:
w,v = x[i,j],x[k,l]
equations.append((w+v)*(w**2+v** 2))
print("Cell equations:", 16)
print("Pair equations:", len(pairs))
print("Total equations:", len(equations))
G = sp.groebner(equations, *list(x), order="lex")
solution = sp.solve(G.polys, list(x), dict=True)
decode = {1:1, sp.I:2, -1:3, -sp.I:4}
print("\nSolution:")
for sol in solution:
for i in range(4):
print([decode[sp.simplify(sol[x[i,j]])] for j in range(4)])
Running the code alongside the complex to real map (1→1, 2→i, 3→−1, 4→−i) yields:
2.3 Boolean Shidoku Representation #
The Boolean Shidoku representation introduces four boolean variables for each Shidoku board cell w<sub>1</sub>,w<sub>2</sub>,w<sub>3</sub>,w4 and encodes each cell as a boolean variable like:
The boolean form uses a one-hot encoding approach, where the correct value equals 1 while the incorrect are zero. This can be encoded as:
For each cell pair, say w and x, one forces w and x to admit different values using the boolean product:
The boolean representation requires 136 polynomial equations to encode Shidoku rules.
However, one experiences a significant speedup because any power of w<sub>k</sub> can be replaced by itself ie. w<sub>2</sub> = w.
Once more, we breakdown the 136 eqs:
- 64 boolean equations. One for each of the 16 * 4 boolean variables b <sup>2</sup> i<sub>j,k</sub> −b<sub>ij,k</sub> =0
- 16 one hot encoding equations. b <sub>ij,1</sub> +b<sub>ij,2</sub> +b<sub>ij,3</sub> +b<sub>ij,4</sub> −1=0.
- 56 pairwise equations derived similar to Section 2.2.
Total is 64+16+56=136 equations.
In Python, we write
import sympy as sp
board = [[0,0,0,4],
[4,0,2,0],
[0,3,0,1],
[1,0,0,0]]
b = sp.MutableDenseNDimArray.zeros(4,4,4)
for i in range(4):
for j in range(4):
for k in range(4):
b[i,j,k] = sp.Symbol(f"b{i+1}{j+1}{k+1}")
equations = []
#Boolean constraints and exactly one value per cell
for i in range(4):
for j in range(4):
equations += [b[i,j,k]**2 - b[i,j,k] for k in range(4)]
equations.append(sum(b[i,j,k] for k in range(4)) - 1)
#Given cells
for i in range(4):
for j in range(4):
if board[i][j]:
equations.append(b[i,j,board[i][j]-1] - 1)
#56 unique pairs sharing a row, column, or block
pairs = set()
for i in range(4):
for j in range(4):
for k in range(4):
for l in range(4):
if (i,j) != (k,l):
if i == k or j == l or (i//2 == k//2 and j//2 == l//2):
pairs.add(tuple(sorted([(i,j),(k,l)])))
for (i,j),(k,l) in pairs:
equations.append(sum(b[i,j,d]*b[k,l,d] for d in range(4)))
variables = [b[i,j,k] for i in range(4) for j in range(4) for k in range(4)]
G = sp.groebner(equations, *variables, order="lex")
#Decode solution
solutions = sp.solve(G.polys, variables, dict=True)
print("Variables:", len(variables))
print("Equations:", len(equations))
print("\nSolution:")
for sol in solutions:
solved_board = []
for i in range(4):
row = []
for j in range(4):
value = next(
k + 1 for k in range(4)
if sol.get(b[i,j,k], b[i,j,k]) == 1
)
row.append(value)
solved_board.append(row)
for row in solved_board:
print(row)
It takes a while but once its done. We get the correct solution!
We saw three different approaches to encoding Sudoku as Grobner basis ideals. Then we coded it all. Pretty cool stuff.
1
Arnold, E., Lucas, S., & Taalman, L. (2010). Gröbner basis representations of Sudoku. The College Mathematics Journal, 41(2), 101–112. https://doi.org/10.4169/074683410x480203