Lecture 7 SAT
Lecture 7 SAT
1
Satisfiability Terminology
q Satisfiability (called “SAT” for short)
q Give me an appropriate representation of function F(x1, x2,
… xn)
q Find an assignment of the variables (“vars” for short) (x1, x2,
… xn) so F( ) = 1
q Note – this assignment need not be unique. Could be many
satisfying solutions
q But if there are no satisfying assignments at all – prove it,
and return this info
q Some things you can do with BDDs, but easier with SAT
q SAT is aimed at scenarios where you just need one satisfying
assignment…
q …or prove that there is no such satisfying assignment
3
Standard SAT Form: CNF
q Conjunctive Normal Form (CNF) = Standard POS form
ɸ = ( a + c ) ( b + c) ( ¬a + ¬b + ¬c)
ɸ = ( a + ¬b ) (¬a + b + ¬ c) ( a + c + d ) ( ¬a + ¬b + ¬c)
6
Example
ɸ = ( x + y + z)( ¬x + y )( ¬y + z ) ( ¬x + ¬y + ¬z)
7
Example
ɸ = ( x + y + z)( ¬x + y )( ¬y + z ) ( ¬x + ¬y + ¬z)
x=1
( 1 )( y )( ¬y + z ) ( ¬y + ¬z )
8
Example
ɸ = ( x + y + z)( ¬x + y )( ¬y + z ) ( ¬x + ¬y + ¬z)
x=1
( 1 )( y )( ¬y + z ) ( ¬y + ¬z )
y=1
( 1 )( z ) ( ¬z )
9
Example
ɸ = ( x + y + z)( ¬x + y )( ¬y + z ) ( ¬x + ¬y + ¬z)
x=1
( 1 )( y )( ¬y + z ) ( ¬y + ¬z )
y=1
( 1 )( z ) ( ¬z )
z=0 z=1
0 0
unSAT unSAT
10
Example
ɸ = ( x + y + z)( ¬x + y )( ¬y + z ) ( ¬x + ¬y + ¬z)
x=1
( 1 )( y )( ¬y + z ) ( ¬y + ¬z )
y=1 y=0
( 1 )( z ) ( ¬z ) 0
0 0
unSAT unSAT
11
Example
ɸ = ( x + y + z)( ¬x + y )( ¬y + z ) ( ¬x + ¬y + ¬z)
x=1 x=0
( 1 )( y )( ¬y + z ) ( ¬y + ¬z ) ( 1 )( y + z ) ( ¬y + z )
y=1 y=0
( 1 )( z ) ( ¬z ) 0
0 0
unSAT unSAT
12
Example
ɸ = ( x + y + z)( ¬x + y )( ¬y + z ) ( ¬x + ¬y + ¬z)
x=1 x=0
( 1 )( y )( ¬y + z ) ( ¬y + ¬z ) ( 1 )( y + z ) ( ¬y + z )
( 1 )( z ) ( ¬z ) 0 (1)(1)
0 0
unSAT unSAT
13
Example
ɸ = ( x + y + z)( ¬x + y )( ¬y + z ) ( ¬x + ¬y + ¬z)
x=1 x=0
( 1 )( y )( ¬y + z ) ( ¬y + ¬z ) ( 1 )( y + z ) ( ¬y + z )
( 1 )( z ) ( ¬z ) 0 (1)(1)
0 0
unSAT unSAT Backtracking algorithm! (ECE 220)
14
Backtracking Algorithm (ECE220)
q Many useful to yes/no problems
q Depth-first search (DFS)
15
Boolean Constraint Propagation (BCP)
q To do “deduction”, use BCP
q Given a set of fixed variable assignments, what else can you
“deduce” about necessary assignments by “propagating
constraints”
q Most famous BCP strategy is “Unit Clause Rule”
q A clause is said to be “unit” if it has exactly one unassigned
literal
q Unit clause has exactly one way to be satisfied, ie, pick
polarity that makes clause=“1”
q This choice is called an “implication”
ɸ = ( a + c ) ( b + c) ( ¬a + ¬b + ¬c)
Assume:
a=1, b=1
ff
16
Boolean Constraint Propagation (BCP)
q To do “deduction”, use BCP
q Given a set of fixed variable assignments, what else can you
“deduce” about necessary assignments by “propagating
constraints”
q Most famous BCP strategy is “Unit Clause Rule”
q A clause is said to be “unit” if it has exactly one unassigned
literal
q Unit clause has exactly one way to be satisfied, ie, pick
polarity that makes clause=“1”
q This choice is called an “implication”
ɸ = ( a + c ) ( b + c) ( ¬a + ¬b + ¬c)
Assume:
c is unit unsigned clause – must
a=1, b=1
be 0 17
BCP Iteration – Go until no more implications
ɸ = (ω1)(ω2)(ω3)(ω4)(ω5)(ω6)(ω7)(ω8)(ω9)
q Example from
q J.P. Marques-Silva and K. Sakallah,
ω1 = ( ¬x1 + x2 )
0 “GRASP: A Search Algorithm for
ω2 = ( ¬x1 + x3 + x9) Propositional Satisfiability”, IEEE Trans.
Computers, Vol 8, No 5, May’99
ω3 = ( ¬x2 + ¬x3 + x4)
0 No SAT
ω4 = ( ¬x4 + x5 + x10) q Partial assignment is:
0 No BCP
ω5 = ( ¬x4 + x6 + x11) q x9=0 x10=0 x11=0 x12=1 x13=1
Now what?
ω6 = ( ¬x5 + ¬x6) q To start…
0 q What are obvious simplifications when
ω7 = ( x1 + x7 + ¬x12)
we assign these variables?
ω8 = ( x1 + x8)
0
ω9 = ( ¬x7 + ¬x8 + ¬x13)
18
BCP Iteration – Go until no more implications
ɸ = (ω1)(ω2)(ω3)(ω4)(ω5)(ω6)(ω7)(ω8)(ω9)
q Partial assignment is:
0 q x9=0 x10=0 x11=0 x12=1 x13=1
ω1 = ( ¬x1 + x2 )
0 0 Unit q Next: Assign a variable to value
ω2 = ( ¬x1 + x3 + x9)
q Assign x1=1
ω3 = ( ¬x2 + ¬x3 + x4)
0
ω4 = ( ¬x4 + x5 + x10)
0
ω5 = ( ¬x4 + x6 + x11)
ω6 = ( ¬x5 + ¬x6) Implications!
1 0 x1=1 à x2=1 && x3=1
ω7 = ( x1 + x7 + ¬x12)
1 SAT
ω8 = ( x1 + x8)
0
ω9 = ( ¬x7 + ¬x8 + ¬x13)
19
BCP Iteration – Go until no more implications
ɸ = (ω1)(ω2)(ω3)(ω4)(ω5)(ω6)(ω7)(ω8)(ω9)
q Partial assignment is:
0 1 q x9=0 x10=0 x11=0 x12=1 x13=1
ω1 = ( ¬x1 + x2 )
0 1 0 SAT q Next: Assign a var to value
ω2 = ( ¬x1 + x3 + x9)
0 0 q Assign x1=1
ω3 = ( ¬x2 + ¬x3 + x4) UNIT
0 q Assign (implied): x2=1,
ω4 = ( ¬x4 + x5 + x10)
0 x3=1
ω5 = ( ¬x4 + x6 + x11)
ω6 = ( ¬x5 + ¬x6) Implications!
1 0 x2=1,x3=1 à x4=1
ω7 = ( x1 + x7 + ¬x12)
1 SAT
ω8 = ( x1 + x8)
0
ω9 = ( ¬x7 + ¬x8 + ¬x13)
20
BCP Iteration – Go until no more implications
ɸ = (ω1)(ω2)(ω3)(ω4)(ω5)(ω6)(ω7)(ω8)(ω9)
q Partial assignment is:
0 1 q x9=0 x10=0 x11=0 x12=1 x13=1
ω1 = ( ¬x1 + x2 )
0 1 0 SAT q Next: Assign a var to value
ω2 = ( ¬x1 + x3 + x9)
0 0 1 q Assign x1=1
ω3 = ( ¬x2 + ¬x3 + x4)
0 0 q Assign (implied): x2=1,
ω4 = ( ¬x4 + x5 + x10)
0 0 UNIT x3=1
ω5 = ( ¬x4 + x6 + x11)
q Assign (implied): x4=1
ω6 = ( ¬x5 + ¬x6)
1 0
ω7 = ( x1 + x7 + ¬x12) Implications!
1 SAT x4=1 à x5=1 && x6=1
ω8 = ( x1 + x8)
0
ω9 = ( ¬x7 + ¬x8 + ¬x13)
21
BCP Iteration – Go until no more implications
ɸ = (ω1)(ω2)(ω3)(ω4)(ω5)(ω6)(ω7)(ω8)(ω9)
q Partial assignment is:
0 1 q x9=0 x10=0 x11=0 x12=1 x13=1
ω1 = ( ¬x1 + x2 )
0 1 0 SAT q Next: Assign a var to value
ω2 = ( ¬x1 + x3 + x9)
0 0 1 q Assign x1=1
ω3 = ( ¬x2 + ¬x3 + x4)
0 1 0 q Assign (implied): x2=1,
ω4 = ( ¬x4 + x5 + x10)
0 1 0 UNIT x3=1
ω5 = ( ¬x4 + x6 + x11)
0 0 q Assign (implied): x4=1
ω6 = ( ¬x5 + ¬x6) CONFLICT!
1 0 q Assign (implied): x5=1 &&
ω7 = ( x1 + x7 + ¬x12)
1 SAT x6=1àunSAT
ω8 = ( x1 + x8) Conflict
0 x5=1 && x6=1 à clause ω6==0!
ω9 = ( ¬x7 + ¬x8 + ¬x13)
22
BCP Iteration – Go until no more implications
ɸ = (ω1)(ω2)(ω3)(ω4)(ω5)(ω6)(ω7)(ω8)(ω9)
0
q 3 cases when BCP finishes
1
ω1 = ( ¬x1 + x2 ) q SAT: Find a SAT assignment, all
0 1 0 SAT clauses resolve to “1”. Return it.
ω2 = ( ¬x1 + x3 + x9)
0 0 1 q UNRESOLVED: One or more clauses
ω3 = ( ¬x2 + ¬x3 + x4) unresolved. Pick another unassigned
0 1 0
ω4 = ( ¬x4 + x5 + x10) var, and recurse more.
0 1 0 SAT
ω5 = ( ¬x4 + x6 + x11) q UNSAT: Like this. Found conflict,
0 0 one or more clauses eval to “0”
ω6 = ( ¬x5 + ¬x6) CONFLICT!
1 0 q Now what?
ω7 = ( x1 + x7 + ¬x12)
1 SAT q You need to undo one of our variable
ω8 = ( x1 + x8)
0 assignments, try again…
ω9 = ( ¬x7 + ¬x8 + ¬x13) UNRESOLVED
23
This has a name: DPLL Algorithm
q Davis-Putnam-Logemann-Loveland Algorithm
q Davis, Putnam published the basic recursive framework
in 1960 (!)
q Davis, Logemann, Loveland: found smarter BCP, eg,
unit-clause rule, in 1962
q Often called “Davis-Putnam” or “DP” in honor of the
first paper in 1960, or (inaccurately) DPLL (all four of
them never did publish this stuff together)
q Big ideas
q A complete, systematic search of variable assignments
q Useful CNF form for efficiency
q BCP makes search stop earlier, “resolving” more
assignments w/o recursing more
24
DPLL’s wiki page …
25
SAT has huge progress > 20 years
q But: DPLL is only the start…
q SAT has been subject of intense work and great progress
q Efficient data structures for clauses (so can search them fast)
q Efficient variable selection heuristics (so search smart, find
lots of implications)
q Efficient BCP mechanisms (because SAT spends MOST of its
time here)
q Learning mechanisms (find patterns of vars that NEVER lead
to SAT, avoid them)
26
Available SAT Solvers
q Many good solvers available online, open source
q Examples
q MiniSAT, from Niklas Eén, Niklas Sörensson in Sweden.
q CHAFF, from Sharad Malik and students, Princeton
University
q GRASP, from Joao Marques-Silva and Karem Sakallah,
University of Michigan
q …and many others too. Go google around for them…
27
More information about SAT
28
Summary
q SAT is very useful when you are solving yes/no prob
q Tiny, neat, clean, CNF formulation
q Better than using BDD packages (H = F xor G)
q Open-source tools (mini-sat, etc.)
q 40 years, but still important and widely used
q Started from DPLL algorithm
q Recent engineering efforts make it stupendously fast
29
BDD vs SAT
q BDDs q SAT
q Often work well for many q Often works well for many
problems problems
q But – no guarantee it will q But – no guarantee it will always
always work
work
q Can build BDD to represent
function ɸ q Can solve for SAT (y/n) on function
q But—sometimes cannot build ɸ
BDD with reasonable q But—sometimes cannot find SAT
computer resources (run out with reasonable computer
of memory SPACE) resources
q Yes -- builds a full (run out of TIME doing search)
representation of ɸ q No – does not represent all of ɸ
• Can do a big set of Boolean
manipulations on data • Can solve for SAT, but does not
structure support big set of operators
q Can build (∃xyz F) and (∀xyz q There are versions of Quantified
F) SAT that solve SAT on (∃xyz F),
(∀xyz F)
9
Typical Practical SAT Problem
q Are these two Boolean network the same?
Z = 1 SAT
F G If z SAT: we can find x such
that F(x) is not equal to G(x)!
F1 F2 G1 G2
Do SAT solve on this new network
à If SAT: networks not same,
and this pattern makes them
give different outputs
à If unSAT: yes, same!
10
Gate-level Network to CNF
q How do I get a CNF for a gate-level network?
q Isn’t this hard? Don’t I need Boolean algebra or BDDs?
No – it’s really easy
1 3
2 4 5
11
Gate-level Network to CNF
q How do I get a CNF for a gate-level network?
q Isn’t this hard? Don’t I need Boolean algebra or BDDs?
No – it’s really easy
1 3
2 4 5
12
EXOR vs EXNOR
13
Gate to CNF
Gate consistency function == “1” just for combinations
of inputs and the output that are consistent with what
the gate actually does
a
b d ɸd = ( a + d ) ( b + d ) (¬ a + ¬ b + ¬ d )
a d
b 1 3
f ɸd = …
h
2 4 5
c g ɸe = …
e
ɸf = …
ɸg = g ⊕[d + e] = …
ɸh = …
15
Derive a CNF for a Boolean Network
16
Rules for All Kinds of Basic Gates
q Gate consistency rules from:
q Fadi Aloul, Igor L. Markov, Karem Sakallah, “MINCE: A Static Global
Variable-Ordering Heuristic for SAT Search and BDD Manipulation,” J. of
Universal Computer Sci., vol. 10, no. 12 (2004), 1562-1596.
17
Rules Exist for MANY Basic Gates …
q EXOR/EXNOR gates are rather unpleasant for SAT
q And the basic “either-or” structure makes for some
tough SAT search, often
q And, they have rather large gate consistency functions
too
q Even small 2-input gates create a lot of terms, like this:
z=EXOR(a, b) z=EXNOR(a, b)
ɸz = z ⊕ (a ⊕ b) ɸz = z ⊕ (a ⊕ b)
18
Using the Rules …
ɸz = ( x1 + z ) ( x2 + z ) (¬ x1 + ¬ x2 + ¬ z )
20
Quiz
q Derive the consistency function for XOR gate
qz = EXOR(a, b), what is ɸz
21
Gate-consistency Function
q How do I get a CNF for a gate-level network?
q Isn’t this hard? Don’t I need Boolean algebra or BDDs?
No – it’s really easy
1 3
2 4 5
2
EXOR vs EXNOR
3
Consistency Function for OR gate
ɸz = [z == (x + y)] = z “exclusive nor” x + y
= z (x + y) + z’ (x + y)’
= zx + zy + z’x’y’
4
Consistency Function for XOR gate
ɸz = [z == (x xor y)] = z “exclusive nor” (xy’ + x’y)
= z (xy’ + x’y) + z’ (xy’ + x’y)’
= zxy’ + zx’y + z’(x’+y)(x+y’)
= zxy’ + zx’y + z’(x’y’ + xy)
= zxy’ + zx’y + z’x’y’ + z’xy
5
CNF for a Logic Network
q For a network: label each wire, build all gate
consistency funcs
7
SAT: Thing we didn’t talk about
q We covered: DPLL at a very high level
q We did not cover many “deep” tricks in a modern
SAT engine
q Efficient data structures for clauses (so can search them
fast)
q Efficient variable selection heuristics (so search smart,
find lots of implications)
q Efficient BCP mechanisms (because SAT spends MOST of
its time here)
q Learning mechanisms (find patterns of variables that
NEVER lead to SAT, avoid them)
q Uses of randomness to accelerate the SAT search
process
8
DPLL Revisited
q CNF = clauses ω1•ω2• … ωk, over variables xi∈{x1,x2,…xn}
q Decision:
q Select a variable xi and assign its value; simplify CNF formula
q Deduction:
q Unit clause rule lets us perform Boolean Constraint Propagation
q Given a partial variable assignment, run BCP to further simplify
q Do this until nothing simplifies. If you can decide SAT yes/no, great.
q If not, then you have to recurse some more, back up to DECIDE
9
Adding Randomness
q Suppose you added some randomness to SAT engine
q Simplest place is the variable DECISION step – which one to pick
10
Why adding randomness?
q What happens if you add this randomness?
q Do this experiment: run the exact same SAT CNF problem,
many times
q Expect some different outcomes– you are deciding to assign
different variables in DPLL
• Note: you still expect CORRECT answers. But maybe different SAT
instances.
q Measure CPU time for each random run. Plot this histogram.
It always looks like this!
# instances
solved in
this much
CPU time
CPU time
11
How to “Read” this result?
12
Can Exploit Randomness
q Strategy: Rapid Random Restarts
q If DPLL is running long, assume you are in one of the “slow” parts
of distribution.
q Stop before you finish SAT solve. Restart SAT solver back at the
beginning
q Big idea: Instead of one deep search, many shallow searches.
q Advanced idea: Keep learned conflict clauses across restarts
q Modern solvers “learn” patterns of variable assignments that can
never be SAT
q This helps solver not keep trying same bad ideas over and over
q These are implemented as new clauses “learned”, added to CNF
database
q Big idea: Do not throw these away across random restarts.
• Keep this “smart stuff” learned from previous random searches for new
ones
13
Does this work actually …?
q Example: Michigan GRASP solver
q “Solve is running too long” heuristic: Whenever the
solver reaches 100 “backtrack” decisions, where it must
undo a variable assignment because it led to an UNSAT
solution, GRASP stops, and does a random restart
q “Keep learned conflicts across restarts” heuristic:
GRASP saves every learned conflict (ie, pattern of bad
variable choices) with ≤ 10 literals, and uses for restart
q Result on one large SAT task:
• SAT without randomness: 42,645 backtracks, 2299.3 CPU sec
• SAT with randomness: 222 backtracks 3 random restarts, 53.5
CPU sec
Numbers courtesy Prof. Karem Sakallah, University of Michigan
See also: Marques-Silva, João P. Search Algorithms for Satisfiability Problems in Combinational Switching Circuits.
Ph.D. Dissertation, EECS Department, University of Michigan, May 1995. 14
Summary
q Modern SAT solver engines use randomness
q Rapid random restarts is a very effective heuristic to reduce
CPU time
q Several ways randomness appears. Easiest for us to explain is
in variable selection heuristics, for which variable to assign a
value to, in evolving DPLL process
q Consequences of randomness
q You get different answers sometimes. (Yes, its weird…)
q You do NOT get wrong answers. Just DIFFERENT anwers, eg,
SAT assignments
q Randomness is a good thing: makes it possible to do huge
SAT problems fast
15