Constraint satisfaction problemsxial/Teaching/2021SAI/slides/... · 2021. 2. 4. · ØConstraint...
Transcript of Constraint satisfaction problemsxial/Teaching/2021SAI/slides/... · 2021. 2. 4. · ØConstraint...
![Page 1: Constraint satisfaction problemsxial/Teaching/2021SAI/slides/... · 2021. 2. 4. · ØConstraint satisfaction problems (CSPs): • A special subset of search problems • State is](https://reader036.fdocuments.us/reader036/viewer/2022090810/611c466857f2112c543f692a/html5/thumbnails/1.jpg)
Lirong Xia
Constraint satisfaction problems
![Page 2: Constraint satisfaction problemsxial/Teaching/2021SAI/slides/... · 2021. 2. 4. · ØConstraint satisfaction problems (CSPs): • A special subset of search problems • State is](https://reader036.fdocuments.us/reader036/viewer/2022090810/611c466857f2112c543f692a/html5/thumbnails/2.jpg)
ØConstraint satisfaction problemsØAlgorithms
• backtracking search
• filtering§ forward checking
§ constraint propagation
1
Today’s schedule
![Page 3: Constraint satisfaction problemsxial/Teaching/2021SAI/slides/... · 2021. 2. 4. · ØConstraint satisfaction problems (CSPs): • A special subset of search problems • State is](https://reader036.fdocuments.us/reader036/viewer/2022090810/611c466857f2112c543f692a/html5/thumbnails/3.jpg)
Ø Standard search problems:• State is a “black box”: arbitrary data structure
• Goal test: any function over states• Successor function can be anything
Ø Constraint satisfaction problems (CSPs):• A special subset of search problems
• State is defined by variables Xi with values from a domain D (sometimes D depends on i )
• Goal test is a set of constraints specifying allowable combinations of values for subsets of variables
Ø Allows useful general-purpose algorithms with more power than standard search algorithms
Constraint Satisfaction Problems
2
![Page 4: Constraint satisfaction problemsxial/Teaching/2021SAI/slides/... · 2021. 2. 4. · ØConstraint satisfaction problems (CSPs): • A special subset of search problems • State is](https://reader036.fdocuments.us/reader036/viewer/2022090810/611c466857f2112c543f692a/html5/thumbnails/4.jpg)
ØVariables: WA, NT, Q, NSW, V, SA, T
ØDomains:
ØConstraints: adjacent regions must have different colors
ØSolutions are assignments satisfying all constraints, e.g.:
Example: Map-Coloring
3
{ }red, green, blueD =
( ) ( ) ( ) ( ){ }, red, green , red, blue , green, red ,...
WA NTWA NT
¹
Î
WA , NT , Q , ,, , red green red NSW green
V red SA blue T green= = = =ì ü
í ý= = =î þ
![Page 5: Constraint satisfaction problemsxial/Teaching/2021SAI/slides/... · 2021. 2. 4. · ØConstraint satisfaction problems (CSPs): • A special subset of search problems • State is](https://reader036.fdocuments.us/reader036/viewer/2022090810/611c466857f2112c543f692a/html5/thumbnails/5.jpg)
ØFormulation 1:• Variables:
• Domains:
• Constraints
Example: N-Queens
4
ijX
∀i, j,k Xij ,Xik( )∈ 0, 0( ), 0, 1( ), 1, 0( ){ }
{ }0,1
∀i, j,k Xij ,Xkj( )∈ 0, 0( ), 0, 1( ), 1, 0( ){ }∀i, j,k Xij ,Xi+k , j+k( )∈ 0, 0( ), 0, 1( ), 1, 0( ){ }∀i, j,k Xij ,Xi+k , j−k( )∈ 0, 0( ), 0, 1( ), 1, 0( ){ }
,ij
i jX N=å
![Page 6: Constraint satisfaction problemsxial/Teaching/2021SAI/slides/... · 2021. 2. 4. · ØConstraint satisfaction problems (CSPs): • A special subset of search problems • State is](https://reader036.fdocuments.us/reader036/viewer/2022090810/611c466857f2112c543f692a/html5/thumbnails/6.jpg)
ØFormulation 2:• Variables:
• Domains:
• Constraints
Example: N-Queens
5
kQ
( ), non-threatening ,i ji j Q Q"
{ }1,2,3,...N
Q1,Q2( )∈ 1, 3( ), 1, 4( ),...{ }
Implicit:-or-
Explicit:
Q1 Q2 Q3 Q4
![Page 7: Constraint satisfaction problemsxial/Teaching/2021SAI/slides/... · 2021. 2. 4. · ØConstraint satisfaction problems (CSPs): • A special subset of search problems • State is](https://reader036.fdocuments.us/reader036/viewer/2022090810/611c466857f2112c543f692a/html5/thumbnails/7.jpg)
ØBinary CSP: each constraint relates (at most) two variables
ØBinary constraint graph: nodes are variables, arcs show constraints
ØGeneral-purpose CSP algorithms use the graph structure to speed up search.
• E.g., Tasmania is an independent subproblem!
Constraint Graphs: Goal test
6
![Page 8: Constraint satisfaction problemsxial/Teaching/2021SAI/slides/... · 2021. 2. 4. · ØConstraint satisfaction problems (CSPs): • A special subset of search problems • State is](https://reader036.fdocuments.us/reader036/viewer/2022090810/611c466857f2112c543f692a/html5/thumbnails/8.jpg)
ØVariables (circles):
ØDomains:
ØConstraints (boxes):
Example: Cryptarithmetic
7
{ }0, 1, 2, 3, 4, 5, 6, 7, 8, 9
alldiff F , T , U , W , R, O( )O+O = R+10• X1
1 2 3 F T U W R O X X XT W O
+ T W OF O U R
![Page 9: Constraint satisfaction problemsxial/Teaching/2021SAI/slides/... · 2021. 2. 4. · ØConstraint satisfaction problems (CSPs): • A special subset of search problems • State is](https://reader036.fdocuments.us/reader036/viewer/2022090810/611c466857f2112c543f692a/html5/thumbnails/9.jpg)
ØVariables: • Each (open) square
ØDomains:•
ØConstraints:
Example: Sudoku
8
1, 2, …, 9{ }
9-way alldiff for each column9-way alldiff for each row9-way alldiff for each region
![Page 10: Constraint satisfaction problemsxial/Teaching/2021SAI/slides/... · 2021. 2. 4. · ØConstraint satisfaction problems (CSPs): • A special subset of search problems • State is](https://reader036.fdocuments.us/reader036/viewer/2022090810/611c466857f2112c543f692a/html5/thumbnails/10.jpg)
ØDiscrete Variables:• Finite domains
§ Size d means O(dn) complete assignments§ E.g., Boolean CSPs, including Boolean satisfiability (NP-
complete)
• Infinite domains (integers, strings, etc.)§ Linear constraints solvable
ØContinuous variables:• Linear constraints solvable in polynomial time by
LP methods
Varieties of CSPs
9
![Page 11: Constraint satisfaction problemsxial/Teaching/2021SAI/slides/... · 2021. 2. 4. · ØConstraint satisfaction problems (CSPs): • A special subset of search problems • State is](https://reader036.fdocuments.us/reader036/viewer/2022090810/611c466857f2112c543f692a/html5/thumbnails/11.jpg)
Ø Variables of Constraints• Unary constraints involve a single variable (equiv. to shrinking
domains):
• Binary constraints involve pairs of variables:
• Higher-order constraints involve 3 or more variables:e.g., cryptarithmetic column constraints
Ø Preferences (soft constraints):• E.g., red is better than green• Often representable by a cost for each variable assignment• Gives constrained optimization problems
Varieties of Constraints
10
SA green¹
SA WA¹
![Page 12: Constraint satisfaction problemsxial/Teaching/2021SAI/slides/... · 2021. 2. 4. · ØConstraint satisfaction problems (CSPs): • A special subset of search problems • State is](https://reader036.fdocuments.us/reader036/viewer/2022090810/611c466857f2112c543f692a/html5/thumbnails/12.jpg)
ØStandard search formulation of CSPs (incremental)
ØLet’s start with the straightforward, dumb approach, then fix it
ØStates are defined by the values assigned so far• Initial state; the empty assignment,• Successor function: assign a value to an unassigned
variable• Goal test: the current assignment is complete and
satisfies all constraint
Standard Search Formulation
11
![Page 13: Constraint satisfaction problemsxial/Teaching/2021SAI/slides/... · 2021. 2. 4. · ØConstraint satisfaction problems (CSPs): • A special subset of search problems • State is](https://reader036.fdocuments.us/reader036/viewer/2022090810/611c466857f2112c543f692a/html5/thumbnails/13.jpg)
ØWhat would BFS do?
ØWhat would DFS do?
Ø Is BFS better or DFS better?
ØCan we use A*?
Search Methods
12
![Page 14: Constraint satisfaction problemsxial/Teaching/2021SAI/slides/... · 2021. 2. 4. · ØConstraint satisfaction problems (CSPs): • A special subset of search problems • State is](https://reader036.fdocuments.us/reader036/viewer/2022090810/611c466857f2112c543f692a/html5/thumbnails/14.jpg)
13
Early detection of non-Goal
![Page 15: Constraint satisfaction problemsxial/Teaching/2021SAI/slides/... · 2021. 2. 4. · ØConstraint satisfaction problems (CSPs): • A special subset of search problems • State is](https://reader036.fdocuments.us/reader036/viewer/2022090810/611c466857f2112c543f692a/html5/thumbnails/15.jpg)
Ø Idea 1: only consider a single variable at each point• Variable assignments are commutative, so fix ordering• I.e., [WA = red then NT = green] same as [NT = green then WA = red]• Only need to consider assignments to a single variable at each step
Ø Idea 2: only allow legal assignments at each point• “Incremental goal test”
Ø DFS for CSPs with these two improvements is called backtracking search
Ø Backtracking search is the basic uninformed algorithm for CSPs
Ø Can solve n-queens for
Backtracking Search
14
25n »
![Page 16: Constraint satisfaction problemsxial/Teaching/2021SAI/slides/... · 2021. 2. 4. · ØConstraint satisfaction problems (CSPs): • A special subset of search problems • State is](https://reader036.fdocuments.us/reader036/viewer/2022090810/611c466857f2112c543f692a/html5/thumbnails/16.jpg)
Backtracking Example
15
![Page 17: Constraint satisfaction problemsxial/Teaching/2021SAI/slides/... · 2021. 2. 4. · ØConstraint satisfaction problems (CSPs): • A special subset of search problems • State is](https://reader036.fdocuments.us/reader036/viewer/2022090810/611c466857f2112c543f692a/html5/thumbnails/17.jpg)
ØOrdering:• Which variable should be assigned next?
• In what order should its values be tried?
ØFiltering: Can we detect inevitable failure early?
Improving Backtracking
16
![Page 18: Constraint satisfaction problemsxial/Teaching/2021SAI/slides/... · 2021. 2. 4. · ØConstraint satisfaction problems (CSPs): • A special subset of search problems • State is](https://reader036.fdocuments.us/reader036/viewer/2022090810/611c466857f2112c543f692a/html5/thumbnails/18.jpg)
Minimum Remaining Values
17
ØMinimum remaining values (MRV):• Choose the variable with the fewest legal values
ØWhy min rather than max?
ØAlso called “most constrained variable”
Ø“Fail-fast” ordering
![Page 19: Constraint satisfaction problemsxial/Teaching/2021SAI/slides/... · 2021. 2. 4. · ØConstraint satisfaction problems (CSPs): • A special subset of search problems • State is](https://reader036.fdocuments.us/reader036/viewer/2022090810/611c466857f2112c543f692a/html5/thumbnails/19.jpg)
Example
• For every variable, keep track of which values are still possible
Q X X X
Q X X X
Q X X X
X
Q X X X
X X
X
Q X X X
Q
Q
Q
Q
Q
Q
Q
Q
Q X X
Q X X
Q X X
X
Q X X
X X Q
X
Q X X
only one possibility for last column; might
as well fill in
now only one left for other two columns
done!(no real branching
needed!)
![Page 20: Constraint satisfaction problemsxial/Teaching/2021SAI/slides/... · 2021. 2. 4. · ØConstraint satisfaction problems (CSPs): • A special subset of search problems • State is](https://reader036.fdocuments.us/reader036/viewer/2022090810/611c466857f2112c543f692a/html5/thumbnails/20.jpg)
Choosing a value
19
ØGiven a choice of variable:• Choose the least constraining
value• The one that rules out the
fewest values in the remaining variables
• May take some computation to determine this!
ØWhy least rather than most?
value 1 for SA
value 0 for SA
![Page 21: Constraint satisfaction problemsxial/Teaching/2021SAI/slides/... · 2021. 2. 4. · ØConstraint satisfaction problems (CSPs): • A special subset of search problems • State is](https://reader036.fdocuments.us/reader036/viewer/2022090810/611c466857f2112c543f692a/html5/thumbnails/21.jpg)
Filtering: Forward Checking
20
Ø Idea: keep track of remaining values for unassigned variables (using immediate constraints)
Ø Idea: terminate when any variable has no legal values
![Page 22: Constraint satisfaction problemsxial/Teaching/2021SAI/slides/... · 2021. 2. 4. · ØConstraint satisfaction problems (CSPs): • A special subset of search problems • State is](https://reader036.fdocuments.us/reader036/viewer/2022090810/611c466857f2112c543f692a/html5/thumbnails/22.jpg)
Filtering: Constraint Propagating
21
Ø 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!Ø Why didn’t we detect this yet?
step 1 step 2
step 1step 2
![Page 23: Constraint satisfaction problemsxial/Teaching/2021SAI/slides/... · 2021. 2. 4. · ØConstraint satisfaction problems (CSPs): • A special subset of search problems • State is](https://reader036.fdocuments.us/reader036/viewer/2022090810/611c466857f2112c543f692a/html5/thumbnails/23.jpg)
Consistency of An Arc
22
Ø An arc is consistent iff for every x in the tail there is some y in the head which could be assigned without violating a constraint
Ø Forward checking = Enforcing consistency of each arc pointing to the new assignment
X Y®
Deletefrom tail!
![Page 24: Constraint satisfaction problemsxial/Teaching/2021SAI/slides/... · 2021. 2. 4. · ØConstraint satisfaction problems (CSPs): • A special subset of search problems • State is](https://reader036.fdocuments.us/reader036/viewer/2022090810/611c466857f2112c543f692a/html5/thumbnails/24.jpg)
Arc Consistency of a CSP
23
Ø A simple form of propagation makes sure all arcs are consistent:
Ø If V loses a value, neighbors of V need to be rechecked!Ø Arc consistency detects failure earlier than forward checkingØ Can be run as a preprocessor or after each assignmentØ Might be time-consuming
Deletefrom tail!
X XX