
Modern SAT solvers: fast, neat and underused - dmit
https://codingnest.com/modern-sat-solvers-fast-neat-and-underused-part-1-5-of-n/
======
ladberg
SAT solvers are something I would like to use more, but I feel like I simply
never come across real world problems that they work well with. I've tried to
use them for program/circuit synthesis, but the problem was intractable at any
scale larger than a tiny proof-of-concept example.

Does anyone have good suggestions for what to use SAT solvers for?

~~~
nightcracker
At a local group we have weekly meetings where we cook dinner. Two people
cook, and two people do the dishes.

I used a SAT solver (z3) to find optimal schedules given that people want the
tasks to be distributed fairly, but also given other secondary criteria such
as mixing up which pairs cook together, prevent being assigned a task two
weeks in a row or getting the same task twice in a row, etc.

~~~
nurettin
It is pretty easy to come up with an integer space in order to simulate week
days, the concept of consecutiveness and homogeneous distribution.

However, as soon as we get custom rules and exceptions things start to become
impossible and we have to start inventing a best effort solution which turns
into minimization or maximization of each human variable.

At this point some humans with higher status want to be favoured and you get a
tug of war instead of an sat solver problem where nothing you suggest pleases
anyone because they have a life and can't afford to tell you every little
detail that interests them and they go back to manually picking their dates.

~~~
wenc
> they go back to manually picking their dates.

I'm not sure if it's all or nothing.

SAT are concerned with satisfying constraints, so you get feasible solutions.
A feasible solution merely satisfies constraints, and does not take objectives
into account.

The optimization-equivalent is an Integer Program (IP), which gives you
optimal solutions for a given objective function while satisfying all
constraints. Most complex scheduling problems are solved as IPs or MIPS (Mixed
Integer Programs).

Priorities can be modeled as weights. Even if everybody's preferences cannot
be met, you can arrive at some sort of weighted compromise (either in a least-
squares sense, or some user-defined distance metric)

The best MIP solvers today (e.g. CPLEX, Gurobi, Xpress) solve problems with
100k variables without breaking a sweat (caveat: they do not handle
nonconvexities, but good modelers know how to develop linear/convex
formulations). They are used in applications ranging from hospital scheduling
to airline scheduling to oil refinery scheduling.

In my experience computer scientists typically know about SAT solvers but only
a small subset know about MIP solvers. Knowing about the latter expands one's
horizons on what's possible.

~~~
salty_biscuits
... only a small subset know about MIP solvers

And an even smaller subset are willing to pay to use gurobi or cplex. It would
be so nice if the open source alternatives had competitive performance!

~~~
nurettin
In your experience, does google's or-tools library have any chance in
comparison?

~~~
disjhshava
I didn't benchmark it and can't give a speed comparison, but I had success
solving a pretty big optimization problem with GLOP, which I beleive uses or-
tools.

------
53x15
Sudoku may be a poor example for illustrating some of the overarching points
of this series of blog posts, which I take to be that modern SAT solvers are,
as someone described them in a previous thread, "little diamonds of
engineering", that they embody decades of research, that you can exploit their
power cheaply and easily, and that it might take significant effort to beat
them if you have a non-trivial problem.

Since specialized solvers _do_ thrash SAT-based solvers for conventional
Sudoku, the author focuses the example less on absolute performance and more
on simplicity, rapid prototyping, and comparison to casual non-SAT
implementations. But conventional Sudoku is a small problem, and in most cases
the SAT-based solver makes so few decisions that it doesn't benefit much from
CDCL. Consider here[1] the comparison to JCZSolve(ZSolver) that the author
mentions in part 1 of the series:

