Presentation is loading. Please wait.

Presentation is loading. Please wait.

21 November 2006 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming ­An overview Boolean Constraint Propagation Finite Domains.

Similar presentations


Presentation on theme: "21 November 2006 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming ­An overview Boolean Constraint Propagation Finite Domains."— Presentation transcript:

1 21 November 2006 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming ­An overview Boolean Constraint Propagation Finite Domains and Constraint Networks Consistency Criteria Maintaning Node Consistency Maintaining Arc Consistency [Dech04] Rina Dechter, Constraint Processing,

2 21 November 2006 Foundations of Logic and Constraint Programming 2 Boolean Satisfiability ­In most Boolean problems, the use of a complete solver is quite innefficient, since it maintains the set of all solutions in a solved form. ­Alternatively, a Boolean satisfiability problem may be solved, usually much more efficiently, by searching for a solution. ­In this case, two alternatives are available, with respect to the role of constraints:  Constraints are used passively. Once the variable appearing in the constraint are instantiated, the constraint is checked. If it is not satisfied, the search backtracks.  Constraints are used actively, to reduce the domain of the variables still to instantiate. ­These alternatives are illustrated by modelling all constraints as clauses.

3 21 November 2006 Foundations of Logic and Constraint Programming 3 Boolean Clauses Syntatically, A literal :  variable or its negation Variables are denoted by upper case identifiers, and negation with . A clause :  set of literals. Clauses are represented with the disjunction operator, e.g. A   B  C A Boolean Satisfiabilty Problem :  set of clauses. Semantically, Boolean variables may take values True / False (or T / F or 1 / 0). A negation “  “ reverses, of course, this value. A clause is interpreted as a disjunction of literals. Hence, a clause is satisfied if at least one of its literals takes value True. Solving a problem corresponds to find values to the variables such that all clauses are satisfied.

4 21 November 2006 Foundations of Logic and Constraint Programming 4 Search Tree Every problem may be represented as a (search) tree, where each node has two children, corresponding to a variable and its negation not appearing in the path from the root to the node. A solution of the problem, a complete asignment of T/F values to all variables, corresponds to a path from the root to a leaf, that satisfies all constraints. The search process may be implemented by traversing the search tree. A complete search will visit (if necessary) all leafs. CC C CC C B BB BB B BB B C CC A AA

5 21 November 2006 Foundations of Logic and Constraint Programming 5 Search Tree A generate and test approach to constraint solving will visit all leafs (generate potential solutions) and test the satisfiability of all clauses for the corresponding assignments of values to variables. Search may be improved by backtracking - at each node (not only in the leafs) the clauses with variables defined are tested for satisfiability. In this form, by testing incomplete solutions, backtracking avoids the “completion” of many impossible partial solutions. c1:  A  B c2:  A  C c3:  B   C CC C CC C B BB BB B BB B C CC A AA c3c2 c1 c3 

6 21 November 2006 Foundations of Logic and Constraint Programming 6 Search Tree Still, backtracking handles constraints passively. First, values are assigned to variables; then, constraints (clauses) are tested. Alternatively, constraints are handled actively with “constraint propagation”. In the Boolean Constraint Propagation (BCP): when all literals but one of a clause are assigned False, then the clause becomes active and propagates the remaining literal is assigned True. c1:  A  B c2:  A  C c3:  B   C CC C CC C B BB BB B BB B C CC A AA A = 1  B  1 (c1) C  1(c2) ? (c3) C = 1  B  1 (c3) 

7 21 November 2006 Foundations of Logic and Constraint Programming 7 This propagation can be noticed in the 4-queens problem. Here are some examples: 1.Propagate a 0-assignment 2.Propagate a 1 Boolean Constraint Propagation 11 0  X 11   X 12  X 12  0 X 21  X 22  X 23  X 24  X 24  1 1 0 00 0 00 00 0 1 0 00 0 00 00 0 1

8 21 November 2006 Foundations of Logic and Constraint Programming 8 Here is the complete search process with constraint propagation Assign X 11 PropagateX 23 & Propagate X 24 & Propagate& Propagate X 12 & Propagate & Propagate & Propagate & Propagate Boolean Constraint Propagation 1100 0 0 0 0 0 0 0 100 0 0 0 0 0 0 0 1 00 0 0 100 0 0 0 0 0 0 0 1 0 0 0 100 0 0 0 0 0 0 0 1 0 0 0 1 0 1 000 000 0 0 0 1 000 000 0 0 0 1 0 0 1 000 000 0 0 0 1 0 01 0 1 000 000 0 0 0 1 0 01 01 Fail ! X 31  X 32  X 33  X 34 Backtrack X 23 Fail ! X 41  X 42  X 43  X 44 Backtrack X 24

