constraint satisfaction problems introduction to ai
TRANSCRIPT
Constraint Satisfaction Problems
Introduction to AI
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
•
Constraint Graph
7
Map Colouring: Example
• Consider some “square” countries:
• How many colours are needed?
8
Example: 3 colours?
NO COLOUR
LEFT!
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!
9
Example: 4 colours?
NO COLOUR
LEFT!
WITH JUST ONE MORE COLOUR IT IS POSSIBLE
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
Soup
Total Cost
< $30
Chicken
Dish
Vegetable
RiceSeafood
Pork Dish
Appetizer
Must be
Hot&Sour
No
Peanuts
No
Peanuts
Not
Chow Mein
Not Both
Spicy
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, www.ilog.com, 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
AB
C
FE
D
15
Map Colouring: Constraints
• Graphs are more common than maps – so convert to a graph• Edge means “share a border”
AB
C
FE
D
16
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) }
AB
C
FE
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
• Standard search problem:– 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
B
C D
E
2nd child of A is not created immediately,
just remember that it is there
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
AA=r
B
C
B=r
C=r
Fail
A=g
B=g
C=g
Fail
Etc, etc
All the attempts to generate a satisfying
assignment fail
CC=r
Fail
C=g
Fail
name of node is just the branch variable
26
Early Failure
• Suppose a partial assignment failsi.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
AA=r
BB=r
Fail
A=g
B=g
CC=r
Fail
C=g
Fail
Did not have to try values for C
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
–
––
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
– 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
3 4
e
a
b
c
d
R
S
(1,a) (1,b) (1,c) (1,d) (1,e)
(2,a) (2,b) (2,c) (2,d) (2,e)
(3,a) (3,b) (3,c) (3,d) (3,e) (3,a) (3,b) (3,c) (3,d) (3,e)
(4,a) (4,b) (4,c) (4,d) (4,e)
X X X
X X X X X X X X X
X X X X
How do we formalize this problem?
A Tiny Transportation problem
• How much should be shipped from several sources to several destinations
2222 2222
nnnn
aaaa 1111 bbbb1111 xxxx11111111 1111
aaaa2222 bbbb2222:::: :::: :::: ::::
aaaammmm bbbbnnnn
xxxx1n1n1n1n xxxx12121212
mmmm
11
Supply Supply cptycpty
Supply Supply cptycpty
SourceSourceSourceSource Qty ShippedQty ShippedQty ShippedQty Shipped DestinationDestinationDestinationDestinationDemand Demand Qty Qty
Demand Demand Qty Qty
xxxx22222222xxxx2n2n2n2n
xxxx21212121
33
Constraint Satisfaction Problems
Soup
Total Cost
< $30
Chicken
Dish
Vegetable
RiceSeafood
Pork Dish
Appetizer
Must be
Hot&SourNo
Peanuts
No
Peanuts
Not
Chow Mein
Not Both
Spicy 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)
••••
• 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?
35