For 17-clue puzzles (which are generally very easy), JCZSolve is 50x as fast
as Minisat, and it makes on average ~1.9 decisions per puzzle compared to
Minisat's ~3.0. But if you look at the hardest dataset JCZSolve is only 10x as
fast as Minisat, and it makes on average ~365 decisions per puzzle compared to
Minisat's ~121. I'm not aware of a highly specialized and optimized Solver for
16x16 clue or larger Sudoku, but given this trend my guess is that it would
take a lot of effort to write one that's faster than what you get with Minisat
for free.

[1]
[https://github.com/t-dillon/tdoku/blob/master/benchmarks/GCE...](https://github.com/t-dillon/tdoku/blob/master/benchmarks/GCE-c2-standard-4_clang-8_O3_native)

------
T-R
SAT solvers are starting to be used as tooling for functional languages -
Liquid Haskell uses one for refinement types[1], and Djinn uses one for
suggesting functions given a type[2]. Similarly to Djinn, Edwin Brady gave a
presentation on using an SMT solver to do live code suggestions/implementation
inference in the successor to Idris[3].

[1] [https://ucsd-progsys.github.io/liquidhaskell-blog/](https://ucsd-
progsys.github.io/liquidhaskell-blog/)

[2] [http://lambda-the-ultimate.org/node/1178](http://lambda-the-
ultimate.org/node/1178)

[3]
[https://www.youtube.com/watch?v=mOtKD7ml0NU](https://www.youtube.com/watch?v=mOtKD7ml0NU)

------
MaxBarraclough
Links to previous HN discussions of this article:

[https://news.ycombinator.com/item?id=19953213](https://news.ycombinator.com/item?id=19953213)

[https://news.ycombinator.com/item?id=17684502](https://news.ycombinator.com/item?id=17684502)

[https://news.ycombinator.com/item?id=17778770](https://news.ycombinator.com/item?id=17778770)

------
bcherny
Previously posted 4 months ago and 1 year ago:
[https://hn.algolia.com/?dateRange=all&page=0&prefix=false&qu...](https://hn.algolia.com/?dateRange=all&page=0&prefix=false&query=modern%20sat%20solvers%3A%20fast&sort=byPopularity&type=story)

------
pthariensflame
If you’re looking for a higher-level interface to SAT, SMT, and general
constraint solvers, I can recommend MiniZinc:
[https://www.minizinc.org/index.html](https://www.minizinc.org/index.html)

------
vector_spaces
Whenever I read about SAT solvers I think of Prolog and Minikanren and
friends. Is there a relationship there? Do they use similar algorithms? I know
that backtracking (mentioned in the article) is a thing in Prolog at least.

~~~
triska
Yes, there are strong connections: In Prolog terminology, SAT is called
CLP(B), constraint logic programming over _Boolean_ variables, and several
Prolog systems provide dedicated libraries to solve SAT instances efficiently.

For instance, SICStus Prolog ships with a nice CLP(B) solver:

[https://sicstus.sics.se/sicstus/docs/latest3/html/sicstus.ht...](https://sicstus.sics.se/sicstus/docs/latest3/html/sicstus.html/CLPB.html)

This particular solver uses Binary Decision Diagrams (BDDs) to model and solve
SAT instances.

CLP(B) blends in seamlessly with Prolog in the sense that Prolog variables and
predicates are used to ask questions and obtain answers about satisfiability.
For instance, we can formulate a Prolog query that asks: “Are there any X and
Y such that the (X∧Y)∨X is satisfiable?”

    
    
        ?- sat(X*Y + X).
        X = 1,
        sat(Y=:=Y).
    

The answer tells us that _yes_ , the expression is satisfiable, if and only if
X = 1. Moreover, we see from the answer that the truth value of the expression
is then independent of Y, because Y is only constrained to be a Boolean
variable (by the tautology Y⇔Y) and not involved in any other constraints.

To obtain concrete assignments that make the expression true, we can use the
library predicate labeling/1\. On backtracking, all solutions are enumerated:

    
    
        ?- sat(X*Y + X), labeling([X,Y]).
        X = 1, Y = 0 ;
        X = Y, Y = 1.
    

There are many different ways and approaches to implement SAT solvers, and
this diversity is also reflected in Prolog implementations that provide them.
For instance, in GNU Prolog, SAT solving is provided as a special case of
integer constraints:

[http://www.gprolog.org/manual/html_node/gprolog061.html](http://www.gprolog.org/manual/html_node/gprolog061.html)

The SAT solver of B-Prolog is so strong that it can compete with some
dedicated SAT solvers. For a nice overview:

[https://www.cs.nmsu.edu/ALP/2013/03/the-sat-compiler-in-b-
pr...](https://www.cs.nmsu.edu/ALP/2013/03/the-sat-compiler-in-b-prolog/)

A very nice SAT solver implementation in Prolog is explained in the paper _A
Pearl on SAT Solving in Prolog_ by Jacob M. Howe and Andy King:

[http://www.staff.city.ac.uk/~jacob/solver/flops.pdf](http://www.staff.city.ac.uk/~jacob/solver/flops.pdf)

The paper shows that 20 lines of Prolog code suffice to implement an elegant
solver for—as the authors put it—"solving some interesting, albeit modest, SAT
instances".

However, SAT is a quite low-level way to encode combinatorial tasks, and for
this reason, CLP(FD/ℤ), i.e., constraint logic programming over _finite
domains_ / _integers_ is typically used instead of SAT when solving
combinatorial tasks with Prolog.

Conceptually, we can think of CLP(ℤ) as a generalization of SAT solving, where
variables can range over the entire integers instead of only 0 and 1. Strong
and fast constraint solvers over integers are a major attraction of commercial
Prolog systems, and often the key reason for buying one.

From a logical perspective, propositional logic can be regarded as a special
case of first-order predicate logic, so a language rooted in predicate logic,
like Prolog, is also related to SAT solving in this way.

------
foxes
The programming language Mercury which is a functional, predicate based, moded
language uses something like a precursor to a sat solver to "run" the program.
As in you can ask for the input of a function to make your program true.

I imagine all sorts of things can be done like this, and the sat solver is
useful for verifying correctness.

------
ausbah
How do SAT problems compare to constraint satisfaction problems? Is the latter
a subset of the former (with the right modeling)?

------
iflp
This is an interesting read, but I'd be interested in a more large-scale
benchmark. Right now it could be possible that the SAT solver is just better
optimized for the particular platform, instead of the SAT solver being
superior algorithm-wise.

------
vasili111
Can you please share your personal experience of using SAT solver with real
world problem?

~~~
ackbar03
Cracking product keys for... Uh... Research. You reverse engineer the binary,
enter the conditions into an sat solver and boom. But I don't think most keys
are implemented this way these days.

Also if you want cool, check out uc Santa Barbara's team for the darpa cyber
challenge a few years ago for an autonomous hacking system. Their framework,
angr, makes use a lot of sat solvers although I didn't quite understand
everything when I read their technical docs

~~~
xfer
Based on my experience, most key requiring programs use public key crypto and
pin their public key in the binary to verify the signature, which is the key.

~~~
nullc
That hasn't been my experience.

I've seen some that use public key crypto, but mostly they do not because even
the smallest secure cryptosystems have signatures on the order of at least 20
bytes (and more conventional choices more like 256-bit ECDSA... 64 bytes)--
which, after conversion to typeable characters, are a bit unwieldy.

------
atilimcetin
You can also watch the talk from same author about the same subject here -
[https://www.youtube.com/watch?v=x2XtpCn0-bM](https://www.youtube.com/watch?v=x2XtpCn0-bM)

------
abbadadda
SAT solvers Wikipedia:
[https://en.m.wikipedia.org/wiki/Boolean_satisfiability_probl...](https://en.m.wikipedia.org/wiki/Boolean_satisfiability_problem)

------
mshockwave
I'm actually finding part 2 more interesting

