Russell & Norvig Ch. 5: - Constraint Satisfaction Offers A Powerful Problem-Solving Paradigm
Russell & Norvig Ch. 5: - Constraint Satisfaction Offers A Powerful Problem-Solving Paradigm
! 8 variables Xi, i = 1 to 8 where Xi is the row number of queen in column i. ! Domain for each variable {1,2,,8} ! Constraints are of the forms: !Xi = k " Xj ! k for all j = 1 to 8, j!i !Xi = ki, Xj = kj "|i-j| !| ki - kj| for all j = 1 to 8, j!i
T1 T2 T3 T4
Examples of scheduling constraints: ! T1 must be done during T3 ! T2 must be achieved before T1 starts ! T2 must overlap with T3 ! T4 must start after T1 is complete
Map coloring
! Variables: A, B, C, D, E all of domain RGB ! Domains: RGB = {red, green, blue} ! Constraints: A!B, A!C,A ! E, A ! D, B ! C, C ! D, D ! E ! A solution: A=red, B=green, C=blue, D=green, E=blue
E A B
=>
E D C A B
Example: SATisfiability
! Given a set of propositions containing variables, find an assignment of the variables to {false, true} that satisfies them. ! For example, the clauses: !(A " B " C) # ( A " D) !(equivalent to (C $ A) " (B # D $ A) are satisfied by A = false, B = true, C = false, D = false ! 3SAT is known to be NP-complete; in the worst case, solving CSP problems requires exponential time
!There are ~190 countries in the world, which we can color using four colors !4190 is a big number!
Real-world problems
CSPs are a good match for many practical problems that arise in the real world ! Scheduling ! Temporal reasoning ! Building design ! Planning ! Optimization/satisfaction ! Vision ! Graph layout ! Network management ! Natural language processing ! Molecular biology / genomics ! VLSI design
Formal definition of a CN
!Instantiations
Binary constraints
NT WA SA V T
T1
Q NSW
T2 T3
T4
!An instantiation of a subset of variables S is an assignment of a value in its domain to each variable in S !An instantiation is legal iff it does not violate any constraints.
! Two variables are adjacent or neighbors if they are connected by an edge or an arc ! Its possible to rewrite problems with higher-order constraints as ones with just binary constraints
Binary CSP
! A binary CSP is a CSP where all constraints are binary or unary ! Any non-binary CSP can be converted into a binary CSP by introducing additional variables ! A binary CSP can be represented as a constraint graph, which has a node for each variable and an arc between two nodes if and only there is a constraint involving the two variables !Unary constraints appear as self-referential arcs
NT Q WA SA V T NSW WA
NT
Q NSW V T
SA
! Seven variables {WA,NT,SA,Q,NSW,V,T} ! Each variable has the same domain {red, green, blue} ! No two adjacent variables have the same value: WA!NT, WA!SA, NT!SA, NT!Q, SA!Q, SA!NSW, SA!V,Q!NSW, NSW!V
! Solutions are complete and consistent assignments ! One of several solutions ! Note that for generality, constraints can be expressed as relations, e.g., WA ! NT is
(WA,NT) in {(red,green), (red,blue), (green,red), (green,blue), (blue,red),(blue,green)}
Backtracking example
Backtracking example
Backtracking example
Backtracking example
! Return failure
Start with CSP-BACKTRACKING({}) Note: this is depth first search; can solve n-queens problems for n ~ 25
Forward Checking
After a variable X is assigned a value v, look at each unassigned variable Y connected to X by a constraint and delete from Ys domain values inconsistent with v
Using forward checking and backward checking roughly doubles the size of N-queens problems that can be practically solved
Forward checking
Forward checking
! Keep track of remaining legal values for unassigned variables ! Terminate search when any variable has no legal values
Forward checking
Forward checking
Constraint propagation
! 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!
Arc consistency
! Simplest form of propagation makes each arc consistent ! X !Y is consistent iff for every value x of X there is some allowed y
Arc consistency
! Simplest form of propagation makes each arc consistent ! X !Y is consistent iff for every value x of X there is some allowed y
Arc consistency
Arc consistency
! Arc consistency detects failure earlier than forward checking ! Can be run as a preprocessor or after each assignment
Algorithm AC3 ! contradiction # false ! Q # stack of all variables ! while Q is not empty and not contradiction do ! X # UNSTACK(Q) ! For every variable Y adjacent to X do ! If REMOVE-ARC-INCONSISTENCIES(X,Y) then ! If Ys domain is non-empty then STACK(Y,Q) ! Else return false
Complexity of AC3
! ! ! ! e = number of constraints (edges) d = number of values per variable Each variables is inserted in Q up to d times REMOVE-ARC-INCONSISTENCY takes O(d2) time ! CP takes O(ed3) time
X1 {1,2,3,4}
X2 {1,2,3,4}
X3 {1,2,3,4}
X4 {1,2,3,4}
1 1 2
! Search: !can find good solutions, but must examine non-solutions along the way ! Constraint Propagation: !can rule out non-solutions, but this is not the same as finding solutions ! Interweave constraint propagation & search: !Perform constraint propagation at each search step
3 4
1 1 2 3 4
4 1 2 3 4
4 1 2 3 4
1 1 2 3 4
X1 {1,2,3,4}
X2 {1,2,3,4}
1 2 3 4
X1 {1,2,3,4}
X2 {1,2,3,4}
X3 {1,2,3,4}
X4 {1,2,3,4}
X3 {1,2,3,4}
X4 {1,2,3,4}
1 1 2 3 4
X1 {1,2,3,4}
X2 {1,2,3,4}
1 2 3 4
X1 {1,2,3,4}
X2 {1,2,3,4}
X3 {1,2,3,4}
X4 {1,2,3,4}
X3 {1,2,3,4}
X4 {1,2,3,4}
1 1 2 3 4
X1 {1,2,3,4}
X2 {1,2,3,4}
1 2 3 4
X1 {1,2,3,4}
X2 {1,2,3,4}
X3 {1,2,3, }
X4 {1,2,3,4}
X3 {1,2,3,4}
X4 {1,2,3,4}
1 1 2 3 4
X1 {1,2,3,4}
X2 {1,2,3,4}
1 2 3 4
X1 {1,2,3,4}
X2 {1,2,3,4}
X3 {1,2,3,4}
X4 {1,2,3,4}
X3 {1,2,3,4}
X4 {1,2,3,4}
1 1 2 3 4
X1 {1,2,3,4}
X2 {1,2,3,4}
1 2 3 4
X1 {1,2,3,4}
X2 {1,2,3,4}
X3 {1,2,3,4}
X4 {1,2,3,4}
X3 {1,2,3,4}
X4 {1,2,3,4}
Sudoku Example
X1 {1,2,3,4} X2 {1,2,3,4}
1 1 2 3 4
X3 {1,2,3,4}
X4 {1,2,3,4}
Backtracking Performance
3 1 3 1 2 3 0 1 3 1
12
16
20
24
28
32
Number of Queens
Time in seconds
Number of Queens
K-consistency
! K- consistency generalizes arc consistency to sets of more than two variables.
!A graph is K-consistent if, for legal values of any K-1 variables in the graph, and for any Kth variable Vk, there is a legal value for Vk
Why do we care?
1.! If we have a CSP with N variables that is known to be strongly N-consistent, we can solve it without backtracking 2.! For any CSP that is strongly Kconsistent, if we find an appropriate variable ordering (one with small enough branching factor), we can solve the CSP without backtracking
! Strong K-consistency = J-consistency for all J<=K ! Node consistency = strong 1-consistency ! Arc consistency = strong 2-consistency ! Path consistency = strong 3-consistency
Intelligent backtracking
! Backjumping: if Vj fails, jump back to the variable Vi with greatest i such that the constraint (Vi, Vj) fails (i.e., most recently instantiated variable in conflict with Vi) ! Backchecking: keep track of incompatible value assignments computed during backjumping ! Backmarking: keep track of which variables led to the incompatible variable assignments for improved backchecking