AI-Lecture 7 (Constraint Satisfaction Problem)
AI-Lecture 7 (Constraint Satisfaction Problem)
Lecture 7
Bicol University
1st Semester 2021-2022
Constraint Satisfaction Problem
(CSP)
Constraint satisfaction problems
What is a CSP?
Finite set of variables V1, V2, …, Vn
Finite set of constraints C1, C2, …, Cm
Non-empty domain of possible values for each variable
DV1, DV2, … DVn
Each constraint Ci limits the values that variables can take,
e.g. V1 ≠ V2
{WA=red,NT=green,Q=red,NSW=green,V=red,SA=blue,T=gre
en}
Constraint graph
CSP benefits
●
Standard representation pattern
●
Generic goal and successor functions
●
Generic heuristics (no domain specific
expertise).
Incremental formulation
Initial State: the empty assignment {}.
Successor function: Assign value to unassigned variable
provided that there is no conflict.
Goal test: the current assignment is complete.
Path cost: as constant cost for every step.
CSP as a standard search problem
Uninformed algorithm
No good general performance
Backtracking search
var SELECT-UNASSIGNED-VARIABLE(VARIABLES[csp],assignment,csp)
Assign {WA=red}
Effects on other variables connected by constraints with WA
NT can no longer be red
SA can no longer be red
Forward checking
Assign {Q=green}
Effects on other variables connected by constraints with WA
NT can no longer be green
NSW can no longer be green
SA can no longer be green
If V is assigned blue
Effects on other variables connected by constraints with WA
SA is empty
NSW can no longer be blue
FC has detected that partial assignment is inconsistent with the
constraints and backtracking can occur.
[4-Queens slides copied from B.J. Dorr CMSC 421 course on AI]
Example: 4-Queens Problem
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}
[4-Queens slides copied from B.J. Dorr CMSC 421 course on AI]
Example: 4-Queens Problem
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}
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, }
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, }
Example: 4-Queens Problem
X1 X2
1 2 3 4 {1,2,3,4} { , ,3,4}
1
2
3
4
X3 X4
Backtrack!!!
{ , , , } { ,2,3, }
Example: 4-Queens Problem
Picking up a little later after two steps of
backtracking....
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}
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}
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}
Example: 4-Queens Problem
X1 X2
1 2 3 4 { ,2,3,4} { , , ,4}
1
2
3
4
X3 X4
{1, , , } {1, ,3, }
Example: 4-Queens Problem
X1 X2
1 2 3 4 { ,2,3,4} { , , ,4}
1
2
3
4
X3 X4
{1, , , } {1, ,3, }
Example: 4-Queens Problem
X1 X2
1 2 3 4 { ,2,3,4} { , , ,4}
1
2
3
4
X3 X4
{1, , , } { , ,3, }
Example: 4-Queens Problem
X1 X2
1 2 3 4 { ,2,3,4} { , , ,4}
1
2
3
4
X3 X4
{1, , , } { , ,3, }
Towards Constraint propagation
●
assigning a value to a variable
– Constraint propagation (inference)
●
Eliminates possible values for a variable if the value
would violate local consistency
– Can do inference first, or intertwine it with search
●
You’ll investigate this in the Sudoku homework
Local consistency
• Node consistency: satisfies unary constraints (This is trivial!)
• Arc consistency : satisfies binary constraints
—X i is arc-consistent w.r.t. X j if for every value v in D i , there is
some value w in D j that satisfies the binary constraint on the arc
between X i and X j .
Example: Sudoku
•Variables: 81 slots
•Domains =
{1,2,3,4,5,6,7,8,9}
•Constraints:
•27 not-equal
Constraint
propagation
23
426
X Red
green
blue
X: red
X • Search
backtracking,
Y Z Red Red
variable/value Y: blue
green green heuristics
Y
blue blue
Z •Inference Z: green
Coloring Consistency
Constraint Graph
Problem enforcement,
forward checking
Variables
Problem Values CSP
Statement Algorithm Solution
Constraints
CSP Representation
Example: cryptharithmetic
Example: cryptharithmetic
CSP Representation:
Example: cryptharithmetic
Get solution by applying backtracking search with forward checking and
heuristics(MRV, LCV, degree heuristic).
Constraints:
(1) Alldiff(T, W, O, F, U, R)
(2) O + O = R + 10 * X1
(3) X1 + W + W = U + 10 * X2
(4) X2 + T + T = O + 10 * X3
(5) X3 = F
Example: cryptharithmetic
Get solution by applying backtracking search with forward checking and
heuristics.
Constraints:
(1) Alldiff(T, W, O, F, U, R)
(2) O + O = R + 10 * X1
(3) X1 + W + W = U + 10 * X2
(4) X2 + T + T = O + 10 * X3
(5) X3 = F
Example: cryptharithmetic
Get solution by applying backtracking search with forward checking and
heuristics.
Constraints:
(1) Alldiff(T, W, O, F, U, R)
(2) O + O = R + 10 * X1
(3) X1 + W + W = U + 10 * X2
(4) X2 + T + T = O + 10 * X3
(5) X3 = F
Example: cryptharithmetic
Get solution by applying backtracking search with forward checking and
heuristics.
Constraints:
(1) Alldiff(T, W, O, F, U, R)
(2) O + O = R + 10 * X1
(3) X1 + W + W = U + 10 * X2
(4) X2 + T + T = O + 10 * X3
(5) X3 = F
Example: cryptharithmetic
Get solution by applying backtracking search with forward checking and
heuristics.
Example: cryptharithmetic
SOLUTION:
Summary