Introduction To Artificial Intelligence in Games
Introduction To Artificial Intelligence in Games
1
AI Search Algorithms
• AI search algorithm is a sequence of steps, which are initially
unknown, to find a solution, which satisfies a number of conditions,
between possible solutions.
2
AI Search Algorithms
Search algorithms are used to:
• Find a path from the start point to the end point (path finding)
• Find a solution for a problem or a game (game playing)
Search Algorithms
Path finding
Uninformed Informed search Game playing
search (Blind) (Heuristic) search
Breadth first search (BFS) Best first search MinMax
Depth first search (DFS) A*
Uniform cost AO*
3
Search Problem
To solve a search problem, we need to define:
• Initial state: the first location (state) we start with.
• Goal state: the final location (state) we target.
• States (Search space): all possible states of the problem (game) to
reach the solution (goal state).
• Actions: possible transitions from one state to another.
• Search tree: visual tree representation of search space, showing
possible solutions from initial state.
4
Example: 8-puzzle
• Initial state
• Goal state(s)
5
Example: 8-puzzle
• Actions: it depends on the current state
6
Example: 8-puzzle
• States: all different states of the game to reach the goal state
4 possible actions
3 possible actions
3 possible actions
2 possible actions
7
Example: Cleaning Robot
8
Example: Cleaning Robot
Problem analyzing
• Initial state: one from the 8 states.
• Actions: Right (R), Left (L), Suck (S)
• Goal state: the room is clean (the
robot is at state 7 or 8).
9
Example: Cleaning Robot
Possible states and actions
Search Tree
10
Search Tree
• Search tree is a tree representation of different states and actions (transitions),
showing possible solutions from initial state.
• It is constructed during the search process.
• Search tree makes the search process easier for finding possible solutions using
the search algorithms.
• However, some complicated problems can’t be described using search tree.
11
Search Tree
Terms
• Nodes: points in hierarchal
levels, each point is a state. root
13
Tree Path
Tree path is a collection of nodes from the root to any leaf of the tree.
S S
a a c
b c b
d
d f
j j
l
Path: S → b → f → l Path: S → a → d → j → n
a b c
g
d f
j
l
E
Solution path: S → b → g → E
16
Generic Search Algorithm
• Fringe = list of nodes generated but not expanded (open nodes).
Steps:
• fringe := {open nodes based on initial state}
• loop:
– if fringe empty, declare failure and exit
– choose and remove a node v from fringe
– check if v’s state is a goal state; if so, declare success
– if not, expand v, insert resulting nodes into fringe
node node
(state) (state)
node node
(state) (state)
18
Searching (Path finding) Techniques
Uninformed Search Informed Search
19
Uninformed search
20
Breadth First Search
Breadth First Search (BFS)
• Nodes are expanded in the same order in which they are
generated
– Fringe can be maintained as a First-In-First-Out (FIFO) queue
1 2
3 4 6
5
7 8
Solution: a → f → g → h
23
Breadth First Search (BFS)
• Time Complexity
– It is the amount of time taken by an algorithm to run, as a function of the input length.
– It depends on how many nodes in the tree
– assume (worst case) that there is 1 goal leaf at the RHS
– So BFS will expand all nodes
= O(n)
– where n is the number of nodes in the tree
1
2 3
4 5 6 7
8 9 10 11 12 13 14 G n
24
Breadth First Search (BFS)
Algorithms require memory to hold temporary data while the 1
Worst case:
1
– Worst case would be storing (n - 1) nodes where all but
the root node are located at the first level.
– So BFS will store (n-1) nodes,
so the space complexity is O(n) 2
a fu t
m
m
m a fu t
a heim
tutt a t
bu
m asse a heim bu asse
m
m m
a s uhe be
fu t a s uhe be fu t che
m m
m
u sbu
u sbu tutt a t
m
che
26
Depth First Search (DFS)
LIFO
or Left first
27
Depth First Search (DFS)
• Depth-First Search (DFS) begins at the root node and search each
branch to it maximum depth till a solution is found
• If greatest depth is reached with not solution, we backtrack till we find
an unexplored branch to follow
• Completeness:
– If cycles (loops) are presented in the graph, DFS will follow these cycles indefinitively.
– If there are no cycles, the algorithm is complete.
– Cycles effects can be limited by imposing a maximal depth of search.
• Optimality:
– The first solution is found and not the shortest path to a solution.
– If we take the closeness as preference, DFS will not optimal.
• The algorithm can be implemented with a Last In First Out (LIFO) stack
28
Depth First Search (DFS)
Example
2
5
6
3 Solution: a → f → g → h
7
4
29
Depth First Search (DFS)
• Time Complexity
– assume (worst case) that there is 1 goal leaf at the RHS
– so DFS will expand all nodes
– Time complexity is the same magnitude as BFS
O(n)
– where n is the number of nodes in the tree
30
Depth First Search (DFS)
• Space Complexity d=0
– It is the amount of space or memory required for
getting a solution d=1
– It depends on how many nodes can be in the O(d)
queue (worst-case) d=2
Best case:
– Best case is when the tree contains all but the root node
located at the first level. O(1)
– So DFS will store the root node besides the current node
so, the space complexity is O(1)
– The best case for DFS is the worst case of BFS 2 3 4 5 6 7 G n
31
BFS or DFS
Select the best algorithm to find the solution for the following:
(a) (b)
root
32
BFS or DFS
Solution
(a)
1
1 2
2 7
root root
1 1
2 3 4 5 6 7
8 2
G
G
34
Variations of DFS
• Depth Limited Search (DLS):
– just like DFS, except never go deeper than some depth d.
– It can solve the problem of infinite path in the DFS.
– In this algorithm, the node at the depth limit will be treated as a dead node.
Advantages:
Depth-limited search is Memory efficient.
Disadvantages: