artificial intelligence - wordpress.com · best-first search select the best (according to the...
TRANSCRIPT
Artificial intelligence
● Heuristic search● First-order logic● Constraint satisfaction problems
Problems without simple algorithmic solutions
Benoît Lemaire
What is a heuristic
● A criteria to select between several alternatives the most promising to reach a goal
● Usually it works, but it might not!– If you see clouds, take your umbrella
(you might carry it all day long for nothing, but it usually works)
a? b? c?
The 8-queen problem
● Heuristic 1: maximize the number of free cells
● Is there a better heuristic?
The 8-queen problem● Heuristic 1 takes into account the total
number of free cells, but not how they are distributed
>
f(S1)=8 f(S2)=8
a? b? c?
The 8-queen problem
● Heuristic 1: maximize the number of free cells
● Heuristic 2: maximize the number of free cells of the line with the fewest free cells
The taquin game● In the 8Q problem, the final state
was unknown● Here the final state is known
Initial state Final state
2 3
1
7
8 4
56
1 3
8
7
2
4
56
● Suppose an exhaustive search is intractable● The distance to the goal has to be estimated
Final state
5
1 3
8
7
2
4
56
2 3
67
1 48
5
2 3
67
1 4
8
5
2 3
67
1 48
5
2 3
67
1 48
a b c
● Heuristic 1: number of pieces at a wrong position
Final state
5
1 3
8
7
2
4
56
2 3
67
1 48
5
2 3
67
1 4
8
5
2 3
67
1 48
5
2 3
67
1 48
a b c
● Heuristic 1: number of pieces at a wrong position● “a” seems to be the best move
Final state
5
1 3
8
7
2
4
56
2 3
67
1 48
5
2 3
67
1 4
8
5
2 3
67
1 48
5
2 3
67
1 48
a b c
f(a)=2 f(b)=3 f(c)=4
● Heuristic 1: number of pieces at a wrong position● Is there a better heuristic?
Final state
5
1 3
8
7
2
4
56
2 3
67
1 48
5
2 3
67
1 4
8
5
2 3
67
1 48
5
2 3
67
1 48
a b c
● Heuristic 1: number of pieces at a wrong position● Heuristic 2: sum of Manhattan distances between
each piece and its correct position
Final state
5
1 3
8
7
2
4
56
2 3
67
1 48
5
2 3
67
1 4
8
5
2 3
67
1 48
5
2 3
67
1 48
a b c
● Heuristic 2: sum of Manhattan distances between each piece and its correct position
● “a” seems the best move
Final state
5
1 3
8
7
2
4
56
2 3
67
1 48
5
2 3
67
1 4
8
5
2 3
67
1 48
5
2 3
67
1 48
a b c
f(a)=2 f(b)=4 f(c)=4
Search spaces
● We have to find a solution in a space which can be viewed as a tree of states
567
5
2 3
67
1 4
8
5
2 3
67
1 48
5
2 3
67
1 48
5
2 3
67
1 4
8
5
2 3
67
1 48
2 3
67
1 48
5
2 3
67
1 4
8
5
2 3
67
1 48
2 3
67
1 48
5
2 3
67
1 4
8
5
2 3
67
1 48
2 3
67
1 48
5
2 3
67
1 4
8
5
2 3
67
1 48
2 3
67
1 48 ......
Search spaces
● We have to find a solution in a space which can be viewed as a tree of states
root node
Nodes (vertices)
A
children nodes of A
parent node of A
goals
branch (edge)
● This search space can be huge– Checkers: 1040 states
– Chess: 10120 states
● It should be explored in an efficient way● Different techniques are available
Heuristic search methods
● Systematic search– Depth-first search
– Breadth-first search
● Heuristic search– Hill climbing
– Simulated annealing
● Best-first search
Depth-first search
● Backtracking if all subnodes are explored● Implemented with a Last In First Out strategy
(stack)● May not end in case of infinite depth
Breadth-first search
● Implemented with a First In First Out strategy (queue)
● Find the shortest path to a solution
Hill climbing
● Heuristics are necessary to prune the search space
● Hill climbing trusts the heuristic : no backtracking!
● Very efficient: no memorization of the path
Heuristic search: hill climbing
● Similar to climbing a hill in the fog● Works well is there no local maximum
Hill climbing● Works well with a good heuristic (no
local maximum)● Example
– Initial state: 000101
– Final state: 111111
– Legal moves: 2 adjacent numbers can be inverted (01, 10)
– 000101110101110110...
Hill climbing
● Heuristic 1: maximize the number of 1
000101 (2)
110101 (4) 011101 (4) 001001 (2) 000011 (2) 000110 (2)
Hill climbing
● Heuristic 1: maximize the number of 1
000101 (2)
110101 (4) 011101 (4) 001001 (2) 000011 (2) 000110 (2)
101101 (4) 010001 (2) 011011 (4) 011110 (4)000101 (2)
Local maximum!
Hill climbing
● Heuristic 2: minimize the maximum distance between 0
000101 (4)
110101 (2) 011101 (4) 001001 (4) 000011 (3) 000110 (5)
Hill climbing
● Heuristic 2: minimize the maximum distance between 0
000101 (4)
110101 (2) 011101 (4) 001001 (4) 000011 (3) 000110 (5)
000101 (4) 111001 (1) 110011 (1) 110110 (3)101101 (3)
Hill climbing
● Heuristic 2: minimize the maximum distance between 0
000101 (4)
110101 (2) 011101 (4) 001001 (4) 000011 (3) 000110 (5)
000101 (4) 111001 (1) 110011 (1) 110110 (3)101101 (3)
111111 (0)
Simulated annealing
● Analogy with annealing in metallurgy● Use a temperature parameter to control
the choice of the next state– Temperature is slightly decreasing
– High temperature → next state can be far from the current state
– Low temperature → next state is close to the current state
Best-first search
● Select the best (according to the heuristic) move from all nodes already processed
● It is like climbing a hill with several people communicating with mobile phones. The one with the best chance to reach the top is doing the next move.
What is the cost of a solution passing by node n?
● Suppose k(ni,n
j) = minimal cost to go from n
i to n
j
● Let g*(n) = k(no,n)
● Let h*(n) = k(n,Goal)● f*(n) = g*(n) + h*(n)
nO
n
GoalIf we could compute f*(n), we would know which path to follow... Goal
Goal
...but we can only rely on estimates
● g(n): estimate of g*(n)– Number of branches from the root to n
● h(n): estimate of h*(n)– Heuristic function
● f(n) = g(n) + h(n)
5
2 3
67
1 4
8
5
2 3
67
1 4
8
5
2 36
7
1 4
8
5
2 36
7
1 4
8
1+5=6
Heuristic 1:h(n)=number of pieces
at wrong position
1+3=4 1+5=6
5
1 2
67
483
Final state
5
2 3
67
1 4
8
5
2 3
67
1 4
8
5
2 36
7
1 4
8
5
2 36
7
1 4
8
1+5=6
Heuristic 1:h(n)=number of pieces
at wrong position
1+3=4 1+5=6
5
2 3
67
1 4
8
5
2 3
67
1 4
8
5
2 3
67
1 48
2+3=5 2+3=5 2+4=6
5
1 2
67
483
Final state
5
2 3
67
1 4
8
5
2 36
7
1 4
8
5
2 36
7
1 4
8
1+5=6 1+3=4 1+5=6
5
2 3
67
1 4
8
5
2 3
67
1 4
8
5
2 3
67
1 48
2+3=5 2+3=5 2+4=6
5
1 2
67
483
Final state
5
2 3
67 1 4
8
52
3
67
1 4
8
3+3=6 3+4=7
5
2 3
67
1 4
8
5
2 3
67
1 4
8
5
2 3
67
1 48
2+3=5 2+3=5 2+4=6
backtracking!
A* algorithm
● What if ∀n h(n) = 0 ?– The algorithm is a breadth-first search (BFS)
● What if ∀n g(n) = 0 ?
A* algorithm
● What if ∀n h(n) = 0 ?– The algorithm is a breadth-first search (BFS)
● What if ∀n g(n) = 0 ?– The algorithm may be fooled by a wrong heuristic
that leads to a path that never gets to a goal
● If h(n) ≤ h*(n), the algorithm is admissible (it finds the shortest path to the goal). It is an A* algorithm.
● In A*, h has to be always optimistic!
● Heuristic 1: number of pieces at a wrong position● Heuristic 2: sum of Manhattan distances between
each piece and its correct position
Is H1 leading to an admissible algorithm?Is H2 leading to an admissible algorithm?
Which one is better?
A* algorithm
● h(n) ≤ h*(n) (the algorithm is admissible: it finds the shortest path to the goal)
● h(n)=0 (BFS) is admissible but it is too long● h(n)=h*(n) is perfect, but impossible to find● Find h(n) as big as possible but less than h*(n)
Exercise● Go from D to A● Heuristic= minimize Manhattan distance
between current location and goal● Generate the search space
Two-player games
● Example– Two players, one group of 7 objects initially
– Each player has to separate any group into unequal parts. The one who cannot play loses the game.
Player 1 Player 2 Player 1
Player 2 looses!
A tree of possible states
● Odd levels → Player1● Even levels → Player2● Each leaf node is marked:
– +1 if Player1 wins
– -1 if Player1 looses
– 0 (draw)
7
6,1 5,2 4,3
5,1,1 4,2,1 3,2,2 3,3,1
4,1,1,1 3,2,1,1 2,2,2,1
3,1,1,1,1
2,1,1,1,1,1
3,1,1,1,1
P1
P1
P1
P2
P2
P2 +1
-1
+1
A tree of possible states
● Values are propagated up the tree
7
6,1 5,2 4,3
5,1,1 4,2,1 3,2,2 3,3,1
4,1,1,1 3,2,1,1 2,2,2,1
3,1,1,1,1
2,1,1,1,1,1
3,1,1,1,1
P1
P1
P1
P2
P2
P2 +1
-1
+1
A tree of possible states
● Values are propagated up the tree
● Player2 can always win the game!
7
6,1 5,2 4,3
5,1,1 4,2,1 3,2,2 3,3,1
4,1,1,1 3,2,1,1 2,2,2,1
3,1,1,1,1
2,1,1,1,1,1
3,1,1,1,1
P1
P1
P1
P2
P2
P2 +1
-1
+1Player1 can choose between a winning state and a loosing state. Obviously, (5,1,1) is a winning state
The MiniMax algorithm
● Problem: the tree cannot be fully developed● A heuristic function is used at a given level
(at least 12 plies for Deep Blue)● The value of each node is the maximum of
its child nodes, for Player1 (MAX)● The value of each node is the minimum of
its child nodes, for Player2 (MIN)
Example: the tic-tac-toe game
● Heuristic– f(State) = +∞ if Max wins
– f(State) = -∞ if Max looses
– f(State) =
O OX
X
Example: the tic-tac-toe game
● Heuristic– f(State) = +∞ if Max wins
– f(State) = -∞ if Max looses
– f(State) = (number of rows open for Max) - (number of rows open for Min)
O OX
X
O
XX X
X X X X XXO
O OO O X
O
6-5=1 5-5=0 6-5=1 5-5=0 4-5=-1 6-4=2 5-4=1
MAX
MIN
-1 1 -2
1
Best move according tothe heuristic
-1
Algorithm● The algorithm maintains two values, and ● is the minimum score that the maximizing
player is assured of● is the maximum score that the minimizing
player is assured of respectively.● Initially =-∞, =+∞
● When >, the current position cannot be the result of best play by both players and hence need not be explored further