38
Heuristics for Efficient SAT Solving As implemented in GRASP, Chaff and GSAT.

Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

Heuristics forEfficient SAT Solving

As implemented in GRASP, Chaff and GSAT.

Page 2: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

The K-Coloring problem:Given an undirected graph G(V,E) and a natural number k, is there an assignment color:

Formulation of famous problems as SAT: k-Coloring (1/2)

Page 3: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

Formulation of famous problems as SAT: k-Coloring (2/2)

xi,j = node i is assigned the ‘color’ j (1 ≤ i ≤ n, 1 ≤ j ≤ k)

Constraints:i) At least one color to each node: (x1,1 ∨ x1,2 ∨ …x1,k ∨ …)

ii) At most one color to each node:

)( ,,1

1

11tiji

k

jt

k

j

n

ixx ¬∨¬∀∀∀

+=

==

iii) Coloring constraints:

)(V ,11

ji

k

j

n

ix

==Λ

).(),( ,,111

cjci

n

j

n

i

k

cxxEji ¬∨¬∈∀∀∀

===

Page 4: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

Given a property p: (e.g. “always signal_a = signal_b”)

Is there a state reachable within k cycles, which satisfies ¬p ?

. . .s0 s1 s2 sk-1 sk

p p p ¬p p

Formulation of famous problems as SAT:Bounded Model Checking (1/4)

Page 5: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

The reachable states in k steps are captured by:

I s s s s s s sk k( ) ( , ) ( , ) ... ( , )0 0 1 1 2 1∧ ∧ ∧ ∧ −ρ ρ ρ

The property p fails in one of the cycles 1..k:

¬ ∨¬ ∨ ∨¬p p pk1 2 ...

Formulation of famous problems as SAT:Bounded Model Checking (2/4)

Page 6: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

Ω( ): ( , )k I s s pi

k

i ii

k

i00

1

10

∧ ∧ ¬∧ ∨=

+ρ=

The safety property p is valid up to cycle k iff Ω(k) is unsatisfiable:

. . .s0 s1 s2 sk-1 sk

p p p ¬p p

Formulation of famous problems as SAT:Bounded Model Checking (3/4)

Page 7: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

Example: a two bit counter

l l rr r' ( )'= ≠=¬

Property: always (¬l ∨ ¬r).

00

01 10

11

ϕ: ( )( )( )

( )( )( )

¬ ∧¬ ∧ = ≠ ∧ =¬ ∧= ≠ ∧ =¬

FHG

IKJ∧

∧ ∨∧ ∨∧

FHGG

IKJJl r

l l r r rl l r r r

l rl rl r

0 01 0 0 1 0

2 1 1 2 1

0 0

1 1

2 2

I l r0: ¬ ∧¬

For k = 2, Ω(k) is unsatisfiable. For k = 4 Ω(k) is satisfiable

Initial state:

Transition:

For k = 2:

Formulation of famous problems as SAT:Bounded Model Checking (4/4)

Page 8: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

What is SAT?

Given a propositional formula in CNF, find an assignment to Boolean variables that makes the formula true:

ω1 = (x2 ∨ x3)

ω2 = (¬x1 ∨ ¬x4)

ω3 = (¬x2 ∨ x4)

A = x1=0, x2=1, x3=0, x4=1

ω1 = (x2 ∨ x3)

ω2 = (¬x1 ∨ ¬x4)

ω3 = (¬x2 ∨ x4)

A = x1=0, x2=1, x3=0, x4=1

Page 9: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

Why SAT?

Fundamental problem from theoretical point of view Numerous applications:

– CAD, VLSI– Optimization– Bounded Model Checking and other type of formal verification– AI, planning, automated deduction

Page 10: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

Given ϕ in CNF: (x,y,z),(-x,y),(-y,z),(-x,-y,-z)

Decide()

Deduce()

Resolve_Conflict()

-xx

-zz-yy

z -z y -y

() ()

(z),(-z) ()

(y),(-y,z),(-y,-z)

()

() ()

(y),(-y)

(y,z),(-y,z)

√X

X X X X

ϕ

A Basic SAT algorithm

Page 11: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

Choose the next variable and value.Return False if all

variables are assigned

Apply unit clause rule.Return False if reached

a conflict

Backtrack until no conflict.

Return False if impossible

A Basic SAT algorithm

Page 12: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

Basic Backtracking Search

Organize the search in the form of a decision tree

– Each node corresponds to a decision

– Depth of the node in the decision tree → decision level

– Notation: x=v@dx 0,1 is assigned to v at decision level d

Page 13: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

Backtracking Search in Action

ω1 = (x2 ∨ x3)

ω2 = (¬x1 ∨ ¬x4)

ω3 = (¬x2 ∨ x4)

ω1 = (x2 ∨ x3)

ω2 = (¬x1 ∨ ¬x4)

ω3 = (¬x2 ∨ x4)

x1

x1 = 0@1

(x1,0), (x2,0), (x3,1)

x2x2 = 0@2

(x1,1), (x2,0), (x3,1) , (x4,0)

x1 = 1@1

x3 = 1@2

x4 = 0@1 x2 = 0@1

x3 = 1@1

Page 14: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

Backtracking Search in Action

ω1 = (x2 ∨ x3)

ω2 = (¬x1 ∨ ¬x4)

ω3 = (¬x2 ∨ x4)

ω4 = (¬x1 ∨ x2 ∨ ¬x3)

ω1 = (x2 ∨ x3)

ω2 = (¬x1 ∨ ¬x4)

ω3 = (¬x2 ∨ x4)

ω4 = (¬x1 ∨ x2 ∨ ¬x3)

x4 = 0@1

x2 = 0@1

x3 = 1@1

(x1,0), (x2,0), (x3,1)

x2

x2 = 0@2 x3 = 1@2

x1 = 0@1

x1

x1 = 1@1

Page 15: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

DLIS (Dynamic Largest Individual Sum)For a given variable x:

– Cx,p – # unresolved clauses in which x appears positively

– Cx,n - # unresolved clauses in which x appears negatively

– Let x be the literal for which Cx,p is maximal

– Let ybe the literal for which Cy,n is maximal

– If Cx,p > Cy,n choose x and assign it TRUE

– Otherwise choose y and assign it FALSE

Requires l (#literals) queries for each decision.

(Implemented in e.g. Grasp)

Decision heuristics

Page 16: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

Compute for every clause ω and every variable l (in each phase):

J(l) :=

Choose a variable l that maximizes J(l).

This gives an exponentially higher weight to literals in shorter clauses.

Jeroslow-Wang method

Decision heuristics

∈∈

ϕωω

ω

,

||2l

Page 17: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

Let f* (x) be the # of unresolved smallest clauses containing x. Choose x that maximizes:

((f* (x) + f* (!x)) * 2k + f* (x) * f* (!x)

k is chosen heuristically.

The idea:

– Give preference to satisfying small clauses.

– Among those, give preference to balanced variables (e.g. f* (x) = 3,f* (!x) = 3 is better than f* (x) = 1, f* (!x) = 5).

MOM (Maximum Occurrence of clauses of Minimum size).

Decision heuristics

Page 18: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

VSIDS (Variable State Independent Decaying Sum)

4. Periodically, all the counters are divided by a constant.

3. The unassigned variable with the highest counter is chosen.

2. When a clause is added, the counters are updated.

1. Each variable in each polarity has a counter initialized to 0.

(Implemented in Chaff)

Decision heuristics

Page 19: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

VSIDS (cont’d)

• Chaff holds a list of unassigned variables sorted by the countervalue.

• Updates are needed only when adding conflict clauses.

• Thus - decision is made in constant time.

Decision heuristics

Page 20: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

VSIDS is a ‘quasi-static’ strategy:

- static because it doesn’ t depend on current assignment

- dynamic because it gradually changes. Variables that appear in recent conflicts have higher priority.

Decision heuristics

This strategy is a conflict-driven decision strategy.

“ ..employing this strategy dramatically (i.e. an orderof magnitude) improved performance ... “

Page 21: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

A (CNF) dependency graph D (V,E):

A partitioning C1..Cn:

An abstract dependency graph D’ (V’ , E’ ):

Variable ordering(Abstract dependency graphs)

Page 22: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

For Ω(k) there exists a partition C1..Cn s.t. the abstract dependency graph is linear

C0 C1 C2 CkC3 Ck-1

V0 V1 V2 VkV3 Vk-1

...

Variable ordering(The natural order of Ω(k))

Page 23: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

I0¬PkRiding on unreachable states...

Ω(k) should satisfy I0

I0Riding on legal executions...

Ω(k) should satisfy ¬ Pk

¬Pk

Variable ordering(simple static ordering)

Page 24: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

Implication graphs and learning

ω1 = (¬x1 ∨ x2)

ω2 = (¬x1 ∨ x3 ∨ x9)

ω3 = (¬x2 ∨ ¬x3 ∨ x4)

ω4 = (¬x4 ∨ x5 ∨ x10)

ω5 = (¬x4 ∨ x6 ∨ x11)

ω6 = (¬x5 ∨ ¬ x6)

ω7 = (x1 ∨ x7 ∨ ¬x12)

ω8 = (x1∨ x8)

ω9 = (¬x7 ∨ ¬x8 ∨ ¬ x13)

ω1 = (¬x1 ∨ x2)

ω2 = (¬x1 ∨ x3 ∨ x9)

ω3 = (¬x2 ∨ ¬x3 ∨ x4)

ω4 = (¬x4 ∨ x5 ∨ x10)

ω5 = (¬x4 ∨ x6 ∨ x11)

ω6 = (¬x5 ∨ ¬ x6)

ω7 = (x1 ∨ x7 ∨ ¬x12)

ω8 = (x1∨ x8)

ω9 = (¬x7 ∨ ¬x8 ∨ ¬ x13)

x9=0@1 ,x10=0@3, x11=0@3, x12=1@2, x13=1@2

x1=1@6

ω6

ω6κ

conflict

x9=0@1

x1=1@6

x10=0@3

x11=0@3

x5=1@6ω4

ω4

ω5

ω5 x6=1@6ω2

ω2

x3=1@6

ω1

x2=1@6

ω3

ω3

x4=1@6

We learn the conflict clause ω10 : ( x1 x9 x11 x10)

Page 25: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

Implication graph, flipped assignment

x1=0@6

x11=0@3

x10=0@3

x9=0@1

x7=1@6

x12=1@2

ω7

ω7

x8=1@6

ω8

ω10

ω10

ω10 ω9

ω9

κ’

x13=1@2

ω9

Due to theconflict clause

ω1 = (¬x1 ∨ x2)

ω2 = (¬x1 ∨ x3 ∨ x9)

ω3 = (¬x2 ∨ ¬x3 ∨ x4)

ω4 = (¬x4 ∨ x5 ∨ x10)

ω5 = (¬x4 ∨ x6 ∨ x11)

ω6 = (¬x5 ∨ x6)

ω7 = (x1 ∨ x7 ∨ ¬x12)

ω8 = (x1∨ x8)

ω9 = (¬x7 ∨ ¬x8 ∨ ¬ x13)

ω10 : ( x1 x9 x11 x10)

ω1 = (¬x1 ∨ x2)

ω2 = (¬x1 ∨ x3 ∨ x9)

ω3 = (¬x2 ∨ ¬x3 ∨ x4)

ω4 = (¬x4 ∨ x5 ∨ x10)

ω5 = (¬x4 ∨ x6 ∨ x11)

ω6 = (¬x5 ∨ x6)

ω7 = (x1 ∨ x7 ∨ ¬x12)

ω8 = (x1∨ x8)

ω9 = (¬x7 ∨ ¬x8 ∨ ¬ x13)

ω10 : ( x1 x9 x11 x10)

Page 26: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

Non-chronological backtracking

Non-chronologicalbacktracking

x1

κ κ’

! "

Which assignments caused the conflicts ? x9= 0@1

x10= 0@3

x11= 0@3

x12= 1@2

x13= 1@2

Backtrack to decision level 3

#

These assignmentsAre sufficient forCausing a conflict.

Page 27: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

Deduction() allocates new implied variables and conflicts. How can this be done efficiently ?

Observation: More than 90% of the time SAT solvers perform Deduction().

More engineering aspects of SAT solvers

Page 28: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

Hold 2 counters for each clause π:

val1(π ) - # of negative literals assigned 0 in π +

# of positive literals assigned 1 in π.

val0(π) - # of negative literals assigned 1 in π +

# of positive literals assigned 0 in π.

Grasp implements Deduction() with counters

Page 29: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

Every assignment to a variable x results in updating the counters for all the clauses that contain x.

Grasp implements Deduction() with counters

π is satisfied iff val1(π) > 0

π is unsatisfied iff val0(π) = |π|

π is unit iff val1(π) = 0 ∧ val0(π) = |π| - 1

π is unresolved iff val1(π) = 0 ∧ val0(π) < |π| - 1..

Backtracking: Same complexity.

Page 30: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

Chaff implements Deduction() with a pair of observers

Observation: during Deduction(), we are only interested in newly implied variables and conflicts.

These occur only when the number of literals in π with value ‘ false’ is greater than |π| - 2

Conclusion: no need to visit a clause unless (val0(π) > |π| - 2)

How can this be implemented ?

Page 31: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

Chaff implements Deduction() with a pair of observers

Define two ‘observers’ : O1(π), O2(π).

O1(π) and O2(π) point to two distinct π literals which are not ‘ false’ .

π becomes unit if updating one observer leads to O1(π) = O2(π).

Visit clause π only if O1(π) or O2(π) become ‘ false’ .

Page 32: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

Both observers of an implied clause are on the highest decision levelpresent in the clause. Therefore, backtracking will un-assign them first.Conclusion: when backtracking, observers stay in place.

Chaff implements Deduction() with a pair of observers

1 2 3 4 5

V[1]=0

1 2 3 4 5

V[5]=0, v[4]= 0

1 2 3 4 5

V[2]=0

O1 O2

1 2 3 4 5 Unit clause

Backtrackv[4] = v[5]= Xv[1] = 1

1 2 3 4 5

Backtracking: No updating. Complexity = constant.

Page 33: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

Chaff implements Deduction() with a pair of observers

The choice of observing literals is important. Best strategy is - the least frequently updated variables.

The observers method has a learning curve in this respect:

1. The initial observers are chosen arbitrarily.

2. The process shifts the observers away from variables that were recently updated (these variables will most probably be reassigned in a short time).

In our example: the next time v[5] is updated, it will point to a significantly smaller set of clauses.

Page 34: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

GSAT: A different approach to SAT solving

!"# $%

&!$# '($$(#

) !"# $% *

$ α +

,, **(-$($($

. $ $/$0$# ',

&! -$(# **,

Given a CNF formula α, choose # $% and # $% *

Page 35: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

Improvement # 1: clause weights

1$ ( $$&"

1$/'0 ( $ $,

$(# $%$ (

Clause weights is another example of conflict-driven decision strategy.

Page 36: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

Improvement # 2: Averaging-in

Q: Can we reuse information gathered in previous tries in orderto speed up the search ?

A: Yes! Rather than choosing T randomly each time, repeat ‘good assignments’ and choose randomly the rest.

Page 37: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

Let X1, X2 and X3 be equally wide bit vectors.

Define a function bit_average : X1 × X2 → X3as follows:

b1i b1

i = b2i

random otherwise

(wherebji is the i-th bit in Xj, j ∈ 1,2,3 )

Improvement # 2: Averaging-in (cont’d)

b3i :=

Page 38: Heuristics for Efficient SAT Solvingemc/15-820A/reading/sat_cmu.pdf · 2003-01-31 · VSIDS is a ‘quasi-static’ strategy:-static because it doesn’t depend on current assignment-dynamic

Improvement # 2: Averaging-in (cont’d)

Let Tiinit be the initial assignment (T) in cycle i.

Let Tibest be the assignment with highest # of satisfied clauses in

cycle i.

T1init := random assignment.

T2init := random assignment.

∀i > 2, Tiinit := bit_average(Ti-1

best, Ti-2best)