9 21 November 2006 Foundations of Logic and Constraint Programming 9 Boolean Clauses in SICStus SICStus does not provide any constraint solver for the satisfiability of Boolean clauses (SAT). Nevertheless, it can be easily implemented as an instance of a finite domains solver, where the domain of the variables are {0,1}. A clause is thus converted into an inequality constraint over an arithmetic expression. An example illustrates this modelling Clause X   Y  Z  X + (1-Y) + Z  1  X – Y + Z  0 Propagation 1: X = 0, Y = 1  0-1+Z  0  Z  1  Z  1 Propagation 2: X = 0, Z = 0  0-Y+0  0  Y  0  Y  0 (see details in queens4_sat_fd.pl)

10 21 November 2006 Foundations of Logic and Constraint Programming 10 Why Finite Domains ? ­In most problems, and notwithstanding the theoretical and practical interest in SAT, problems are more naturally specified with variables whose domain is not restricted to 0/1 values, but may take a finite set of values. ­In particular, the N queens problem is more “easily” programmed with n variables, one for each row, whose value denotes the column that the queen of that row must be in. The values allowed are, of course, the number of the column, i.e. integers from 1 to n. ­Such specification, not only is more natural, but it also may lead to a substantial increase in the efficiency of the solving process. ­Finite Domain encodings of problems where n variables can take k different values implement a search space with a substantially different size from the corresponding Boolean coding where each value is represented by a distinct Boolean variable.

11 21 November 2006 Foundations of Logic and Constraint Programming 11 Why Finite Domains ? ­To compare the search spaces of Boolean and Finite Domain encodings for problems where n variables can take k different values we note that: ­ Boolean Encodings - k*n boolean variables ( 0/1). Search Space = 2 kn. ­Finite Domains - n variables each with k possible values Search Space = k n ­Ratio of the Search Spaces r= 2 kn / 2 log 2 k*n = 2 (k-log 2 (k))*n For example, in the N-queens problem, we have  n = 8  k = 8, and r=2 (8-3)*8 = 2 40  10 12  n = 16  k =16, and r=2 (16-4)*16 = 2 192  10 58

12 21 November 2006 Foundations of Logic and Constraint Programming 12 Finite Domains ­Of course, the ratio of the search spaces that are actually searched is not so large, namely due to constraint propagation. Still, the differences may be quite significant. ­The efficiency of the CLP programs over finite domains (including the Boolean case) may still be improved by making use of  Adequate Propagation Algorithms  Appropriate Heuristics for selection of the Variable to label and the Value to assign ­Before investigating these issues, a number of concepts are introduced  Constraints and Constraint Network  Partial and Total Solutions  Satisfaction and Consistency

13 21 November 2006 Foundations of Logic and Constraint Programming 13 Basic Concepts: Domains Definition (Domain of a Variable):  The domain of a variable is the (finite) set of values that can be assigned to that variable. ­Given some variable X, its domain will be usually referred to as dom(X) or, simply, Dx. ­Example: The N queens problem may be modelled by means of N variables, X 1 to X n, all with the domain from 1 to n. Dom(X i ) = {1,2,..., n} or X i :: 1..n.

14 21 November 2006 Foundations of Logic and Constraint Programming 14 Basic Concepts: Labels ­To formalise the notion of the state of a variable (i.e. its assignment with one of the values in its domain) we have the following Definition (Label):  A label is a Variable-Value pair, where the Value is one of the elements of the domain of the Variable. ­The notion of a partial solution, in which some of the variables of the problem have already assigned values, is captured by the following Definition (Compound Label):  A compound label is a set of labels with distinct variables.

15 21 November 2006 Foundations of Logic and Constraint Programming 15 Basic Concepts: Constraints ­We come now to the formal definition of a constraint Definition (Constraint):  Given a set of variables, a constraint is a set of compound labels on these variables. ­Alternatively, a constraint may be defined simply as a relation, i.e. a subset of the cartesian product of the domains of the variables involved in that constraint. ­For example, given a constraint R ijk involving variables X i, X j and X k, then R ijk  dom(X i ) x dom(X j ) x dom(X k )

