Package: pycryptosat
pycryptosat SAT solverThis 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.Installingpip install pycryptosatCompilingIf you don't want to use the pip package, you can compile it as:apt-get install python-dev
python -m buildTo help with debug, you can also:python setup.py bdist_wheelUsageThepycryptosatmodule has one object,Solverthat has two functionssolveandadd_clause.The funcionadd_clause()takes an iterable list of literals such as[1, 2]which represents the truth1 or 2 = True. For example,add_clause([1])sets variable1toTrue.The functionsolve()solves the system of equations that have been added
withadd_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'sTrue, 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
variable1.Thesolve()method optionally takes an argumentassumptionsthat
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, thensolve([-2])will return
UNSAT. However, a subsequent call tosolve()will still return a solution.
If instead of an assumptionadd_clause()would have been used, subsequentsolve()calls would have returned unsatisfiable.Solvertakes the following keyword arguments:time_limit: the time limit (integer)confl_limit: the propagation limit (integer)verbose: the verbosity level (integer)Bothtime_limitandconfl_limitset a budget to the solver. The former is based on time elapsed while the former is based on 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 are hit (whichever first). Warning: Results fromtime_limitmay differ from run to run, depending on compute load, etc. Useconfl_limitfor more reproducible runs.ExampleLet us consider the following clauses, represented using
the DIMACScnf <http://en.wikipedia.org/wiki/Conjunctive_normal_form>_
format::p cnf 5 3
1 -5 4 0
-1 5 3 4 0
-3 -4 0Here, we have 5 variables and 3 clauses, the first clause being
(x\ :sub:1or not x\ :sub:5or x\ :sub:4).
Note that the variable x\ :sub:2is not used in any of the clauses,
which means that for each solution with x\ :sub:2= True, we must
also have a solution with x\ :sub:2= 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 i\ :sup:thvariable::>>> 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: x\ :sub:1= x\ :sub:4= x\ :sub:5= True,
x\ :sub:2= x\ :sub:3= FalseSpecial options (e.g. LARGEMEM, etc)In case you need to e.g. have LARGEMEM, you must modifysetup.pyand add'-DLARGE_OFFSETS'toextra_compile_args. Similarly for other options.