+ All Categories
Home > Documents > 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially...

1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially...

Date post: 20-Dec-2015
Category:
View: 214 times
Download: 0 times
Share this document with a friend
Popular Tags:
101
1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer Science the importance of this domain is obvious: ultimately, all programs are compiled into machine code, i.e. to specifications coded in bits, to be handled by some processor. More pragmatically, a vast number of problems may be naturally specified through a set of boolean constraints, coded in a variety of forms. Among these forms, a quite useful one is the clausal form, which corresponds to the
Transcript
Page 1: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

1

Boolean Satisfaction - SAT

Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1.

In Computer Science the importance of this domain is obvious: ultimately, all programs are compiled into machine code, i.e. to specifications coded in bits, to be handled by some processor.

More pragmatically, a vast number of problems may be naturally specified through a set of boolean constraints, coded in a variety of forms.

Among these forms, a quite useful one is the clausal form, which corresponds to the Conjunctive Normal Form (CNF) of any Boolean function.

Page 2: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

2

Boolean Satisfaction - SAT

Example_1: n-queens

This is the classic problem that aims at placing n queens in an n*n chess board so that no two queens attack each other.

Associating the presence of a queen in any of the n2 positions with a 0/1 variable, the problem is modelled through two types of constraints:

• There must be at least a queen in some sets of positions (rows and columns)

• There must be at most one queen in some sets of positions (rows, columns and diagonals)

Such constraints are easily coded in clausal form.

Page 3: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

3

Boolean Satisfaction - SAT Example_1: n-queens

R1: There must be at least a queen in the set of positions Q1 a Qn

• 1 single n-ary clause: Q1 Q2 Qn

R2: There must be at most one queen in the set of positions Q1 a Qn

• Several (n*(n-1)/2) binary clauses are required :

Q1 Q2 Q1 Q3 .. Q1 Qn

...

Qn-1 Qn

Page 4: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

4

Boolean Satisfaction - SAT

Example_2: Graph Colouring

Given a certain graph, check whether it is possible to guarantee that adjacent nodes (i.e. connected by an arc) have different colours.

This example has a less obvious coding. Firstly, the colours have to be coded. We notice that k colours require ceiling(log2(k)) bits to be coded.

Assuming 4 colours, each of the colour is coded in a different combination of two boolean variables.

Hence, we boolean variables will use Ci,1 e Ci,2 to encode the colour assigned to node i.

Page 5: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

5

Boolean Satisfaction - SAT

Example_2: Graph Colouring

Secondly, one must constrain adjacent nodes to have diferent colours. For nodes i e j, such constraint can be coded through

(Ci,1 Cj,1 )(Ci,2 Cj,2 )

But a b (a b) (a b) (a b) (a b)

Hence the above constraint can be coded as

[(Ci,1 Cj,1 ) (Ci,1 Cj,1 )] [(Ci,2 Cj,2 ) (Ci,2 Cj,2 )]

which is equivalent to the four clauses

Ci,1 Cj,1 Ci,2 Cj,2 Ci,1 Cj,1 Ci,2 Cj,2

Ci,1 Cj,1 Ci,2 Cj,2 Ci,1 Cj,1 Ci,2 Cj,2

Page 6: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

6

Boolean Satisfaction - SAT

Example_3: Knapsack (Decision)

Given a set of n objects, each occupying a certain (integer) volume vi and worth zi, check whether in a knapsack with volume capacity it is possible to place a set of objects with worth in total no less than Z.

This problem can be modelled by means of n boolean variables, Xi, that denote whether the corresponding object goes into the knapsack or not.

Hence, the constraint of not to exceed the capacity of the knapsack is expressed as

v1 X1 + v2 X2 + ... + vn Xn =< V

Page 7: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

7

Boolean Satisfaction - SAT

Example_3: Knapsack (Decision)

v1 X1 + v2 X2 + ... + vn Xn =< V

Although numerical, this constraint may be modelled with boolean constraints (clauses) by encoding

• the multiplication of an integer by a bit (0/1)

• the sum of 2 integers

• the comparison (=<) of 2 integers

Page 8: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

8

Boolean Satisfaction - SAT

Example_3: Knapsack (Decision)