16 21 November 2006 Foundations of Logic and Constraint Programming 16 Basic Concepts: Constraints ­Given a constraint C, the set of variables involved in that constraint is denoted by vars(C). ­Simetrically, the set of constraints in which variable X participates is denoted by cons(X). ­Notice that a constraint is a relation, not a function, so that it is always C ij = C ji. ­In practice, constraints may be specified by Extension: through an explicit enumeration of the allowed compound labels; Intension: through some predicate (or procedure) that determines the allowed compound labels.

17 21 November 2006 Foundations of Logic and Constraint Programming 17 Basic Concepts: Constraint Definition ­For example, the constraint C 13 involving X 1 and X 3 in the 4-queens problem, may be specified ­By extension (label form): C 13 ={{X 1 -1,X 3 -2},{X 1 -1,X 3 -4},{X 1 -2,X 3 -1},{X 1 -2,X 3 -3}, {X 1 -3,X 3 -2},{X 1 -3,X 3 -4},{X 1 -4,X 3 -1},{X 1 -4,X 3 -3}}. or, in tuple (relational) form, omitting the variables C 13 = {,,,,,,, }. ­By intension: C13 = (X 1  X 3 )  (1+X 1  3+X 3 )  (3+X 1  1+X 3 ).

18 21 November 2006 Foundations of Logic and Constraint Programming 18 Basic Concepts: Constraint Arity Definition (Constraint Arity):  The constraint arity of some constraint C is the number of variables over which the constraint is defined, i.e. the cardinality of the set Vars(C). ­Despite the fact that constraints may have an arbitrary arity, an important subset of the constraints is the set of binary constraints. ­The importance of such constraints is two fold  All constraints may be converted into binary constraints  A number of concepts and algorithms are appropriate for these constraints.

19 21 November 2006 Foundations of Logic and Constraint Programming 19 Basic Concepts: Binary Constraints Conversion to Binary Constraints ­An n-ary constraint, C, defined by k compound labels on its variables X 1 to X n, is equivalent to n binary constraints, C 0i, through the addition of a new variable, X 0, whose domain is the set 1 to k. In practice, ­The k n-ary labels may be sorted in some arbitrary order. ­Each of the n binary constraints C 0i relates the new variable X 0 with the variable X i. ­The compound label {X i -v i, X 0 -j} belongs to constraint C 0i iff X i -v i belongs to the j-th compound label that defines C.

20 21 November 2006 Foundations of Logic and Constraint Programming 20 Basic Concepts: Binary Constraints Example: Given variables X 1 to X 3, with domains 1..3, the following ternary constraint C is composed of 6 labels.. C(X 1, X 2, X 3 ) = {,,,,, } Each of the labels may be associated to a value from 1 to 6 1 :, 2 :,..., 6: Now, the following binary constraints, C 01 to C 03, are equivalent to the initial ternary constraint C C 01 (X 0, X 1 )= {,,,,, } C 02 (X 0, X 2 )= {,,,,, } C 03 (X 0, X 3 )= {,,,,, }

21 21 November 2006 Foundations of Logic and Constraint Programming 21 Basic Concepts: Constraint Satisfaction Definition (Constraint Satisfaction 1):  A compound label satisfies a constraint if their variables are the same and if the compound label is a member of the constraint. ­In practice, it is convenient to generalise constraint satisfaction to compound labels that strictly contain the constraint variables. Definition (Constraint Satisfaction 2):  A compound label satisfies a constraint if its variables contain the constraint variables and the projection of the compound label to these variables is a member of the constraint.

22 21 November 2006 Foundations of Logic and Constraint Programming 22 Basic Concepts: Constraint Satisfaction Problem Definition (Constraint Satisfaction Problem): A constraint satisfaction problem is a triple where  V is the set of variables of the problem  D is the domain(s) of its variables  C is the set of constraints of the problem Definition (Problem Solution): A solution to a Constraint Satisfaction Problem P:, is a compound label over the variables V of the problem, which satisfies all constraints in C.

23 21 November 2006 Foundations of Logic and Constraint Programming 23 Basic Concepts: Constraint Graph ­For convenience, the constraints of a problem may be considered as forming a special graph. Definition (Constraint Graph or Constraint Network): The Constraint Graph or Constraint Network of a binary constraint satisfaction problem is defined as follows  There is a node for each of the variables of the problem.  For each non-trivial constraint of the problem, involving one or two variables, the graph contains an arc linking the corresponding nodes. ­When the problems include constraints with arbitrary arity, the Constraint Network may be formed after converting these constraints on its binary equivalent.

