Skip to content

Constraint Solving

Given some variables and constraints, find an assignment that satisfies all of them, or prove there isn't one. SAT, SMT and ASP solve this problem, and differ in how you write the constraints.

Boolean Satisfiability Problem / SAT

The variables are booleans, and the constraints are clauses in propositional logic.

Algorithm

There are two main algorithms to solve SAT

  • Davis-Putnam-Logemann-Loveland (DPLL)
  • Conflict-Driven Clause Learning (CDCL)

Info

These algorithms still have worse case exponential time which is the same as brute-force search.

Boolean Constraint Propagation (BCP)

Implication Graph

Computational Complexity

SAT is NP-Complete. Based on the Cook-Levin theorem, any problem in NP can be reduced to SAT in polynomial time.

SMT

Satisfiability Modulo Theories adds theories like integers and arrays on top of SAT. A SAT solver picks which atoms are true, and a theory solver checks they're consistent, like whether \(x > 3 \land x < 2\) has an integer solution. Z3 is the standard solver, and it's what Dafny uses underneath.

Answer Set Programming / ASP

You write rules describing what a solution looks like, and the solver finds the solutions, called answer sets.

node(1..3).
edge(1,2). edge(2,3). edge(1,3).
color(r;g;b).

1 { assign(N,C) : color(C) } 1 :- node(N).    % each node gets exactly one colour
:- edge(X,Y), assign(X,C), assign(Y,C).       % adjacent nodes can't share a colour

Each answer set is one valid colouring. A rule with no head, like the last line, is a constraint that throws out any answer set where its body holds.

To solve it, the solver first grounds the program, replacing variables with every constant so no variables are left, then searches with a CDCL-style solver. So ASP is close to SAT, but you write rules instead of raw clauses. clingo, from the Potassco group, is the standard solver.

Answer sets come from the stable model semantics (Gelfond and Lifschitz, 1988). The key feature is negation as failure, not p means \(p\) can't be derived, not that \(p\) is false.

Compared with Prolog

Prolog answers one query top-down, while ASP computes whole models bottom-up and always terminates on finite programs.