5 CONSTRAINT SATISFACTION PROBLEMS In which we see ho reating states as more than just black boxes leads tothe complken search methods and a deeper understanding Chapters 3 and 4 explored the idea that problems can be solved by searching in a space of states.These states can be evaluated by domain-specific heuristics and tested to see whether they are goal states.From the point of view of the search algorithm,however, BLACK BOX each state is a black box with no discernible internal structure.It is represented by an arbi- trary data structure that can be accessed only by the problem-specific routines-the successor function heuristic function and goal test This chapter examines constraint satisfaction problems.whose states and goal test conform to a standard,structured,and very simple rep resentation (Section 5 1)Search al- cture of states and use general-purpose 0ns5.2 5.3).Perhaps most tantly,the standard representation of the ture of the itself (Section 5.4).This anderstanding of the intimate between the structure of a problem and of solving it 5.1 CONSTRAINT SATISFACTION PROBLEMS Formally speaking,a constraint satisfaction problem(or CSP)is defined by a set of vari- ARIABLES ables,X1,X2,....Xn.and a set of constraints,C1,C2.....Cm.Each variable Xi has a CONSTRAINTS nonempty domain Di of possible values.Each constraint Ci involves some subset of the DOMAIN variables and specifies the allowable combinations of values for that subset.A state of the VALUES problem is defined by an assignment of values to some or all ofthe variables,{X;=vi,X;= ASSIGNENT ...)An assignment that does not violate any constraints is called a consistent or legal CONSISTENT assignment.A complete assignment is one in which every variable is mentioned,and a so- lution to a CSP is a complete assignment that satisfies all the constraints.Some CSPs also 8 require a solution that maximizes an objective function. 137
5 CONSTRAINT SATISFACTION PROBLEMS In which we see how treating states as more than just little black boxes leads to the invention of a range of powerful new search methods and a deeper understanding of problem structure and complexity. Chapters 3 and 4 explored the idea that problems can be solved by searching in a space of states. These states can be evaluated by domain-specific heuristics and tested to see whether they are goal states. From the point of view of the search algorithm, however, BLACK BOX each state is a black box with no discernible internal structure. It is represented by an arbitrary data structure that can be accessed only by the problem-specific routines—the successor function, heuristic function, and goal test. This chapter examines constraint satisfaction problems, whose states and goal test REPRESENTATION conform to a standard, structured, and very simple representation (Section 5.1). Search algorithms can be defined that take advantage of the structure of states and use general-purpose rather than problem-specific heuristics to enable the solution of large problems (Sections 5.2– 5.3). Perhaps most importantly, the standard representation of the goal test reveals the structure of the problem itself (Section 5.4). This leads to methods for problem decomposition and to an understanding of the intimate connection between the structure of a problem and the difficulty of solving it. 5.1 CONSTRAINT SATISFACTION PROBLEMS Formally speaking, a constraint satisfaction problem (or CSP) is defined by a set of vari- CONSTRAINT SATISFACTION PROBLEM VARIABLES ables, X1, X2, . . . , Xn, and a set of constraints, C1, C2, . . . , Cm. Each variable Xi has a CONSTRAINTS nonempty domain Di of possible values. Each constraint Ci involves some subset of the DOMAIN VALUES variables and specifies the allowable combinations of values for that subset. A state of the problem is defined by an assignment of valuesto some or all of the variables, {Xi = vi , Xj = ASSIGNMENT vj , . . .}. An assignment that does not violate any constraints is called a consistent or legal CONSISTENT assignment. A complete assignment is one in which every variable is mentioned, and a solution to a CSP is a complete assignment that satisfies all the constraints. Some CSPs also require a solution that maximizes an objective function. OBJECTIVE FUNCTION 137
138 Chapter 5.Constraint Satisfaction Problems what does all this mean?Suppose that,having tired of Romnia.eare ookin at a map of. eachregion either red.green,or blue insandh of c owing cach and territories,as ing regions nis as a CSP,we to be the regions the se (red,green).(red,biue),(green,red),(green,blue).(bue,red),(blue,green) (The constraint can also be represented more succinctly as the inequality WA NT,pro- vided the constraint satisfaction algorithm has some way to evaluate such expressions.)There are many possible solutions such as {WA=red,NT=green,Q=red,NSW green,V=red,SA=blue,T=red } It is helpful to visualize a CSPas a constraint graph,as shown in Figure 5.1(b).The nodes of the graph correspond to variables of the problem and the arcs correspond to constraints Treating a problem as a CSP confers several important benefits.Because the representa tion of states in a CSP conforms to a standard pattern-that is,a set of variables with assigned -the successor function and goal test can written in a generic way that applies to al CSPs.Furthermore,we can develop effective,generic heuristics that require no additional domain-specific expertise.Finally,the structure of the constraint graph can be used to sim- plify the solution process,in some cases giving an exponential reduction in complexity.The CSP representation is the first,and simplest,in a series of representation schemes that will be developed throughout the book. NT SA Victoria Tasmania (a) (b) Figure 5.1 on pro s to map-c ng pr m represente as a constraint graph
138 Chapter 5. Constraint Satisfaction Problems So what does all this mean? Suppose that, having tired of Romania, we are looking at a map of Australia showing each of its states and territories, as in Figure 5.1(a), and that we are given the task of coloring each region either red, green, or blue in such a way that no neighboring regions have the same color. To formulate this as a CSP, we define the variables to be the regions: WA, NT, Q, NSW , V , SA, and T. The domain of each variable is the set {red, green, blue}. The constraints require neighboring regions to have distinct colors; for example, the allowable combinations for WA and NT are the pairs {(red, green),(red, blue),(green, red),(green, blue),(blue, red),(blue, green)} . (The constraint can also be represented more succinctly as the inequality WA 6= NT, provided the constraint satisfaction algorithm has some way to evaluate such expressions.) There are many possible solutions, such as {WA = red, NT = green, Q = red, NSW = green, V = red, SA = blue, T = red }. CONSTRAINT GRAPH It is helpful to visualize a CSP as a constraint graph, as shown in Figure 5.1(b). The nodes of the graph correspond to variables of the problem and the arcs correspond to constraints. Treating a problem as a CSP confers several important benefits. Because the representation of states in a CSP conforms to a standard pattern—that is, a set of variables with assigned values—the successor function and goal test can written in a generic way that applies to all CSPs. Furthermore, we can develop effective, generic heuristics that require no additional, domain-specific expertise. Finally, the structure of the constraint graph can be used to simplify the solution process, in some cases giving an exponential reduction in complexity. The CSP representation is the first, and simplest, in a series of representation schemes that will be developed throughout the book. Western Australia Northern Territory South Australia Queensland New South Wales Victoria Tasmania WA NT SA Q NSW V T (a) (b) Figure 5.1 (a) The principal states and territories of Australia. Coloring this map can be viewed as a constraint satisfaction problem. The goal is to assign colors to each region so that no neighboring regions have the same color. (b) The map-coloring problem represented as a constraint graph
Section 5.1. Constraint Satisfaction Problems 139 It is fairly easy to see that a CSP can be given an incremental formulation as a standard search problem as follows Initial state:the empty assignment in which all variables are unassigned. Successor function:a value can be assigned to any unassigned variable,provided that it does not conflict with previously assigned variables Goal test:the curent assignment is complete. Path cost:a constant cost (e.g..1)for every step. earch algor thaplete assignment and therelore appears at depth n if therear nds are popu CSP 2)t that which eve a night the co forhSth 53 riables that are 1 ems are o f this n Chapt te-domain CS where each queen in c cach v ne do 2,6 .8 th main size of an then hat is,exponential in the Ite-a BOOLEAN CSPS include Boolean CSPs, whos can be e or fals Boolean CSP as special ca NP-complete probler ms,such as 3s (See Chapter 7.)Int cas ,therefore we cannot expect to solve finite-domain CSPs in less than exponentia In most practical applications,however,general-purpose CSP algorithms can solve problems orders ofmagnitde larger than those solvable via the general-purpose search algorithms that we saw in Chapter NFINTE DOMAINS Discrete variables can also have infinite domains-for example,the set of integers o the set of strings.For example,when scheduling construction jobs onto a calendar,each job's start date is a variable and the possible values are integer numbers of days from the current date.With infinite domains.it is no longer possible to describe constraints by enumerating aR all allowed combinations of values.Instead,a constraint language must be used.For ex- ample,if Job.which takes five days,must precede Jobs.then we would need a constraint language of algebraic inequalities such as StartJob+5<StartJob3.It is also no longer possible to solve such constraints by enumerating all possible assignments,because there are nfinitely many of them.Special solution algorithms(which we will not discuss here)exist ONSTPAINTS for linear constraints on integer variables-that is,constraints,such as the one just given, in which each variable appears only in linear form.It can be shown that no algorithm exists &8棉S for solving general nonlinear constraints on integer variables.In some cases.we can reduce integer constraint problems to finite-domain problems simply by bounding the values of all the variables.For example,in a scheduling problem,we can set an upper bound equal to the total length of all the jobs to be scheduled. CONANSOUS Constraint satisfaction problems with continuous domains are very common in the real world and are widely studied in the field of operations research.For example,the scheduling
Section 5.1. Constraint Satisfaction Problems 139 It is fairly easy to see that a CSP can be given an incremental formulation as a standard search problem as follows: ♦ Initial state: the empty assignment {}, in which all variables are unassigned. ♦ Successor function: a value can be assigned to any unassigned variable, provided that it does not conflict with previously assigned variables. ♦ Goal test: the current assignment is complete. ♦ Path cost: a constant cost (e.g., 1) for every step. Every solution must be a complete assignment and therefore appears at depth n if there are n variables. Furthermore, the search tree extends only to depth n. For these reasons, depth- first search algorithms are popular for CSPs. (See Section 5.2.) It is also the case that the path by which a solution is reached is irrelevant. Hence, we can also use a complete-state formulation, in which every state is a complete assignment that might or might not satisfy the constraints. Local search methods work well for this formulation. (See Section 5.3.) FINITE DOMAINS The simplest kind of CSP involves variables that are discrete and have finite domains. Map-coloring problems are of this kind. The 8-queens problem described in Chapter 3 can also be viewed as a finite-domain CSP, where the variables Q1, . . . , Q8 are the positions of each queen in columns 1, . . . , 8 and each variable has the domain {1, 2, 3, 4, 5, 6, 7, 8}. If the maximum domain size of any variable in a CSP is d, then the number of possible complete assignments is O(d n )—that is, exponential in the number of variables. Finite-domain CSPs BOOLEAN CSPS include Boolean CSPs, whose variables can be either true or false. Boolean CSPs include as special cases some NP-complete problems, such as 3SAT. (See Chapter 7.) In the worst case, therefore, we cannot expect to solve finite-domain CSPs in less than exponential time. In most practical applications, however, general-purpose CSP algorithms can solve problems orders of magnitude larger than those solvable via the general-purpose search algorithms that we saw in Chapter 3. INFINITE DOMAINS Discrete variables can also have infinite domains—for example, the set of integers or the set of strings. For example, when scheduling construction jobs onto a calendar, each job’s start date is a variable and the possible values are integer numbers of days from the current date. With infinite domains, it is no longer possible to describe constraints by enumerating all allowed combinations of values. Instead, a constraint language must be used. For ex- CONSTRAINT LANGUAGE ample, if Job1, which takes five days, must precede Job3, then we would need a constraint language of algebraic inequalities such as StartJob1 + 5 ≤ StartJob3. It is also no longer possible to solve such constraints by enumerating all possible assignments, because there are infinitely many of them. Special solution algorithms (which we will not discuss here) exist for linear constraints on integer variables—that is, constraints, such as the one just given, LINEAR CONSTRAINTS in which each variable appears only in linear form. It can be shown that no algorithm exists for solving general nonlinear constraints on integer variables. In some cases, we can reduce NONLINEAR CONSTRAINTS integer constraint problems to finite-domain problems simply by bounding the values of all the variables. For example, in a scheduling problem, we can set an upper bound equal to the total length of all the jobs to be scheduled. Constraintsatisfaction problems with continuous domains are very common in the real CONTINUOUS DOMAINS world and are widely studied in the field of operations research. For example, the scheduling
140 Chapter 5.Constraint Satisfaction Problems on the Hubble Space Telescope requires very precise timing of obse are contnuous-valu variab must obey a van e,and power constraint The best-known ntinuo of linear programming problems straints can be solved in time polynomial in the numb er of varial ferent types o constraints and objective unctions have also been studied- -quadratic programming,seconc order conic programming,and so on. In addition to examining the types of variables that can appear in CSPs,it is useful to NARY CONSTRAINT look at the types of constraints.The simplest type is the unary constraint,which restricts the value of a single variable.For example.it could be the case that South Australians actively dislike the color green.Every unary constraint can be eliminated simply by preprocessing the domain of the corresponding variable to remove any value that violates the constraint.A BINARY CONSTRAINT binary constraint relates two variables.For example.SA NSW is a binary constraint.A binary CSP is one with only binary constraints,it can be represented as a constraint graph,as in Figure 5.1(b). Higher-order constraints involve three or more variables.A familiar example is pro- vided by cryptarithmetic puzzles.(See Figure 5.2(a).)It is usual to insist that each letter in a cryptarithmetic puzzle represent a different digit.For the case in Figure 5.2(a)),this would be represented as the six-variable constraint Alldiff(F,T,U,W,R,O).Alternatively,it can be represented by a collection of binary constraints such as..The addition constraints on the four columns of the puzzle also involve several variables and can be written as O+0=R+10,X: X1+W+W=U+10,X2 X2+T+T=0+10:X3 X3=F where X,X2,and X3 are auxiliary variables representing the digit(0 or 1)carried over into the next column.Higher-order constraints can be represented in a constraint hypergraph such as the one shown in Figure 5.2(b).The sharp-eyed reader will have noticed that the Alldiff constraint can be broken down into binary constraints-FTFU,and so on. In fact,as Exercise 5.11 asks you to prove,every higher-order,finite-domain constraint can be reduced to a set of binary constraints ifenough auxiliary variables are introduced.Because of this.we will deal only with binary constraints in this chapter. The constraints we have described so far have all been absolute constraints,violation of which rules out a potential solution.Many real-world CSPs include preference constraints indicating which solutions are preferred.For example,in a university timetabling problem Prof.X might prefer teaching in the morning whereas Prof.Y prefers teaching in the after- noon.A timetable that has Prof.X teaching at 2 p.m.would still be a solution(unless Prof.X happens to be the department chair).but would not be an optimal one.Preference constraints can often be encoded as costs on individual variable assi nments -for example,assigning an afternoon slot for Prof.X costs 2 points against the overall objective function.whereas a morning slot costs 1.With this formulation,CSPs with preferences can be solved using opti-
140 Chapter 5. Constraint Satisfaction Problems of experiments on the Hubble Space Telescope requires very precise timing of observations; the start and finish of each observation and maneuver are continuous-valued variables that must obey a variety of astronomical, precedence, and power constraints. The best-known category of continuous-domain CSPs is that of linear programming problems, where con- LINEAR PROGRAMMING straints must be linear inequalities forming a convex region. Linear programming problems can be solved in time polynomial in the number of variables. Problems with different types of constraints and objective functions have also been studied—quadratic programming, secondorder conic programming, and so on. In addition to examining the types of variables that can appear in CSPs, it is useful to UNARY CONSTRAINT look at the types of constraints. The simplest type is the unary constraint, which restricts the value of a single variable. For example, it could be the case that South Australians actively dislike the color green. Every unary constraint can be eliminated simply by preprocessing the domain of the corresponding variable to remove any value that violates the constraint. A BINARY CONSTRAINT binary constraint relates two variables. For example, SA 6= NSW is a binary constraint. A binary CSP is one with only binary constraints; it can be represented as a constraint graph, as in Figure 5.1(b). Higher-order constraints involve three or more variables. A familiar example is proCRYPTARITHMETIC vided by cryptarithmetic puzzles. (See Figure 5.2(a).) It is usual to insist that each letter in a cryptarithmetic puzzle represent a different digit. For the case in Figure 5.2(a)), this would be represented as the six-variable constraint Alldiff (F, T,U, W, R, O). Alternatively, it can be represented by a collection of binary constraints such as F 6= T. The addition constraints on the four columns of the puzzle also involve several variables and can be written as O + O = R + 10 · X1 X1 + W + W = U + 10 · X2 X2 + T + T = O + 10 · X3 X3 = F where X1, X2, and X3 are auxiliary variables representing the digit (0 or 1) carried over into AUXILIARY VARIABLES the next column. Higher-order constraints can be represented in a constraint hypergraph, CONSTRAINT HYPERGRAPH such as the one shown in Figure 5.2(b). The sharp-eyed reader will have noticed that the Alldiff constraint can be broken down into binary constraints—F 6= T, F 6= U, and so on. In fact, as Exercise 5.11 asks you to prove, every higher-order, finite-domain constraint can be reduced to a set of binary constraints if enough auxiliary variables are introduced. Because of this, we will deal only with binary constraints in this chapter. The constraints we have described so far have all been absolute constraints, violation PREFERENCE of which rules out a potential solution. Many real-world CSPs include preference constraints indicating which solutions are preferred. For example, in a university timetabling problem, Prof. X might prefer teaching in the morning whereas Prof. Y prefers teaching in the afternoon. A timetable that has Prof. X teaching at 2 p.m. would still be a solution (unless Prof. X happens to be the department chair), but would not be an optimal one. Preference constraints can often be encoded as costs on individual variable assignments—for example, assigning an afternoon slot for Prof. X costs 2 points against the overall objective function, whereas a morning slot costs 1. With this formulation, CSPs with preferences can be solved using opti-
Section 5.2.Backtracking Search for CSPs 141 TWO F(T)mRO +TWO FOUR a (b) Figure5.2 (a)A cry arithmetic problem fach letter stands for a distinet digit the aim i to find a substitution of digits for letters such that the resulting sum is arithmetically correct. for the crypt em,sho sthecolumn addition constraints. chapter.erther patd or we do not discus such CPs further in ide some pointers in the notes sectio 5.2 BACKTRACKING SEARCH FOR CSPS The preceding section gave a formulation of CSPs as search problems.Using this formula- tion,any of the search algorithms from Chapters 3 and 4 can solve CSPs.Suppose we apply breadth-first search to the generic CSP problem formulation given in the preceding section. We quickly notice something terrible:the branching factor at the top level is nd,because any of d values can be assigned to any of n variables.At the next level,the branching factor is (n-1)d,and so on for n levels.We generate a tree with n!dn leaves,even though there are only d"possible complete assignments! Our seemingly reasonable but naive problem formulation has ignored a crucial property COMMUTATIVITY common to all CSPs:commutativity.A problem is commutative if the order of application of any given set of actions has no effect on the outcome.This is the case for CSPs be- cause,when assigning values to variables,we reach the same partial assignment,regardless 哈 of order.Therefore,all CSP search algorithms generate successors by considering possible assignments for only a single variable at each node in the search tree.For example,at the root node of a search tree for coloring the map of Australia,we might have a choice between SA=red,SA=green,and SA=blue,but we would never choose between SA=red and WA=blue.With this restriction,the number of leaves is d",as we would hope. The term backtracking search is used for a depth-first search that chooses values for one variable at a time and backtracks when a variable has no legal values left to assign.The algorithm is shown in Figure 5.3.Notice that it uses,in effect,the one-at-a-time method of
Section 5.2. Backtracking Search for CSPs 141 (a) F T U W R O (b) + F T T O W W U O O R X3 X2 X1 Figure 5.2 (a) A cryptarithmetic problem. Each letter stands for a distinct digit; the aim is to find a substitution of digits for letters such that the resulting sum is arithmetically correct, with the added restriction that no leading zeroes are allowed. (b) The constraint hypergraph for the cryptarithmetic problem, showing the Alldiff constraint as well asthe column addition constraints. Each constraint is a square box connected to the variables it constrains. mization search methods, either path-based or local. We do not discuss such CSPs further in this chapter, but we provide some pointers in the bibliographical notes section. 5.2 BACKTRACKING SEARCH FOR CSPS The preceding section gave a formulation of CSPs as search problems. Using this formulation, any of the search algorithms from Chapters 3 and 4 can solve CSPs. Suppose we apply breadth-first search to the generic CSP problem formulation given in the preceding section. We quickly notice something terrible: the branching factor at the top level is nd, because any of d values can be assigned to any of n variables. At the next level, the branching factor is (n − 1)d, and so on for n levels. We generate a tree with n! · d n leaves, even though there are only d n possible complete assignments! Our seemingly reasonable but na¨ıve problem formulation has ignored a crucial property COMMUTATIVITY common to all CSPs: commutativity. A problem is commutative if the order of application of any given set of actions has no effect on the outcome. This is the case for CSPs because, when assigning values to variables, we reach the same partial assignment, regardless of order. Therefore, all CSP search algorithms generate successors by considering possible assignments for only a single variable at each node in the search tree. For example, at the root node of a search tree for coloring the map of Australia, we might have a choice between SA = red, SA = green, and SA = blue, but we would never choose between SA = red and WA = blue. With this restriction, the number of leaves is d n , as we would hope. The term backtracking search is used for a depth-first search that chooses values for BACKTRACKING SEARCH one variable at a time and backtracks when a variable has no legal values left to assign. The algorithm is shown in Figure 5.3. Notice that it uses, in effect, the one-at-a-time method of