• Stars
    star
    182
  • Rank 211,154 (Top 5 %)
  • Language
    C
  • License
    MIT License
  • Created over 11 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Python bindings to picosat (a SAT solver)

pycosat: bindings to picosat (a SAT solver)

PicoSAT is a popular SAT solver written by Armin Biere in pure C. This package provides efficient Python bindings to picosat on the C level, i.e. when importing pycosat, the picosat solver becomes part of the Python process itself. For ease of deployment, the picosat source (namely picosat.c and picosat.h) is included in this project. These files have been extracted from the picosat source (picosat-965.tar.gz).

Usage

The pycosat module has two functions solve and itersolve, both of which take an iterable of clauses as an argument. Each clause is itself represented as an iterable of (non-zero) integers.

The function solve returns one of the following:
  • one solution (a list of integers)
  • the string "UNSAT" (when the clauses are unsatisfiable)
  • the string "UNKNOWN" (when a solution could not be determined within the propagation limit)

The function itersolve returns an iterator over solutions. When the propagation limit is specified, exhausting the iterator may not yield all possible solutions.

Both functions take the following keyword arguments:
  • prop_limit: the propagation limit (integer)
  • vars: number of variables (integer)
  • verbose: the verbosity level (integer)

Example

Let us consider the following clauses, represented using the DIMACS cnf format:

p cnf 5 3
1 -5 4 0
-1 5 3 4 0
-3 -4 0

Here, we have 5 variables and 3 clauses, the first clause being (x1 or not x5 or x4). Note that the variable x2 is not used in any of the clauses, which means that for each solution with x2 = True, we must also have a solution with x2 = False. In Python, each clause is most conveniently represented as a list of integers. Naturally, it makes sense to represent each solution also as a list of integers, where the sign corresponds to the Boolean value (+ for True and - for False) and the absolute value corresponds to ith variable:

>>> import pycosat
>>> cnf = [[1, -5, 4], [-1, 5, 3, 4], [-3, -4]]
>>> pycosat.solve(cnf)
[1, -2, -3, -4, 5]

This solution translates to: x1 = x5 = True, x2 = x3 = x4 = False

To find all solutions, use itersolve:

>>> for sol in pycosat.itersolve(cnf):
...     print sol
...
[1, -2, -3, -4, 5]
[1, -2, -3, 4, -5]
[1, -2, -3, 4, 5]
...
>>> len(list(pycosat.itersolve(cnf)))
18

In this example, there are a total of 18 possible solutions, which had to be an even number because x2 was left unspecified in the clauses.

The fact that itersolve returns an iterator, makes it very elegant and efficient for many types of operations. For example, using the itertools module from the standard library, here is how one would construct a list of (up to) 3 solutions:

>>> import itertools
>>> list(itertools.islice(pycosat.itersolve(cnf), 3))
[[1, -2, -3, -4, 5], [1, -2, -3, 4, -5], [1, -2, -3, 4, 5]]

Implementation of itersolve

How does one go from having found one solution to another solution? The answer is surprisingly simple. One adds the inverse of the already found solution as a new clause. This new clause ensures that another solution is searched for, as it excludes the already found solution. Here is basically a pure Python implementation of itersolve in terms of solve:

def py_itersolve(clauses): # don't use this function!
    while True:            # (it is only here to explain things)
        sol = pycosat.solve(clauses)
        if isinstance(sol, list):
            yield sol
            clauses.append([-x for x in sol])
        else: # no more solutions -- stop iteration
            return

This implementation has several problems. Firstly, it is quite slow as pycosat.solve has to convert the list of clauses over and over and over again. Secondly, after calling py_itersolve the list of clauses will be modified. In pycosat, itersolve is implemented on the C level, making use of the picosat C interface (which makes it much, much faster than the naive Python implementation above).

More Repositories

1

conda

A system-level, binary package and environment manager running on all major operating systems and platforms.
Python
6,285
star
2

conda-pack

Package conda environments for redistribution
Python
519
star
3

conda-lock

Lightweight lockfile for conda environments
Python
461
star
4

constructor

tool for creating installers from conda packages
Python
453
star
5

conda-docs

Conda documentation
378
star
6

conda-build

Commands and tools for building conda packages
Python
375
star
7

grayskull

Grayskull πŸ’€ - Recipe generator for Conda
Python
306
star
8

rattler

Rust crates to work with the Conda ecosystem.
Rust
260
star
9

conda-libmamba-solver

The libmamba based solver for conda.
Python
188
star
10

cookiecutter-conda-python

A cookiecutter template for conda packages using Python
Python
102
star
11

menuinst

Cross platform menu item installation
Python
34
star
12

conda-package-handling

Create and extract conda packages of various formats
Python
26
star
13

governance

The Conda & Conda-Incubator Governance Policy
Python
25
star
14

ceps

Conda Enhancement Proposals
19
star
15

conda-verify

Verify conda recipes and packages
Python
17
star
16

conda-prefix-replacement

CPR resuscitates packages in new locations
Python
11
star
17

infrastructure

A repo to report issues and have discussions about the conda infrastructure
Python
11
star
18

conda-index

conda index, formerly part of conda-build. Create channels from collections of packages.
Python
9
star
19

schemas

Conda file formats and schemas
Python
9
star
20

conda-package-streaming

An efficient library to read from new and old format .conda and .tar.bz2 conda packages.
Python
9
star
21

conda-plugin-template

Python
8
star
22

conda-content-trust

Signing and verification tools for conda
Python
7
star
23

conda-benchmarks

Benchmarking the conda project
Python
4
star
24

issue-tracker

A visualization of the issue count of conda org repositories over time.
Python
2
star
25

conda_build_test_recipe

This repo is used for testing conda-build
Python
2
star
26

actions

A collection of custom GitHub Actions for the conda org.
Python
1
star
27

test-cla

1
star
28

communications

Repo to organize conda communications work
1
star
29

conda-standalone

A standalone conda executable built with PyInstaller.
Python
1
star