pycosat

raw JSON →
0.6.6 verified Sat May 09 auth: no python maintenance

Python bindings to PicoSAT, a popular SAT solver written in C. Version 0.6.6 is the latest. The library is mature but receives infrequent updates; it is in maintenance mode.

pip install pycosat
error AttributeError: module 'pycosat' has no attribute 'solve'
cause pycosat does not expose a `solve` function; use `itersolve` or `blocksolve` instead.
fix
Use next(pycosat.itersolve(cnf)) or list(pycosat.itersolve(cnf)).
error MemoryError: Unable to allocate ...
cause pycosat uses the PicoSAT library which may allocate large arrays for huge CNF formulas. Also, a memory leak in versions <0.6.4 could cause OOM.
fix
Upgrade to 0.6.4+ and consider splitting the problem into smaller chunks.
deprecated The function `solve` is not available; use `itersolve` or `blocksolve`.
fix Replace `pycosat.solve(...)` with `next(pycosat.itersolve(...))` for a single solution.
breaking In version 0.6.4, a memory leak was fixed in `blocksolve()`. If you rely on heavy iteration, upgrade to avoid cumulative memory usage.
fix Upgrade to 0.6.4 or later: `pip install --upgrade pycosat`.
gotcha pycosat only accepts DIMACS CNF format, not other SAT formats like DIMACS WCNF or OPB. Variables must be positive integers.
fix Convert your problem to DIMACS CNF (list of lists of non-zero integers, no 0).

Solve a simple CNF SAT problem using the generator itersolve.

import pycosat
# Solve a SAT problem: each clause is a list of integers
cnf = [[1, -2], [-1, 3], [-2, -3]]
solution = pycosat.itersolve(cnf)
for sol in solution:
    print(sol)