• the multiplication of an integer by a bit (0/1)

Given an integer B represented in binary by bits Bk to B0, its multiplication by a bit X results in another integer M, coded with a similar number of bits Mk a M0, such that

Mi = X Bi

This equality may be imposed through the following clauses

(X Mi) ( X Mi=Bi)

(X Mi)(X Mi = Bi)

(X Mi)(X ( (MiBi) (MiBi))

( X Mi)(X MiBi) (X MiBi)

Page 9: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

9

Boolean Satisfaction - SAT

Example_3: Knapsack (Decision)

• the sum of 2 integers

Given integers A e B represented in binary with bits ak/bk to ak/b0, its sum S may be represented through the following constraints

First bit S0 = A0 B0 C1 = A0 B0

Last bit Sn+1 = Cn+1

Other bits Si = Ai Bi Ci

Ci+1 = (AiBi) (AiCi) (BiCi)

that are converted in the clausal form as before.

Page 10: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

10

Boolean Satisfaction - SAT

First bit

• S0 = A0 B0

(S0 (A0 B0)) (S0 (A0 B0))

(S0((A0B0)(A0B0)))( S0 ((A0B0)(A0B0)))

(S0 A0B0 )(S0A0B0)(S0A0B0)(S0A0B0)

• C1 = A0 B0

(C1 (A0 B0)) (C1 (A0 B0))

(C1 A0 B0) (C1 A0 ) (C1 B)

Last bit

• Sn+1 = Cn+1

(Sn+1 Cn+1) ( Sn+1 Cn+1)

Page 11: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

11

Boolean Satisfaction - SAT

Other bits

• Si = Ai Bi Ci

(Si (Ai Bi Ci)) (Si (Ai Bi Ci))...

(Si Ai Bi Ci) (Si Ai Bi Ci)

(Si Ai Bi Ci) (Si Ai Bi Ci)

(Si Ai Bi Ci) (Si Ai Bi Ci)

(Si Ai Bi Ci) (Si Ai Bi Ci)

• Ci+1 = (AiBi) (AiCi) (BiCi)...

(Ai Bi Ci+1) (Ai Bi Ci+1)

(Ai Ci Ci+1) (Ai Ci Ci+1)

(Bi Ci Ci+1) (Bi Ci Ci+1)

Page 12: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

12

Boolean Satisfaction - SAT

Example_3: Knapsack (Decision)

• the comparison (=<) of 2 integers

Given integers A and B, coded in binary with bits Ak/Bk to A0/B0, its comparison (A =< B) may use additional variables X0 a Xk, where Xi indicates that Ai ... A0 < Bi ... B0. Hence,

• X0 = (A0 < B0)

(A0 X0)(A0 X0 B0)(A0 X0 B0)

• Xk = (Ak < Bk) ((Ak=Bk) Xk-1) for k 1

....

Page 13: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

13

SAT and 3SAT

Although some clauses have more than 3 variables, they can always be converted in a set of clauses with 3 variables alone, which are equivalent.

Exemple: The satisfaction of clause P

P: A B C D

is equivalent to the satisfaction of clauses Q e R below

Q: A B X and R: C D X

where X is a new variable (this is resolution, the other way round). In fact, one may prove

• P Q, R: whenever P is satisfiable Q and R are also satisfiable

• Q,R P: whenever Q and R are satisfiable P is also satisfiable

Page 14: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

14

SAT e 3SAT

P: A B C D Q: A B X and R: C D X

If clause P may be satisfied, then at least one variable A a D takes value 1. Without loss of generality, let us assume it is A. Then clause Q is satisfied. Clause R can be satisfied with X = 0.

Q: A B X and R: C D X P: A B C D

Since X = 0 or X=1, if both Q and R are satisfied, then either clause AB (for X=0) or CD (for X=1) is satisfied.

But then one of the variables A a D takes value 1, guaranteeing the satisfaction of clause P: A B C D.

Page 15: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

15

Importance of SAT

As can be easily seen, many decision problems can be modelled as Boolean satisfaction problems (SAT).

In fact, all decision problems in finite domains may be modelled as a Boolean satisfaction problem

Hence, a general algorithm to solve this type of SAT problems, would solve any decision problem.

Of course, one would only be interested in having an “eficient” algorithm for SAT.

Decision Problem

SAT Problem

Page 16: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

16

Importance of SAT

A SAT problem with n variables (or rather, that can be described with a string with size proportional to n) may be solved in polinomial time in a non-deterministic machine (i.e. one that correctly guesses the appropriate values to the n variables).

Such decision problems, such as SAT, are thus problems that belong to class NP.

More realistically, a deterministic machine (those that exist) may verify, in polinomial time, whether a potential solution satisfies all the constraints, i.e. O(nk).

Page 17: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

17

Importance of SAT

Considering n boolean variables, there are 2n potential solutions.

The satisfiability test with clauses on n variables may thus be performed with a generate and test procedure:

• Generate each of the 2n possibilities;

• Test each of these possibilities.

This procedure is of course very inneficient. In the worst case, it has an exponential complexity, O(2n).

Of course, not all decision problems have the same complexity.

Page 18: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

18

Importance of SATFor certain problems there are polinomial algorithms (in n) that allow decisions on the satisfiability of the problem.

Such problems, that form the class P, are clearly polinomial.

•For example, many list processing algorithms, e.g. to obtain a sorted list of n integers from a non-sorted one, have quadratic complexity O(n2). (quick-sort has a better complexity O(n log n)).

For other problems, even those not in this class, there might be instances particularly simple to solve (with a certain solver).

•For example, the knapsack is trivial if all objects have the same volume (not the same value !). Chose first the most valuable items.

Page 19: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

19

Importance of SAT

Naturally, all problems P belong to class NP, since if a deterministic machine solves them in polinomial time, so would do a non-deterministic machine.

What is not so clear is whether the class NP contains problems that are not P. This is, by now, an important conjecture, known as the P ≠ NP problem.

In practice, no one has ever found any algorithm that solves an arbitrary instance of SAT in polinomial time, which suggests that P is indeed strictly contained in NP.

From a theoretical viewpoint, if that will ever happen, (highly improbable), than P=NP.

Page 20: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

20

Importance of SAT

From a more practical viewpoint, if such an algorithm would be found, then all decision problems that are “interesting”, would be solved in polinomial time.

In fact, one must only guarantee that the transformation of the problem in a SAT problem takes polinomial time.

These transformations may usually be made in a way similar to that that was used in previous examples (queens, graph colouring and knapsack).

This result is one of the motivations for the thorough study that has been done to the SAT (or 3SAT) problem.

Page 21: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

21

Importance of SAT

The generality of the SAT approach, in which all decision problems may be converted, is another reason for the study of the SAT / 3SAT problem.

Although it is extremely unlikely that a general polinomial algorithm will ever be found, there are many algorithms and software packages that solve very satisfactorily a large number of instances of SAT.

In particular, there are SAT solvers that can handle problems with millions of variables and tens of millions of clauses.

We should however analyse the possibilities of LP and CLP to solve these problems.

Page 22: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

22

SAT Solving - Generate and Test

The generation of all solutions may be obtained by the backtracking mechanism built-in in Logic Programming tools..

Example:

test([Q1, Q2, Q3, ..., Q16]):-

at_most_one([Q1,Q2,Q3,Q4]),

at_least_one([Q1,Q2,Q3,Q4]),

...

at_most_one([Q9,Q14]).

queens_4(L):-

generate(L),

test(L),

report(L).

generate([]).

generate([H|T]):-

member(H,[1,0]),

generate(T).

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

Page 23: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

23

SAT Solving - Generate and Test

For simplicity, in program queens4_sat_gt all the tests are programmed explicitely.

1ª linha

% at_most_one([Q1,Q2,Q3,Q4])

~Q1 # ~Q2,

~Q1 # ~Q3, ~Q2 # ~Q3,

~Q1 # ~Q4, ~Q2 # ~Q4,

~Q3 # ~Q4

% at_least_one([Q1,Q2,Q3,Q4]),

Q1 # Q2 # Q3 # Q4,

Page 24: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

24

SAT Solving - Generate and Test

This program queens4_sat_gt also makes use of the usual syntatic facilities available in Logic Programming, namely the definition of operators.

% boolean operators

:- op(300,fy,~). % negation

:- op(400,xfy,#). % disjunction

X # Y :- exp(X # Y, Exp), Exp >= 1.

exp(X, X):- atomic(X), !.

exp(~X, 1-ExpX):- !,

exp(X, ExpX).

exp(X # Y, ExpX + ExpY):- !,

exp(X, ExpX), exp(Y, ExpY).

% test for some clause

Page 25: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

25

SAT Solving - Backtracking

• Many potential solutions can be discarded by simple analysis of partial solutions. For example, any potential solution that includes Q1=Q2=1 can be discarded, since no two queens can be in the same row (namely the first row).

• A more efficient way to check the satisfaction of a problem is the utilization of a constructive method, in which partial solutions (i.e. with only a few variables instantiated) are being “completed”, as long as they do not violate any constraint.

• If the partial solution violates any constraint, it is not completed. Instead, one of its values is backtracked, thus enabling the exploitation of other alternative solutions.

Page 26: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

26

SAT Solving - Backtracking

If partial solution a=b=1 does not satisfy some constraint, (e.g. among a, b and c, only one may have value 1) no complete solutions including a=b=1 are considered.

The “first” solution <a,b,c,d>= <1,0,0,1> is thus found avoiding the test of 6 leafs.

a a

b b b b

c cc c c cc c

d dd dd dd d d dd dd dd d

X

X

Page 27: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

27

SAT Solving - Backtracking

The completion of partial solutions can be easily performed by the backtracking mechanism built-in in Logic Programming.

The main problem here is to guarantee that the tests are performed as soon as adequate. For example,

• After instantiation of variables Q1 e Q2, in the same row, they not take both the same value 1.

• ...

• After instantiating variable Q4, it should be checked that at least one of the variables Q1 to Q4 takes value 1.

Page 28: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

28

SAT Solving - Backtracking

Again, to simplify the program, all these checkings are performed explicitely in programa queens4_sat_bk.

queens_4(_):- label(Q1), % 1st line label(Q2), ~Q1 # ~Q2, label(Q3), ~Q1 # ~Q3, ~Q2 # ~Q3, label(Q4), Q1 # Q2 # Q3 # Q4, ~Q1 # ~Q4, ~Q2 # ~Q4, ~Q3 # ~Q4, ...

% the labeling uses the member predicate label(Q):- mb(Q,[0,1]).

Page 29: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

29

SAT Solving - Backtracking

As easily understood, the backtracking version is much more efficient.

The account of the tests done and the valuations performed is maintained by the use of counters (impure LP, using asserts and retracts).

% counting of the valuations performed

mb(X,[X|_]):- inc(l).

mb(X,[_|T]):- mb(X,T).

% counting of the tests performedX # Y :- exp(X # Y, Exp), inc(t), Exp >= 1.

Page 30: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

30

Constraint Propagation

• An alternative to checking the compatibility between the values of “past” variables (with values already assigned) consists of eliminating from the “future” variables (with values yet to be assigned), those values that are incompatible with those already assigned.

• These alternative method should enable, and in many cases it actaully enables:

•Detect more quickly the impossibility of completing a partial solution.

•Backtrack to the relevant causes of the failure.

• The following slides will illustrate these effects

Page 31: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

31

Backtracking

Tests 0 Backtracks 0

Page 32: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

32

Backtracking

Tests 0 + 1 = 1 Backtracks 0

Q1 \= Q2, L1+Q1 \= L2+Q2, L1+Q2 \= L2+Q1.

Page 33: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

33

Backtracking

Tests 1 + 1 = 2 Backtracks 0

Q1 \= Q2, L1+Q1 \= L2+Q2, L1+Q2 \= L2+Q1.

Page 34: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

34

Backtracking

Tests 2 + 1 = 3 Backtracks 0

Q1 \= Q2, L1+Q1 \= L2+Q2, L1+Q2 \= L2+Q1.

Page 35: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

35

Backtracking

Tests 3 + 1 = 4 Backtracks 0

Page 36: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

36

Backtracking

Tests 4 + 2 = 6 Backtracks 0

Page 37: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

37

Backtracking

Tests 6 + 1 = 7 Backtracks 0

Page 38: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

38

Backtracking

Tests 7 + 2 = 9 Backtracks 0

Page 39: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

39

Backtracking

Tests 9 + 2 = 11 Backtracks 0

Page 40: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

40

Backtracking

Tests 11 + 1 + 3 = 15 Backtracks 0

Page 41: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

41

Backtracking

Tests 15 + 1 + 4 + 2 + 4 = 26 Backtracks 0

Page 42: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

42

Backtracking

Tests 26 + 1 = 27 Backtracks 0

Page 43: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

43

Backtracking

Tests 27 + 3 = 30 Backtracks 0

Page 44: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

44

Backtracking

Tests 30 + 2 = 32 Backtracks 0

Page 45: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

45

Backtracking

Tests 32 + 4 = 36 Backtracks 0

Page 46: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

46

Backtracking

Tests 36 + 3 = 39 Backtracks 0

Page 47: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

47

Backtracking

Tests 39 + 1 = 40 Backtracks 0

Page 48: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

48

Backtracking

Tests 40 + 2 = 42 Backtracks 0

Page 49: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

49

Backtracking

Tests 42 + 3 = 45 Backtracks 0

Page 50: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

50

Backtracking

Tests 45 Backtracks 0

Fails

6

Backtracks

5

Page 51: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

51

Backtracking

Tests 45 Backtracks 1

Page 52: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

52

Backtracking

Tests 45 + 1 = 46 Backtracks 1

Page 53: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

53

Backtracking

Tests 46 + 2 = 48 Backtracks 1

Page 54: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

54

Backtracking

Tests 48 + 3 = 51 Backtracks 1

Page 55: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

55

Backtracking

Tests 51 + 4 = 55 Backtracks 1

Page 56: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

56

Backtracking

Tests 55+1+3+2+4+3+1+2+3 = 74 Backtracks 1

Fails

6

Backtracks

5 and 4

Page 57: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

57

Backtracking

Tests 74+2+1+2+3+3= 85 Backtracks 1+2 = 3

Page 58: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

58

Backtracking

Tests 85 + 1 + 4 = 90 Backtracks 3

Page 59: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

59

Backtracking

Tests 90 +1+3+2+5 = 101 Backtracks 3

Page 60: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

60

Backtracking

Tests 101+1+5+2+4+3+6= 122 Backtracks 3

Page 61: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

61

Backtracking

Tests 122+1+5+2+6+3+6+4+1= 150 Backtracks 3+1=4

Fails

8

Backtracks

7

Page 62: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

62

Backtracking

Tests 150+1+2= 153 Backtracks 4+1=5

Fails

7

Backtracks

6

Page 63: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

63

Backtracking

Tests 153+3+1+2+3= 162 Backtracks 5+1=6

Fails

6

Backtracks

5

Page 64: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

64

Retrocesso

Tests 162+2+4= 168 Backtracks 6+1=7

Page 65: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

65

Backtracking

Tests 168+1+3+2+5+3+1+2+3= 188 Backtracks 7

Fails

6

Backtracks

5

Page 66: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

66

Backtracking

Tests 188+1+2+3+4= 198 Backtracks 7+1=8

Fails

5

Backtracks

4

Page 67: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

67

Backtracking

Tests 198 + 3 = 201 Backtracks 8+1=9

Page 68: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

68

Backtracking

Tests 201+1+4 = 206 Backtracks 9

Page 69: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

69

Backtracking

Tests 206+1+3+2+5 = 217 Backtracks 9

Page 70: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

70

Backtracking

Tests 217+1+5+2+5+3+6 = 239 Backtracks 9

Page 71: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

71

Backtracking

Tests 239+1+5+2+4+3+6+7+7= 274 Backtracks 9+1=10

Fails

8

Backtracks

7

Page 72: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

72

Backtracking

Tests 274+1+2= 277 Backtracks 10+1=11

Fails

7

Backtracks

6

Page 73: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

73

Backtracking

Tests 277+3+1+2+3= 286 Backtracks 11+1=12

Fails

6

Backtracks

5

Page 74: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

74

Backtracking

Tests 286+2+4= 292 Backtracks 12

Page 75: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

75

Backtracking

Tests 292+1+3+2+5+3+1+2+3= 312 Backtracks 12+1=13

Fails

6

Backtracks

5

Page 76: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

76

Backtracking

Tests 312+1+2+3+4= 322 Backtracks 13+2=15

Fails

5

Backtracks

4 e 3

Page 77: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

77

Backtracking

Tests 322 + 2 = 324 Backtracks 15

X1=1

X2=3

X3=5

Impossible !

Page 78: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

78

Propagation

Tests 0 Backtracks 0

Page 79: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

79

Propagation

1 1

1

1

1

1

11

1

1

1

1

1

1

Tests 8 * 7 = 56 Backtracks 0

Q1 #\= Q2, L1+Q1 #\= L2+Q2, L1+Q2 #\= L2+Q1.

Page 80: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

80

Propagation

1 1

1

1

1

1

11

1

1

1

1

1

12

2

2

2

2

2

2

2

2

2

2

Tests 56 + 6 * 6 = 92 Backtracks 0

Page 81: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

81

Propagation

1 1

1

1

1

1

11

1

1

1

1

1

12

2

2

2

2

2

2

2

2

2

2

3

3

3

3

3

33

Tests 92 + 4 * 5 = 112 Backtracks 0

Page 82: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

82

Propagation

1 1

1

1

1

1

11

1

1

1

1

1

12

2

2

2

2

2

2

2

2

2

2

3

3

3

3

3

33

Tests 92 + 4 * 5 = 112 Backtracks 0

X6 só pode tomar o valor 4

Page 83: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

83

Propagation

1 1

1

1

1

1

11

1

1

1

1

1

12

2

2

2

2

2

2

2

2

2

2

3

3

3

3

3

33

6

6

6

6

6 6

Tests 112+3+3+3+4 = 125 Backtracks 0

Page 84: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

84

Propagation

1 1

1

1

1

1

11

1

1

1

1

1

12

2

2

2

2

2

2

2

2

2

2

3

3

3

3

3

33

6

6

2

6

6 6

Tests 125 Backtracks 0

X8 só pode tomar o valor 7

Page 85: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

85

Propagation

1 1

1

1

1

1

11

1

1

1

1

1

12

2

2

2

2

2

2

2

2

2

2

3

3

3

3

3

33

6

6

2

6

6 6

Tests 125 Backtracks 0

Page 86: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

86

Propagation

1 1

1

1

1

1

11

1

1

1

1

1

12

2

2

2

2

2

2

2

2

2

2

3

3

3

3

3

33

6

6

2

6

6 6

8

8

Tests 125+2+2+2=131 Backtracks 0

Page 87: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

87

Propagation

1 1

1

1

1

1

11

1

1

1

1

1

12

2

2

2

2

2

2

2

2

2

2

3

3

3

3

3

33

6

6

2

6

6 6

8

8

Tests 131 Backtracks 0

X4 só pode tomar o valor 8

Page 88: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

88

Propagation

1 1

1

1

1

1

11

1

1

1

1

1

12

2

2

2

2

2

2

2

2

2

2

3

3

3

3

3

33

6

6

2

6

6 6

8

8

Tests 131 Backtracks 0

Page 89: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

89

Propagation

1 1

1

1

1

1

11

1

1

1

1

1

12

2

2

2

2

2

2

2

2

2

2

3

3

3

3

3

33

6

6

2

6

6 6

8

8

4

Tests 131+2+2=135 Backtracks 0

Page 90: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

90

Propagation

1 1

1

1

1

1

11

1

1

1

1

1

12

2

2

2

2

2

2

2

2

2

2

3

3

3

3

3

33

6

6

2

6

6 6

8

8

4

Tests 135 Backtracks 0

X5 só pode tomar o valor 2

Page 91: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

91

Propagation

1 1

1

1

1

1

11

1

1

1

1

1

12

2

2

2

2

2

2

2

2

2

2

3

3

3

3

3

33

6

6

2

6

6 6

8

8

4

Tests 135 Backtracks 0

Page 92: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

92

Propagation

1 1

1

1

1

1

11

1

1

1

1

1

12

2

2

2

2

2

2

2

2

2

2

3

3

3

3

3

33

6

6

2

6

6 6

8

8

4

5

Tests 135+1=136 Backtracks 0

Page 93: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

93

Propagation

1 1

1

1

1

1

11

1

1

1

1

1

12

2

2

2

2

2

2

2

2

2

2

3

3

3

3

3

33

6

6

2

6

6 6

8

8

4

5

Tests 136 Backtracks 0

Page 94: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

94

Propagation

1 1

1

1

1

1

11

1

1

1

1

1

12

2

2

2

2

2

2

2

2

2

2

3

3

3

3

3

33

6

6

2

6

6 6

8

8

4

5

Tests 136 Backtracks 0+1=1

Fails

7

Backtracks

3 !

Page 95: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

95

Propagation

1 1

1

1

1

1

11

1

1

1

1

1

12

2

2

2

2

2

2

2

2

2

2

3

33

3

3

3

3

3

Tests 136 Backtracks 1

3

X1=1

X2=3

X3=5

Impossible !

Tests

136

(324)

Backtracks

1

(15)

Page 96: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

96

Constraint Propagation

The programming of this propagation mechanism in Constraint Logic Programming follows the following programming pattern.

Problem ( Vars):-

Variables and Domains Declaration,

Specification of Constraints,

Labelling of the Variables.

In CLP, whenever a variable is labelled, the effects of this labling are propagated automatically through the relevant constraints, possibly pruning the domains of the remaining variables.

Whenever a domain becomes empty, a failure is detected, and backtracking (on labeled values) is performed.

Page 97: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

97

Constraint Propagation

This is the programming style adopted in program queens4_sat_fd.

% Domains Declaration

domain([Q1,Q2,Q3,Q4,...,Q13,Q14,Q15,Q16],0,1)

% Specification of Constraints (1st row)

~Q1 # ~Q2, ~Q1 # ~Q3, ~Q2 # ~Q3,

~Q1 # ~Q4, ~Q2 # ~Q4, ~Q3 # ~Q4,

Q1 # Q2 # Q3 # Q4,

% Labeling of variables

labeling([], [Q1,Q2,Q3,Q4,...,Q13,Q14,Q15,Q16])

Page 98: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

98

Constraint Propagation

The specification of constraints (in SICStus) adopts the usual relational operators, but preceded by the # character.

... and assumes that the module for finite domains is loaded with the command :- use_module(library(clpfd)).X # Y :-

exp(X # Y, Exp), Exp #>= 1.

exp(X, X):- var(X), !.

exp(X, X):- atom(X), !.

exp(~X, Exp):- !, exp(X, ExpX), Exp #= 1-ExpX.

exp(X # Y, Exp):- !,

exp(X, ExpX), exp(Y, ExpY), Exp #= ExpX + ExpY.

Page 99: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

99

Constraint Propagation

For example,

X | ?- domain([X,Y], 0,1), X # Y.

X in 0..1,Y in 0..1 ? ; % does not propagate

no

?- domain([X,Y], 0,1), X # Y, X #= 1.

X = 1,Y in 0..1 ? ; % does not propagate

no

?- domain([X,Y], 0,1), X # Y, X #= 0.

X = 0,Y = 1 ? ; % propagates X to Y

no

?- domain([X,Y], 0,1), X + Y #> 1.

X = 1,Y = 1 ? ; % propagates to X and Y

no

Page 100: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

100

Constraint Propagation

In general, solving a problem using constraint propagation is (much) more efficient than the alternative of using pure backtracking.

In SICStus Prolog, the measuring of efficiency can be made with built-in predicates fd_statistics/0 and fd_statistics/2

| ?- fd_statistics. Resumptions: 28 Entailments: 2 Prunings: 23 Backtracks: 0 Constraints created: 4 yes

Page 101: 1 Boolean Satisfaction - SAT Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1. In Computer.

101

Constraint Propagation

For problems as small as 4-queens, the difference in efficiency is not meaningful.

However, this is not the case with larger instances of the N-queens problem.

Such difference may be observed in the execution of programs

queens_sat_bk e queens_sat_fd

that are parameterisable to the intended dimension of the problem.


Recommended