Series: Modeling logic puzzles
Phần: 1 / 91. Introduction
Sudoku, originally called Number Place, is a logic-based, combinatorial number-placement puzzle. In classic Sudoku, the objective is to fill a grid with digits so that each column, each row, and each of the nine subgrids that compose the grid (also called "boxes", "blocks", or "regions") contains all of the digits from 1 to 9. The puzzle setter provides a partially completed grid, which for a well-posed puzzle has a single solution.
Source: Wikipedia

2. Modeling the puzzle
Some symbols used in this problem
- is a perfect square representing the size of the grid.
- is the set of pairs representing the coordinates of the pre-filled cells in the grid.
- is the number filled in the cell
- is the set of cells in the -th block().
2.1. Decision variables
For each cell in the grid and a number , we define a variable such that
2.2. Constraints
- Each cell contains only one number.
- Each row contains all numbers from 1 to
- Each column contains all numbers from 1 to
- Each block contains all numbers from 1 to
- Pre-filled cells
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
Sudoku is one of the fundamental exercises for mastering modeling skills. When I first started learning modeling, I was very impressed by the idea of using a three-dimensional binary variable for this problem. All the remaining constraints of the problem were modeled very easily with this variable assignment. This is also a common technique used in modeling, and we will encounter these binary variables again in future articles of this series.
For more Sudoku puzzles and variations, please refer to Krazydad. The Python code for Sudoku modeling is available at Tung-hehe
Happy modeling!


