Bindings to CryptoMiniSat 5.6.9 (a SAT solver)
Project description
This directory provides Python bindings to CryptoMiniSat on the C++ level, i.e. when importing pycryptosat, the CryptoMiniSat solver becomes part of the Python process itself.
Compiling
The pycryptosat python package compiles while compiling CryptoMiniSat. It cannotbe compiled on its own, it must be compiled at the same time as CryptoMiniSat. You will need the python development libraries in order to compile:
apt-get install python-dev
After this, cmake then indicate that pycryptosat will be compiled:
cd cryptominisat mkdir build cd build cmake .. [...] -- Found PythonInterp: /usr/bin/python2.7 (found suitable version "2.7.9", minimum required is "2.7") -- Found PythonLibs: /usr/lib/x86_64-linux-gnu/libpython2.7.so (found suitable version "2.7.9", minimum required is "2.7") -- PYTHON_EXECUTABLE:FILEPATH=/usr/bin/python2.7 -- PYTHON_LIBRARY:FILEPATH=/usr/lib/x86_64-linux-gnu/libpython2.7.so -- PYTHON_INCLUDE_DIR:FILEPATH=/usr/include/python2.7 -- PYTHONLIBS_VERSION_STRING=2.7.9 -- OK, found python interpreter, libs and header files -- Building python interface [...]
It will then generate the pycryptosat library and install it when calling make install.
Usage
The pycryptosat module has one object, Solver that has two functions solve and add_clause.
The funcion add_clause() takes an iterable list of literals such as [1, 2] which represents the truth 1 or 2 = True. For example, add_clause([1]) sets variable 1 to True.
The function solve() solves the system of equations that have been added with add_clause():
>>> from pycryptosat import Solver >>> s = Solver() >>> s.add_clause([1, 2]) >>> sat, solution = s.solve() >>> print(sat) True >>> print(solution) (None, True, True)
The return value is a tuple. First part of the tuple indicates whether the problem is satisfiable. In this case, it’s True, i.e. satisfiable. The second part is a tuple contains the solution, preceded by None, so you can index into it with the variable number. E.g. solution[1] returns the value for variable 1.
The solve() method optionally takes an argument assumptions that allows the user to set values to specific variables in the solver in a temporary fashion. This means that in case the problem is satisfiable but e.g it’s unsatisfiable if variable 2 is FALSE, then solve([-2]) will return UNSAT. However, a subsequent call to solve() will still return a solution. If instead of an assumption add_clause() would have been used, subsequent solve() calls would have returned unsatisfiable.
- Solver takes the following keyword arguments:
time_limit: the time limit (integer)
confl_limit: the propagation limit (integer)
verbose: the verbosity level (integer)
Both time_limit and confl_limit set a budget to the solver. The former is based on time elapsed while the latter is based on the number of conflicts met during search. If the solver runs out of budget, it returns with (None, None). If both limits are used, the solver will terminate whenever one of the limits is reached (whichever comes first). Warning: Results from time_limit may differ from run to run, depending on compute load, etc. Use confl_limit for more reproducible runs.
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 pycryptosat >>> solver = pycryptosat.Solver() >>> solver.add_clause([1, -5, 4]) >>> solver.add_clause([-1, 5, 3, 4]) >>> solver.add_clause([-3, -4]) >>> solver.solve() (True, (None, True, False, False, True, True))
This solution translates to: x1 = x4 = x5 = True, x2 = x3 = False
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 Distributions
Hashes for pycryptosat-0.7.0.dev3-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1603dc550fcd3836f1f0e5e826a949d2240e2edbb1f47dcd6e4e7b0703b84bf5 |
|
MD5 | 65ee55c82657c13e5cf72d95b2b09a22 |
|
BLAKE2b-256 | 3cbf8826fa6b88c4a7b189c969da16df003b448fe424d1f49d9a6f806c1e43d7 |
Hashes for pycryptosat-0.7.0.dev3-cp27-cp27mu-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b0bead36263efbecbfe51b54523ab3f03391c66af069d4ba0cf770ceaa6ab18d |
|
MD5 | 057fbe7acf0eed4d3c86c2b4d1e0631d |
|
BLAKE2b-256 | 1b0f18aca37caed4236036e7d6cad31b9021427c5f611e75fa5ce3cd40267494 |
Hashes for pycryptosat-0.7.0.dev3-cp27-cp27m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2c00a49fc12fff42e189f497343d621e898d2b96f1e65c3883b164c0ddc3a280 |
|
MD5 | aebfdb4f5acd305b85e662ad594b1c86 |
|
BLAKE2b-256 | 9c7e5a971863c3f44f02bbfc7854fecaa9c98d76438807ae6058523beb902eca |