Download Presentation
## Constraint Satisfaction Problems

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

**Constraint Satisfaction Problems**Introduction to AI**Map Colouring**• Given a map of “countries”: • Assign a colour to each country such that IF two countries share a border THEN they are given different colours • We can only use a limited number of colours • Alternatively, we must use the minimal possible number of colours**Example: Map-Coloring**• VariablesWA, NT, Q, NSW, V, SA, T • DomainsDi = {red,green,blue} • Constraints: adjacent regions must have different colors • e.g., WA ≠ NT, or (WA,NT) in {(red,green),(red,blue),(green,red), (green,blue),(blue,red),(blue,green)}**Example: Map-Coloring**• Solutions are complete and consistent assignments, e.g., WA = red, NT = green,Q = red,NSW = green,V = red,SA = blue,T = green**Map Colouring: Example**• Consider some “square” countries: • How many colours are needed?**Example: 3 colours?**Take the 3 colours to be red, green, blue How would you be sure that you have not missed out some possible 3 colouring? Need some complete search method! NO COLOUR LEFT!**Example: 4 colours?**WITH JUST ONE MORE COLOUR IT IS POSSIBLE NO COLOUR LEFT!**Motivations**• Map Colouring is a specific problem • so why care? • Map Colouring is a typical “Constraint Satisfaction Problem (CSP)” • CSPs have many uses • scheduling • timetabling • window (task pane) manager in a GUI • and many other common optimization problems with industrial applications**Constraint Satisfaction Problems**Must be Hot&Sour Soup No Peanuts Chicken Dish Appetizer Total Cost < $30 No Peanuts Pork Dish Vegetable Seafood Rice Not Both Spicy Not Chow Mein Constraint Network**Potential Follow-up**“Constraint Programming” • A different programming paradigm • “you tell it what to solve, the system decides how to solve” • a program to solve Sudoku can be only 20 lines of code! • e.g. constraints on each row that all numbers in a row are different is just forall( j in 1..9 ) alldifferent( all(i in 1..9) pos[i,j] ); • Very different from the usual paradigms: • Procedural (Fortran, Pascal, C) • Object-Oriented (C++, Java, C#) • Functional (Lisp, ML, Haskell) • Commercialised by ILOG, www.ilog.com, and others**From Maps to CSPs**• Will convert the map colouring into general idea of a CSP: • Assign values • such that the assigned values satisfy some constraints**B**A C D E F Map Colouring • Firstly add some labels**B**A C D E F Map Colouring: Constraints • Graphs are more common than maps – so convert to a graph • Edge means “share a border”**B**A C D E F Graph 3-Colouring • Node, or “variable”, must be given a value from the set of colours { r, g , b } • E.g. B := b • Edge between two nodes only allowed pairs of values from the set{ (r,g), (r,b), (g, r), (g, b), (b, r), (b, g) }**Search Methods**• If want a complete search method then it is standard to use depth-first search with partial assignments • Work with Partial Assignments • Start with the empty assignment • Generate children by adding a value for one more variable • Analogy: path-finding – we started with the empty path, and then added one more segment at each time • We already did this with the map colouring at the start of the lecture!**Backtracking Search Animation**• Nodes are not created until they are ready to be used – to reduce memory usage A 2nd child of A is not created immediately,just remember that it is there B E D C**A**B C Can we 2-colour a Triangle? • Can we assign values from { r , g } with the following variables and constraints? • Obviously not! • But how can we see this using search?**name of node is just the branch variable**Can we 2-colour a Triangle? • Can we assign values from { r , g } to nodes A, B, and C • “Generate-and-Test”: Colour nodes in the order, A, B, C A A=g A=r All the attempts to generate a satisfying assignment fail B B=r B=g Etc, etc C C C=r C=r C=g C=g Fail Fail Fail Fail**Early Failure**• Suppose a partial assignment fails i.e. violates a constraint • Whatever values we eventually give to the so-far un-assigned variables the constraint will stay violated, and the solution will fail • In the backtracking search: • as soon as a constraint is violated by the current partial assignment then we can prune the search**Can we 2-colour a Triangle?**• Naive Backtracking Search: • Backtracking with pruning of bad partial assignments A A=g A=r B B=r B=g C Fail C=r Did not have to try values for C C=g Fail Fail**Varieties of constraints**• Unary constraints involve a single variable, • e.g., SA ≠ green • Binary constraints involve pairs of variables, • e.g., value(SA) ≠ value(WA) • Higher-order constraints involve 3 or more variables, • e.g., cryptarithmetic column constraints**Example: Latin Squares Puzzle**X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 X16 red RT RS RC RO green GT GS GC GO blue BT BS BC BO yellow YT YS YC YO Variables Values Constraints: In each row, each column, each major diagonal, there must be no two markers of the same color or same shape.**Real-world CSPs**• Assignment problems • e.g., who teaches what class • Timetabling problems • e.g., which class is offered when and where? • Transportation scheduling • Factory scheduling Notice that many real-world problems involve real-valued variables**Graph Matching Example**Find a subgraph isomorphism from R to S. R 2 1 (1,a) (1,b) (1,c) (1,d) (1,e) 4 3 (2,a) (2,b) (2,c) (2,d) (2,e) X X X S e (3,a) (3,b) (3,c) (3,d) (3,e) (3,a) (3,b) (3,c) (3,d) (3,e) X X X X X X X X X a c (4,a) (4,b) (4,c) (4,d) (4,e) X X X X b d How do we formalize this problem?**Example: 4-Queens**• States: 4 queens in 4 columns (44 = 256 states) • Actions: move queen in column • Goal test: no attacks • Evaluation: h(n) = number of attacks • Given random initial state, can solve n-queens in almost constant time for arbitrary n with high probability (e.g., n = 10,000,000)