Artificial Intelligence Lecture 3
The problem Depth first Not optimal Uses O(n) space Optimal Uses O(B n ) space Can we combine the advantages of both approaches? 2
Iterative deepening (IDA) Let M be a maximum depth. Run depth first, but only until the given depth. Repeat for increasing values of M. M=1, M=2... 3
Iterative deepening def idasearch( start, goal ): M = 0 ; l=[] while 1: if l == []: M = M+1 ; l = [[start]] path = l.pop() if path[ 1] == goal: return path if len(path) > M: continue choices = nextstates( path[ 1] ) for c in choices: if c not in path: l = [path+[c]] + l return [] 4
Iterative deepening : water jugs problem >>> idasearch( (0,0), (2,0) ) [(0, 0), (0, 3), (3, 0), (3, 3), (4, 2), (0, 2), (2, 0)] >>> idasearch( (0,0), (0,1) ) [(0, 0), (4, 0), (1, 3), (1, 0), (0, 1)] How many nodes where visited? >>> idasearch ( (0,0), (0,2) ) 73 nodes visited [(0, 0), (0, 3), (3, 0), (3, 3), (4, 2), (0, 2)] >>> dfsearch ( (0,0), (0,2) ) 25 nodes visited [(0, 0), (0, 3), (3, 0), (3, 3), (4, 2), (0, 2)] 5
Analysing iterative deepening Completeness Yes, will return the best (shortest) solution Space complexity O(n) Time complexity Each iteration of M takes: O(B M ) time Total time: O(B 1 )+O(B 2 )+...+O(B N ) = O(B N ) That's the same complexity as both df and breadth first! In practice, if we skip the big O notation IDA takes B times longer Use breadth first if we have enough memory, otherwise IDA Using too much memory causes swapping which is slow! 6
Heuristic search Basic idea Use some domain knowledge to create a heuristic that tells how close to the goal a solution is. Example: In navigation, count the distance to the destination Heuristic does not have to be perfect, only give a rough guide to how good/bad a solution is When searching, expand first the nodes that have a good heuristic value 7
Use a cost function: f(n) = g(n) + h(n) g(n): cost from root node to this node h(n): admissible heuristic cost from this node to goal Admissible heuristic: must never overestimate the distance to the goal For each node, keep track of cost f(n) Expand the node n that have the lowest cost Compute cost of children. Insert sorted into list of nodes Sort explicitly (inefficient) or, Iterate over list and insert at right place 8
The efficiency of A* depends on the heuristic Provides a good way of combining domain knowledge with general search. Provides optimal solutions iff heuristic is admissible Time complexity In worst case: O(B n ) Space complexity In worst case: O(B n ) 9
Games Search for games have the small problem, that we do not know what our opponents will do. Eg. chess, tic tac toe,... Search algorithms Min max Alpha beta pruning 10
Example: Tic Tac Toe 11
Solution to Tic Tac Toe Represent current state as an integer Create table with best move for each state 19683 possible game states How do we write that table? Generalize the problem? 12
Another solution to tic tac toe Create ad hoc algorithm Blank=2, X=3, O=5 Make2: return center if it is empty, otherwise a non empty non corner. Posswin: return the square that constitues a winning move. (if product is 18 then X can win, if product is 50 then 0 can win). 13
Another solution to tic tac toe Easier to understand and change Total strategy have to be figured out Not generalizable 14
A general and systematic solution Try every combination of moves, assume that your opponent is a genius. Don't make moves allowing him to win. 15
Example search space Rectangle = our move, circle = opponents move We win if get a positive score What should we do? 16
Min Max Keep track of whose turn it is in each state Each players move called one ply. Compute the score for each state If someone has won, give +/ infinity as score If our move, the max of any children If his move, the min of any children Expensive to perform full search of whole game tree. Stop at a certain depth Give a meaningfull score for that position (heuristic!) eg. In chess: value of all pieces taken plus extra points for strategic positions such as centre 17
Observations Not all games suitable to represent by these rules Random outcomes Large branching factors (eg. Go) Pruning the search tree 18
Min max for Reversi Use given representation of states Function generating legal moves about 4 10 choices each turn Search 3 4 ply's Heuristic at leaf nodes eg. AI score Human score eg. Extra points for corners 19
Alpha beta pruning Reduces the size of the search tree Can we compute the final score without expanding node D? 20
Alpha beta pruning continued Alpha: Best choise for a MAX node so far. Beta: Best choise for a MIN node so far. Unbound MAX nodes have alpha = infinity Unbound MIN nodes have beta = +infinity Alpha pruning: Cut of under MIN nodes having beta value < alpha value of any of it's MAX node ancestors. Beta pruning: Cut of under MAX nodes having alpha value > beta value of any of it's MIN node ancestors. 21
Alpha beta pruning in reversi AI vs. AI: 4 ply search 11m46s for example game Alpha beta prunning 6m 36s for same game 22