sat

advertisement
Satisfiability and SAT Solvers
CS 270 Math Foundations of CS
Jeremy Johnson
Conjunctive Normal Form
Conjunctive normal form (products of
sums)
 Conjunction of clauses (disjunction of literals)
For each row in the truth table where the
output is false, write a sum such that the
corresponding input not in that row
 Alternatively use Demorgan’s law for the
negation of dnf for f (zero rows)
 E.G. (multiplexor function)
(𝑠 + 𝑥0 + 𝑥1 )(𝑠 + 𝑥0 + 𝑥1 )(𝑠 + 𝑥0 + 𝑥1 ) (𝑠 +
𝑥0 + 𝑥1 )
s
x0
x1
f
0 0
0 0
0 0
1 0
0 1
0 1
0 1
1 1
1 0
0 0
1 0
1 1
1 1
0 0
1 1
1 1
2
Satisfiability
 A formula is satisfiable if there is an assignment
to the variables that make the formula true
 A formula is unsatisfiable if all assignments to
variables eval to false
 A formula is falsifiable if there is an assignment
to the variables that make the formula false
 A formula is valid if all assignments to variables
eval to true (a valid formula is a theorem or
tautology)
Satisfiability
 Checking to see if a formula f is satisfiable can be
done by searching a truth table for a true entry
 Exponential in the number of variables
 Does not appear to be a polynomial time algorithm
(satisfiability is NP-complete)
 There are efficient satisfiability checkers that work
well on many practical problems
 Checking whether f is satisfiable can be
done by checking if  f is not valid
 An assignment that evaluates to false
provides a counter example to validity
DNF vs CNF
 It is easy to determine if a boolean expression in
DNF is satisfiable but difficult to determine if it is
valid
 It is easy to determine if a boolean expression in
CNF is valid but difficult to determine if it is
satisfiable
 It is possible to convert any boolean expression
to DNF or CNF; however, there can be
exponential blowup
SAT Solvers
 Input expected in CNF
 Using DIMACS format
 One clause per line delimited by 0
 Variables encoded by integers, not variable
encoded by negating integer
 We will use MiniSAT (minisat.se)
MiniSAT Example
 (x1 | -x5 | x4) & (-x1 | x5 | x3 | x4) & (-x3 |
x4).
 DIMACS format
 (c = comment, “p cnf” = SAT problem in CNF)
c SAT problem in CNF with 5 variables and 3 clauses
p cnf 5 3
1 -5 4 0
-1 5 3 4 0
-3 -4 0
MiniSAT Example
 (x1 | -x5 | x4) & (-x1 | x5 | x3 | x4) & (-x3 |
x4).
This is MiniSat 2.0 beta
============================[ Problem Statistics ]==================
||
| Number of variables: 5 |
| Number of clauses: 3 |
| Parsing time: 0.00 s |
….
SATISFIABLE
v -1 -2 -3 -4 -5 0
Avionics Application
 Aircraft controlled by (real time) software
applications (navigation, control, obstacle
detection, obstacle avoidance …)
 Applications run on computers in different
cabinets
 500 apps
 20 cabinets
 Apps 1, 2 and 3 must run in separate cabinets
 Problem: Find assignment of apps to
cabinets that satisfies constraints
Corresponding SAT problem
 AC is a map from apps to cabinents
 [indicator variable] AC(app,cab) = t iff AC(app)
= cab
 [Valid Mapping] ∀𝑎 ∃𝑐 𝐴𝐶𝑎𝑐 ≡∧𝑎∈𝐴 ∨𝑐∈𝐶 𝐴𝐶𝑎𝑐
 [constaints]
∀𝑐 𝐴𝐶1𝑐 → ¬ 𝐴𝐶2𝑐 ∧ ¬ 𝐴𝐶3𝑐
∀𝑐 𝐴𝐶2𝑐 → ¬ 𝐴𝐶3𝑐
∧𝑐∈𝐶 𝐴𝐶1𝑐 → ¬ 𝐴𝐶2𝑐 ∧ ¬ 𝐴𝐶3𝑐
∧𝑐∈𝐶 𝐴𝐶2𝑐 → ¬ 𝐴𝐶3𝑐
Constaints in CNF
∧𝑐∈𝐶 𝐴𝐶1𝑐 → ¬ 𝐴𝐶2𝑐 ∧ ¬ 𝐴𝐶3𝑐
≡ ∧𝑐∈𝐶 ¬𝐴𝐶1𝑐 ∨ ¬ 𝐴𝐶2𝑐 ∧ ¬𝐴𝐶1𝑐 ∨ ¬ 𝐴𝐶3𝑐
∧𝑐∈𝐶 𝐴𝐶2𝑐 → ¬ 𝐴𝐶3𝑐
≡ ∧𝑐∈𝐶 ¬𝐴𝐶2𝑐 ∨ ¬ 𝐴𝐶3𝑐
DIMACS Format
Var(𝐴𝐶𝑎𝑐 ) = 20(a-1)+c
¬𝐴𝐶1𝑐 ∨ ¬ 𝐴𝐶2𝑐 = -c –(20+c)
¬𝐴𝐶1𝑐 ∨ ¬ 𝐴𝐶3𝑐 = -c -(40+c)
𝐴𝐶𝑎1 ∨ ⋯ ∨ 𝐴𝐶𝑎20 = 20(a-1)+1 … 20(a-1)+20
-1 -21 0
-1 -41 0
…
1 2 3 … 20 0 … 9981 … 10000 0
Avionics Example
 10 apps and 5 cabinets




