Skip to main content

A collection of SAT and SMT solvers for solving Sudoku puzzles

Project description

Sudoku-SMT-Solvers

Pytest + CI/CD Coverage Status Docs Build Deployment Documentation

About

This repository contains the code for the study "Evaluating SMT-Based Solvers on Sudoku". Created by Liam Davis (@liamjdavis) and Tairan "Ryan" Ji (@TairanJ) as their for COSC-241 Artificial Intelligence at Amherst College, it evaluates the efficacy of SMT-Based Solvers by benchmarking three modern SMT solvers (DPLL(T), Z3, and CVC5) against the DPLL algorithm on a collection of 100 25x25 Sudoku puzzles of varying difficulty. The corresponding paper can be found here.

Along with the study, we also published sudoku-smt-solvers, a Python package that provides the various SMT-based Sudoku solvers and benchmarking tools we built for this study. The package features DPLL(T), Z3, and CVC5 solvers optimized for 25x25 Sudoku puzzles, a puzzle generator for creating test cases, and a comprehensive benchmarking suite. Available through pip, it offers a simple API for solving Sudoku puzzles using state-of-the-art SMT solvers while facilitating performance comparisons between different solving approaches.

The study aims to answer three research questions:

  1. How have logical solvers evolved over time in terms of performance and capability?
  2. How do different encodings of Sudoku affect the efficiency and scalability of these solvers?
  3. Are there specific features or optimizations in SMT solvers that provide a significant advantage over traditional SAT solvers for this class of problem?

Getting started

Installation

To run the code locally, you can install with pip

pip install sudoku-smt-solvers

Solvers

This package includes the DPLL solver and three modern SMT solvers:

  • DPLL(T)
  • CVC5
  • Z3

To run any of the solvers on a 25x25 Sudoku puzzle, you can create an instance of the solver class and call the solve method in a file at the root (Sudoku-smt-solvers). Here is an example using Z3:

from sudoku_smt_solvers import Z3Solver

# Example grid (25x25)
grid = [[0] * 25 for _ in range(25)]
solver = Z3Solver(grid)
solution = solver.solve()

if solution:
    print(f"Solution:\n\n{solution}")
else:
    print("No solution exists.")

Sudoku Generator

This package also includes a generator for creating Sudoku puzzles to be used as benchmarks. To generate a puzzle, create an instance of the SudokuGenerator class and call the generate method. Here is an example:

from sudoku_smt_solvers import SudokuGenerator

generator = SudokuGenerator(size = 25, givens = 80, timeout = 5, difficulty = "Medium", puzzles_dir = "benchmarks/puzzles", solutions_dir = "benchmarks/solutions")

generator.generate()

Due to the computational complexity of generating large sudoku puzzles, it is recommended that you run multiple generator instances in parallel to create benchmarks.

Benchmark Runner

To run the benchmarks you created on all four solvers, create an instance of the BenchmarkRunner class and call the run_benchmarks method. Here is an example:

from sudoku_smt_solvers import BenchmarkRunner

runner = BenchmarkRunner(
    puzzles_dir='resources/benchmarks/puzzles/',
    results_dir='results/'
)
runner.run_benchmarks()

Contributing

We welcome contributions in the form of new solvers, additions to our benchmark suite, or anything that improves the tool! Here's how to get started:

Development Setup

  1. Fork and Clone:
    Begin by forking the repository and cloning your fork locally:

    git clone https://github.com/yourusername/Sudoku-SMT-Solvers.git
    cd Sudoku-SMT-Solvers
    
  2. Create and Activate a Virtual Environment:
    Set up a Python virtual environment to isolate your dependencies:

    python3 -m venv venv
    source venv/bin/activate  # On Windows, use `venv\Scripts\activate`
    
  3. Install Dependencies:
    Install the required dependencies from the requirements.txt file:

    pip install -r requirements.txt
    
  4. Set Up Pre-Commit Hooks:
    Install and configure pre-commit hooks to maintain code quality:

    pip install pre-commit
    pre-commit install
    

    To manually run the hooks and verify code compliance, use:

    pre-commit run
    
  5. Testing and Coverage Requirements:

    • Write tests for any new code or modifications.
    • Use pytest for running tests:
      pytest
      
    • Ensure the test coverage is at least 90%:
  6. Add and Commit Your Changes:

    • Follow the existing code style and structure.
    • Verify that all pre-commit hooks pass and the test coverage meets the minimum requirement.
    git add .
    git commit -m "Description of your changes"
    
  7. Push Your Branch: Push your changes to your forked repository:

    git push origin your-branch-name
    
  8. Open a PR for us to review


Thank you for your interest in contributing to Sudoku-SMT-Solvers! Your efforts help make this project better for everyone.

Contact Us

For any questions or support, please reach out to Liam at ljdavis27 at amherst.edu and Ryan at tji26 at amherst.edu

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sudoku_smt_solvers-1.0.0.tar.gz (25.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sudoku_smt_solvers-1.0.0-py3-none-any.whl (23.0 kB view details)

Uploaded Python 3

File details

Details for the file sudoku_smt_solvers-1.0.0.tar.gz.

File metadata

  • Download URL: sudoku_smt_solvers-1.0.0.tar.gz
  • Upload date:
  • Size: 25.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for sudoku_smt_solvers-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a8562ff49678b12c51bb9e1e39b7aaa7a8f4c93f4156057c7cc6ebb438110a31
MD5 c4d553388aa1405e63a493ba544b68cc
BLAKE2b-256 71a5c17e8b63ef09217a263089eb3bee43c80e5e9964e00c3fdd56dfb9e646c0

See more details on using hashes here.

Provenance

The following attestation bundles were made for sudoku_smt_solvers-1.0.0.tar.gz:

Publisher: publish.yml on liamjdavis/Sudoku-SMT-Solvers

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sudoku_smt_solvers-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sudoku_smt_solvers-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 efb69126c3a72ea74e9fa0aafda35f0ef7f78c57234d3d262f3e114060f29359
MD5 11c14066158eb13a7892b162eb55a8c5
BLAKE2b-256 665e62a45979075cff678eca0ce39abdbc434b3e83307f4228f3ec6caa5f29ea

See more details on using hashes here.

Provenance

The following attestation bundles were made for sudoku_smt_solvers-1.0.0-py3-none-any.whl:

Publisher: publish.yml on liamjdavis/Sudoku-SMT-Solvers

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page