Problem |
- 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 objective∑kxi,j,k=1∀i,jOne k in each cell∑i,j|ua,i,jxi,j,k=1∀a,kUnique values in each areaxi,j,k=1where k=Giveni,jFix given valuesxi,j,k∈{0,1} |
Linear integer programming model for Sudoku variant
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=∑kk⋅xi,j,kcell values as constraint−5≤vi,j−vi′,j′≤5for neigboring cells (i,j) and (i′,j′)
With this, we can formulate:
Mixed-Integer Programming Model for Sudoku Variant |
---|
min0Dummy objective∑kxi,j,k=1∀i,jOne k in each cell∑i,j|ua,i,jxi,j,k=1∀a,kUnique values in each areavi,j=∑kk⋅xi,j,k∀i,jValue of cell−5≤vi,j−vi′,j′≤5∀i,j,i′,j′|nbi,j,i′,j′Neighbors 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,kx∗i,j,kxi,j,k≤92−1 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
- A Sudoku With Just 11 Digits = Dutch Genius!, https://www.youtube.com/watch?v=ZU5fSDHJq8k
- 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