1.01k likes | 1.16k Views
Boolean Satisfaction - SAT. Among all possible finite domains, the Booleans is a specially interesting case, where all variables take values 0/1.
E N D
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.
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.
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
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.
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
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
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
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 ( (Mi Bi) (Mi Bi)) • (X Mi)( X Mi Bi) (X Mi Bi)
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 bitSn+1 = Cn+1 • Other bitsSi = Ai Bi Ci • Ci+1 = (AiBi)(AiCi)(BiCi) • that are converted in the clausal form as before.
Boolean Satisfaction - SAT • First bit • S0 = A0 B0 • (S0 (A0 B0)) (S0 (A0 B0)) • (S0((A0B0)(A0B0)))( S0 ((A0B0)(A0B0))) • (S0A0B0 )(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)
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)
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 • ....
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
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.
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
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).
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.
Importance of SAT • For 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 integersfrom 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.
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.
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.
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.
queens_4(L):- generate(L), test(L), report(L). generate([]). generate([H|T]):- member(H,[1,0]), generate(T). test([Q1, Q2, Q3, ...,Q16]):- at_most_one([Q1,Q2,Q3,Q4]), at_least_one([Q1,Q2,Q3,Q4]), ... at_most_one([Q9,Q14]). SAT Solving - Generate and Test The generation of all solutions may be obtained by the backtracking mechanism built-in in Logic Programming tools.. Example:
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,
% test for some clause exp(X, X):- atomic(X), !. exp(~X, 1-ExpX):- !, exp(X, ExpX). exp(X # Y, ExpX + ExpY):- !, exp(X, ExpX), exp(Y, ExpY). X # Y :- exp(X # Y, Exp), Exp >= 1. 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
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.
a a b b b b X c c c c c c c c X d d d d d d d d d d d d d d d d 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.
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.
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]).
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 performed • X # Y :- • exp(X # Y, Exp), • inc(t), • Exp >= 1.
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
Backtracking Tests 0 Backtracks 0
Backtracking Q1\= Q2,L1+Q1 \= L2+Q2,L1+Q2 \= L2+Q1. Tests 0 + 1 = 1 Backtracks 0
Backtracking Q1\= Q2,L1+Q1 \= L2+Q2,L1+Q2 \= L2+Q1. Tests 1 + 1 = 2 Backtracks 0
Backtracking Q1\= Q2,L1+Q1 \= L2+Q2,L1+Q2 \= L2+Q1. Tests 2 + 1 = 3 Backtracks 0
Backtracking Tests 3 + 1 = 4 Backtracks 0
Backtracking Tests 4 + 2 = 6 Backtracks 0
Backtracking Tests 6 + 1 = 7 Backtracks 0
Backtracking Tests 7 + 2 = 9 Backtracks 0
Backtracking Tests 9 + 2 = 11 Backtracks 0
Backtracking Tests 11 + 1 + 3 = 15 Backtracks 0
Backtracking Tests 15 + 1 + 4 + 2 + 4 = 26 Backtracks 0
Backtracking Tests 26 + 1 = 27 Backtracks 0
Backtracking Tests 27 + 3 = 30 Backtracks 0
Backtracking Tests 30 + 2 = 32 Backtracks 0
Backtracking Tests 32 + 4 = 36 Backtracks 0
Backtracking Tests 36 + 3 = 39 Backtracks 0
Backtracking Tests 39 + 1 = 40 Backtracks 0
Backtracking Tests 40 + 2 = 42 Backtracks 0
Backtracking Tests 42 + 3 = 45 Backtracks 0
Backtracking Fails 6 Backtracks 5 Tests 45 Backtracks 0