https://github.com/lorransutter/sudoku
Solve Sudoku using linear programming
https://github.com/lorransutter/sudoku
gurobi gurobipy linear-programming problem-solving puzzle python python3 sudoku
Last synced: 3 months ago
JSON representation
Solve Sudoku using linear programming
- Host: GitHub
- URL: https://github.com/lorransutter/sudoku
- Owner: LorranSutter
- License: lgpl-3.0
- Created: 2017-08-24T04:09:00.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2022-05-20T01:23:06.000Z (about 4 years ago)
- Last Synced: 2025-01-01T05:30:02.028Z (over 1 year ago)
- Topics: gurobi, gurobipy, linear-programming, problem-solving, puzzle, python, python3, sudoku
- Language: Python
- Homepage:
- Size: 1.37 MB
- Stars: 0
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
Sudoku
Project created to solve sudoku game for any square dimension (9x9, 16x16, 25x25 ...) employing linear problem concepts.
You can also check a linear solution to solve Radar Positioning problem here.
How to run |
Dependencies |
What is sudoku? |
What is linear programming? |
Sudoku model |
Credits
## :runner: How to run
```sh
python3 LP_Sudoku.py sudoku_9x9.txt
```
Input example
```
9
3 0 6 5 0 8 4 0 0
5 2 0 0 0 0 0 0 0
0 8 7 0 0 0 0 3 1
0 0 3 0 1 0 0 8 0
9 0 0 8 6 3 0 0 5
0 5 0 0 9 0 6 0 0
1 3 0 0 0 0 2 5 0
0 0 0 0 0 0 0 7 4
0 0 5 2 0 6 3 0 0
```
Output example
```
3 1 6 | 5 7 8 | 4 9 2
5 2 9 | 1 3 4 | 7 6 8
4 8 7 | 6 2 9 | 5 3 1
-------------------------------
2 6 3 | 4 1 5 | 9 8 7
9 7 4 | 8 6 3 | 1 2 5
8 5 1 | 7 9 2 | 6 4 3
-------------------------------
1 3 8 | 9 4 7 | 2 5 6
6 9 2 | 3 5 1 | 8 7 4
7 4 5 | 2 8 6 | 3 1 9
```
## :pencil: Dependencies
gurobipy library must be installed.
Installation instructions in [Gurobi](https://www.gurobi.com/documentation/9.0/quickstart_mac/py_python_interface.html).
---
## :game_die: What is sudoku?
Sudoku is a logic puzzle game with the objective to fill a 9x9 grid with digits from 1 to 9. It respects a set of rules to fill this grid:
- Each row must have all digits from 1 to 9
- Each column must have all digits from 1 to 9
- Each of the nine 3x3 subgrid must have all digits from 1 to 9
## :pencil2: What is linear programming?
Linear programming is a technique used to solve optimization problems where the elements have a linear relationship.
Linear programs aims to maximize a **objective function** made of **decision variables** subject to **constraints** which ensures that all the elements have a linear relationship and all variables are non-negative.
## :triangular_ruler: Sudoku model
In this case we just want to find a combination of variables that solves the puzzle. Therefore there will be no objetive function do be maximized, only linear constraints as follows:
$$\sum_{i=1}^nx_{ijk} \ = \ 1 \ \text{for} \ j,k \ \in \ [1,n]$$
$$\sum_{j=1}^nx_{ijk} \ = \ 1 \ \text{for} \ i,k \ \in \ [1,n]$$
$$\sum_{k=1}^nx_{ijk} \ = \ 1 \ \text{for} \ i,j \ \in \ [1,n]$$
$$\sum_{j=3p-2}^{3p} \ \sum_{i=3q-2}^{3q}x_{ijk} \ = \ 1 \ \text{for} \ k \ \in \ [1,n] \ \text{and} \ p,q \ \in \ \left[ 1,\sqrt n \right]$$
We want to generalize the problem to solve a sudoku of any square dimension (9x9, 16x16, 25x25 ...). For that purpose, $n$ represents the dimension of the puzzle, $x_{ijk}$ are decision variables, $i$ represents the columns, $j$ represents the rows, $k$ represents all possible digitis depending on the puzzle dimension, and $p$ and $q$ represents an auxiliar variable to iterate in all subgrids.
The first and the second constraints ensures that all columns and all rows will be filled must have only one of the available digits. The third constraint ensures that each cell in the grid will have only one digit. The last constraint ensures that all subgrid will have only one of the available digits.
---
## :cookie: Credits
Thanks to [Harshit Sidhwa](https://github.com/harshitsidhwa?tab=repositories) in providing back tracking [code](https://repl.it/@rui1337/backtrack-sudoku-solver) as an inspiration to solve this problem.