Ask Your Question

Is there anything in Sage which solves #SAT ?

asked 2018-10-19 11:29:29 +0200

Sébastien gravatar image

updated 2018-10-20 17:36:04 +0200

tmonteil gravatar image

For example using sharpsat or relsat or Dsharp?

edit retag flag offensive close merge delete


It's possible you will find this SAT solver information useful, but probably not since you are a seasoned Sage pro and probably #SAT is not the same thing as SAT ... nonetheless I'll leave this as a comment in case it helps people seeing this know that's not what you're looking for.

kcrisman gravatar imagekcrisman ( 2018-10-19 17:35:37 +0200 )edit

@kcrisman : SAT is about telling whether there is a solution to some boolean formula, #SAT is about telling how many solutions there are.

tmonteil gravatar imagetmonteil ( 2018-10-20 16:41:27 +0200 )edit

1 Answer

Sort by » oldest newest most voted

answered 2018-10-20 16:39:54 +0200

tmonteil gravatar image

updated 2018-10-20 18:29:46 +0200

Since the solutions of a SAT formula corresponds to integer points of some polytope, you can solve #SAT by asking Sage to construct that polytope and count its integer points. See the code below.

The default integral_points_count for rational polytopes basically enumerates all integer points, so the choice of the backend could have an importance on the speed. I bet normaliz will be faster, it requires the pynormaliz optional package to be installed. Please report about your real-life experiments.

Also, the latte_int optional package could speed things in some cases, please report your experiments, since that could depend on the actual problem (on the code below, you should use latte=True to use it).

That said for further improvements, we really need to know your real problem.

Typically, the polytopes we consider are included in the unit cube of large dimension, hence I do not see how Barvinok theory (which is what latte_int relies on) could speed things up since no set of >=3 points is aligned.

So, depending on your problem, one could imagine to describe it with a "fatter" polytope of smaller dimension, whithout reducing it to SAT first, so that Barvinok theory could be much more efficient.

Other approaches could be thought about too. For example, given a SAT formula, it is easy to enumerate all solution with a SAT solver: for each solution, add its "opposit" as a new constraint and solve it again. I have plans to add such an __iter__ method to the current SAT solvers in Sage.

def sharp_sat(clauses, backend='cdd', latte=False):
    Count the number of solutions of the given CNF formula.


    - clauses is a list of lists of integers representing a CNF formula in the
      DIMACS notation, for example:

      [[1,2,-3], [-1,2]] means

          (x1 or x2 or (not x3)) and ((not 1) or 2)

    - backend : one of `'cdd'`, `'normaliz'`, `'ppl'`, `'polymake'`.

    - latte : if `True`, use `latte_int` (optional package to be installed) for
      the computation.
    p = MixedIntegerLinearProgram(solver='ppl')
    x = p.new_variable(binary=True)
    for clause in clauses:
        p.add_constraint(sum([x[i] if i>0 else (1-x[-i]) for i in clause]) >= 1)

    P = p.polyhedron(backend=backend)
    if latte:
        return P.integral_points_count(explicit_enumeration_threshold=0)
        return P.integral_points_count()
edit flag offensive delete link more


Thank you for your idea of using integral_points_count. I will compare the speed with dancing links which was the only way I knew to count solutions.

Sébastien gravatar imageSébastien ( 2018-10-22 17:40:06 +0200 )edit

For an small typical example I have,

sage: p
Boolean Program (maximization, 275 variables, 65 constraints)
sage: p.polyhedron(backend=backend) # takes forever
Sébastien gravatar imageSébastien ( 2018-10-25 16:17:20 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2018-10-19 11:29:29 +0200

Seen: 633 times

Last updated: Oct 20 '18