Ai Unit 3-3
Ai Unit 3-3
1
CSP
∙ Use of Factored representation for each state: a
set of variables, each of which has a value.
∙ A problem is solved when each variable has a
value that satisfies all the constraints on the
variable.
∙ A problem described this way is called a
constraint satisfaction problem, or CSP.
∙ CSPsearch algorithms take advantage of the
structure of states and use general-purpose
rather than problem-specific heuristics to enable
the solution of complex problems.
2
Constraint satisfaction problems
∙ What is a CSP?
• Finite set of variables V1, V2, …, Vn
• Nonempty domain of possible values for each variable
DV1, DV2, … DVn
• Finite set of constraints C1, C2, …, Cm
• Each constraint Ci limits the values that variables can
take, e.g., V1 ≠ V2
∙ A state is an assignment of values to some or all
variables.
∙ Consistent assignment: assignment does not
violate the constraints.
3
Constraint satisfaction problems
∙ An assignment is complete when every variable
has a value.
∙ A solution to a CSP is a complete assignment
that satisfies all constraints.
∙ Some CSPs require a solution that maximizes an
objective function.
∙ Applications:
• Scheduling the Hubble Space Telescope,
• Floor planning for VLSI,
• Map coloring,
• Cryptography
4
Example: Map-Coloring
5
∙ X ={WA,NT,Q,NSW,V,SA,T}.
∙ 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),(bl
ue,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 }.
6
Example: Map-Coloring
7
Constraint graph
∙ Binary CSP: each constraint relates
two variables
∙ Constraint graph:
• nodes are variables
• arcs are constraints
∙ CSP benefits
• Standard representation pattern
• Generic goal and successor functions
• Generic heuristics (no domain specific expertise).
8
Why formulate a problem as a CSP?
∙ CSPs yield a natural representation for a wide
variety of problems; if you already have a
CSP-solving system, it is often easier to solve a
problem using it than to design a custom solution
using another search technique
∙ CSP solvers can be faster than state-space
searchers because the CSP solver can quickly
eliminate large swatches of the search space.
∙ Eg: 3^5 =243 assignments for the five
neighboring variables; with constraint
propagation we never have to consider blue as a
value, so we have only 2^5 =32 assignments to
look at, a reduction of 87%.
9
Varieties of CSPs
∙ Discrete variables
• finite domains:
— n variables, domain size d O(dn) complete assignments
— e.g., Boolean CSPs, includes Boolean satisfiability (NP-complete)
• infinite domains:
— integers, strings, etc.
— e.g., job scheduling, variables are start/end days for each job
— need a constraint language, e.g., StartJob1 + 5 ≤ StartJob3
∙ Continuous variables
• e.g., start/end times for Hubble Space Telescope observations
• linear constraints solvable in polynomial time by linear
programming
10
Varieties 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., asserting that the value of Y is between X and Z, with the
ternary constraint Between(X,Y,Z).
∙ Global Constriants involve an arbitrary number of
variables
• e.g., AllDiff in Sudoku.
∙ Preference (soft constraints) e.g. red is better than
green can be represented by a cost for each variable
assignment => Constrained optimization problems.
11
Example: Cryptarithmetic
∙ Variables: FTUWRO
X1 X 2 X3
∙ Domain: {0,1,2,3,4,5,6,7,8,9}
∙ Constraints: Alldiff (F,T,U,W,R,O)
• O + O = R + 10 · X1
• X1 + W + W = U + 10 · X2
• X2 + T + T = O + 10 · X3
• X3 = F, T ≠ 0, F ≠ 0
12
CSP as a standard search problem
13
Backtracking search
∙ Variable assignments are commutative,
• Eg [ WA = red then NT = green ]
equivalent to [ NT = green then WA = red ]
14
Backtracking search
function BACKTRACKING-SEARCH(csp) % returns a solution or failure
return RECURSIVE-BACKTRACKING({} , csp)
15
Backtracking example
16
Backtracking example
17
Backtracking example
18
Backtracking example
19
Improving backtracking efficiency
20
Choice: Most constrained variable
21
Most constraining variable
22
Order: Least constraining value
∙ Given a variable, choose the least constraining
value:
• the one that rules out the fewest values in the remaining
variables
23
Forward Checking
∙ Idea:
• Keep track of remaining legal values for unassigned
variables
• Terminate search when any variable has no legal values
24
Forward checking
∙ Idea:
• Keep track of remaining legal values for unassigned
variables
• Terminate search when any variable has no legal values
25
Forward checking
∙ Idea:
• Keep track of remaining legal values for unassigned
variables
• Terminate search when any variable has no legal values
26
Forward checking
∙ Idea:
• Keep track of remaining legal values for unassigned
variables
• Terminate search when any variable has no legal values
27
Constraint propagation
Consist
ent!
Arc consistency
A D
B
A D
C
X1 X2
1 2 3 4 {1,2,3,4} {1,2,3,4}
1
2
3
4
X3 X4
{1,2,3,4} {1,2,3,4}
X1 X2
1 2 3 4 {1,2,3,4} {1,2,3,4}
1
2
3
4
X3 X4
{1,2,3,4} {1,2,3,4}
40
Example: 4-Queens Problem
X1 X2
1 2 3 4 {1,2,3,4} { , ,3,4}
1
2
3
4
X3 X4
{ ,2, ,4} { ,2,3, }
41
Example: 4-Queens Problem
X1 X2
1 2 3 4 {1,2,3,4} { , ,3,4}
1
2
3
4
X3 X4
{ ,2, ,4} { ,2,3, }
42
Example: 4-Queens Problem
X1 X2
1 2 3 4 {1,2,3,4} { , ,3,4}
1
2
3
4
X3 X4
{ , , , } { ,2, , }
43
Example: 4-Queens Problem
X1 X2
1 2 3 4 {1,2,3,4} { , , ,4}
1
2
3
4
X3 X4
{ ,2, ,4} { ,2,3, }
44
Example: 4-Queens Problem
X1 X2
1 2 3 4 {1,2,3,4} { , , ,4}
1
2
3
4
X3 X4
{ ,2, , } { , ,3, }
45
Example: 4-Queens Problem
X1 X2
1 2 3 4 {1,2,3,4} { , , ,4}
1
2
3
4
X3 X4
{ ,2, , } { , ,3, }
46
Example: 4-Queens Problem
X1 X2
1 2 3 4 {1,2,3,4} { , , ,4}
1
2
3
4
X3 X4
{ ,2, , } { , , , }
47
Example: 4-Queens Problem
X1 X2
1 2 3 4 { ,2,3,4} {1,2,3,4}
1
2
3
4
X3 X4
{1,2,3,4} {1,2,3,4}
48
Example: 4-Queens Problem
X1 X2
1 2 3 4 { ,2,3,4} { , , ,4}
1
2
3
4
X3 X4
{1, ,3, } {1, ,3,4}
49
Example: 4-Queens Problem
X1 X2
1 2 3 4 { ,2,3,4} { , , ,4}
1
2
3
4
X3 X4
{1, ,3, } {1, ,3,4}
50
Example: 4-Queens Problem
X1 X2
1 2 3 4 { ,2,3,4} { , , ,4}
1
2
3
4
X3 X4
{1, , , } {1, ,3, }
51
Example: 4-Queens Problem
X1 X2
1 2 3 4 { ,2,3,4} { , , ,4}
1
2
3
4
X3 X4
{1, , , } {1, ,3, }
52
Example: 4-Queens Problem
X1 X2
1 2 3 4 { ,2,3,4} { , , ,4}
1
2
3
4
X3 X4
{1, , , } { , ,3, }
53
Example: 4-Queens Problem
X1 X2
1 2 3 4 { ,2,3,4} { , , ,4}
1
2
3
4
X3 X4
{1, , , } { , ,3, }
54
Intelligent Backtracking
∙ Chronological backtracking - the most recent decision
point is revisited
∙ Suppose we have generated the partial assignment
{Q=red,NSW =green,V =blue,T =red}
When we try the next variable, SA, we see that every
value violates a constraint. We back up to T and try a
new color – this cannot possibly resolve the problem
with SA
∙ 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.
55
Backjumping
∙ 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 .
∙ A backjumping algorithm that uses conflict sets defined in this
way is called conflict-directed backjumping.
∙ Constraint learning is the idea of finding a minimum set of
variables from the conflict set that causes the problem. This set
of variables, along with their corresponding values, is called a
no-good.
56
Local Search for CSPs
∙ To apply to CSPs:
57
Example: n-queens
∙ States: 4 queens in 4 columns (44 = 256 states)
∙ Actions: move queen in column
∙ Goal test: no attacks
∙ Evaluation: h(n) = number of attacks
58
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
∙ Notice that many real-world problems involve
real-valued variables
59
Summary
∙ CSPs are a special kind of problem:
60