a sudoku solver using ILP
Python 100.0%
3 1 0

Clone this repository

https://tangled.org/ahmig.es/solpoku
git@tangled.org:ahmig.es/solpoku

For self-hosted knots, clone URLs may differ based on your setup.

README.md

solpoku#

a sudoku solver using integer linear programming with optlang, made for fun.

Usage#

the SudokuProblem class in solpoku.py handles the problem formulation. variables are bounded between 0 and 1, and encoded as vRCN, where R and C represent the number of the row and column (respectively) from 1 to 9, and N represents the digit. if a digit n is present at row r, column c, then vrcn is set to be maximized. maximizing the sum of variables that refer to posed digits renders a feasible sudoku solution.

the from_csv and from_json class methods are provided to facilitate handling of input files. from_array handles input numpy arrays. the solve method performs the optimization and builds a 9x9 numpy array with the proposed solution.

the gui.py script provides a simple GUI using tkinter. run it to launch it. click on the grid to change the numbers; click on "Solve" to fill the sudoku.

empty tiles are represented with zeros.

Dependencies#

a pre-made environment can be set up with:

mamba env create -p .env -f environment.yaml

other versions may work too. however, Python ≥3.10 is required.

Example#

run solpoku.py to solve the following sudoku three times: a) hard-coded in the script, b) read from a CSV file and c) read from a JSON file:

- - - - 6 - - - -
- - 5 2 1 3 8 - -
- 2 - - - - - 4 -
4 - - 9 - 1 - - 8
- - 6 4 8 5 3 - -
1 - - 7 - 6 - - 4
- 6 - - - - - 2 -
- - 1 3 4 9 7 - -
- - - - 7 - - - -