unit-2
unit-2
the domain {A,B}, then the constraint saying the two variables must have different values
can be written as (X1 , X2 ), [(A, B), (B, A)] or as (X1 , X2 ), X1 = X2 .
6 CONSTRAINT
SATISFACTION PROBLEMS
ASSIGNMENT
CONSISTENT
COMPLETE
ASSIGNMENT
SOLUTION
To solve a CSP, we need to define a state space and the notion of a solution. Each
state in a CSP is defined by an assignment of values to some or all of the variables, {Xi =
vi , Xj = vj , . . .}. An assignment that does not violate any constraints is called a consistent
or legal assignment. A complete assignment is one in which every variable is assigned, and
a solution to a CSP is a consistent, complete assignment. A partial assignment is one that
PARTIAL
ASSIGNMENT assigns values to only some of the variables.
Chapters 3 and 4 explored the idea that problems can be solved by searching in a space of The domain of each variable is the set Di = {red , green, blue}. The constraints require
states. These states can be evaluated by domain-specific heuristics and tested to see whether neighboring regions to have distinct colors. Since there are nine places where regions border,
they are goal states. From the point of view of the search algorithm, however, each state is there are nine constraints:
atomic, or indivisible—a black box with no internal structure. C = {SA = WA, SA = NT , SA = Q, SA = NSW , SA = V,
This chapter describes a way to solve a wide variety of problems more efficiently. We WA = NT , NT = Q, Q = NSW , NSW = V } .
use a factored representation for each state: a set of variables, each of which has a value. Here we are using abbreviations; SA = WA is a shortcut for (SA, WA), SA = WA, where
A problem is solved when each variable has a value that satisfies all the constraints on the SA = WA can be fully enumerated in turn as
CONSTRAINT
SATISFACTION variable. A problem described this way is called a constraint satisfaction problem, or CSP.
PROBLEM {(red , green), (red , blue), (green, red ), (green, blue), (blue, red ), (blue, green)} .
CSP search algorithms take advantage of the structure of states and use general-purpose
rather than problem-specific heuristics to enable the solution of complex problems. The main There are many possible solutions to this problem, such as
idea is to eliminate large portions of the search space all at once by identifying variable/value {WA = red , NT = green, Q = red , NSW = green, V = red , SA = blue, T = red }.
combinations that violate the constraints.
CONSTRAINT GRAPH It can be helpful to visualize a CSP as a constraint graph, as shown in Figure 6.1(b). The
nodes of the graph correspond to variables of the problem, and a link connects any two vari-
6.1 D EFINING C ONSTRAINT S ATISFACTION P ROBLEMS ables that participate in a constraint.
Why formulate a problem as a CSP? One reason is that the CSPs yield a natural rep-
resentation for a wide variety of problems; if you already have a CSP-solving system, it is
A constraint satisfaction problem consists of three components, X, D, and C: often easier to solve a problem using it than to design a custom solution using another search
X is a set of variables, {X1 , . . . , Xn }. technique. In addition, CSP solvers can be faster than state-space searchers because the CSP
D is a set of domains, {D1 , . . . , Dn }, one for each variable. solver can quickly eliminate large swatches of the search space. For example, once we have
C is a set of constraints that specify allowable combinations of values. chosen {SA = blue} in the Australia problem, we can conclude that none of the five neighbor-
Each domain Di consists of a set of allowable values, {v1 , . . . , vk } for variable Xi . Each ing variables can take on the value blue. Without taking advantage of constraint propagation,
constraint Ci consists of a pair scope, rel , where scope is a tuple of variables that participate a search procedure would have to consider 35 = 243 assignments for the five neighboring
in the constraint and rel is a relation that defines the values that those variables can take on. A variables; with constraint propagation we never have to consider blue as a value, so we have
relation can be represented as an explicit list of all tuples of values that satisfy the constraint, only 25 = 32 assignments to look at, a reduction of 87%.
or as an abstract relation that supports two operations: testing if a tuple is a member of the In regular state-space search we can only ask: is this specific state a goal? No? What
relation and enumerating the members of the relation. For example, if X1 and X2 both have about this one? With CSPs, once we find out that a partial assignment is not a solution, we can
202
204 Chapter 6. Constraint Satisfaction Problems Section 6.1. Defining Constraint Satisfaction Problems 205
In our example, the axles have to be in place before the wheels are put on, and it takes 10
NT minutes to install an axle, so we write
Q Axle F + 10 ≤ Wheel RF ; Axle F + 10 ≤ Wheel LF ;
WA Axle B + 10 ≤ Wheel RB ; Axle B + 10 ≤ Wheel LB .
Northern
Territory Next we say that, for each wheel, we must affix the wheel (which takes 1 minute), then tighten
Queensland
Western SA NSW the nuts (2 minutes), and finally attach the hubcap (1 minute, but not represented yet):
Australia
South
Australia
Wheel RF + 1 ≤ Nuts RF ; Nuts RF + 2 ≤ Cap RF ;
New
South Wheel LF + 1 ≤ Nuts LF ; Nuts LF + 2 ≤ Cap LF ;
V
Wales Wheel RB + 1 ≤ Nuts RB ; Nuts RB + 2 ≤ Cap RB ;
Victoria Wheel LB + 1 ≤ Nuts LB ; Nuts LB + 2 ≤ Cap LB .
T Suppose we have four workers to install wheels, but they have to share one tool that helps put
Tasmania DISJUNCTIVE
CONSTRAINT the axle in place. We need a disjunctive constraint to say that Axle F and Axle B must not
(a) (b) overlap in time; either one comes first or the other does:
Figure 6.1 (a) The principal states and territories of Australia. Coloring this map can
(Axle F + 10 ≤ Axle B ) or (Axle B + 10 ≤ Axle F ) .
be viewed as a constraint satisfaction problem (CSP). The goal is to assign colors to each This looks like a more complicated constraint, combining arithmetic and logic. But it still
region so that no neighboring regions have the same color. (b) The map-coloring problem reduces to a set of pairs of values that Axle F and Axle F can take on.
represented as a constraint graph. We also need to assert that the inspection comes last and takes 3 minutes. For every
variable except Inspect we add a constraint of the form X + dX ≤ Inspect . Finally, suppose
immediately discard further refinements of the partial assignment. Furthermore, we can see there is a requirement to get the whole assembly done in 30 minutes. We can achieve that by
why the assignment is not a solution—we see which variables violate a constraint—so we can limiting the domain of all variables:
focus attention on the variables that matter. As a result, many problems that are intractable Di = {1, 2, 3, . . . , 27} .
for regular state-space search can be solved quickly when formulated as a CSP.
This particular problem is trivial to solve, but CSPs have been applied to job-shop schedul-
6.1.2 Example problem: Job-shop scheduling ing problems like this with thousands of variables. In some cases, there are complicated
constraints that are difficult to specify in the CSP formalism, and more advanced planning
Factories have the problem of scheduling a day’s worth of jobs, subject to various constraints.
techniques are used, as discussed in Chapter 11.
In practice, many of these problems are solved with CSP techniques. Consider the problem of
scheduling the assembly of a car. The whole job is composed of tasks, and we can model each 6.1.3 Variations on the CSP formalism
task as a variable, where the value of each variable is the time that the task starts, expressed
as an integer number of minutes. Constraints can assert that one task must occur before
DISCRETE DOMAIN The simplest kind of CSP involves variables that have discrete, finite domains. Map-
another—for example, a wheel must be installed before the hubcap is put on—and that only
FINITE DOMAIN coloring problems and scheduling with time limits are both of this kind. The 8-queens prob-
so many tasks can go on at once. Constraints can also specify that a task takes a certain lem described in Chapter 3 can also be viewed as a finite-domain CSP, where the variables
amount of time to complete. Q1 , . . . , Q8 are the positions of each queen in columns 1, . . . , 8 and each variable has the
We consider a small part of the car assembly, consisting of 15 tasks: install axles (front domain Di = {1, 2, 3, 4, 5, 6, 7, 8}.
and back), affix all four wheels (right and left, front and back), tighten nuts for each wheel, INFINITE A discrete domain can be infinite, such as the set of integers or strings. (If we didn’t put
affix hubcaps, and inspect the final assembly. We can represent the tasks with 15 variables: a deadline on the job-scheduling problem, there would be an infinite number of start times
for each variable.) With infinite domains, it is no longer possible to describe constraints by
X = {Axle F , Axle B , Wheel RF , Wheel LF , Wheel RB , Wheel LB , Nuts RF , CONSTRAINT
enumerating all allowed combinations of values. Instead, a constraint language must be
Nuts LF , Nuts RB , Nuts LB , Cap RF , Cap LF , Cap RB , Cap LB , Inspect } . LANGUAGE
used that understands constraints such as T1 + d1 ≤ T2 directly, without enumerating the
The value of each variable is the time that the task starts. Next we represent precedence set of pairs of allowable values for (T1 , T2 ). Special solution algorithms (which we do not
PRECEDENCE
CONSTRAINTS constraints between individual tasks. Whenever a task T1 must occur before task T2 , and LINEAR
discuss here) exist for linear constraints on integer variables—that is, constraints, such as
CONSTRAINTS
task T1 takes duration d1 to complete, we add an arithmetic constraint of the form the one just given, in which each variable appears only in linear form. It can be shown that
T1 + d1 ≤ T2 . NONLINEAR
CONSTRAINTS no algorithm exists for solving general nonlinear constraints on integer variables.
206 Chapter 6. Constraint Satisfaction Problems Section 6.1. Defining Constraint Satisfaction Problems 207
CONTINUOUS
DOMAINS Constraint satisfaction problems with continuous domains are common in the real
world and are widely studied in the field of operations research. For example, the scheduling
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
T W O F T U W R O
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- + T W O
straints must be linear equalities or inequalities. Linear programming problems can be solved
F O U R
in time polynomial in the number of variables. Problems with different types of constraints
and objective functions have also been studied—quadratic programming, second-order conic C3 C2 C1
programming, and so on.
In addition to examining the types of variables that can appear in CSPs, it is useful to (a) (b)
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, in the map-coloring problem it could be the case Figure 6.2 (a) A cryptarithmetic problem. Each letter stands for a distinct digit; the aim is
that South Australians won’t tolerate the color green; we can express that with the unary to find a substitution of digits for letters such that the resulting sum is arithmetically correct,
constraint (SA), SA = green with the added restriction that no leading zeroes are allowed. (b) The constraint hypergraph
BINARY CONSTRAINT A binary constraint relates two variables. For example, SA = NSW is a binary for the cryptarithmetic problem, showing the Alldiff constraint (square box at the top) as
constraint. A binary CSP is one with only binary constraints; it can be represented as a well as the column addition constraints (four square boxes in the middle). The variables C1 ,
constraint graph, as in Figure 6.1(b). C2 , and C3 represent the carry digits for the three columns.
We can also describe higher-order constraints, such as asserting that the value of Y is
between X and Z, with the ternary constraint Between(X, Y, Z).
GLOBAL
CONSTRAINT A constraint involving an arbitrary number of variables is called a global constraint. one binary constraint for each pair of constraints in the original graph that share variables. For
(The name is traditional but confusing because it need not involve all the variables in a prob- example, if the original graph has variables {X, Y, Z} and constraints (X, Y, Z), C1 and
lem). One of the most common global constraints is Alldiff , which says that all of the (X, Y ), C2 then the dual graph would have variables {C1 , C2 } with the binary constraint
variables involved in the constraint must have different values. In Sudoku problems (see (X, Y ), R1 , where (X, Y ) are the shared variables and R1 is a new relation that defines the
Section 6.2.6), all variables in a row or column must satisfy an Alldiff constraint. An- constraint between the shared variables, as specified by the original C1 and C2 .
CRYPTARITHMETIC other example is provided by cryptarithmetic puzzles. (See Figure 6.2(a).) Each letter in a There are however two reasons why we might prefer a global constraint such as Alldiff
cryptarithmetic puzzle represents a different digit. For the case in Figure 6.2(a), this would rather than a set of binary constraints. First, it is easier and less error-prone to write the
be represented as the global constraint Alldiff (F, T, U, W, R, O). The addition constraints problem description using Alldiff . Second, it is possible to design special-purpose inference
on the four columns of the puzzle can be written as the following n-ary constraints: algorithms for global constraints that are not available for a set of more primitive constraints.
We describe these inference algorithms in Section 6.2.5.
O + O = R + 10 · C10
The constraints we have described so far have all been absolute constraints, violation of
C10 + W + W = U + 10 · C100 PREFERENCE
which rules out a potential solution. Many real-world CSPs include preference constraints
C100 + T + T = O + 10 · C1000 CONSTRAINTS
indicating which solutions are preferred. For example, in a university class-scheduling prob-
C1000 = F ,
lem there are absolute constraints that no professor can teach two classes at the same time.
where C10 , C100 , and C1000 are auxiliary variables representing the digit carried over into the But we also may allow preference constraints: Prof. R might prefer teaching in the morning,
tens, hundreds, or thousands column. These constraints can be represented in a constraint whereas Prof. N prefers teaching in the afternoon. A schedule that has Prof. R teaching at
CONSTRAINT
HYPERGRAPH hypergraph, such as the one shown in Figure 6.2(b). A hypergraph consists of ordinary nodes 2 p.m. would still be an allowable solution (unless Prof. R happens to be the department chair)
(the circles in the figure) and hypernodes (the squares), which represent n-ary constraints. but would not be an optimal one. Preference constraints can often be encoded as costs on in-
Alternatively, as Exercise 6.6 asks you to prove, every finite-domain constraint can be dividual variable assignments—for example, assigning an afternoon slot for Prof. R costs
reduced to a set of binary constraints if enough auxiliary variables are introduced, so we could 2 points against the overall objective function, whereas a morning slot costs 1. With this
transform any CSP into one with only binary constraints; this makes the algorithms simpler. formulation, CSPs with preferences can be solved with optimization search methods, either
CONSTRAINT
DUAL GRAPH Another way to convert an n-ary CSP to a binary one is the dual graph transformation: create OPTIMIZATION path-based or local. We call such a problem a constraint optimization problem, or COP.
PROBLEM
a new graph in which there will be one variable for each constraint in the original graph, and Linear programming problems do this kind of optimization.