AI-Lecture 7 (Constraint Satisfaction Problems)
AI-Lecture 7 (Constraint Satisfaction Problems)
Lecture 7
Bicol University
1st Semester 2023-2024
Constraint Satisfaction Problem
(CSP)
Constraint satisfaction
Constraint problems
satisfaction problems (CSPs)
●
Standard search problem:
– state is a "black box“ – any data structure that supports successor
function, heuristic function, and goal test
●
CSP:
– state is defined by variables Xi with values from domain Di
– goal test is a set of constraints specifying allowable combinations
of values for subsets of variables
●
Simple example of a formal representation language
●
Allows useful general-purpose algorithms with more power
than standard search algorithms
Constraint satisfaction
Constraint Satisfaction Problems problems
●
What is a CSP? (Problem Formulation)
– Finite set of variables X1, X2, …, Xn
– Nonempty domain of possible values for each variable
D1, D2, …, Dn
– Finite set of constraints C1, C2, …, Cm
●
Each constraint Ci limits the values that variables can take,
●
e.g., X1 ≠ X2
– Each constraint Ci is a pair <scope, relation>
●
Scope = Tuple of variables that participate in the constraint.
●
Relation = List of allowed combinations of variable values.
– May be an explicit list of allowed combinations,e.g.⟨(X1,X2 ),
{(3,1),(3,2),(2,1)}⟩
– May be an abstract relation allowing membership testing and
listing,e.g. ⟨(X1 ,X2 ),X1 > X2⟩
CSP example: map coloring
CSP example: map coloring
●
A state is an assignment of values to some or all variables.
– An assignment is complete when every variable has a value.
– An assignment is partial when some variables have no values.
●
Consistent assignment
– assignment does not violate the constraints
●
A solution to a CSP is a complete and consistent
assignment.
●
Some CSPs require a solution that maximizes an objective
function.
CSP example: map coloring
{WA=red,NT=green,Q=red,NSW=green,V=red,SA=blue,T=gre
en}
Constraint graphs
Constraint graphs
• Constraint graph:
●
Unary constraints involve a single variable.
– e.g. SA green
– initial specification of the domain of a variable can also be seen as a unary constraint.
●
Binary constraints involve pairs of variables.
– e.g. SA WA
●
Higher-order constraints involve 3 or more
variables.
– Professors A, B,and C cannot be on a committee together
– Can always be represented by multiple binary constraints
●
Preference (soft constraints)
– e.g. red is better than green often can be represented by a cost for each variable assignment
– combination of optimization with CSPs
CSP Example: Cryptharithmetic puzzle
Each letter stands for a distinct digit; the aim
is to find a substitution of digits for letters
such that the resulting sum is arithmetically
correct, with the added restriction that no
leading zeroes are allowed.
CSP
CSPExample: Cryptharithmetic
Example: Cryptharithmetic puzzle puzzle
constraint
hypergraph
●
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
CSP as a standard search problem
●
A CSP can easily be expressed as a standard search
problem.
●
Incremental formulation
– Path cost: constant cost for every step (not really relevant)
●
Can also use complete-state formulation
– Local search techniques
CSP as a standard search problem
●
This is the same for all CSP’s !!!
●
Solution is found at depth n (if there are n variables).
●
Hence depth first search can be used.
●
Path is irrelevant, so complete state representation can also be used.
●
Branching factor b at the top level is nd.
●
b=(n-l)d at depth l, hence n!dn leaves (only dn complete assignments).
Commutativity
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
Local consistency
• Node consistency: satisfies unary constraints
—This is trivial!
• Arc consistency : satisfies binary constraints
—Xi is arc-consistent w.r.t. Xj if for every value v in Di , there is some
value w in Dj that satisfies the binary constraint on the arc between Xi
and Xj .
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
End