Title: Artificial Intelligence
1Artificial Intelligence
- Constraint satisfaction
- Chapter 5, AIMA
2Constraint Satisfaction Problem (CSP)
- A set X X1,X2,...,Xn of variables
- Each variable Xi has a nonempty domain Di
v1,v2,...,vk(i) of possible values - A set C C1,C2,...,Cm of constraints
- A solution is a complete assignment of values for
the variables, which satisfies all the
constraints. - Examples Map coloring, scheduling, logistics
(transportation), N-queens, crossword puzzles,...
3Map coloring
(Image borrowed from V. Pavlovic)
- X WA, NT, Q, NSW, V, SA, T
- D R,G,B (for all variables)
- C WA ? NT, WA ? SA, NT ? SA, NT ? Q, SA ? Q,
SA ? NSW, SA ? V, Q ? NSW, NSW ? V
(Neighboring regions must have different colors) - Goal All regions must have a color
4Map coloring
- CSP can be visualized with constraint graph
- Nodes variables
- Arcs constraints
5Types of constraints
- Unary Xi ? vj
- Binary Xi ? Xj
- Higher order
- Preferences
6CSP cast as a search problem
- Depth-first search (depth limit n)?
Backtracking search
- Initial state
- Successor function
- Goal test
- Path cost (not relevant)
Very inefficient
7Vanilla backtracking
NT
Q
WA
NSW
SA
V
Q
Animation adapted from V. Pavlovic
8Improve vanilla Backtracking
- Smart choice of next variable- Min. remaining
values ( degree heuristic)- Least constraining
value - Check consequences of choice- Forward checking-
Arc consistency - Intelligent backtracking- Backjumping
9Minimum remaining values
Choose next the variable that is most constrained
based on current assignment ( choose the one
with the highest degree first in ties)
NT
Q
WA
NSW
SA
V
T
Animation adapted from V. Pavlovic
10Least constraining value
Select the value that least constrains the other
variables (rules our fewest other variables)
Image borrowed from V. Pavlovic
11Forward checking
Delete values that are inconsistent with the last
selection
Stop search early, if necessary
Animation adapted from V. Pavlovic
12Arc consistency
- The arc Xi ? Xj is consistent if for every value
of Xj there exists some valid value of Xi.
- Detects failures sooner than forward checking.
- Use as preprocessor or as check after each move
(MAC)
Animation adapted from V. Pavlovic
134-queens
7888894 0q064 0 64 0 640q0 6122223
q1
q3
q2
q4
- X q1, q2, q3, q4 (qi queen in column i)
- D 1,2,3,4 (row for queen)
- C No queen may attack another queen
- Goal Place all queens on the board
14Arc consistency 4-queens
- qi queen placed in column i
- 1,2,3,4 The allowed values (row number) for
the queen. - F
Example from N.J. Nilsson, Artificial
Intelligence A New Synthesis, 1998
15Arc consistency 4-queens
- qi queen placed in column i
- 1,2,3,4 The allowed values (row number) for
the queen. - First move q1 1
7888894q0 0640 0 64 0 0640 0 6122223
Example from N.J. Nilsson, Artificial
Intelligence A New Synthesis, 1998
16Arc consistency 4-queens
- qi queen placed in column i
- 1,2,3,4 The allowed values (row number) for
the queen. - First move q1 1
7888894q0 0640 0 64 0 0640 0 6122223
The arc (i,j) is consistent if each value at the
tail jof the arc has at least one value at the
head i thatdoes not violate the constraints.
Example from N.J. Nilsson, Artificial
Intelligence A New Synthesis, 1998
17Arc consistency 4-queens
- qi queen placed in column i
- 1,2,3,4 The allowed values (row number) for
the queen. - First move q1 1
7888894q0 0640 0 64 0 0640 0 6122223
The arc (i,j) is consistent if each value at the
tail jof the arc has at least one value at the
head i thatdoes not violate the constraints.
Example from N.J. Nilsson, Artificial
Intelligence A New Synthesis, 1998
18Arc consistency 4-queens
- qi queen placed in column i
- 1,2,3,4 The allowed values (row number) for
the queen. - First move q1 1
7888894q0 0640 0 64 0 0640 0 6122223
The arc (i,j) is consistent if each value at the
tail jof the arc has at least one value at the
head i thatdoes not violate the constraints.
Example from N.J. Nilsson, Artificial
Intelligence A New Synthesis, 1998
19Arc consistency 4-queens
- qi queen placed in column i
- 1,2,3,4 The allowed values (row number) for
the queen. - First move q1 1
7888894q0 0640 0 64 0 0640 0 6122223
The arc (i,j) is consistent if each value at the
tail jof the arc has at least one value at the
head i thatdoes not violate the constraints.
Example from N.J. Nilsson, Artificial
Intelligence A New Synthesis, 1998
20Arc consistency 4-queens
- qi queen placed in column i
- 1,2,3,4 The allowed values (row number) for
the queen. - First move q1 1
7888894q0 0640 0 64 0 0640 0 6122223
The arc (i,j) is consistent if each value at the
tail jof the arc has at least one value at the
head i thatdoes not violate the constraints.
Example from N.J. Nilsson, Artificial
Intelligence A New Synthesis, 1998
21Arc consistency 4-queens
- qi queen placed in column i
- 1,2,3,4 The allowed values (row number) for
the queen. - First move q1 1
7888894q0 0640 0 64 0 0640 0 6122223
q4
The arc (i,j) is consistent if each value at the
tail jof the arc has at least one value at the
head i thatdoes not violate the constraints.
Example from N.J. Nilsson, Artificial
Intelligence A New Synthesis, 1998
22Arc consistency 4-queens
- qi queen placed in column i
- 1,2,3,4 The allowed values (row number) for
the queen. - First move q1 1
7888894q0 0640 0 64 0 0640 0 6122223
The arc (i,j) is consistent if each value at the
tail jof the arc has at least one value at the
head i thatdoes not violate the constraints.
Example from N.J. Nilsson, Artificial
Intelligence A New Synthesis, 1998
23Arc consistency 4-queens
- qi queen placed in column i
- 1,2,3,4 The allowed values (row number) for
the queen. - First move q1 1
7888894q0 0640 0 64 0 0640 0 6122223
The arc (i,j) is consistent if each value at the
tail jof the arc has at least one value at the
head i thatdoes not violate the constraints.
Example from N.J. Nilsson, Artificial
Intelligence A New Synthesis, 1998
24Arc consistency 4-queens
- qi queen placed in column i
- 1,2,3,4 The allowed values (row number) for
the queen. - First move q1 1
7888894q0 0640 0 64 0 0640 0 6122223
The arc (i,j) is consistent if each value at the
tail jof the arc has at least one value at the
head i thatdoes not violate the constraints.
Example from N.J. Nilsson, Artificial
Intelligence A New Synthesis, 1998
25Arc consistency 4-queens
- qi queen placed in column i
- 1,2,3,4 The allowed values (row number) for
the queen. - First move q1 1
7888894q0 0640 0 64 0 0640 0 6122223
The arc (i,j) is consistent if each value at the
tail jof the arc has at least one value at the
head i thatdoes not violate the constraints.
? No solution with q1 1
Example from N.J. Nilsson, Artificial
Intelligence A New Synthesis, 1998
26Arc consistency 4-queens
- qi queen placed in column i
- 1,2,3,4 The allowed values (row number) for
the queen. - First move q1 2
7888894 0q064 0 64 0 640q0 6122223
Arc consistency (constraint propagation) finds
thesolution
Example from N.J. Nilsson, Artificial
Intelligence A New Synthesis, 1998
27Local search Simple and efficient
- Start with initial (invalid) state
- Modify this state, using the min-conflicts
heuristic Select the value that minimizes the
number of conflicts - Continue until a solution is found
28Local search 4-queens
7888894 0 064 0 64 0q0640q0q6122223
7888894 0 64 0 64 0q0640q0 6122223
7888894 0q64 0 64 0 0640q0 6122223
7888894 0q064 0 64 0 640q0 6122223
Stuck!
Choose random movethat does not
increaseconflicts.
Solution in three moves!
29Map coloring Australia
- Using Java code _at_ AIMA site, calling from within
MATLAB. - Backtracking 1.5 sec
7 variables (countries) 9 constraints 3 values
(R,G,B)
30Map coloring Africa
49 variables (countries) 107 constraints 4 values
(R,G,B,Y)
31Map coloring Africa
- Modified the AIMA Java code, calling from within
MATLAB. - Backtracking 5.5 min