24 21 November 2006 Foundations of Logic and Constraint Programming 24 Basic Concepts: Constraint Hyper-Graph ­When it is not convenient to convert n-ary constraints to their binary equivalent, the problem may be represented by an hiper-graph. Definition (Constraint Hyper-Graph): Any constraint satisfaction problem with arbitrary n-ary constraints may be represented by a Constraints Hiper-Graph formed as follows:  There is a node for each of the variables of the problem.  For each constraint of the problem, the graph contains an hiper-arc linking the corresponding nodes. ­Of course, when the problem has only binary constraints, the hiper-graph degenerates into the simpler graph.

25 21 November 2006 Foundations of Logic and Constraint Programming 25 Example: 4-Queens Example: The 4 queens problem may be specified by the following constraint network: X 1 in 1..4 X 4 in 1..4 X 3 in 1..4X 2 in 1..4 C 12 R 23 R 14 C 24 C 34 C 13 C13:,,,,,,,

26 21 November 2006 Foundations of Logic and Constraint Programming 26 Basic Definitions – Graph Density Definition (Complete Constraint Network): A constraint netwok is complete, when there is an arc connecting any two nodes of the graph (i.e. when there is a constraint over any pair of variables). ­The 4 queens problem (in fact, any n queens problem) has a complete graph. ­However, this is often not the case: most graphs are sparse. In this case, it is important to measure the density of the constraint network. Definition (Density of a Constraint Graph): The density of a constraint graph is the ratio between the number of its arcs and the number of arcs of a complete graph on the same number of nodes

27 21 November 2006 Foundations of Logic and Constraint Programming 27 Basic Definitions – Problem Difficulty ­In principle, the “difficulty” of a constraint problem is related to the density of its graph. ­Intuitively, the denser the graph is, the more difficult the problem becomes, since there are more opportunities to invalidate a compound label. ­It is important to distinguish between the difficulty of a problem and the difficulty of solving the problem.  In particular, a difficult problem may be so difficult that it is trivial to find it to be impossible! ­The “difficulty” of a problem is also related to the difficulty of satisfying each of its constraints. Such difficulty may be measured through its “tightness”.

28 21 November 2006 Foundations of Logic and Constraint Programming 28 Basic Definitions – Constraint Tightness Definition (Tightness of a Constraint):  Given a constraint C on variables X 1... X n, with domains D 1 to D n, the tightness of C is defined as the ratio between the number of labels that define the constraint and the size (i.e. the cardinality) of the cartesian product D 1 x D 2 x.. D n. ­For example, the tightness of constraint C 13 of the 4 queens problem, on variables X 1 and X 3 with domains 1..4 C 13 = {,,,,,,, } is ½, since there are 8 tuples in the relation out of the possible 16 (4*4). ­The notion of tightness may be generalised for the whole problem.

29 21 November 2006 Foundations of Logic and Constraint Programming 29 Basic Definitions – Problem Tightness ­Definition (Tightness of a Problem):  The tightness of a constraint satisfaction problem with variables X 1... X n, is the ratio between the number of its solutions and the cardinality of the cartesian product D 1 x D 2 x.. D n. ­For example, the 4 queens problem, that has only two solutions and has tightness 2/(4*4*4*4) = 1/128.

30 21 November 2006 Foundations of Logic and Constraint Programming 30 Basic Definitions – Problem Solving Difficulty ­The difficulty in solving a constraint satisfaction problem is related both with the  The density of its graph and  Its tightness. ­As such, when testing algorithms to solve this type of problems, it is usual to generate random problem instances, parameterised not only by the number of variables and the size of their domains, but also by the density of its graph and the tightness of its constraints. ­The study of these issues has led to the conclusion that constraint satisfaction problems often exhibit a phase transition, which should be taken into account in the study of the algorithms. ­This phase transition typically contains the most difficult instances of the problem, and separates the instances that are trivially satisfied from those that are trivially insatisfiable.

31 21 November 2006 Foundations of Logic and Constraint Programming 31 Basic Definitions – Problem Solving Difficulty ­For example, in SAT, it has been found that the phase transition occurs when the ration of clauses to variables is around 4.3. # clauses / # variables difficultydifficulty 4.3

