Solving SUDOKU with Binary Integer Linear Programming(BILP)
Last Updated on September 17, 2022 by Editorial Team
Author(s): Harjot Kaur
Originally published on Towards AI the World’s Leading AI and Technology News and Media Company. If you are building an AI-related product or service, we invite you to consider becoming an AI sponsor. At Towards AI, we help scale AI and technology startups. Let us help you unleash your technology to the masses.
Background
Sudoku is a logic-based puzzle that ο¬rst appeared in the U.S. under the title βNumber Placeβ in 1979 in the magazine Dell Pencil Puzzles & Word Games [6]. The game was designed by Howard Garns, an architect who, upon retirement, turned to puzzle creation. In the 1980s, the game grew in popularity in Japan and was renamed by publisher Nikoli to βsuji wa dokushin ni kagiru,β which translates as βthe digits must remain single.β This was eventually shortened to βsudokuβ or βsingleΒ number.β
Heads upβββassuming readers already know the basics of Linear Programming
Is Sudoku an optimization problem?
No. Mostly not. Because there is no objective function we want to maximize or minimize. The sudoku problem can be called a satisfiability or feasibility problem.
Letβs begin with theΒ rulesβ¦
Sudoku most commonly appears in its 9 Γ9 matrix form. The rules are simple: ο¬ll in the matrix so that every row, column, and 3 Γ3 submatrix contains the digits 1 through 9 exactly once. Each puzzle appears with a certain number of givens. The number and location of these determine the gameβs level of diο¬culty.
As we know, each ILP problem is composed of an objective function(not applicable in the case of Sudoku unless there are alternate solutions), decision variables, and constraints.
The good news is that we donβt write constraints for this, we just mathematically put up the rules of the puzzle as constraints!
Integer Programming mathematical model of the SudokuΒ puzzle
For ease of explanation, Iβll be using a 4×4 matrix up ahead. Let us consider the following puzzle to be solved usingΒ BILP.
More speciο¬cally, we will formulate a binary integer program (BILP) for a general nΓn puzzle. Once the program is developed to solve the BILP for Figure 1, it can be easily adapted to solve any SudokuΒ puzzle.
To begin, we deο¬ne our decision variables:
When the values of the decision variables are determined, we will know whether each integer k(1 β€kβ€4) appears in each element (i, j) of the nΓn Sudoku matrix. That is, the solution to the corresponding Sudoku puzzle will beΒ deο¬ned.
We now turn to the ever-important objective function and set of constraints. Notice how the constraints require only a knowledge of the rules of Sudoku puzzles (with the addition of one implicit constraint, which is a constraint (6) below) and do not require a proο¬ciency with the logic necessary to solve such puzzles by hand. A BILP formulation suitable for Sudoku puzzles is asΒ follows:
Min 0 (let us define this as a constant, as there is no function to minimize or maximize)
Subject to
Constraint 1: Each cell contains a single integerΒ k
Constraint 2: Each integer k appears once in eachΒ row
Constraint 3: Each integer k appears once in eachΒ column
Constraint 4: Each integer k appears once in each submatrix
Constraint 5: Given elements G in the matrix are setΒ βonβ
Constraint 6: Defining the possible value ofΒ integers
And, thatβsΒ that!
Letβs solve the above 4×4 Sudoku puzzle(Figure1) using the Excel Solver. The simulation template for a 4×4 puzzle with 64 binary decision variables will look something likeΒ this.
Now, letβs interpret the aforementioned constraints:
Constraint 1: Each cell contains a single integer k. Tabs colored in green(Sum of i,j = 1) will make sure that every position is filled with an integerΒ value.
Constraint 2: Each integer k appears once in each row. Tabs colored in brick red(Sum of row 1:4 >= 1) will ensure that each k appears just once in everyΒ row.
Constraint 3: Each integer k appears once in each column. Tabs colored in olive (Sum of column 1:16 >= 1) will ensure that each k appears just once in everyΒ column.
Constraint 4: Each integer k appears once in each submatrix. Tabs colored in turquoise(Sum of quadrant 1:4 >= 1) will ensure that each k appears just once in every 2×2Β matrix.
Constraint 5: Given elements G in the matrix are set βonβ. The given value of k is hardΒ coded.
The last step is to convert the Integer Program solution as given in the 64 yellow, peach, grey and blue decision variable cells to the required 4 by 4 Sudoku grid. Each binary cell is then multiplied by the possible set of values a βkβ can take(which is {1,2,3,4}).
Now letβs get the solutionβ¦
This is how the Excel Solver shall lookΒ like
β¦and Voila! The solution to the 4×4 puzzle shall look likeΒ this.
Last words
The above logic can be used not just to solve any Sudoku puzzle but also to create one. I hope, you folks found it easy to grasp and fun too! Connect with me on Linkedin to talk more about optimization.
Solving SUDOKU with Binary Integer Linear Programming(BILP) was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.
Join thousands of data leaders on the AI newsletter. Itβs free, we donβt spam, and we never share your email address. Keep up to date with the latest work in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aΒ sponsor.
Published via Towards AI