Var(𝐴𝐶𝑎𝑐 ) = 5(a-1)+c
50 variables
25 clauses
Valid Map
 ∧𝑎=1…10 𝐴𝐶𝑎1 ∨ ⋯ ∨ 𝐴𝐶𝑎5
 Constaints
 ∧𝑐∈𝐶 ¬𝐴𝐶1𝑐 ∨ ¬ 𝐴𝐶2𝑐
 ∧𝑐∈𝐶 ¬𝐴𝐶1𝑐 ∨ ¬ 𝐴𝐶3𝑐
 ∧𝑐∈𝐶 ¬𝐴𝐶2𝑐 ∨ ¬ 𝐴𝐶3𝑐
Avionics Example
p cnf 50 25
c clauses for valid map forall a exists c AC^c_a
123450
6 7 8 9 10 0
11 12 13 14 15 0
16 17 18 19 20 0
21 22 23 24 25 0
26 27 28 29 30 0
31 32 33 34 35 0
36 37 38 39 40 0
41 42 43 44 45 0
46 47 48 49 50 0
Avionics Example
c constaints ~AC^c_1 + ~AC^c_2 and ~AC^c_1 + ~AC^c_3
-1 -6 0
-1 -11 0
-2 -7 0
-2 -12 0
-3 -8 0
-3 -13 0
-4 -9 0
-4 -14 0
-5 -10 0
-5 -15 0
c constraint ~AC^c_2 + ~AC^c_3
-6 -11 0
-7 -12 0
-8 -13 0
-9 -14 0
-10 -15 0
Avionics Example
[jjohnson@tux64-12 Programs]$ ./MiniSat_v1.14_linux aircraft assignment
==================================[MINISAT]===================================
| Conflicts | ORIGINAL |
LEARNT
| Progress |
|
| Clauses Literals | Limit Clauses Literals Lit/Cl |
|
==============================================================================
|
0 | 25
80 |
8
0
0 nan | 0.000 % |
==============================================================================
restarts
:1
conflicts
:0
(nan /sec)
decisions
: 39
(inf /sec)
propagations
: 50
(inf /sec)
conflict literals : 0
( nan % deleted)
Memory used
: 1.67 MB
CPU time
:0s
SATISFIABLE
Avionics Assignment
SAT
-1 -2 3 -4 -5 -6 7 -8 -9 -10 11 -12 -13 -14 -15 16 -17 -18 -19 -20 21 -22 -23
-24 -25 26 -27 -28 -29 -30 31 -32 -33 -34 -35 36 -37 -38 -39 -40 41 -42 -43
-44 -45 46 -47 -48 -49 -50 0
True indicator variables:
3 = 5*0 + 3 => AC(1,3)
7 = 5*1 + 2 => AC(2,2)
11 = 5*2 + 1 => AC(3,1)
16 = 5*3+1 => AC(4,1)
21 = 5*4+1 => AC(5,1)
26 = 5*5=1 => AC(6,1)
31 = 5*6+1 => AC(7,1)
36 = 5*7+1 => AC(8,1)
41 = 5*8 + 1 => AC(9,1)
46 = 5*9+1 => AC(10,1)
N-Queens Problem
 Given an N x N chess board
 Find a placement of N queens such that no two
