2a Davis Putnam

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

Knowledge Representation 2a:

SAT Solving with the


Davis-Putnam algorithm
Stefan Schlobach
Vrije Universiteit Amsterdam
Remember from 1st lecture
• A problem P to be solved can be formalised as a statement S in
propositional logic
• Finding a truth assignment that makes S true
(an “S-satisfying truth assignment”)
corresponds to finding a solution to P
(example: solving the diplomatic party problem)
• A truth assignment for S is an interpretation I(v){0,1} for each
vS
• So, we need a method for choosing I(v) for each vS
(= an assignment I that will “satisfy” S)
• Question: what would it mean if S is inconsistent?
Question: what would it mean if S is a tautology?
The truth-table method is too expensive
1. List all combinations I(v) {0,1} for all vS
2. Calculate the truth value I(S) for all of these
combinations
3. Stop if I(S)=1
For N variables, there are 2N combinations:

2100 = 1.267.650.000.000.000.000.000.000.000.000

and realistic problems have 1 million variables


So we need a more efficient method!
Bad news….
• SAT is NP-Complete
(proved by Cook in 1971, as the first NP-complete problem)
• This means that any fast SAT-solver could also be used
to solve lots of other exponential problems
• This is widely believed to be impossible:
nobody believes NP=P (but still an open problem)
• So should we give up all hope?
• Remarkably, even though they are exponential in the worst case
(Cook was right in 1971….),
modern SAT solver are very fast most of the time
Davis-Putnam algorithm
• It was not developed by Davis & Putnam,
but by Davis, Logemann & Loveland….
• The original algorithm by Davis & Putnam used
potentially exponential space
• The improved algorithm by DLL is now known as DP…

• It dates from the ‘60s, is (of course) exponentional in


the worst case, but performs remarkably well (if we
add a few heuristics)
DPLL version 1
Davis – Putnam – Loveland – Logemann 1962

dpll_1(pa){
if (pa makes α false) return false;
if (pa makes α true) return true;

choose P in α ;
if (dpll_1(pa ∪ {P=false}))
return true;
else return dpll_1(pa ∪ {P=true});
}

Returns true if α is satisfiable, false otherwise


14 january 2020 Logic: semantics and calculus 6
DPLL Version 1
a
(a  b  c)
b
(a  ¬b) b

(a  ¬c)
c
c
(¬a  c)

14 january 2020 Logic: semantics and calculus 7


We need a few new terms
• A “variable” is one of the simple statements (the letters) that
make up the problem statement
• A clause is one of the disjuncts in the big disjunction in the
input statement (which is in clausal normal form)
• A literal is a variable or its negation (both P and P are literals)
• A unit clause is a clause of length 1 (= only one literal)
• A literal P is pure if P appears nowhere (and vice versa)

(¬A ∨ B ∨ E) ∧ (¬E ∨ A) ∧ (¬E ∨ B) ∧ (¬C ∨ F) ∧ (¬F ∨ C) ∧ (I)


clause literal pure unit
literal clause

• An empty clause is (trivially) false


(disjunction: at least one of these must be true)
• An empty set of clauses is (trivially) true
(conjunction: all of these have to be true)
Davis-Putnam procedure
It consists of two stages:
1. It simplifies the formula, and checks a few easy cases.
split simplify

If such an easy case exists


it can assign the right value to one of the truth values
and repeats the process
2. If there is no such easy case,
it picks one of the predicates and picks a truth value to it,
and then repeats the process
3. The process stops
1. when all clauses have been satisfied (success!), or
2. when one becomes inconsistent (failure)
If there is failure, that could be because we picked the wrong
backtrack

truth value for a letter in step 2. So we have to “backtrack”:


