Post on 20-Dec-2015
Artificial Intelligence
Constraint satisfactionChapter 5, AIMA
Constraint 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,...
Map 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
Map coloring
• CSP can be visualized with constraint graph– Nodes = variables– Arcs = constraints
Types of constraints
• Unary: Xi vj
• Binary: Xi Xj
• Higher order
• Preferences
CSP cast as a search problem
• Depth-first search (depth limit = n)
Backtracking search
Very inefficient
• Initial state• Successor
function• Goal test• Path cost
(not relevant)
”Vanilla” backtracking
WA
NT
SA
Q
NSW
V
T
WA
NTQ
SA
Animation adapted from V. Pavlovic
V
NSW
Q
WA WA WA
NTNT NT
Q QQ
SA SASA
VV V
NSW NSWNSW
V VV
Improve ”vanilla” Backtracking
1. Smart choice of next variable- Min. remaining values (+ degree heuristic)- Least constraining value
2. Check consequences of choice- Forward checking- Arc consistency
3. Intelligent backtracking- Backjumping
Minimum remaining valuesChoose next the variable that is most constrained based on current
assignment (& choose the one with the highest degree first in ties)
WA
NT
SA
Q
NSW
V
T
WA
NT
SA
Animation adapted from V. Pavlovic
Q
NSW
V
T
Least constraining value
Image borrowed from V. Pavlovic
Select the value that least constrains the other variables (rules our fewest other variables)
Forward checkingDelete values that are inconsistent with the last selection
Animation adapted from V. Pavlovic
Stop search early, if necessary
Arc 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
4-queens
• 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
7888894 0q064% 0 64 0 %640q0 6122223
q1
q4
q3q2
Arc consistency: 4-queens
• qi = queen placed in column i
• {1,2,3,4} = The allowed values (row number) for the queen.
F
{1,2,3,4}
{1,2,3,4}
{1,2,3,4}{1,2,3,4}
q1
q3q2
q4
Example from N.J. Nilsson, ”Artificial Intelligence: A New Synthesis”, 1998
Arc 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
1
{1,2,3,4}
{1,2,3,4}{1,2,3,4}
q1
q3q2
q4
Example from N.J. Nilsson, ”Artificial Intelligence: A New Synthesis”, 1998
7888894q0 0640 0 64 0 0640 0 6122223
Arc 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
1
{1,2,3,4}
{1,2,3,4}{1,2,3,4}
q1
q3q2
q4
Example from N.J. Nilsson, ”Artificial Intelligence: A New Synthesis”, 1998
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.
Arc 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
Example from N.J. Nilsson, ”Artificial Intelligence: A New Synthesis”, 1998
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.
1
{1,2,3,4}
{1,2,3,4}{1,2,3,4}
q1
q3q2
q4
(q2,q1)
Arc 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
Example from N.J. Nilsson, ”Artificial Intelligence: A New Synthesis”, 1998
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.
1
{1,2,3,4}
{1,2,3,4}{1,2,3,4}
q1
q3q2
q4
(q3,q1)
Arc 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
Example from N.J. Nilsson, ”Artificial Intelligence: A New Synthesis”, 1998
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.
1
{1,2,3,4}
{1,2,3,4}{1,2,3,4}
q1
q3q2
q4
(q4,q1)
Arc 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
Example from N.J. Nilsson, ”Artificial Intelligence: A New Synthesis”, 1998
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.
1
{1,2,3,4}
{1,2,3,4}{1,2,3,4}
q1
q3q2
q4
(q3,q2)
Arc 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
q4
Example from N.J. Nilsson, ”Artificial Intelligence: A New Synthesis”, 1998
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.
1
{1,2,3,4}
{1,2,3,4}{1,2,3,4}
q1
q3q2
(q3,q2)
Arc 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
Example from N.J. Nilsson, ”Artificial Intelligence: A New Synthesis”, 1998
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.
1
{1,2,3,4}
{1,2,3,4}{1,2,3,4}
q1
q3q2
q4
(q2,q3)
Arc 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
Example from N.J. Nilsson, ”Artificial Intelligence: A New Synthesis”, 1998
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.
1
{1,2,3,4}
{1,2,3,4}{1,2,3,4}
q1
q3q2
q4
(q2,q3)
Arc 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
Example from N.J. Nilsson, ”Artificial Intelligence: A New Synthesis”, 1998
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.
1
{1,2,3,4}
{1,2,3,4}{1,2,3,4}
q1
q3q2
q4
(q3,q4)
Arc 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
Example from N.J. Nilsson, ”Artificial Intelligence: A New Synthesis”, 1998
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.
1
{1,2,3,4}
{1,2,3,4}{1,2,3,4}
q1
q3q2
q4
(q3,q4)
No solution with q1 = 1
Arc 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
2
{1,2,3,4}
{1,2,3,4}{1,2,3,4}
q1
q3q2
q4
Example from N.J. Nilsson, ”Artificial Intelligence: A New Synthesis”, 1998
7888894 0q064% 0 64 0 %640q0 6122223Arc consistency (constraint propagation) finds the
solution
Local 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
7888894 0 064% 0 64 0q0640q0q6122223
Local search: 4-queens
7888894 0 %64% 0 64 0q0640q0 6122223
Stuck!
Choose random movethat does not increaseconflicts.
7888894 0q%64% 0 64 0 0640q0 6122223
7888894 0q064% 0 64 0 %640q0 6122223
Solution in three moves!
Map coloring: Australia
Using Java code @ AIMA site, calling from within MATLAB.
• Backtracking ~ 1.5 sec
7 variables (countries)9 constraints3 values (R,G,B)
Map coloring: Africa
49 variables (countries)107 constraints4 values (R,G,B,Y)
Map coloring: Africa
Modified the AIMA Java code, calling from within MATLAB.
• Backtracking ~ 5.5 min