An interface to SAT solver tools (like minisat)
Project description
SATisPy
Satispy is a Python library that aims to be an interface to various SAT (boolean satisfiability) solver applications.
Supported solvers:
Support for other solvers should be fairly easy to add as long as they accept the DIMACS CNF SAT format.
Installing
You can grab the current version from pypi:
$ sudo pip install satispy
Or you can download a copy from https://github.com/netom/satispy, and run
pip install .
in the directory.
You can run the tests found in the test folder by
./tests/run_tests.py.
All the solvers need to be installed to be able to run the tests.
How it works
You need the minisat SAT solver to be installed on your machine for this to work.
Let's see an example:
from satispy import Variable, Cnf
from satispy.solver import Minisat
v1 = Variable('v1')
v2 = Variable('v2')
v3 = Variable('v3')
exp = v1 & v2 | v3
solver = Minisat()
solution = solver.solve(exp)
if solution.success:
print "Found a solution:"
print v1, solution[v1]
print v2, solution[v2]
print v3, solution[v3]
else:
print "The expression cannot be satisfied"
This program tries to satisfy the boolean expression
v1 & v2 | v3
You can make this true by assigning true to all variables for example, but there are other solutions too. This program finds a single arbitrary solution.
First, the program imports the various classes so we can build an expression and try to solve it.
SAT solvers expect their input to be in CNF, but we don't have to enter out expression as a CNF. The Cnf class takes care of the proper arranging of the boolean terms.
Expressions can be built by creating variables and gluing them together arbitrarily with boolean operators:
- NOT: - (unary)
- AND: &
- OR: |
- XOR: ^
- IMPLICATION >>
The solver class Minisat, Picosat, Sat4j, or Lingeling is used to solve the formula.
Note: these classes create temporary files, so they need write access to the system's temporary directory
The returned solution can be checked by reading the "success" boolean flag.
Then, the solution can be queried for variable assignments by using it like a dictionary. Note that Variable objects are used, not strings.
(This very example can be found in the examples directory)
Parsing Boolean expressions
It is also possible to create a Cnf object directly, without first creating
Variable objects:
from satispy import CnfFromString
from satispy.solver import Minisat
exp, symbols = CnfFromString.create("v1 & v2 | v3")
solver = Minisat()
solution = solver.solve(exp)
if solution.success:
print "Found a solution:"
for symbol_name in symbols.keys():
print "%s is %s" % (symbol_name, solution[symbols[symbol_name]])
else:
print "The expression cannot be satisfied"
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file satispy-1.4.tar.gz.
File metadata
- Download URL: satispy-1.4.tar.gz
- Upload date:
- Size: 10.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
31661514e123663ce3d4cedd88e44fc4ef90721caa0b7c9ab6f961160013147c
|
|
| MD5 |
bcb607d0c99977d2ecec8e0f244cfa93
|
|
| BLAKE2b-256 |
2734cfddede428fc4766e59ed5bb9e779985fbea9cfd2f9d3290d1fd5f1f02cc
|
File details
Details for the file satispy-1.4-py3-none-any.whl.
File metadata
- Download URL: satispy-1.4-py3-none-any.whl
- Upload date:
- Size: 14.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6dfbbc1cab7ec4e21be9f5e5a444e9e5dba0564ac86460da99e9f1195d05a255
|
|
| MD5 |
825201f7c0d775035988a311fa9f4b93
|
|
| BLAKE2b-256 |
63db464a3e21096de40337f61e137abcbffbbe209dba23e81295ebe26aea4962
|