cs188 Fa22 Lec04
cs188 Fa22 Lec04
cs188 Fa22 Lec04
[These slides were created by Dan Klein and Pieter Abbeel for CS188 Intro to AI at UC Berkeley. All CS188 materials are available at http://ai.berkeley.edu.]
Announcements
§ Project 1 is due Friday, September 9, 11:59 PM PT
§ Start soon if you haven’t already! It’s one of the longer projects.
§ HW1 is due Friday, September 9, 11:59 PM PT
Today
§ Local Search
Reminder: CSPs
§ CSPs:
§ Variables
§ Domains
§ Constraints
§ Implicit (provide code to compute)
§ Explicit (provide a list of the legal tuples)
§ Unary / Binary / N-ary
§ Goals:
§ Here: find any solution
§ Also: find all, find best, etc.
Backtracking Search
Improving Backtracking
§ Ordering:
§ Which variable should be assigned next? (MRV)
§ In what order should its values be tried? (LCV)
NT Q
WA
SA
NSW
V
NT Q
WA
SA
NSW
V
NT Q
WA
SA
NSW
V
NT Q
WA SA
NSW
V
§ Arc V to NSW is consistent: for every x in the tail there is some y in the head which
could be assigned without violating a constraint
Arc Consistency of an Entire CSP
§ A simple form of propagation makes sure all arcs are consistent:
NT Q
WA SA
NSW
V
§ Arc SA to NSW is consistent: for every x in the tail there is some y in the head which
could be assigned without violating a constraint
Arc Consistency of an Entire CSP
§ A simple form of propagation makes sure all arcs are consistent:
NT Q
WA SA
NSW
V
§ Arc NSW to SA is not consistent: if we assign NSW = blue, there is no valid assignment
left for SA
§ To make this arc consistent, we delete NSW = blue from the tail
Arc Consistency of an Entire CSP
§ A simple form of propagation makes sure all arcs are consistent:
NT Q
WA SA
NSW
V
§ Remember that arc V to NSW was consistent, when NSW had red and blue in its
domain
§ After removing blue from NSW, this arc might not be consistent anymore! We need to
recheck this arc.
§ Important: If X loses a value, neighbors of X need to be rechecked!
Arc Consistency of an Entire CSP
§ A simple form of propagation makes sure all arcs are consistent:
NT Q
WA SA
NSW
V
§ Arc SA to NT is inconsistent. We make it consistent by deleting from the tail (SA = blue).
Arc Consistency of an Entire CSP
§ A simple form of propagation makes sure all arcs are consistent:
NT Q
WA SA
NSW
V
§ SA has an empty domain, so we detect failure. There is no way to solve this CSP with
WA = red and Q = green, so we backtrack.
§ Arc consistency detects failure earlier than forward checking
§ Can be run as a preprocessor or after each assignment
Enforcing Arc Consistency in a CSP
§ Lots of middle ground between arc consistency and n-consistency! (e.g. k=3, called
path consistency)
Ordering
Ordering: Minimum Remaining Values
§ Variable Ordering: Minimum remaining values (MRV):
§ Choose the variable with the fewest legal left values in its domain
§ 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)
§ This property also applies to probabilistic reasoning (later): an example of the relation
between syntactic restrictions and the complexity of reasoning
Tree-Structured CSPs
§ Algorithm for tree-structured CSPs:
§ Order: Choose a root variable, order variables so that parents precede children
§ Claim 2: If root-to-leaf arcs are consistent, forward assignment will not backtrack
§ Proof: Induction on position
§ Why doesn’t this algorithm work with cycles in the constraint graph?
§ Note: we’ll see this basic idea again with Bayes’ nets
Improving Structure
Nearly Tree-Structured CSPs
Choose a cutset
SA
M1 M2 M3 M4
¹ ¹ ¹ ¹
Agree on
Agree on
Agree on
NS NS
WA NT NT Q Q W W
V
¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹
shared vars
shared vars
shared vars
SA SA SA SA
§ To apply to CSPs:
§ Take an assignment with unsatisfied constraints
§ Operators reassign variable values
§ No fringe! Live on the edge.
§ 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 (no fringe!)
§ Generally much faster and more memory efficient (but incomplete and suboptimal)
Hill Climbing
§ Simple, general idea:
§ Start wherever
§ Repeat: move to the best neighboring state
§ If no neighbors better than current, quit
46
Simulated Annealing
§ Theoretical guarantee:
§ Stationary distribution:
§ If T decreased slowly enough,
will converge to optimal state!
§ Possibly the most misunderstood, misapplied (and even maligned) technique around
Example: N-Queens