Professional Documents
Culture Documents
AI-Module3-2-Constraint Satisfaction Problems
AI-Module3-2-Constraint Satisfaction Problems
● 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.
● CSPs are mathematical questions defined as a set of objects whose state must satisfy a number of constraints or
limitations.
Department of CSE, GST CSEN2031: 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.
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.
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 , . . .}.
• 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.
• 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)} .
• 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.
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.
• 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.
• 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
• 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):
• 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.
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.
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.
● 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 right hand side (R.H.S).
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.
● 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.
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.
● 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.
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 colouring is the procedure of assignment of colours to each vertex of a graph G such that no adjacent
vertices get same color.
• The objective is to minimize the number of colours while colouring a graph.
• The smallest number of colours required to color a graph G is called its chromatic number of that graph.
Department of CSE, GST CSEN2031: AI 21
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.
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.
• Whenever a variable X is assigned, the forward-checking process establishes arc consistency for it:
• 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.
● 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 CSEN2031: AI 27