State-Space Searches
The states of Pac-Man
The ghosts in Pac-Man
Other uses of states zThe Pac-Man game as a whole can be described by a state machine zStates are: Title screen, instructions screen, various game levels, high scores screen zDesigning "by states" is a useful techinque for all kinds of programs, not just games
State-space searches zMany problems in Artificial Intelligence can be describes as a search through a space of states zThere is a start state and one or more goal states; the object is to navigate a path from the start state to some goal state zIf you have an opponent, this isn't just a matter of finding a path zRules control the transition from one state to the next
Tic-Tac-Toe (Naughts and Crosses)
Basic searches zThere are two fundamental kinds of search: yBreadth-first search yDepth-first search zThere are many variations on search methods yThe most important in AI is the A* search zWhen alternating moves with an opponent, a different kind of search is needed yThe most important kind is the alpha-beta search
Breadth-first search
Depth-first search
The basic search algorithm zInitialize: put the start node into OPEN zwhile OPEN is not empty ytake a node N from OPEN yif N is a goal node, report success yput the children of N onto OPEN Report failure zIf OPEN is a stack, this is a depth-first search zIf OPEN is a queue, this is a breadth-first search zOPEN may be a fancier structure
Breadth-first searching zPut 1 on queue zTake 1 off queue; not a goal zPut 2, 3, 4 on queue zTake 2 off queue; not a goal zPut 5, 6, 7 on queue zTake 3 off queue... queue: 1 2, 3, 4 3, 4, 5, 6, 7,... search order: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13
Depth-first searching zPut 1 on stack zTake 1 off stack; not a goal zPut 2, 3, 4 on stack zTake 4 off stack; not a goal zPut 11, 12, 13 on stack zTake 13 off stack... stack: 1 4, 3, 2 13, 12, 11, 3, 2,... search order: 1, 4, 13, 12, 11, 3, 10, 9, 8, 2, 7, 6, 5
Depth-first searches and stacks zA breadth-first search uses a queue; a depth-first search uses a stack zRecursive routines automatically use a stack zIt is possible (and easy) to write a depth-first search using the automatic stack zIf you do a breadth-first search, you have to implement the queue yourself
Recursive depth-first search zboolean search (node N) yif N is a goal node, return true yfor each child C of N xif search(C) succeeds, return true yreturn false
Advantages/disadvantages zIf a breadth-first search succeeds, it finds the nearest possible goal zIf a depth-first search succeeds, it may find an arbitrarily bad path to a goal zA depth-first search requires an amount of memory that is linear in the search depth zA breadth-first search requires an amount of memory that is exponential in the search depth
Best-first searches zIf OPEN is neither a stack nor a queue, but is sorted according to most promising, we have a best-first search Initialize: put the start node into OPEN while OPEN is not empty take a node N from OPEN if N is a goal node, report success put the children of N onto OPEN Report failure
Heuristic searches zHow do we know which nodes are most promising? yAnswer 1: it depends on the problem yAnswer 2: we don't; we have to guess zA heuristic is a "rule of thumb" yIt uses incomplete information to evaluate our possible choices yAlgorithms guarantee a solution; heuristics don't yIt's (almost) always better to have an algorithm
The A* search zThe A* search combines the advantages of breadth-first search (good solutions) with the advantages of depth-first search (low memory requirements) zA* is a kind of heuristic search zWe won't get to it today
The End