Artificial Intelligence
CSP
UNIT III”
1
SYLLABUS
Constraint Satisfaction Problems
Backtracking Search
Variable and Value Ordering
Constraint Propagation
Intelligent Backtracking
2
STANDARD SEARCH PROBLEMS
Previous search problems:
Problems can be solved by searching in a space
of states
state is a “black box” – any data structure that
supports successor function, heuristic function,
and goal test – problem-specific
Constraint satisfaction problem
states and goal test conform to a standard,
structured and simple representation
general-purpose heuristic
3
Constraint Satisfaction Problems
o CSP is defined by 3 components (X, D, C):
state: a set of variables X, each Xi , with values from
domain Di
goal test: a set of constraints C, each Ci involves some
subset of the variables and specifies the allowable
combinations of values for that subset
Each constraint Ci consists of a pair , where scope is a
tuple of variables and rel is the relation, either
represented explicitly or abstractly
o X1 and X2 both have the domain {A, B}
• Constraints:
• <(X1, X2), [(A, B), (B, A)]>, OR
• <(X1, X2), X1 ≠ X2>
4
SOLUTION
Each state in a CSP is defined by an
assignment of values to some or all of the
variables
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
A solution to a CSP is consistent and complete
assignment
Allows useful general-purpose algorithms with
more power than standard search algorithms 5
EXAMPLE: MAP COLORING
Variables: X = {WA, NT, Q, NSW, V, SA, T }
Domains: Di = {red, green, blue}
Constraints: adjacent regions must have different
colors
Solution?
6
SOLUTION: COMPLETE AND CONSISTENT
ASSIGNMENT
Variables: X = {WA, NT, Q, NSW, V, SA, T }
Domains: Di = {red, green, blue}
Constraints: adjacent regions must have different
colors
Solution? {WA = red, NT = green, Q = red, NSW = 7
green, V = red,SA = blue, T = red}.
CONSTRAINT GRAPH
Constraint graph: nodes are variables, arcs
are constraints
Binary CSP: each constraint relates two
variables
CSP conforms to a standard pattern
a set of variables with assigned values
generic successor function and goal test
generic heuristics
reduce complexity
8
CSP AS A SEARCH PROBLEM
Initial state:
{} – all variables are unassigned
Successor function:
a value is assigned to one of the unassigned variables with
no conflict
Goal test:
a complete assignment
Path cost:
a constant cost for each step
Solution appears at depth n if there are n variables
Depth-first or local search methods work well
9
EXAMPLE : CRYPTARITHMETIC
Variables: F,T,U,W,R,O X1,X2,X3
Domain: 0,1----9 0,1
10
EXAMPLE 1 : CRYPTARITHMETIC
WIRE + MORE = MONEY
C4= C3= C2= C1=
alphabe digit
t
W
+ I
R
E
M
O
R
Y
Constraints
1. No alphabet is assigned same digit
2. Sum should be valid as per given problem 11
EXERCISE (2)
Problem formulation as CSP:
Class scheduling: There is a fixed number of
professors and classrooms, a list of classes to be offered,
and a list of possible time slots for classes. Each
professor has a set of classes that he or she can teach.
12
TYPES OF VARIABLES
Discrete variables
finite domains:
n variables, domain size d (d n) complete assignments
e.g., Boolean CSPs, such as 3-SAT (NP-complete)
Worst case, can’t solve finite-domain CSPs in less than exponential time
infinite domains:
integers, strings, etc.
e.g., job scheduling, variables are start/end days for each job
need a constraint language, e.g., StartJob 1 + 5 ≤ StartJob3
Continuous variables
e.g., start/end times for Hubble Space Telescope observations
linear constraints solvable in polynomial time by linear programming
TYPES OF CONSTRAINTS
Unary constraints involve a single variable,
e.g., SA ≠ green
Binary constraints involve pairs of variables,
e.g., SA ≠ WA
Higher-order constraints involve 3 or more variables
e.g., cryptarithmetic column constraints
REAL-WORLD CSPS
Assignment problems
e.g., who teaches what class
Timetabling problems
e.g., which class is offered when and where?
Transportation scheduling
Factory scheduling
SEARCH FORMULATION FOR CSP
Nx d
WA WA WA NT NT
( Nx d)(N-1 x d)
WA WA WA NT
NT WA
( Nx d)(N-2 x d )
-
-
N! dn Same Values
- Initial State : none of the variable is assigned a value
- Successor State : One of the variable without value will get the
value
- Goal : All variables have a value and no constraints are violated
16
BACKTRACKING SEARCH
Only need to consider assignments to a single variable at each
node b = d and there are d n leaves
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
Backtracking search is the basic uninformed algorithm for
CSPs
BACKTRACKING SEARCH ALGORITHM
BACKTRACKING EXAMPLE
BACKTRACKING EXAMPLE
BACKTRACKING EXAMPLE
BACKTRACKING EXAMPLE
IMPROVING BACKTRACKING EFFICIENCY
We can solve CSPs efficiently without domain-specific
knowledge, addressing the following questions:
Which variable should be assigned next?
In what order should its values be tried?
Can we detect inevitable failure first?
VARIABLE NEXT
Minimum remaining values (MRV)
choose the variable with the fewest “legal” values
also called most constrained variable or fail-first
heuristic
does it help in choosing the first variable?
VARIABLE NEXT
Most constraining variable:
selecting the variable that has the largest number of
constraints on other unassigned variables
also called degree heuristics
Tie-breaker among MRV
ORDERING OF VALUE
Given a variable, choose the least constraining
value:
the one that rules out the fewest values in the remaining
variables
Combining these heuristics makes 1000
queens feasible
2
6
PROPAGATING INFORMATION THROUGH CONSTRAINTS
Forward checking:
Keep track of remaining legal values for unassigned variables
Terminate search when any variable has no legal values
PROPAGATING INFORMATION THROUGH CONSTRAINTS
Forward checking:
Keep track of remaining legal values for unassigned variables
Terminate search when any variable has no legal values
PROPAGATING INFORMATION THROUGH CONSTRAINTS
Forward checking:
Keep track of remaining legal values for unassigned variables
Terminate search when any variable has no legal values
PROPAGATING INFORMATION THROUGH CONSTRAINTS
Forward checking:
Keep track of remaining legal values for unassigned variables
Terminate search when any variable has no legal values
PROPAGATING INFORMATION THROUGH CONSTRAINTS
Forward checking propagates information from assigned to unassigned
variables, but doesn't provide early detection for all failures:
NT and SA cannot both be blue!
Constraint propagation repeatedly enforces constraints locally by propagating
implications of a constraint of one variable onto other variables
NODE CONSISTENCY
A single variable is node-consistent if all
the values in the variable’s domain satisfy
the variable’s unary constraints
For example, SA dislikes green
A network is node-consistent if every
variable in the network is node-consistent
ARC CONSISTENCY
Simplest form of propagation makes each arc consistent
X -> Y is consistent iff
for every value x of X there is some allowed y
ARC CONSISTENCY
Simplest form of propagation makes each arc consistent
X -> Y is consistent iff
for every value x of X there is some allowed y
ARC CONSISTENCY
Simplest form of propagation makes each arc consistent
X -> Y is consistent iff
for every value x of X there is some allowed y
If X loses a value, neighbors of X need to be rechecked
ARC CONSISTENCY
Simplest form of propagation makes each arc consistent
X -> Y is consistent iff
for every value x of X there is some allowed y
If X loses a value, neighbors of X need to be rechecked
Arc consistency detects failure earlier than forward checking
Can be run as a preprocessor or after each assignment
AC-3 Algorithm
Time complexity O (n 2d 3)
EXAMPLE
Use the AC-3 algorithm to show that arc consistency is able to detect
the inconsistency of the partial assignment {WA = red, V = blue} in
the following map coloring problem.
ONE POSSIBLE TRACE
One possible trace of the algorithm:
remove SA-WA, delete R from SA
remove SA-V, delete B from SA, leaving only G
remove NT-WA, delete R from NT
remove NT-SA, delete G from NT, leaving only
B
remove NSW-SA, delete G from NSW
remove NSW-V, delete B from NSW, leaving
only R
remove Q-NT, delete B from Q
remove Q-SA, delete G from Q
remove Q-NSW, delete R from Q, leaving no
39
proper assignment for Q
PATH CONSISTENCY
Consider map-coloring with only two colors
Every arc is consistent initially
Check the set {WA, SA} path consistenty with respect
to NT
More generally, k-consistency
1-consistency = node consistency
2-consistency = arc consistency
3-consistency = path consistency