PySAT: A Python Library for SAT Oracles

1.9.dev2 · active · verified Thu Apr 16

PySAT is a Python library that provides a simple and unified incremental interface to a variety of state-of-the-art Boolean satisfiability (SAT) solvers. It enables researchers and developers to easily prototype with SAT oracles in Python, exploiting the power of low-level C/C++ implementations of modern SAT solvers. The library offers various propositional encodings for linear (cardinality and pseudo-Boolean) constraints. It is currently at version 1.9.dev2 and follows a rolling release model with frequent, small updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a Conjunctive Normal Form (CNF) formula and solve it using different SAT solvers available through the PySAT interface. It also shows how to get a model or an unsatisfiable core if applicable.

from pysat.solvers import Solver
from pysat.formula import CNF

# Create a CNF formula for (x1 or not x2) and (not x1 or x2)
cnf = CNF()
cnf.append([1, -2])
cnf.append([-1, 2])

# Initialize a Glucose3 solver
with Solver(name='glucose3') as solver:
    solver.append_formula(cnf)
    
    if solver.solve():
        model = solver.get_model()
        print(f"Satisfiable. Model: {model}")
    else:
        print("Unsatisfiable.")

# Example with specific solver import
from pysat.solvers import Minisat22
with Minisat22(bootstrap_with=[[-1, 2], [-2, 3]]) as m:
    print(m.solve(assumptions=[1, -3]))
    print(m.get_core())

view raw JSON →