32 21 November 2006 Foundations of Logic and Constraint Programming 32 Basic Definitions – Redundancy ­Another issue to consider in the difficulty of solving a constraint satisfaction problem is the potential existence of redundant values and labels in its constraints. Definition (Redundant Value):  A value in the domain of a variable is redundant, if it does not appear in any solution of the problem. Definition (Redundant Label):  A compound label of a constraint is redundant if it is not the projection to the constraint variables of a solution to the whole problem. ­Redundant values and labels increase the search space useless, and should thus be avoided. There is no point in testing a value that does not appear in any solution !

33 21 November 2006 Foundations of Logic and Constraint Programming 33 Basic Definitions – Redundancy Example: The 4 queens problem only admits two solutions: and. ­Hence, values 1 and 4 are redundant in the domain of variables X 1 and X 4, and values 2 and 3 are redundant in the domain of variables X 2 and X 3. ­Regarding redundant labels, labels (shown in the figure) and are redundant in constraint C 13.

34 21 November 2006 Foundations of Logic and Constraint Programming 34 Basic Definitions – Redundancy Example: The 4 queens problem, which only admits the two solutions and may be “simplified” by elimination of the redundant values and labels. C13:,,,,,,, X 1 in 1,2,3,4 X 4 in 1,2,3,4 X 3 in 1,2,3,4X 2 in 1,2,3,4 C 12 R 23 R 14 C 24 C 34 C 13

35 21 November 2006 Foundations of Logic and Constraint Programming 35 Basic Definitions – Equivalent Problems ­Of course, any problem should be equivalent to any of its simplified versions. Formally, Definition (Equivalent Problems): Two problems P1 = and P2 = are equivalent iff they have the same variables (i.e. V1 = V2) and the same set of solutions. ­The “simplification” of a problem may also be formalised Definition (Reduced Problem): A problem P= is reduced to P’= if  P and P’ are equivalent;  The domains D’ are included in D; and  The constraints C’ are at least as restrictive as those in C.

36 21 November 2006 Foundations of Logic and Constraint Programming 36 Constraint Solving Methods ­As shown before, and independently of any problem reduction, a generate and test procedure to solve a Constraint Satisfaction Problem is usually very inneficient. ­Nevertheless, this is the approach taken in local search methods (simulated annealing or genetic algorithms) – although mostly in an optimisation context ! ­It is thus often preferable to use a solving method that is constructive and incremental, whereby a compound label is being completed (constructive), one variable at a time (incremental), until a solution is reached. ­However, one must check that at every step in the construction of a solution the resulting label has still the potential to reach a complete solution.

37 21 November 2006 Foundations of Logic and Constraint Programming 37 Constraint Solving Methods ­Ideally, a compound label should be the projection of some problem solution. ­Unfortunately, in the process of solving a problem, its solutions are not known yet! ­Hence, one must use a notion weaker than that of a (complete) solution, namely Definition (k-Partial Solution):  A k-partial solution of a constraint solving problem P =, is a compound label on a subset of k of its variables, V k, that satisfies all the constraints in C whose variables are included in V k.

38 21 November 2006 Foundations of Logic and Constraint Programming 38 Constructive Solving Methods ­This is of course, the basis of the solving methods that use some form of backtracking. If conveniently performed, backtracking may be regarded as a tree search, where the partial solutions correspond to the internal nodes of the tree and complete solutions to its leaves. 2 4 1 3 3 14 1 4 2

39 21 November 2006 Foundations of Logic and Constraint Programming 39 Problem Search Space ­Clearly, the more a problem is reduced, the easier it is, in principle, to solve it. ­Given a problem P= with n variables X 1,..,X n the potential search space where solutions can be found (i.e. the leaves of the search tree with compound labels {,..., }) has cardinality #S = #D 1 * #D 2 *... * #D n ­Assuming identical cardinality (or some kind of average of the domains size) for all the variable domains, (#D i = d) the search space has cardinality #S = d n which is exponential on the “size” n of the problem.

40 21 November 2006 Foundations of Logic and Constraint Programming 40 ­If instead of the cardinality d of the initial problem, one solves a reduced problem whose domains have lower cardinality d’ (<d) the size of the potential search space also decreases exponentially! S’/S = d’ n / d n = (d’/d) n ­Such exponential decrease may be very significant for “reasonably” large values of n, as shown in the table. Problem Search Space

