Sunday, March 22, 2020

Special Sudoku

Here is a difficult Sudoku variant designed by Aad van de Wetering. Aad is a former colleague of mine.

Problem
The standard Sudoku rules apply:

  • Each cell has an integer value between 1 and 9 
  • In each row, column and sub-block cells must be unique
  • Some cells have a predefined value

In addition, we have the following restriction:

  • The maximum difference between the value of two neighboring cells (orthogonal, that is horizontal or vertical) is 5

To solve this you can follow this video [1]:



Of course, we want to try to solve this as a Mixed-Integer Programming model.

Review: a linear integer programming model for standard Sudoku


When we want to solve Sudokus, the easiest approach is to define the following binary decision variables [2]: xi,j,k={1if cell (i,j) has value k0otherwise Here k{1,,9}. We have 27 areas we need to check for unique values: rows, columns and sub-blocks. We can organize this as a set:  ua,i,jexists if and only if area a contains cell (i,j) This is data. We also have a set of given cells, which we can fix. The resultimg MIP model can look like [2]:

Mixed-Integer Programming Model for standard Sudoku
min0Dummy objectivekxi,j,k=1i,jOne k in each celli,j|ua,i,jxi,j,k=1a,kUnique values in each areaxi,j,k=1where k=Giveni,jFix given valuesxi,j,k{0,1}

During post-processing, we can calculate the completed grid using the optimal values of x: vi,j=kkxi,j,k

Linear integer programming model for Sudoku variant


We need to introduce the concept of "neighbors". We can define the set: nbi,j,i,jexists iff cells (i,j) and (i,j) are neighbors I excluded symmetric cases. To be precise: in GAMS, I populated this set with:

alias (i,ii),(j,jj);
set
  nb(i,j,ii,jj)
'neighbors'
;

nb(i,j,i-1,j) =
yes;
nb(i,j,i,j-1) =
yes;


We can introduce the post-processing step directly into the MIP and then enforce our difference constraint on the variables vi,j. This can look like vi,j=kkxi,j,kcell values as constraint5vi,jvi,j5for neigboring cells (i,j) and (i,j)
With this, we can formulate:


Mixed-Integer Programming Model for Sudoku Variant
min0Dummy objectivekxi,j,k=1i,jOne k in each celli,j|ua,i,jxi,j,k=1a,kUnique values in each areavi,j=kkxi,j,ki,jValue of cell5vi,jvi,j5i,j,i,j|nbi,j,i,jNeighbors differ by max 5xi,j,k=1where k=Giveni,jFix given valuesxi,j,k{0,1}vi,j{1,,9}

This model solves very easily. The presolver can solve this model completely, so we don't even have to start the branch-and-bound phase. As a result, we can make a much shorter movie:




The solution of this model looks like:


----     80 VARIABLE v.L  value of each cell

            c1          c2          c3          c4          c5          c6          c7          c8          c9

r1           2           4           9           6           7           5           8           3           1
r2           3           8           5           1           4           9           6           7           2
r3           7           6           1           2           3           8           9           5           4
r4           8           9           6           3           1           4           5           2           7
r5           5           7           3           8           6           2           1           4           9
r6           1           2           4           5           9           7           3           8           6
r7           6           1           2           4           8           3           7           9           5
r8           4           3           7           9           5           1           2           6           8
r9           9           5           8           7           2           6           4           1           3


Unique solution


A proper Sudoku puzzle has one unique solution. We can verify this by adding a cut to the problem after solving it. The cut should forbid the current solution (and allow all other solutions). In this case, the cut can look like i,j,kxi,j,kxi,j,k921 where x is the previously found solution. Indeed, this puzzle has a single, unique solution: after adding the cut, the problem becomes (integer) infeasible.

References


  1. A Sudoku With Just 11 Digits = Dutch Genius!, https://www.youtube.com/watch?v=ZU5fSDHJq8k
  2. MIP Modeling: from Sudoku to KenKen via Logarithms, https://yetanothermathprogrammingconsultant.blogspot.com/2016/10/mip-modeling-from-sudoku-to-kenken.html

No comments:

Post a Comment