AI Search Methods

Overview

Recently I am learning Berkley AI Pac-man Project, and it requires to use Search methods for finding fixed food dots, this article is used to record the learning process.

pacman-game

Uninformed Search Methods

Unlike Breadth-first Search (BFS) is to try all the possibilities of a vertex before accessing the next vertex., DFS is from vertex to vertex. After every possible attempt at a given vertex, it goes back to the last vertex to try the next vertex.
Algorithm template for DFS:

1
2
3
4
5
6
7
void Dfs(Vertex V)
{
Visited[v] = True;
for each W adjacent to V
if(!Visited[W])
Dfs(W);
}

Illustration: (Nodes at depth 3 are assumed to have no successors)
AI-DFS

DFS is like a stack, Last in first out (LIFO), and the successors in searchAgent.py is described as

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def getSuccessors(self, state):
"""
Returns successor states, the actions they require, and a cost of 1.

As noted in search.py:
For a given state, this should return a list of triples,
(successor, action, stepCost), where 'successor' is a
successor to the current state, 'action' is the action
required to get there, and 'stepCost' is the incremental
cost of expanding to that successor
"""

successors = []
for action in [Directions.NORTH, Directions.SOUTH, Directions.EAST, Directions.WEST]:
x,y = state
dx, dy = Actions.directionToVector(action)
nextx, nexty = int(x + dx), int(y + dy)
if not self.walls[nextx][nexty]:
nextState = (nextx, nexty)
cost = self.costFn(nextState)
successors.append( ( nextState, action, cost) )

# Bookkeeping for display purposes
self._expanded += 1
if state not in self._visited:
self._visited[state] = True
self._visitedlist.append(state)

return successors

West is the last direction appending to the successors[], so that the agent will turn left at every crossings with DFS algorithm, just like the gif below:
AI-DFS
It is very obvious that DFS algorithm is not optimal nor complete.

AI-BFS

TBC…

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2018-2022 James Wang
  • Visitors: | Views:

Buy me a coffee~

支付宝
微信