41 21 November 2006 Foundations of Logic and Constraint Programming 41 Reduction of the Search Space ­In practice, this potential narrowing of the search space has a cost involved in finding the redundant values (and labels). ­A detailed analysis of the costs and benefits in the general case is extremely complex, since the process depends highly on the instances of the problem to be solved. ­However, it is reasonable to assume that the computational effort spent on problem reduction is not proportional to the reduction achieved, becoming less and less efficient. ­After some point, the gain obtained by the reduction of the search space does not compensate the extra effort required to achieve such reduction.

42 21 November 2006 Foundations of Logic and Constraint Programming 42 Reduction of the Search Space Qualitatively, this process may be represented by means of the following graph Computational Cost R - Reduction Cost S- Search Cost R+S Combined Cost Effort spent in solving the problem Amount of Reduction Achieved

43 21 November 2006 Foundations of Logic and Constraint Programming 43 Reduction of the Search Space ­The effort in reducing the domains must be considered within the general scheme to solve the problem. ­In Constraint Logic Programming, the specification of the constraints usually precedes the enumeration of the variables. Problem(Vars):- Declaration of Variables and Domains, Specification of Constraints, Labelling of the Variables. ­In general, search is performed exclusively on the labelling of the variables. ­The execution model alternates enumeration with propagation, making it possible to reduce the problem at various stages of the solving process.

44 21 November 2006 Foundations of Logic and Constraint Programming 44 Reduction of the Search Space ­Hence, given a problem with n variables X 1 to X n the execuction model follows the following pattern: Declaration of Variables and Domains, Specification of Constraints, indomain(X 1 ), % value selection with backtraking propagation, % reduction of problem X 2 to X n indomain(X 2 ), propagation, % reduction of problem X3 to Xn... indomain(X n-1 ) propagation, % reduction of problem X n indomain(X n )

45 21 November 2006 Foundations of Logic and Constraint Programming 45 Domain Reduction – Consistency Criteria ­Once formally defined the notion of problem reduction, one must discuss the actual procedure that may be used to achieve it. ­First of all, one must ensure that whatever procedure is used, the reduction keeps the problem equivalent to the initial one. ­Here we have a small problem, since the definition of equivalence requires the solutions to be the same and we do not know in general what the solutions are! ­Nevertheless, the solutions will be the same if in the process of reduction we have the guarantee that “no solutions are lost”. Such guarantees are met by several criteria.

46 21 November 2006 Foundations of Logic and Constraint Programming 46 Domain Reduction – Consistency Criteria ­Consistency criteria enable to establish redundant values in the variables domains in an indirect form, i.e. requiring no prior knowledge on the set of problem solutions. ­Hence, procedures that maintain these criteria during the “propagation” phases, will eliminate redundant values and so decrease the search space on the variables yet to be enumerated. ­For constraint satisfaction problems with binary constraints, the most usual criteria are, in increasingly complexity order,  Node Consistency  Arc Consistency  Path Consistency

47 21 November 2006 Foundations of Logic and Constraint Programming 47 Domain Reduction – Node Consistency Definition (Node Consistency): A constraint satisfaction problem is node-consistent if no value on the domain of its variables violates the unary constraints. ­This criterion may seem both obvious and useless. After all, who would specify a domain that violates the unary constraints ?! ­However, this criterion must be regarded within the context of the execution model that incrementally completes partial solutions. Constraints that were not unary in the initial problem become so when one (or more) variables are enumerated.

48 21 November 2006 Foundations of Logic and Constraint Programming 48 Domain Reduction – Node Consistency Example: ­After the initial posting of the constraints, the constraint network model at the right represents the 4-queens problem. ­After enumeration of variable X 1, i.e. X 1 =1, constraints C 12, C 13 and C 14 become unary !! X 1 in 1..4 X 4 in 1..4 X 3 in 1..4X 2 in 1..4 C 12 C 23 R 14 C 24 C 34 R 13 X 4 in 1..4 X 3 in 1..4X 2 in 1..4 C 23 C 24 C 34 X 2  1,2X 3  1,3 X 4  1,4

49 21 November 2006 Foundations of Logic and Constraint Programming 49 Domain Reduction – Node Consistency ­An algorith that maintains node consistency should remove from the domains of the “future” variables the appropriate values. ­Maintaining node consistency achieves a domain reduction similar to what was achieved in propagation with the boolean formulation. X 4 in 2,3 X 3 in 2,4X 2 in 3,4 C 23 C 24 C 34 X 2  1,2X 3  1,3 X 4  1,4 X 2  1,2 X 3  1,3