4a. for the letter from step 2 we pick the other truth value,
and try again.
4b. In step 2 we pick another letter
Part 1: Simplication rules
1.1 Tautology:
if a clause contains P  P it can be removed
(because the clause will be satisfied in an case)
1.2 if there is a pure literal (occurring only positive or negative),
it can be set to true (or false)
(because this choice doesn’t affect any other clause)
1.3 if there is a unit clause (consisting of only one literal),
that literal can be set to true
(because this is the only way the clause can be satisfied)
(of course, if the literal is of the form P, that makes P false)
Part 1: Simplication rules (b)
If literal L1 is true, then clause ( L1  L2  ...) is true
If clause C1 is true, then C1  C2  C3  ... has the same
value as C2  C3  ...
Therefore: Okay to delete clauses containing true literals!
If literal L1 is false, then clause ( L1  L2  L3  ...) has
the same value as ( L2  L3  ...)
Therefore: Okay to delete shorten containing false literals!
If literal L1 is false, then clause ( L1 ) is false
Therefore: the empty clause means false!
14 january 2020 Logic: semantics and calculus 11
DPLL version 2
Davis – Putnam – Loveland – Logemann
dpll_2(α, literal){
remove clauses from α containing literal
shorten clauses from α containing literal

if (α contains no clauses) return true;


if (α contains empty clause return false;

choose P in α;
if (dpll_2(α, P))return true;
return dpll_2(α, P);
}

Partial assignment corresponding to a node is the set of chosen literals on


the path from the root to the node
14 january 2020 Logic: semantics and calculus 12
DPLL Version 2
a
(a  b  c)
b
(a  ¬b) b

(a  ¬c)
c
c
(¬a  c)

14 january 2020 Logic: semantics and calculus 13


Further Improvements
Formula ( L)  C2  C3  ... is only true when literal L is true
Therefore: Branch immediately on unit literals!
If literal L does not appear negated in formula F , then setting
L true preserves satisfiability of F
Therefore: Branch immediately on pure literals!

14 january 2020 Logic: semantics and calculus 14


DPLL (for real)
a
(a  b  c)
(a  ¬b) b c

(a  ¬c)
c
(¬a  c)

14 january 2020 Logic: semantics and calculus 15


Davis-Putnam algorithm
Example: The diplomatic puzzle
We have to find a satisfying assignment for
(P  Q)  (Q  R)  (R  P)
Example: The diplomatic puzzle
We have to find a satisfying assignment for
(P  Q)  (Q  R)  (R  P)
• No easy cases, so split. Let’s say P=1
• Simplify: (Q  R)  (R)
• Pure literal Q, so Q=1
• Simplify: (R)
• Pure literal (and unit clause), so R=1, and R=0
Solution: P=1,Q=1,R=0
You can easily check that this does indeed satisfy the
input formula.
Therefore: invite Peru and Quatar, but not Rumania
Example: The diplomatic puzzle
We could have made another split choice in the first step:
(P  Q)  (Q  R)  (R  P)
• No easy cases, so split. This time let’s say R=1
• Simplify: (P  Q)  (P)
• Pure literal Q, so Q=1, so Q=0
• Simplify: (P)
• Unit clause (and pure literal), P=1, so P=0
• Solution: P=0,Q=0,R=1
• You can easily check that this also satisfies the input
formula: only invite Rumania
• We know from the truth table method that these are
the only two satisfying assignments
(= the only two solutions to the puzzle)
Another example:
Find a satisfying truth assignment for
((PR)  (PQ, R)  (QRP)  (RP Q))
We can also write this formula as:
((P, R),(P, Q, R), (Q,R, P), (R, P, Q))
• No easy case, split: let’s choose P=1
• Simplify: ((Q,R), (R,Q))
• With naïve DPLL: let’s choose Q=0
• Simplify: ((R), (R))
• Unit clause: R=1
• Simplify: false! We must backtrack to our most
recent choice, which was Q=0, and change it to Q=1
Another example (continued)
• We had ((Q,R), (R,Q))
• Now split with Q=1
• Simplify: all clauses satisfied, success;
Thus: P=1,Q=1 satisfies the original formula.
Notice that this is independent of the value of R.
So there are two full solutions:
(P=1,Q=1,R=1) and (P=1,Q=1,R=0)
Exercise: write DP as a tree on
((PR)  (PQ, R)  (QRP)  (RP Q))
Variations

• The Tautology rule only needs to be checked once at the start


• The Pure rule is often deleted because the cost of using it
might be more than the benefits (Question: why is it so
expensive?)
• The Unit rule is not strictly essential
(if you do a clever choice for split),
but it is essential for good performance
• The Unit rule by itself is complete for Horn clauses!
The special case of Horn Clauses
Rules: 𝑃1 ∧ 𝑃2 ∧ … ∧ 𝑃𝑛−1 → Pn
= ¬𝑃1 ∨ ¬𝑃2 ∨ … ∨ ¬𝑃𝑛−1 ∨ Pn
Facts: 𝑃𝑘

DP chooses “facts” (= unit clauses),


Uses them to make the conditions of some rule true;
If all conditions of some rule are true,
the conclusion of that rule becomes a (derived) “fact”,
This derived fact can be used
in the condition of other rules
= rule chaining
Heuristics for splitting
The secret sauce: how to split?
Notation:
CP(v) is nr of occurrences of v
CN(v) is nr of occurrences of v

• RAND: pick a random literal


• DLCS (Dynamic Largest Combined Sum):
• Pick v with largest CP(v)+CN(v) (= most frequent v)
• If CP(v)>CN(v) then v=1 else v=0
• DLIS (Dynamic Largest Individual Sum):
• Pick v with largest CP(v) or largest CN(v)
• If CP(v)>CN(v) then v=1 else v=0
The secret sauce: how to split?

{{A,B},
{-B,C},
Sum over 2 to the power of length of clause {-A,D,E}}
The secret sauce: how to split?
{{A,B},
{-B,C},
{-A,D,E}}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy