You are on page 1of 36

Module-III

Chapter 6: Constraint Satisfaction Problems

Dr. T L Siva Rama Krishna


Department of CSE
GITAM School of Technology (GST)
Hyderabad

Department of CSE, GST CSEN2031 1


6.1 Defining Constraint Satisfaction Problems
● Constraint satisfaction means solving a problem under certain constraints or rules.

● Constraint satisfaction is a technique where a problem is solved when its values satisfy certain constraints or rules
of the problem.

Such type of technique leads to a deeper understanding of the problem structure as well as its
complexity.

● Constraint satisfaction problems (or CSPs) consist of variables with constraints on them.

● Many important real-world problems can be described as CSPs.

● The structure of a CSP can be represented by its constraint graph.

● CSPs are mathematical questions defined as a set of objects whose state must satisfy a number of constraints or
limitations.
Department of CSE, GST ECS302: AI 2
Continued...
• A constraint satisfaction problem consists of three components, X,D, and C:
X is a set of variables, {X1, . . . ,Xn}.
D is a set of domains, {D1, . . . ,Dn}, one for each variable.
C is a set of constraints that specify allowable combinations of values.

• Each domain Di consists of a set of allowable values, {v1, . . . , vk} for variable Xi.

• Each constraint Ci consists of a pair <scope, rel> , where


scope is a tuple of variables that participate in the constraint and
rel is a relation that defines the values that those variables can take on.

 Relation: It is an explicit list of all tuples of values that satisfy the constraint, or
as an abstract relation that supports two operations:
testing if a tuple is a member of the relation and enumerating the members of the
relation.

Department of CSE, GST ECS302: AI 3


Continued...

Ex: if X1 and X2 both have 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.

• Each state in a CSP is defined by an assignment of values to some or all of the variables,
{Xi =vi, Xj = vj , . . .}.

 Consistent or Legal assignment:


An assignment that does not violate any constraints.
 Complete assignment:
It is one in which every variable is assigned.
 Solution to a CSP:
It is a consistent, complete assignment.
 Partial assignment:
It assigns values to only some of the variables.

Department of CSE, GST ECS302: AI 4


Continued...

6.1.1 Example: Map Colouring

Department of CSE, GST ECS302: AI 5


Continued...

• We are given the task of coloring each region of Australia map either red, green, or blue in such a way that
no neighbouring regions have the same colour.

• To formulate this as a CSP, we define the variables to be the regions X = {WA,NT,Q,NSW,V,SA,T} .


• The domain of each variable is the set Di = {red , green, blue}.
• The constraints require neighbouring regions to have distinct colours.

• Since there are nine places where regions border, there are nine constraints:
C = {SA ≠ WA, SA ≠ NT, SA ≠ Q, SA ≠ NSW, SA ≠ V, WA ≠ NT, NT ≠ Q, Q ≠ NSW, NSW ≠ V } .
• SA ≠ WA is a shortcut for <(SA,WA), SA ≠ WA>,
where SA ≠ WA can be fully enumerated in turn as
{(red , green), (red , blue), (green, red ), (green, blue), (blue, red ), (blue, green)} .

• There are many possible solutions to this problem, such as


{WA=red ,NT =green, Q=red, NSW =green, V =red, SA=blue, T =red }.

Department of CSE, GST ECS302: AI 6


Continued...

• We can visualize a CSP as a constraint graph (Figure 6.1(b)).


• The nodes of the graph correspond to variables of the problem, and
a link connects any two variables that participate in a constraint.

Department of CSE, GST ECS302: AI 7


Continued...

6.1.2 Example: Job-shop Scheduling

• Consider the problem of scheduling the assembly of a car.

• The whole job is composed of tasks.


Each task is 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 another—
for example, a wheel must be installed before the hubcap is put on—and that
only so many tasks can go on at once.

• Constraints can also specify that a task takes a certain amount of time to complete.

Department of CSE, GST ECS302: AI 8


Continued...
Ex: A small part of the car assembly, consisting of 15 tasks:

install axles (front and back), affix all four wheels (right and left, front and back),
tighten nuts for each wheel, affix hubcaps, and inspect the final assembly.

 We can represent the tasks with 15 variables:

• X = {AxleF, AxleB, WheelRF, WheelLF, WheelRB, WheelLB, NutsRF, NutsLF, NutsRB, NutsLB,
CapRF, CapLF, CapRB, CapLB, Inspect} .

• The value of each variable is the time that the task starts.

• Next we represent precedence constraints between individual tasks.


• Whenever a task T1 must occur before task T2, and task T1 takes duration d1 to complete,
we add an arithmetic constraint of the form T1 + d1 ≤ T2 .

Department of CSE, GST ECS302: AI 9


Continued...

• In our example, the axles have to be in place before the wheels are put on, and it takes 10 minutes to install an
axle, so we write

AxleF + 10 ≤ WheelRF ; AxleF + 10 ≤ WheelLF;


AxleB + 10 ≤ WheelRB; AxleB +10 ≤ WheelLB.

• Next we say that, for each wheel, we must affix the wheel (which takes 1 minute),
then tighten the nuts (2 minutes), and finally attach the hubcap (1 minute, but not represented yet):

WheelRF + 1 ≤ NutsRF ; NutsRF + 2 ≤ CapRF ;


WheelLF + 1 ≤ NutsLF ; NutsLF +2 ≤ CapLF ;
WheelRB + 1 ≤ NutsRB; NutsRB + 2 ≤ CapRB;
WheelLB + 1 ≤ NutsLB; NutsLB + 2 ≤ CapLB .

Department of CSE, GST ECS302: AI 10


Continued...

• Suppose we have four workers to install wheels, but they have to share one tool that helps put the axle in place.

• We need a disjunctive constraint to say that AxleF and AxleB must not overlap in time; either one comes first or
the other does:
(AxleF + 10 ≤ AxleB) or (AxleB + 10 ≤ AxleF ).

• 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 there is a requirement to get the whole assembly done in 30 minutes.

• We can achieve that by limiting the domain of all variables:


Di = {1, 2, 3, . . . , 27} .

Department of CSE, GST ECS302: AI 11


Continued...

6.1.3 Variations on the CSP Formalism

Discrete, finite domains:

Exs: Map-colouring problems and scheduling with time limits.

The 8-queens problem is also 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 Di = {1, 2, 3, 4, 5, 6, 7, 8}.

Discrete, infinite domains:

Exs: The set of integers or strings.


If we didn’t put 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 enumerating all allowed combinations of values.
Department of CSE, GST ECS302: AI 12
Continued...
Unary constraint:
It is the value of a single variable.
Ex: In the map-coloring problem it could be the case that South Australians won’t tolerate the color green;
we can express that with the unary constraint <(SA), SA ≠ green>

Binary constraint:
It relates two variables.
Ex: SA ≠ NSW is a binary constraint.
A binary CSP is one with only binary constraints. It can be represented as a Constraint graph.

Global constraint:
It is a constraint involving an arbitrary number of variables.
(it need not involve all the variables in a problem)

Ex: Alldiff , which says that all of the variables involved in the constraint must have different values.

Department of CSE, GST ECS302: AI 13


Continued...

Cryptarithmetic Problem:
● Cryptarithmetic Problem is a type of constraint satisfaction problem where the game is about digits
and its unique replacement either with alphabets or other symbols.

● Cryptarithmetic problem is a mathematical puzzle, in which the digits (0-9) get substituted by some possible
alphabets or symbols.

● The task in cryptarithmetic problem is to substitute each digit with an alphabet to get the result arithmetically
correct.

● This problem has one most important constraint that is, we cannot assign a different digit to the same character.
All digits should contain a unique alphabet.

Department of CSE, GST ECS302: AI 14


Department of CSE, GST ECS302: AI 15
Department of CSE, GST ECS302: AI 16
Continued...
● There should be a unique digit to be replaced with a unique alphabet.
● The result should satisfy the predefined arithmetic rules, i.e., 2+2 =4, nothing else.
● Digits should be from 0-9 only.

● There should be only one carry forward, while performing the addition operation on a problem.
● The problem can be solved from both sides, i.e., left hand side (L.H.S), or righthand side (R.H.S).

● All the same alphabets have the same numbers.


● The numbers should satisfy all the operations that any normal number does.
Other Examples:
TWO TO CR OSS SOME IS 21+81 = 102
+TWO +GO + ROADS +TI ME +THIS 9567+1085 = 10652
-------------- --------- ----------------- ------------- ----------- 65+8965 = 9030
FOUR OUT DANGER SPENT YEAR 96233+62513 = 158746
1934+8534 = 10468

Department of CSE, GST ECS302: AI 17


solve
• Imagine that There are two water jugs here.One is 4 gallons (4G) and
the other is 9 gallons (9G). One of these two jugs, however, does not
have a measurement mark, so we do not know the exact amount that
is in it. Now, let’s say there is an endless supply of water. Can we use
these empty jugs to measure all 1G, 3G, and 6G?

Department of CSE, GST ECS302: AI 18


Continued...

Sudoku:
● Consider a Sudoku game with some numbers filled initially in some squares.

● You are expected to fill the empty squares with numbers ranging from 1 to 9 in such a way that no row, column or
a block has a number repeating itself.

● This is a very basic constraint satisfaction problem.

● You are supposed to solve a problem keeping in mind some constraints.

● The remaining squares that are to be filled are known as variables, and the range of numbers (1-9) that can fill
them is known as a domain.

● Variables take on values from the domain. The conditions governing how a variable will choose its domain are
known as constraints.

Department of CSE, GST ECS302: AI 19


Continued...
● The game play where the constraint is that no number from 0-9 can be repeated in the same row or column and
block.
● Suppose that a row, column and block already have 3, 5 and 7 filled in.
● Then the domain for all the variables in that row, column and block will be {1, 2, 4, 6, 8, 9}.
Alldiff constraints: one for each row, column, and box of 9 squares.
Alldiff (A1,A2,A3,A4,A5,A6, A7, A8, A9)
Alldiff (B1,B2,B3,B4,B5,B6,B7,B8,B9)
・・・

Department of CSE, GST ECS302: AI 20


Continued...

Department of CSE, GST ECS302: AI 21


Continued...

Variables:
X = {A1, A2, …, I8, I9}.
We’ll just have a variable for each cell in the sudoku puzzle for a total of 81 variables.
The variable A1 is just a variable that represents the cell in the Ath row and the 1st column.

Domains:
D = {1, 2, 3, 4, 5, 6, 7, 8, 9}.
For the domain of each variable, we’ll just start with all of the numbers from 1 to 9.

Constraints:
We’ll have three types of constraints here:
a constraint that says all of the variables in each column has to differ,
all of the variables in each row has to differ, and
all of the variables in each square has to differ.

Department of CSE, GST ECS302: AI 22


6.3 Backtracking Search for CSPs
● Backtracking search is just a search algorithm that finds some assignment that satisfies a CSP.

● At a very high level, the algorithm assigns a value to each variable one-by-one
until either reaching a valid assignment or an invalid assignment that violates some constraint,
at which point, it backtracks to a place in the search where there are no violations yet.
 It keeps only a single representation of a state and alters that representation rather than creating new ones.

Department of CSE, GST ECS302: AI 23


Continued...

Steps:
● Which variable should be assigned next (SELECT -UNASSIGNED -VARIABLE ), and in what order should its values be
tried (ORDER -DOMAIN -VALUES )?
● What inferences should be performed at each step in the search (INFERENCE)?
● When the search arrives at an assignment that violates a constraint, can the search avoid repeating this failure?
Ex: Map colouring

• Graph coloring is the procedure of assignment of colors to each vertex of a graph G such that no adjacent vertices
get same color.
• The objective is to minimize the number of colors while coloring a graph.
• The smallest number of colors required to color a graph G is called its chromatic number of that graph.
Department of CSE, GST ECS302: AI 24
Continued...
6.3.1 Variable and Value Ordering:
● The simplest strategy for SELECT-UNASSIGNED-VARIABLE is to choose the next unassigned variable in order,
{X1,X2, . . .}.
● This static variable ordering seldom results in the most efficient search.

Ex: After the assignments for WA=red and NT =green in Figure,


there is only one possible value for SA, so it makes sense to assign
SA=blue next rather than assigning Q.

 In fact, after SA is assigned, the choices for Q, NSW, and V are all forced.

● Choosing the variable with the fewest “legal” values—is called the minimum- remaining-values (MRV) heuristic.

● It is also called the “most constrained variable” or REMAINING-VALUES “fail-first” heuristic,


the latter because it picks a variable that is most likely to cause a failure soon,
thereby pruning the search tree.
Department of CSE, GST ECS302: AI 25
Continued...
● If some variable X has no legal values left,
the MRV heuristic will select X and failure will be detected immediately—avoiding pointless searches
through other variables.

6.3.2 Interleaving Search and Inference:

• One of the simplest forms of inference is called forward checking.

• Whenever a variable X is assigned, the forward-checking process establishes arc consistency for it:

for each unassigned variable Y that is connected to X by a constraint,


delete from Y ’s domain any value that is inconsistent with the value chosen for X.

Department of CSE, GST ECS302: AI 26


Continued...

Progress with forward checking

Department of CSE, GST ECS302: AI 27


Continued...
• Hence, forward checking has detected that–
the partial assignment {WA=red, Q=green, V =blue} is inconsistent with the constraints of the problem,
and
the algorithm will therefore backtrack immediately.

• Although forward checking detects many inconsistencies, it does not detect all of them.

• The problem is that it makes the current variable arc-consistent, but doesn’t look ahead and make all the other
variables arc-consistent.
Ex: Consider the third row of Figure 6.7.
It shows that when WA is red and Q is green, both NT and SA are forced to be blue.

 Forward checking does not look far enough ahead to notice that this is an inconsistency:
NT and SA are adjacent and so cannot have the same value.

Department of CSE, GST ECS302: AI 28


Continued...
6.3.2 Intelligent Backtracking: Looking backward

• The BACKTRACKING-SEARCH algorithm in Figure 6.5 has a very simple policy for what to do when a branch of the
search fails:
back up to the preceding variable and try a different value for it.
 This is called chronological backtracking because the most recent decision point is revisited.

 A more intelligent approach to backtracking is to backtrack to a variable that might fix the problem—
a variable that was responsible for making one of the possible values of SA impossible.

• To do this, we will keep track of a set of assignments that are in conflict with some value for SA.
• The set (in this case { Q=red, NSW=green, V=blue }), is called the conflict set for SA.

• The backjumping method backtracks to the most recent assignment in the conflict set;
in this case, backjumping would jump over Tasmania and try a new value for V .

Department of CSE, GST ECS302: AI 29


6.4 Local Search for CSPs
• Local search algorithms (informed search techniques) turn out to be effective in solving many CSPs.

• They use a complete-state formulation:


the initial state assigns a value to every variable, and the search changes the value of one variable at a
time.

Ex: In the 8-queens problem (Figure 4.3),


the initial state might be a random configuration of 8 queens in 8 columns, and
each step moves a single queen to a new position in its column.

 Typically, the initial guess violates several constraints.

 The point of local search is to eliminate the violated constraints.

Department of CSE, GST ECS302: AI 30


Continued…

● In choosing a new value for a variable, the most obvious heuristic is to select the value
that results in the minimum number of conflicts with other variables the min-conflicts heuristic.

Department of CSE, GST ECS302: AI 31


6.5 The Structure of Problems
• The only way we can possibly hope to deal with the real world is to decompose it into many sub-problems.

Ex: Map Colouring problem:


Tasmania is not connected to the mainland.
 It is obvious that colouring Tasmania and colouring the mainland are independent sub-problems—
any solution for the mainland combined with any solution for Tasmania yields a solution
for the whole map.

• Independence can be ascertained simply by finding connected components of the constraint graph.
• A constraint graph is a tree when any two variables are connected by only one path.

• To solve a tree-structured CSP, first pick any variable to be the root of the tree, and choose an ordering of the
variables such that each variable appears after its parent in the tree.
• Such an ordering is called a topological sort.

Department of CSE, GST ECS302: AI 32


Continued...
• Figure 6.10(a) shows a sample tree and (b) shows one possible ordering.

• Any tree with n nodes has n−1 arcs, so we can make this graph directed arc-consistent in O(n) steps.

• Since each link from a parent to its child is arc consistent, we know that for any value we choose for the parent,
there will be a valid value left to choose for the child.

• That means we won’t have to backtrack; we can move linearly through the variables.
Department of CSE, GST ECS302: AI 33
Continued...
Reducing Constraint graphs: There are two ways.
(1) The first approach is based on removing nodes.
• It involves assigning values to some variables so that the remaining variables form a tree.
• From the constraint graph for Australia (6.12(a)),
if we could delete South Australia, the graph would become a tree (6.12(b)).
• We can do this by fixing a value for SA and deleting from the domains of the other variables
any values that are inconsistent with the value chosen for SA.
• Now, any solution for the CSP after SA and its constraints are removed will be consistent with the value chosen for
SA.

Department of CSE, GST ECS302: AI 34


Continued...
(2) The second approach is based on collapsing nodes together.
• It involves constructing a tree decomposition of the constraint graph into a set of connected sub-problems.
• Each sub-problem is solved independently, and the resulting solutions are then combined.

6.13 Tree decomposition of the map colouring problem into five sub-problems.
Department of CSE, GST ECS302: AI 35
Continued...
• Tree decomposition must satisfy the following three requirements:

 Every variable in the original problem appears in at least one of the sub-problems.
 If two variables are connected by a constraint in the original problem, they must appear together (along with the
constraint) in at least one of the sub-problems.
 If a variable appears in two sub-problems in the tree, it must appear in every sub-problem along the path
connecting those sub-problems.

• The first two conditions ensure that all the variables and constraints are represented in the decomposition.

• The third condition reflects the constraint that any given variable must have the same value in every sub-problem
in which it appears.

****

Department of CSE, GST ECS302: AI 36

You might also like