50 21 November 2006 Foundations of Logic and Constraint Programming 50 Maintaining Node Consistency ­Given the simplicity of the node consistency criterion, an algorithm to maintain it is very simple and with low complexity. ­ A possible algorithm is NC-1, below. procedure NC-1(V, D, R); for X in V for v in Dx do for Cx in {Cons(X): Vars(Cx) = {X}} do if not satisfy(X-v, Cx) then Dx <- Dx \ {v} end for end procedure

51 21 November 2006 Foundations of Logic and Constraint Programming 51 Maintaining Node Consistency Space Complexity of NC-1: O(nd) ­Assuming n variables in the problem, each with d values in its domain, and assuming that the variable’s domains are represented by extension, a space nd is required to keep explicitely the domains of the variables. ­For example, the initial domain 1..4 of variables X i in the 4 queens problem is represented by a boolean vector (where 1 means the value is in the domain) X i = [1,1,1,1] or 1111. ­After enumeration X 1 =1, node consistency prunes the domain of other variables to X 1 = 1000, X 2 = 0011, X 3 = 0101 and X 4 = 0110 ­Algorithm NC-1 does not require additional space, so its space complexity is O(nd).

52 21 November 2006 Foundations of Logic and Constraint Programming 52 Maintaining Node Consistency Time Complexity of NC-1: O(nd)  Assuming n variables in the problem, each with d values in its domain, and taking into account that each value is evaluated one single time, it is easy to conclude that algorithm NC-1 has time complexity O(nd). ­The low complexity, both temporal and spatial, of algorithm NC-1, makes it suitable to be used in virtual all situations by a solver. ­However, node consistency is rather incomplete, not being able to detect many possible reductions.

53 21 November 2006 Foundations of Logic and Constraint Programming 53 Domain Reduction – Arc Consistency ­A more demanding and complex criterion of consistency is that of arc- consistency Definition (Arc Consistency): A constraint satisfaction problem is arc-consistent if, It is node-consistent; and For every label X i -v i of every variable X i, and for all constraints C ij, defined over variables X i and X j, there must exist a value v j that supports v i, i.e. such that the compound label {X i -v i, X j -v j } satisfies constraint Cij.

54 21 November 2006 Foundations of Logic and Constraint Programming 54 Domain Reduction – Arc Consistency Example: ­After enumeration of variable X 1 =1, and making the network node-consistent, the 4 queens problem has the following constraint network: ­However, label X 2 -3 has no support in variable X 3, since neither compound label {X 2 - 3, X 3 - 2 } nor {X 2 - 3, X 3 - 4 } satisfy constraint C 23. ­Therefore, value 3 can be safely removed from the domain of X 2. X 4 in 2,3 X 3 in 2,4X 2 in 3,4 C 23 C 24 C 34 X 2  1,2X 3  1,3 X 4  1,4 X 2  1,2 X 3  1,3

55 21 November 2006 Foundations of Logic and Constraint Programming 55 Domain Reduction – Arc Consistency Example (cont.): ­In fact, none (!) of the values of X 3 has support in variables X 2 and X 4., as shown below:  label X 3 -4 has no support in variable X 2, since none of the compound labels {X 2 -3, X 3 -4} and {X 2 -4, X 3 -4} satisfy constraint C 23.  label X 3 -2 has no support in variable X 4, since none of the compound labels {X 3 -2, X 4 -2} and {X 3 -2, X 4 -3} satisfy constraint C 34. X 4  1,4 X 2  1,2 X 3  1,3

56 21 November 2006 Foundations of Logic and Constraint Programming 56 Domain Reduction – Arc Consistency Example (cont.): ­Since none of the values from the domain of X 3 has support in variables X 2 and X 4, maintenance of arc-consistency empties the domain of X 3 ! ­Hence, maintenance of arc-consistency not only prunes the domain of the variables but also antecipates the detection of unsatisfiability in variable X 3 ! In this case, backtracking of X 1 =1 may be started even before the enumeration of variable X 2. ­Given the good trade-of between pruning power and simplicity of arc- consistency, a number of algorithms have been proposed to maintain it. X 4  1,4 X 2  1,2 X 3  1,3

