Robotics
Robotics
Robotics
South
Australia
Chapter 5 New South Wales
Victoria
Tasmania
Variables W A, N T , Q, N SW , V , SA, T
Domains Di = {red, green, blue}
Constraints: adjacent regions must have different colors
e.g., W A 6= N T (if the language allows this), or
(W A, N T ) ∈ {(red, green), (red, blue), (green, red), (green, blue), . . .}
Chapter 5 1 Chapter 5 4
South
Australia
New South Wales
Victoria
Tasmania
Chapter 5 2 Chapter 5 5
Standard search problem: Binary CSP: each constraint relates at most two variables
state is a “black box”—any old data structure
Constraint graph: nodes are variables, arcs show constraints
that supports goal test, eval, successor
CSP: NT
state is defined by variables Xi with values from domain Di Q
WA
goal test is a set of constraints specifying
allowable combinations of values for subsets of variables SA NSW
Chapter 5 7 Chapter 5 10
Higher-order constraints involve 3 or more variables, ♦ Successor function: assign a value to an unassigned variable
e.g., cryptarithmetic column constraints that does not conflict with current assignment.
⇒ fail if no legal assignments (not fixable!)
Preferences (soft constraints), e.g., red is better than green
often representable by a cost for each variable assignment ♦ Goal test: the current assignment is complete
→ constrained optimization problems
1) This is the same for all CSPs!
2) Every solution appears at depth n with n variables
⇒ use depth-first search
3) Path is irrelevant, so can also use complete-state formulation
4) b = (n − `)d at depth `, hence n!dn leaves!!!!
Chapter 5 8 Chapter 5 11
Chapter 5 9 Chapter 5 12
Backtracking search Backtracking example
Chapter 5 13 Chapter 5 16
Chapter 5 14 Chapter 5 17
Chapter 5 15 Chapter 5 18
Minimum remaining values Forward checking
Minimum remaining values (MRV): Idea: Keep track of remaining legal values for unassigned variables
choose the variable with the fewest legal values Terminate search when any variable has no legal values
WA NT Q NSW V SA T
Chapter 5 19 Chapter 5 22
WA NT Q NSW V SA T
Chapter 5 20 Chapter 5 23
Chapter 5 21 Chapter 5 24
Forward checking Arc consistency
Idea: Keep track of remaining legal values for unassigned variables Simplest form of propagation makes each arc consistent
Terminate search when any variable has no legal values
X → Y is consistent iff
for every value x of X there is some allowed y
WA NT Q NSW V SA T
WA NT Q NSW V SA T
Chapter 5 25 Chapter 5 28
WA NT Q NSW V SA T
WA NT Q NSW V SA T
Chapter 5 26 Chapter 5 29
WA NT Q NSW V SA T WA NT Q NSW V SA T
Chapter 5 27 Chapter 5 30
Arc consistency algorithm Tree-structured CSPs
function AC-3( csp) returns the CSP, possibly with reduced domains
inputs: csp, a binary CSP with variables {X1, X2, . . . , Xn} A E
local variables: queue, a queue of arcs, initially all the arcs in csp
while queue is not empty do B D
(Xi, Xj ) ← Remove-First(queue)
if Remove-Inconsistent-Values(Xi , Xj ) then
for each Xk in Neighbors[Xi] do
C F
add (Xk , Xi) to queue
Theorem: if the constraint graph has no loops, the CSP can be solved in
function Remove-Inconsistent-Values( Xi , Xj ) returns true iff succeeds
O(n d2) time
removed ← false
for each x in Domain[Xi] do Compare to general CSPs, where worst-case time is O(dn)
if no value y in Domain[Xj ] allows (x,y) to satisfy the constraint Xi ↔ Xj
then delete x from Domain[Xi ]; removed ← true This property also applies to logical and probabilistic reasoning:
return removed an important example of the relation between syntactic restrictions
and the complexity of reasoning.
O(n2d3), can be reduced to O(n2d2) (but detecting all is NP-hard)
Chapter 5 31 Chapter 5 34
Chapter 5 32 Chapter 5 35
T T
Chapter 5 33 Chapter 5 36
Iterative algorithms for CSPs Summary
Hill-climbing, simulated annealing typically work with CSPs are a special kind of problem:
“complete” states, i.e., all variables assigned states defined by values of a fixed set of variables
goal test defined by constraints on variable values
To apply to CSPs:
allow states with unsatisfied constraints Backtracking = depth-first search with one variable assigned per node
operators reassign variable values
Variable ordering and value selection heuristics help significantly
Variable selection: randomly select any conflicted variable
Forward checking prevents assignments that guarantee later failure
Value selection by min-conflicts heuristic:
choose value that violates the fewest constraints Constraint propagation (e.g., arc consistency) does additional work
i.e., hillclimb with h(n) = total number of violated constraints to constrain values and detect inconsistencies
The CSP representation allows analysis of problem structure
Tree-structured CSPs can be solved in linear time
Iterative min-conflicts is usually effective in practice
Chapter 5 37 Chapter 5 40
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
Chapter 5 38
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
number of constraints
R=
number of variables
CPU
time
R
critical
ratio
Chapter 5 39