Presentation is loading. Please wait.

Presentation is loading. Please wait.

Constraint Satisfaction Problems

Similar presentations


Presentation on theme: "Constraint Satisfaction Problems"— Presentation transcript:

1 Constraint Satisfaction Problems
Introduction to AI

2 Real World Samples of CSP
Assignment problems e.g., who teaches what class Timetabling problems e.g., which class is offered when and where? Transportation scheduling Factory scheduling

3 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

4 Example: Map-Coloring
Variables WA, NT, Q, NSW, V, SA, T Domains Di = {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)}

5 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

6 Constraint Graph

7 Map Colouring: Example
Consider some “square” countries: How many colours are needed?

8 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!

9 WITH JUST ONE MORE COLOUR IT IS POSSIBLE
Example: 4 colours? WITH JUST ONE MORE COLOUR IT IS POSSIBLE NO COLOUR LEFT!

10 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

11 Constraint Satisfaction Problems
Must be Hot&Sour Soup No Peanuts Chicken Dish Appetizer Total Cost < $30 No Peanuts Pork Dish Vegetable Unary, binary and N-ry constraints. Seafood Rice Not Both Spicy Not Chow Mein Constraint Network

12 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, and others

13 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

14 Map Colouring Firstly add some labels A B C F E D

15 Map Colouring: Constraints
Graphs are more common than maps – so convert to a graph Edge means “share a border” A B C F E D

16 Graph 3-Colouring A B C F E D
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) } A B C F E D

17 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!

18 CSPs vs. Standard Search Problems
state is a "black box“ – any data structure that supports successor function, heuristic function, and goal test CSP: state is defined by variables Xi with values from domain Di 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

19 Backtracking Example

20 Backtracking Example

21 Backtracking Example

22 Backtracking Example

23 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

24 Can we 2-colour a Triangle?
Can we assign values from { r , g } with the following variables and constraints? A C B Obviously not! But how can we see this using search?

25 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 name of node is just the branch variable C=r C=r C=g C=g Fail Fail Fail Fail

26 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

27 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

28 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

29 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.

30 Real-world CSPs Assignment problems Timetabling 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

31 Graph Matching Example Find a subgraph isomorphism from R to S.
1 2 (1,a) (1,b) (1,c) (1,d) (1,e) 3 4 (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?

32 A Tiny Transportation problem
How much should be shipped from several sources to several destinations Demand Qty Supply cpty Source Qty Shipped Destination a 1 x 11 1 b 1 1 x 12 x 1n x 21 a 2 2 2 b 2 x 22 x 2n : : : : a m n b m n

33 Constraint Satisfaction Problems
Must be Hot&Sour Soup No Peanuts Chicken Dish Appetizer Total Cost < $30 No Peanuts Pork Dish Vegetable Unary, binary and N-ry constraints. Seafood Rice Not Both Spicy Not Chow Mein Constraint Network

34 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)

35 The path cost is shown by the number on the links; the heuristic evaluation is shown by the number in the box. Assume that S is the start state and G is the goal state. (a) What is the order that breadth-first search will expand the nodes? (b) What is the order that depth-first search will expand the nodes? (c) What is the order that hill climbing search will expand the nodes? (d) What is the order that A* search will expand the nodes?


Download ppt "Constraint Satisfaction Problems"

Similar presentations


Ads by Google