57 21 November 2006 Foundations of Logic and Constraint Programming 57 Maintaining Arc Consistency: AC-1 ­AC-1, shown below, is a very simple algorithm to maintain Arc Consistency´ procedure AC-1(V, D, C); NC-1(V,D,C);% node consistency Q = {aij | Cij  C  Cji  C }; % see note repeat changed <- false; for aij in Q do changed <- changed or revise_dom(aij,V,D,C) end for until not change end procedure Note: for constraint C ij two directed arcs, a ij and a ji, are considered

58 21 November 2006 Foundations of Logic and Constraint Programming 58 Maintaining Arc Consistency: AC-1 ­Predicate rev_dom(aij,V,D,R) succeeds iff it prunes values from the domain of X i predicate revise_dom(aij,V,D,C): Boolean; success <- false; for vi in dom(Xi) do if there is no vj in dom(Xj) such that satisfies({Xi-vi,Xj-vj},Cij) then dom(Xi) <- dom(Xi) \ {vi}; success <- true; end if end for revise_dom <- success; end predicate

59 21 November 2006 Foundations of Logic and Constraint Programming 59 Time Complexity of AC-1 Time Complexity of AC-1 : O(nad 3 ) ­Assuming n variables in the problem, each with d values in its domain, and a total of a arcs, in the worst case, predicate revise_dom, checks d 2 pairs of values. ­The number of arcs a ij in queue Q is 2a (2 directed arcs a ij and a ji are considered for each constraint C ij ). For each value removed from one domain, revise_dom is called 2a times. ­In the worst case, only one value from one variable is removed in each cycle, and the cycle is executed nd times. ­Therefore, the worst-case time complexity of AC-1 is O( d 2 2a*nd), i.e. O(nad 3 )

60 21 November 2006 Foundations of Logic and Constraint Programming 60 Space Complexity of AC-1 Space Complexity of AC-1 : O(ad 2 ) = O(n 2 d 2 ) ­AC-1 must maintain a queue Q, with maximum size 2a. Hence the inherent spacial complexity of AC-1 is O(a). ­To this space, one has to add the space required to represent the domains O(nd) and the constraints of the problem. Assuming a constraints and d values in each variable domain the space required is O(ad 2 ), and a total space requirement of O(nd + ad 2 ) which dominates O(a). ­For “dense” constraint networks”, a  n 2 /2. This is then the dominant term, and the space complexity becomes O(ad 2 ) = O(n 2 d 2 )

61 21 November 2006 Foundations of Logic and Constraint Programming 61 Maintaining Arc Consistency: From AC-1 to AC-3 Inefficiency of AC-1  Every time a value v i is removed from the domain of variable X i by predicate revise_dom(aij,V,D,R), all arcs are reexamined.  However, only the arcs a ki (for k  i and k  j ) should be reexamined.  This is because the removal of v i may eliminate the support from some value v k of some variable X k for which there is a constraint C ki (or C ik ).  Such inefficiency is eliminated in algorithm AC-3.

62 21 November 2006 Foundations of Logic and Constraint Programming 62 Maintaining Arc Consistency: AC-3 ­AC-3 only revisits arcs for which the domain of the “leading” variable has been “revised” (narrowed). procedure AC-3(V, D, C); NC-1(V,D,R); % node consistency Q = {aij | Rij  C  Cji  C }; while Q   do Q = Q \ {aij} % removes an element from Q if revise_dom(aij,V,D,C) then % revised Xi Q = Q  {aki | Cki  C  k  i  k  j} end if end while end procedure ­Intuitively, AC-3 must have not only better worst-case, but also a better typical- case complexity than AC-1.

63 21 November 2006 Foundations of Logic and Constraint Programming 63 Time and Space Complexity of AC-3 Time Complexity of AC-3: O(ad 3 ) ­Each arc a ki is only added to Q when some value v i is removed from the domain of X i. ­In total, each of the 2a arcs may be added to Q (and removed from Q) d times. ­Every time that an arc is removed, predicate revise_dom is called, to check at most d 2 pairs of values. ­All things considered, and in contrast with AC-1, with complexity O(nad 3 ), the time complexity of AC-3, in the worst case, is O(2ad * d 2 ), i.e. O(ad 3 ). Space Complexity of AC-3: O(ad 2 ) ­As to space complexity AC-3 has the same requirements than AC-1, and the same worst-case space complexity of O(ad 2 )  O(n 2 d 2 ), due to the representation of constraints by extension.


Download ppt "21 November 2006 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming ­An overview Boolean Constraint Propagation Finite Domains."

Similar presentations


Ads by Google