queens can take each other
N Queens
N Queens
N Queens
Backtrack
N Queens
N Queens
N Queens
Backtrack
N Queens
Backtrack
N Queens
N Queens
N Queens
N Queens
Solution Found
Recursive Solution to N-Queens
Define Queens(board, current, size)
Input: board a size x size chess board with placement of current queens
in positions without conflict only using the first current columns
Output: true if board is a conflict free placement of size queens
if (current = size) then return true
for row = 0 to size-1 do
position := (row,column+1)
if ConflictFree(board,position)
Update(board,position)
done := Queens(board,column+1,size)
if done = true return true
return false
N-Queens as a SAT Problem
 Introduce variables Bij for 0 ≤ i,j < N
 Bij = T if queen at position (i,j) F otherwise
 Constraints
 Exactly one queen per row
 Rowi = Bij, j=0…N-1
 Exactly one queen per column
 Columnj = Bij, i=0…N-1
 At most one queen on diagonal
 Diagonalk- = Bij, i-j = k = -N+1…,N-1
 Diagonalk+ = Bij, i+j = k = 0…,2N-2
00
01
02
03
10
11
12
13
20
21
22
23
30
31
32
33
4-Queens SAT input
 Exactly one queen in row i




Bi0  Bi1  Bi2  Bi3
Bi0 Bi1  Bi2  Bi3
Bi1 Bi2  Bi3
Bi2 Bi3
4-Queens SAT input
 Exactly one queen in column j




B0j  B1j  B2j  B3j
B0j B1j  B2j  B3j
B1j B2j  B3j
B2j B3j
4-Queens SAT input
 At most one queen in diagonal k B20 B31
…
 B00 B11  B22  B33
 B11 B22  B33
 B22 B33
…
 B02 B13
4-Queens SAT input
 At most one queen in diagonal k+
 B01 B10
…
 B30 B21  B12  B03
 B21 B12  B03
 B12 B03
…
 B32 B23
DPLL Algorithm
 Tries to incrementally build a satisfying
assignment A: V  {T,F} (partial
assignment) for a formula  in CNF
 A is grown by either
 Deducing a truth value for a literal
 Whenever all literals except one are F then the
remaining literal must be T (unit propagation)
 Guessing a truth value
 Backtrack when guess (leads to
inconsistency) is wrong
DPLL Example
Operation
Assign
Formula
1  2, 2  3  4, 1  2, 1  3  4, 1
DPLL Example
Operation
Assign
Formula
1  2, 2  3  4, 1  2, 1  3  4, 1
Deduce
1
1  2, 2  3  4, 1  2, 1  3  4, 1
DPLL Example
Operation
Assign
Formula
1  2, 2  3  4, 1  2, 1  3  4, 1
Deduce
1
1  2, 2  3  4, 1  2, 1  3  4, 1
Deduce
2
1  2, 2  3  4, 1  2, 1  3  4, 1
DPLL Example
Operation
Assign
Formula
1  2, 2  3  4, 1  2, 1  3  4, 1
Deduce
1
1  2, 2  3  4, 1  2, 1  3  4, 1
Deduce
1, 2
1  2, 2  3  4, 1  2, 1  3  4, 1
Guess
1, 2, 3
1  2, 2  3  4, 1  2, 1  3  4, 1
DPLL Example
Operation
Assign
Formula
1  2, 2  3  4, 1  2, 1  3  4, 1
Deduce
1
1  2, 2  3  4, 1  2, 1  3  4, 1
Deduce
1, 2
1  2, 2  3  4, 1  2, 1  3  4, 1
Guess
1, 2, 3
1  2, 2  3  4, 1  2, 1  3  4, 1
Deduce
1, 2, 3, 4
1  2, 2  3  4, 1  2, 1  3  4, 1
Inconsistency
DPLL Example
Operation
Assign
Formula
1  2, 2  3  4, 1  2, 1  3  4, 1
Deduce 1
1
1  2, 2  3  4, 1  2, 1  3  4, 1
Deduce 2
1, 2
1  2, 2  3  4, 1  2, 1  3  4, 1
Guess 3
1, 2, 3
1  2, 2  3  4, 1  2, 1  3  4, 1
Deduce 4
1, 2, 3, 4
1  2, 2  3  4, 1  2, 1  3  4, 1
Undo 3
1, 2
1  2, 2  3  4, 1  2, 1  3  4, 1
Backtrack
DPLL Example
Operation
Assign
Formula
1  2, 2  3  4, 1  2, 1  3  4, 1
Deduce 1
1
1  2, 2  3  4, 1  2, 1  3  4, 1
Deduce 2
1, 2
1  2, 2  3  4, 1  2, 1  3  4, 1
Guess 3
1, 2, 3
1  2, 2  3  4, 1  2, 1  3  4, 1
Deduce 4
1, 2, 3, 4
1  2, 2  3  4, 1  2, 1  3  4, 1
Undo 3
1, 2
1  2, 2  3  4, 1  2, 1  3  4, 1
Guess 3
1, 2, 3
1  2, 2  3  4, 1  2, 1  3  4, 1
Assignment found
Download