CS 106: Artificial Intelligence: Constraint Satisfaction Problems
CS 106: Artificial Intelligence: Constraint Satisfaction Problems
https://www.cs.upc.edu/~erodri/webpage/cps/cps.html
Constraint Graphs
Constraint Graphs
▪ Binary CSP: each constraint relates (at most) two
variables
Implicit:
Explicit:
Example: N-Queens
▪ Variables:
♛
▪ Domains: ♛
♛
▪ Constraints: ♛
Implicit:
♛
Explicit:
♛
♛
Example: Sudoku
▪ Variables:
▪ Each (open) square
▪ Domains:
▪ {1,2,…,9}
▪ Constraints:
▪ Continuous variables
▪ E.g., start/end times for Hubble Telescope observations
Varieties of Constraints
▪
Real-World CSPs
▪ Assignment problems: e.g., who teaches what class
▪ Timetabling problems: e.g., which class is offered when and where?
▪ Hardware configuration
▪ Transportation scheduling
▪ Factory scheduling
▪ Circuit layout
▪ Fault diagnosis
▪ … lots more!
♛
♛
▪ What would DFS do?
♛
♛
▪ What problems does naïve search have?
BFS?
♛ ♛
♛
♛
♛
…
♛
♛ ♛ ♛
♛
♛
… … … … … ♛
♛ ♛
♛ ♛ ♛
♛
♛ … ♛
♛ … ♛
♛
… … … ♛
♛
♛
… ♛
♛
♛
♛ ♛ ♛ ♛ ♛ ♛ ♛
♛
♛
♛
… ♛
♛
♛
… ♛
♛
♛ … ♛ ♛
♛ …… ♛
♛
♛
… ♛
♛
♛ …… ♛
♛
♛
DFS?
♛ ♛
♛
♛
♛
…
♛
♛ ♛ ♛ ♛
♛ ♛
♛ …… ♛
…
♛
♛
♛ ♛
♛ ♛
♛
♛
♛
♛
♛
♛
♛
…
♛
♛
♛ … ♛
… ♛
♛ ♛ ♛
♛ ♛ ♛ ♛
♛
♛
♛
♛♛
♛
♛ …
♛
♛
♛
♛
… ♛
♛
♛
♛ ♛ ♛ ♛
♛ ♛
Backtracking Search
Backtracking Search
▪ Backtracking search is the basic uninformed algorithm for solving CSPs
▪ Idea 1: One variable at a time
▪ Only need to consider assignments to a single variable at each step
♛
♛
♛
♛
♛ ♛
♛
♛
♛ ♛
♛ ♛
♛ ♛
♛ ♛
♛
♛
♛
♛
Map Coloring with Backtracking
Where are we
now?
https://vi.wikipedia.org/
Map Coloring with Backtracking
https://camranhcitygate.info/
Backtracking Example
Backtracking Search
▪ Ordering:
▪ Which variable should be assigned next?
▪ In what order should its values be tried?
Vạn Ninh Ninh Hòa Khánh Vĩnh Diên Khánh Nha Trang Cam Lâm Khánh Sơn Cam Ranh Trường Sa
Filtering: Constraint Propagation
▪ Forward checking propagates information from assigned to unassigned variables, but
doesn't provide early detection for all failures:
Vạn Ninh Ninh Hòa Khánh Vĩnh Diên Khánh Nha Trang Cam Lâm Khánh Sơn Cam Ranh Trường Sa
https://www.cs.upc.edu/~erodri/webpage/cps/cps.html
Arc Consistency of an Entire CSP
▪ A simple form of propagation makes sure all arcs are consistent:
Vạn Ninh Ninh Hòa Khánh Vĩnh Diên Khánh Nha Trang Cam Lâm Khánh Sơn Cam Ranh Trường Sa
Remember: Delete
from the tail!
https://www.cs.upc.edu/~erodri/webpage/cps/cps.html
Limitations of Arc Consistency
▪ Ordering:
▪ Which variable should be assigned next?
▪ In what order should its values be tried?
Vạn Ninh Ninh Hòa Khánh Vĩnh Diên Khánh Nha Trang Cam Lâm Khánh Sơn Cam Ranh Trường Sa
Ordering: Minimum Remaining Values
▪ Variable Ordering: Minimum remaining values (MRV):
▪ Choose the variable with the fewest legal left values in its domain
Vạn Ninh Ninh Hòa Khánh Vĩnh Diên Khánh Nha Trang Cam Lâm Khánh Sơn Cam Ranh Trường Sa
Ordering: Least Constraining Value
▪ Value Ordering: Least Constraining Value
▪ Given a choice of variable, choose the least
constraining value
▪ I.e., the one that rules out the fewest values in
the remaining variables
▪ Note that it may take some computation to
determine this! (E.g., rerunning filtering)
▪ Ordering:
▪ Which variable should be assigned next?
▪ In what order should its values be tried?
▪ Theorem: if the constraint graph has no loops, the CSP can be solved in O(n d2) time
▪ Compare to general CSPs, where worst-case time is O(dn)
Tree-Structured CSPs
Improving Structure
Nearly Tree-Structured CSPs
Vạn Ninh Ninh Hòa Khánh Vĩnh Diên Khánh Nha Trang Cam Lâm Khánh Sơn Cam Ranh Trường Sa
Tree-Structured CSP
▪ Algorithm for tree-structured CSPs:
▪ Order: Choose a root variable, order variables so that parents precede children
Vạn Ninh Ninh Hòa Nha Trang Khánh Vĩnh Khánh Sơn Cam Ranh
▪ To apply to CSPs:
▪ Take an assignment with unsatisfied constraints
▪ Operators reassign variable values
▪ No fringe! Live on the edge.
♛ ♛ 0 3 1 ♛
♛ ♛ ♛
♛ ♛ ♛
♛ ♛ ♛
Performance of Min-Conflicts
▪ Given random initial state, can solve n-queens in almost constant time for arbitrary
n with high probability (e.g., n = 10,000,000)!
▪ The same appears to be true for any randomly-generated CSP except in a narrow
range of the ratio
Summary: CSPs
▪ CSPs are a special kind of search problem:
▪ States are partial assignments
▪ Goal test defined by constraints
▪ Basic solution: backtracking search
▪ Speed-ups:
▪ Ordering
▪ Filtering
▪ Structure
▪ Local search: improve a single option until you can’t make it better
▪ New successor function: local changes
▪ Generally much faster and more memory efficient (but incomplete and suboptimal)
▪ Pretty much unavoidable when the state is “yourself”
Hill Climbing
▪ Simple, general idea:
▪ Start wherever
▪ Repeat: move to the best neighboring state
▪ If no neighbors better than current, quit
Hill Climbing
64
Hill Climbing Diagram
Hill Climbing Quiz
67
Simulated Annealing
▪ Theoretical guarantee:
▪ Stationary distribution (Boltzmann):
▪ If T decreased slowly enough,
will converge to optimal state!
▪ Possibly the most misunderstood, misapplied (and even maligned) technique around
Example: N-Queens