Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Gudoku is a differentiable Sudoku solver that leverages gradient-based optimization to solve Sudoku puzzles using PyTorch

License

Notifications You must be signed in to change notification settings

Estaheri7/Gudoku

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

🧠 Gudoku: A Differentiable Sudoku Solver with PyTorch

Gudoku is a differentiable Sudoku solver that leverages gradient-based optimization to solve Sudoku puzzles using PyTorch. Instead of traditional backtracking or constraint propagation, it encodes Sudoku rules as differentiable loss functions, enabling learning-based or gradient descent-based puzzle solving.


🌀 Project Structure

This notebook is divided into two main phases:

Phase 1: Optimization-Based Solver

Uses gradient descent and continuous relaxation to solve Sudoku by minimizing constraint violations directly. No training is involved — the puzzle is solved purely through optimization.

Phase 2: Neural Network-Based Solver

Uses a trainable neural network to learn a mapping from partially-filled Sudoku grids to complete solutions. A custom loss function enforces Sudoku rules during training, and fixed clues are respected during both training and inference.


🧩 What is Sudoku?

Sudoku is a combinatorial logic puzzle composed of a $9 \times 9$ grid divided into $3 \times 3$ subgrids (boxes). The goal is to fill all empty cells so that:

  • Each row contains the digits $1$ through $9$ (no repeats)
  • Each column contains the digits $1$ through $9$ (no repeats)
  • Each $3 \times 3$ box contains the digits $1$ through $9$ (no repeats)

Objective

This project explores differentiable programming techniques for constraint satisfaction problems. Sudoku rules are expressed as soft differentiable constraints, allowing gradient-based methods to operate on a problem typically solved by discrete logic.


🧮 Mathematical Formulation

Let the Sudoku grid be a 9 x 9 x 9 tensor X, where:

  • X[i,j,k] = 1 if cell (i, j) is assigned number k + 1
  • X[i,j,k] ∈ [0, 1] during optimization or training

Constraints as Loss Terms

  1. Cell Constraint
    Each cell must contain exactly one number:

    For all i,j in {1,...,9}:
    sum over k=1 to 9 of X[i,j,k] = 1

  2. Row Constraint
    Each number must appear exactly once in every row:

    For all i,k in {1,...,9}:
    sum over j=1 to 9 of X[i,j,k] = 1

  3. Column Constraint
    Each number must appear exactly once in every column:

    For all j,k in {1,...,9}:
    sum over i=1 to 9 of X[i,j,k] = 1

  4. Box Constraint
    Each number must appear exactly once in every 3x3 box:

    For all a,b in {0,1,2} and k in {1,...,9}:
    sum over i=3a+1 to 3a+3 and j=3b+1 to 3b+3 of X[i,j,k] = 1

  5. Clue Constraint (for given values)
    Fixed cells are clamped to their given number:

    If cell (i,j) is fixed with number k+1:
    X[i,j,k] = 1 and X[i,j,l] = 0 for all l ≠ k


📊 Dataset

The dataset is stored in a Parquet file:

  • grid: The initial Sudoku state (0s indicate blanks)
  • solution: The full correct solution

Data is parsed into tensors for training and optimization.

You can find and download dataset here


⚙️ How It Works

Phase 1: Optimization Solver

  • Inputs: One-hot relaxed representation of unknown cells
  • Loss: Sum of squared constraint violations
  • Method: Gradient descent (e.g., Adam)

Phase 2: Neural Network Solver

  • Inputs: Partial grid (with clues)
  • Network: MLP or CNN (simple ResNet model used in this notebook)
  • Loss: Same constraint loss as optimization + optional supervised loss on known solutions

Further implementation details are available in the notebook.

📝 License

This project is licensed under the MIT License.

About

Gudoku is a differentiable Sudoku solver that leverages gradient-based optimization to solve Sudoku puzzles using PyTorch

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published