2PP

CS 188: Artificial Intelligence
Spring 2006
Lecture 4: CSPs
9/7/2006
Dan Klein – UC Berkeley
Many slides over the course adapted from either Stuart
Russell or Andrew Moore
Announcements
ƒ Reminder:
ƒ Project 1.1 is due Friday at 11:59pm!
ƒ Check web page for this week’s office hours
ƒ Sections this Monday
ƒ Can go to any of them, or multiple (unless over
capacity of room)
ƒ Dan / John back late today
ƒ Don’t forget about the newsgroup
ƒ Good for course questions
ƒ Good for finding partners
1
Constraint Satisfaction Problems
ƒ Standard search problems:
ƒ State is a “black box”: any old data structure
ƒ Goal test: any function over states
ƒ Successors: any map from states to sets of states
ƒ Constraint satisfaction problems (CSPs):
ƒ 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
ƒ Simple example of a formal representation
language
ƒ Allows useful general-purpose algorithms with
more power than standard search algorithms
Example: N-Queens
ƒ Formulation 1:
ƒ Variables:
ƒ Domains:
ƒ Constraints
2
Example: N-Queens
ƒ Formulation 2:
ƒ Variables:
ƒ Domains:
ƒ Constraints:
… there’s an even better way! What is it?
Example: Map-Coloring
ƒ Variables:
ƒ Domain:
ƒ Constraints: adjacent regions must have
different colors
ƒ Solutions are assignments satisfying all
constraints, e.g.:
3
Example: The Waltz Algorithm
ƒ The Waltz algorithm is for interpreting line drawings of
solid polyhedra
ƒ An early example of a computation posed as a CSP
?
ƒ Look at all intersections
ƒ Adjacent intersections impose constraints on each other
Waltz on Simple Scenes
ƒ Assume all objects:
ƒ Have no shadows or cracks
ƒ Three-faced vertices
ƒ “General position”: no junctions
change with small movements of
the eye.
ƒ Then each line on image is
one of the following:
ƒ Boundary line (edge of an
object) (→) with right hand of
arrow denoting “solid” and left
hand denoting “space”
ƒ Interior convex edge (+)
ƒ Interior concave edge (-)
4
Legal Junctions
ƒ Only certain junctions are
physically possible
ƒ How can we formulate a CSP to
label an image?
ƒ Variables: vertices
ƒ Domains: junction labels
ƒ Constraints: both ends of a line
should have the same label
x
(x,y) in
y
,
,…
Constraint Graphs
ƒ Binary CSP: each constraint
relates (at most) two variables
ƒ 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!
5
Example: Cryptarithmetic
ƒ Variables:
ƒ Domains:
ƒ Constraints:
Varieties of CSPs
ƒ 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.)
ƒ E.g., job scheduling, variables are start/end times for each job
ƒ Need a constraint language, e.g., StartJob1 + 5 < StartJob3
ƒ Linear constraints solvable, nonlinear undecidable
ƒ Continuous variables
ƒ E.g., start/end times for Hubble Telescope observations
ƒ Linear constraints solvable in polynomial time by LP methods
(see cs170 for a bit of this theory)
6
Varieties of Constraints
ƒ Varieties 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
(We’ll ignore these until we get to Bayes’ nets)
Real-World CSPs
ƒ Assignment problems: e.g., who teaches what class
ƒ Timetabling problems: e.g., which class is offered when
and where?
ƒ Hardware configuration
ƒ Spreadsheets
ƒ Transportation scheduling
ƒ Factory scheduling
ƒ Floorplanning
ƒ Many real-world problems involve real-valued
variables…
7
Standard Search Formulation
ƒ 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
constraints
Search Methods
ƒ What does BFS do?
ƒ What does DFS do?
ƒ [ANIMATION]
ƒ What’s the obvious problem here?
ƒ What’s the slightly-less-obvious problem?
8
Backtracking Search
ƒ Idea 1: Only consider a single variable at each point:
ƒ
ƒ
ƒ
ƒ
Variable assignments are commutative
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
How many leaves are there?
ƒ Idea 2: Only allow legal assignments at each point
ƒ I.e. consider only values which do not conflict previous assignments
ƒ Might have to do some computation to figure out whether a value is ok
ƒ Depth-first search for CSPs with these two improvements is called
backtracking search
ƒ [ANIMATION]
ƒ Backtracking search is the basic uninformed algorithm for CSPs
ƒ Can solve n-queens for n ≈ 25
Backtracking Search
ƒ What are the choice points?
9
Backtracking Example
Improving Backtracking
ƒ General-purpose ideas can give huge gains in
speed:
ƒ
ƒ
ƒ
ƒ
Which variable should be assigned next?
In what order should its values be tried?
Can we detect inevitable failure early?
Can we take advantage of problem structure?
10
Minimum Remaining Values
ƒ Minimum remaining values (MRV):
ƒ Choose the variable with the fewest legal values
ƒ Why min rather than max?
ƒ Called most constrained variable
ƒ “Fail-fast” ordering
Degree Heuristic
ƒ Tie-breaker among MRV variables
ƒ Degree heuristic:
ƒ Choose the variable with the most constraints on
remaining variables
ƒ Why most rather than fewest constraints?
11
Least Constraining Value
ƒ Given a choice of variable:
ƒ Choose the least constraining
value
ƒ The one that rules out the fewest
values in the remaining variables
ƒ Note that it may take some
computation to determine this!
ƒ Why least rather than most?
ƒ Combining these heuristics
makes 1000 queens feasible
Forward Checking
NT
WA
SA
Q
NSW
V
ƒ Idea: Keep track of remaining legal values for
unassigned variables
ƒ Idea: Terminate when any variable has no legal values
12
Constraint Propagation
NT
WA
SA
Q
NSW
V
ƒ 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?
ƒ Constraint propagation repeatedly enforces constraints (locally)
Arc Consistency
NT
WA
SA
Q
NSW
V
ƒ Simplest form of propagation makes each arc consistent
ƒ X → Y is consistent iff for every value x there is some allowed y
ƒ
ƒ
ƒ
ƒ
If X loses a value, neighbors of X need to be rechecked!
Arc consistency detects failure earlier than forward checking
What’s the downside of arc consistency?
Can be run as a preprocessor or after each assignment
13
Arc Consistency
ƒ Runtime: O(n2d3), can be reduced to O(n2d2)
ƒ … but detecting all possible future problems is NP-hard – why?
Problem Structure
ƒ Tasmania and mainland are
independent subproblems
ƒ Identifiable as connected
components of constraint graph
ƒ Suppose each subproblem has c
variables out of n total
ƒ Worst-case solution cost is
O((n/c)(dc)), linear in n
ƒ E.g., n = 80, d = 2, c =20
ƒ 280 = 4 billion years at 10 million
nodes/sec
ƒ (4)(220) = 0.4 seconds at 10 million
nodes/sec
14
Tree-Structured CSPs
ƒ Theorem: if the constraint graph has no loops, the CSP can be
solved in O(n d2) time (next slide)
ƒ Compare to general CSPs, where worst-case time is O(dn)
ƒ This property also applies to logical and probabilistic reasoning: an
important example of the relation between syntactic restrictions and
the complexity of reasoning.
Tree-Structured CSPs
ƒ Choose a variable as root, order
variables from root to leaves such
that every node's parent precedes
it in the ordering
ƒ For i = n : 2, apply RemoveInconsistent(Parent(Xi),Xi)
ƒ For i = 1 : n, assign Xi consistently with Parent(Xi)
ƒ Runtime: O(n d2)
15
Nearly Tree-Structured CSPs
ƒ Conditioning: instantiate a variable, prune its neighbors' domains
ƒ Cutset conditioning: instantiate (in all ways) a set of variables such
that the remaining constraint graph is a tree
ƒ Cutset size c gives runtime O( (dc) (n-c) d2 ), very fast for small c
Iterative Algorithms for CSPs
ƒ Greedy and local methods typically work with “complete”
states, i.e., all variables assigned
ƒ To apply to CSPs:
ƒ Allow states with unsatisfied constraints
ƒ Operators reassign variable values
ƒ Variable selection: randomly select any conflicted
variable
ƒ Value selection by min-conflicts heuristic:
ƒ Choose value that violates the fewest constraints
ƒ I.e., hill climb with h(n) = total number of violated constraints
16
Example: 4-Queens
ƒ
ƒ
ƒ
ƒ
States: 4 queens in 4 columns (44 = 256 states)
Operators: move queen in column
Goal test: no attacks
Evaluation: h(n) = number of attacks
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
17
Summary
ƒ
CSPs are a special kind of search problem:
ƒ
ƒ
States defined by values of a fixed set of variables
Goal test defined by constraints on variable values
ƒ
Backtracking = depth-first search with one legal variable assigned per node
ƒ
Variable ordering and value selection heuristics help significantly
ƒ
Forward checking prevents assignments that guarantee later failure
ƒ
Constraint propagation (e.g., arc consistency) does additional work to constrain
values and detect inconsistencies
ƒ
The constraint graph representation allows analysis of problem structure
ƒ
Tree-structured CSPs can be solved in linear time
ƒ
Iterative min-conflicts is usually effective in practice
Local Search Methods
ƒ Queue-based algorithms keep fallback
options (backtracking)
ƒ Local search: improve what you have until
you can’t make it better
ƒ Generally much more efficient (but
incomplete)
18
Types of Problems
ƒ Planning problems:
ƒ We want a path to a solution
(examples?)
ƒ Usually want an optimal path
ƒ Incremental formulations
ƒ Identification problems:
ƒ We actually just want to know what
the goal is (examples?)
ƒ Usually want an optimal goal
ƒ Complete-state formulations
ƒ Iterative improvement algorithms
Hill Climbing
ƒ Simple, general idea:
ƒ Start wherever
ƒ Always choose the best neighbor
ƒ If no neighbors have better scores than
current, quit
ƒ Why can this be a terrible idea?
ƒ Complete?
ƒ Optimal?
ƒ What’s good about it?
19
Hill Climbing Diagram
ƒ Random restarts?
ƒ Random sideways steps?
Simulated Annealing
ƒ Idea: Escape local maxima by allowing downhill moves
ƒ But make them rarer as time goes on
20
Simulated Annealing
ƒ Theoretical guarantee:
ƒ Stationary distribution:
ƒ If T decreased slowly enough,
will converge to optimal state!
ƒ Is this an interesting guarantee?
ƒ Sounds like magic, but reality is reality:
ƒ The more downhill steps you need to escape, the less
likely you are to every make them all in a row
ƒ People think hard about ridge operators which let you
jump around the space in better ways
Beam Search
ƒ Like greedy search, but keep K states at all
times:
Greedy Search
ƒ
ƒ
ƒ
ƒ
Beam Search
Variables: beam size, encourage diversity?
The best choice in MANY practical settings
Complete? Optimal?
Why do we still need optimal methods?
21
Genetic Algorithms
ƒ Genetic algorithms use a natural selection metaphor
ƒ Like beam search (selection), but also have pairwise
crossover operators, with optional mutation
ƒ Probably the most misunderstood, misapplied (and even
maligned) technique around!
Example: N-Queens
ƒ
ƒ
ƒ
ƒ
Why does crossover make sense here?
When wouldn’t it make sense?
What would mutation be?
What would a good fitness function be?
22
Continuous Problems
ƒ Placing airports in Romania
ƒ States: (x1,y1,x2,y2,x3,y3)
ƒ Cost: sum of squared distances to closest city
Gradient Methods
ƒ How to deal with continous (therefore infinite)
state spaces?
ƒ Discretization: bucket ranges of values
ƒ E.g. force integral coordinates
ƒ Continuous optimization
ƒ E.g. gradient ascent
Image from vias.org
23