Create Presentation
Download Presentation

Download Presentation
## Constraint Satisfaction

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**Constraint Satisfaction**Reading: Russell & Norvig Chapter 5, Kumar: “Algorithms for constraint satisfaction problems: A survey”**Overview**• Constraint Satisfaction Problems (CSP) share some common features and have specialized methods • View a problem as a set of variables to which we have to assign values that satisfy a number of problem-specific constraints. • Constraint solvers, constraint logic programming… • Algorithms for CSP • Backtracking (systematic search) • Constraint propagation (k-consistency) • Variable ordering heuristics • Backjumping and dependency-directed backtracking**Informal Definition of CSP**• CSP = Constraint Satisfaction Problem • Given (1) a finite set of variables (2) each with a domain of possible values (often finite) (3) a set of constraints that limit the values the variables can take on • A solution is an assignment of a value to each variable such that all the constraints are satisfied. • Tasks might be to decide if a solution exists, to find a solution, to find all solutions, or to find the “best solution” according to some metric.**SEND + MORE = MONEY**Assign distinct digits to the letters S, E, N, D, M, O, R, Y such that S E N D + M O R E = M O N E Y holds.**SEND + MORE = MONEY**Solution 9 5 6 7 + 1 0 8 5 = 1 0 6 5 2 Assign distinct digits to the letters S, E, N, D, M, O, R, Y such that S E N D + M O R E = M O N E Y holds.**Modeling**Formalize the problem as a CSP: • number of variables: n • constraints: c1,…,cm n • problem: Find a =(v1,…,vn) nsuch that a ci , for all 1 i m**A Model for MONEY**• number of variables: 8 • constraints: c1={(S,E,N,D,M,O,R,Y) 8 | 0 S,…,Y 9 } c2={(S,E,N,D,M,O,R,Y) 8 | 1000*S + 100*E + 10*N + D + 1000*M + 100*O + 10*R + E = 10000*M + 1000*O + 100*N + 10*E + Y}**A Model for MONEY(continued)**• more constraints c3= {(S,E,N,D,M,O,R,Y) 8 | S 0 } c4= {(S,E,N,D,M,O,R,Y) 8 | M 0 } c5= {(S,E,N,D,M,O,R,Y) 8 | S…Y all different}**Solution for MONEY**c1={(S,E,N,D,M,O,R,Y) 8 | 0S,…,Y9 } c2={(S,E,N,D,M,O,R,Y) 8 | 1000*S + 100*E + 10*N + D + 1000*M + 100*O + 10*R + E = 10000*M + 1000*O + 100*N + 10*E + Y} c3= {(S,E,N,D,M,O,R,Y) 8 | S 0 } c4= {(S,E,N,D,M,O,R,Y) 8 | M 0 } c5= {(S,E,N,D,M,O,R,Y) 8 | S…Y all different} Solution: (9,5,6,7,1,0,8,2)8**E**D A B C Example: Map Coloring • Color the following map using three colors (red, green, blue) such that no two adjacent regions have the same color.**E**E D A B D A B C C Example: Map Coloring • Variables: A, B, C, D, E all of domain RGB • Domains: RGB = {red, green, blue} • Constraints: AB, AC,A E, A D, B C, C D, D E • One solution: A=red, B=green, C=blue, D=green, E=blue =>**N-queens Example (4 in our case)**• Standard test case in CSP research • Variables are the rows: r1, r2, r3, r4 • Values are the columns: {1, 2, 3, 4} • So, the constraints include: • Cr1,r2 = {(1,3),(1,4),(2,4),(3,1),(4,1),(4,2)} • Cr1,r3 = {(1,2),(1,4),(2,1),(2,3),(3,2),(3,4), (4,1),(4,3)} • Etc. • What do these constraints mean?**Example: SATisfiability**• Given a set of propositions containing variables, find an assignment of the variables to {false,true} that satisfies them. • Example, the clauses: • A \/ B \/ ~C, ~A \/ D • (equivalent to C -> A \/ B, A -> D) • Are satisfied by A = false B = true C = false D = false**Scheduling**Temporal reasoning Building design Planning Optimization/satisfaction Vision Graph layout Network management Natural language processing Molecular biology / genomics VLSI design Real-world problems**Formal definition of a CSP**A constraint satisfaction problem (CSP) consists of • a set of variables X = {x1, x2, … xn} • each with an associated domain of values {d1, d2, … dn}. • The domains are typically finite • a set of constraints {c1, c2 … cm} where • each constraint defines a predicate which is a relation over a particular subset of X. • E.g., Ci involves variables {Xi1, Xi2, … Xik} and defines the relation Ri Di1 x Di2 x … Dik • Unary constraint: only involves one variable • Binary constraint: only involves two variables**Formal definition of a CSP**• Instantiations • An instantiation of a subset of variables S is an assignment of a legal domain value to each variable in in S • An instantiation is legal iff it does not violate any (relevant) constraints. • A solution is an instantiation of all of the variables in the network.**Typical Tasks for CSP**• Solutions: • Does a solution exist? • Find one solution • Find all solutions • Given a partial instantiation, do any of the above • Transform the CSP into an equivalent CSP that is easier to solve.**Binary CSP**• A binary CSP is a CSP in which all of the 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 constraint appears as self-referential arc**Binary Constraint Graph**X1 X2 Nodes are Variables {(5,7),(2,2)} X5 {(3,7)} X3 {(1,3),(2,4),(7,6)} Edges are constraints X4**Matrix Representationfor Binary Constraints**X1 X2 {(5,7),(2,2)} X5 {(3,7)} X3 {(1,3),(2,4),(7,6)} X4 Matrix for the constraint between X4 and X5 in the graph**Constraint Solving is Hard**Constraint solving is not possible for general constraints. Example: C: n > 2 C’: an + bn = cn Constraint programming separates constraints into • basic constraints: complete constraint solving • non-basic constraints: propagation (incomplete); search needed**CSP as a Search Problem**States are defined by the values assigned so far • Initial state: the empty assignment { } • Successor function: assign a value to an unassigned variable that does not conflict with current assignment fail if no legal assignments • Goal test: the current assignment is complete • This is the same for all CSPs • Every solution appears at depth n with n variables use depth-first search • Path is irrelevant, so can also use complete-state formulation • Local search methods are useful.**Systematic search: Backtracking(a.k.a. depth-first search)**• Consider the variables in some order • Pick an unassigned variable and give it a provisional value such that it is consistent with all of the constraints • If no such assignment can be made, we’ve reached a dead end and need to backtrack to the previous variable • Continue this process until a solution is found or we backtrack to the initial variable and have exhausted all possible values.**Backtracking search**• Variable assignments are commutative, i.e., [ A = red then B = green ] same as [ B = green then A = red ] • Only need to consider assignments to a single variable at each node b = d and there are dn leaves • Depth-first search for CSPs with single-variable assignments is called backtracking search • Backtracking search is the basic algorithm for CSPs • Can solve n-queens for n≈ 100**1**2 3 4 5 Example: Crossword Puzzle**1**2 3 4 5 Example: XWORD Puzzle • Variables and their domains • X1 is 1 across D1 is 5-letter words • X2 is 2 down D2 is 4-letter words • X3 is 3 down D3 is 3-letter words • X4 is 4 across D4 is 4-letter words • X5 is 5 across D5 is 2-letter words • Constraints (implicit/intensional) • C12 is “the 3rd letter of X1 must equal the 1st letter of X2” • C13 is “the 5th letter of X1 must equal the 1st letter of X3”. • C24 is … • C25 is … • C34 is ...**1**2 3 4 5 Backtracking: XWORD h o s e s u a n X1=hoses X1=laser m e X2=hike … X2=hike … X2=aron X2=same … X3=run X3=sun X3=let … X4=hike X4=same**S **E N D M O R Y S E N D+ M O R E= M O N E Y 0S,…,Y9 S 0 M 0 S…Y all different 1000*S + 100*E + 10*N + D + 1000*M + 100*O + 10*R + E = 10000*M + 1000*O + 100*N + 10*E + Y**Propagate**S E N D+ M O R E= M O N E Y S {0..9} E {0..9} N {0..9} D {0..9} M {0..9} O {0..9} R {0..9} Y {0..9} 0S,…,Y9 S 0 M 0 S…Y all different 1000*S + 100*E + 10*N + D + 1000*M + 100*O + 10*R + E = 10000*M + 1000*O + 100*N + 10*E + Y**Propagate**S E N D+ M O R E= M O N E Y S {1..9} E {0..9} N {0..9} D {0..9} M {1..9} O {0..9} R {0..9} Y {0..9} 0S,…,Y9 S 0 M 0 S…Y all different 1000*S + 100*E + 10*N + D + 1000*M + 100*O + 10*R + E = 10000*M + 1000*O + 100*N + 10*E + Y**Propagate**S E N D+ M O R E= M O N E Y S {9} E {4..7} N {5..8} D {2..8} M {1} O {0} R {2..8} Y {2..8} 0S,…,Y9 S 0 M 0 S…Y all different 1000*S + 100*E + 10*N + D + 1000*M + 100*O + 10*R + E = 10000*M + 1000*O + 100*N + 10*E + Y**Branch**S {9} E {4..7} N {5..8} D {2..8} M {1} O {0} R {2..8} Y {2..8} S E N D+ M O R E= M O N E Y 0S,…,Y9 S 0 M 0 S…Y all different 1000*S + 100*E + 10*N + D + 1000*M + 100*O + 10*R + E = 10000*M + 1000*O + 100*N + 10*E + Y E 4 E = 4 S {9} E {4..7} N {5..8} D {2..8} M {1} O {0} R {2..8} Y {2..8} S {9} E {4..7} N {5..8} D {2..8} M {1} O {0} R {2..8} Y {2..8}**Propagate**S {9} E {4..7} N {5..8} D {2..8} M {1} O {0} R {2..8} Y {2..8} S E N D+ M O R E= M O N E Y 0S,…,Y9 S 0 M 0 S…Y all different 1000*S + 100*E + 10*N + D + 1000*M + 100*O + 10*R + E = 10000*M + 1000*O + 100*N + 10*E + Y E 4 E = 4 S {9} E {5..7} N {6..8} D {2..8} M {1} O {0} R {2..8} Y {2..8}**Branch**S {9} E {4..7} N {5..8} D {2..8} M {1} O {0} R {2..8} Y {2..8} S E N D+ M O R E= M O N E Y E 4 E = 4 S {9} E {5..7} N {6..8} D {2..8} M {1} O {0} R {2..8} Y {2..8} E = 5 E 5 S {9} E {5..7} N {6..8} D {2..8} M {1} O {0} R {2..8} Y {2..8} S {9} E {5..7} N {6..8} D {2..8} M {1} O {0} R {2..8} Y {2..8}**Propagate**S {9} E {4..7} N {5..8} D {2..8} M {1} O {0} R {2..8} Y {2..8} S E N D+ M O R E= M O N E Y E 4 E = 4 S {9} E {5..7} N {6..8} D {2..8} M {1} O {0} R {2..8} Y {2..8} E = 5 E 5 S {9} E {5} N {6} D {7} M {1} O {0} R {8} Y {2} S {9} E {6..7} N {7..8} D {2..8} M {1} O {0} R {2..8} Y {2..8}**Complete**Search Tree S {9} E {4..7} N {5..8} D {2..8} M {1} O {0} R {2..8} Y {2..8} S E N D+ M O R E= M O N E Y E = 4 E 4 S {9} E {5..7} N {6..8} D {2..8} M {1} O {0} R {2..8} Y {2..8} E = 5 E 5 S {9} E {5} N {6} D {7} M {1} O {0} R {8} Y {2} S {9} E {6..7} N {7..8} D {2..8} M {1} O {0} R {2..8} Y {2..8} E 6 E = 6**Problems with backtracking**• Thrashing: keep repeating the same failed variable assignments • Consistency checking can help • Intelligent backtracking schemes can also help • Inefficiency: can explore areas of the search space that aren’t likely to succeed • Variable ordering can help**Improving backtracking efficiency**• General-purpose methods 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?**Most constrained variable**• Most constrained variable: choose the variable with the fewest legal values • a.k.a. minimum remaining values (MRV) heuristic**Most constraining variable**• Tie-breaker among most constrained variables • Most constraining variable: • choose the variable with the most constraints on remaining variables**Least constraining value**• Given a variable, choose the least constraining value: • the one that rules out the fewest values in the remaining variables • Combining these heuristics makes 1000 queens feasible**Forward checking**• Idea: • Keep track of remaining legal values for unassigned variables • Terminate search when any variable has no legal values**Forward checking**• Idea: • Keep track of remaining legal values for unassigned variables • Terminate search when any variable has no legal values**Forward checking**• Idea: • Keep track of remaining legal values for unassigned variables • Terminate search when any variable has no legal values**Forward checking**• Idea: • Keep track of remaining legal values for unassigned variables • Terminate search when any variable has no legal values