0% found this document useful (0 votes)
9 views

Introduction To Artificial Intelligence in Games

cloud computing

Uploaded by

saif elshamy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Introduction To Artificial Intelligence in Games

cloud computing

Uploaded by

saif elshamy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Introduction to

Artificial Intelligence in Games


Lecture 2

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

4 possible actions 3 possible actions 2 possible actions

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

• There are two rooms: right and left.


• Room can be dusty or clean.
• The robot can be in the right or the
left room.
• Three possible actions: Go right, Go
left, or Suck.
• 8 possible states.

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

• Search space: all nodes in the


search tree. node node
(state) (state)
• Root: the root of the tree,
node node node
which is the initial state we (state) (state) (state)
start from. node node
(state) (state)
• Parent: the node that has
branching nodes towards the
dead point dead point dead point dead point
below level. The branching
nodes are called child.
• Dead point (leaves): the node root is a parent for a and b. a is a parent for c and d, and so on.

that has no childs. 12


Search Tree
Terms (Cont.)
• Node expansion: generating
new node related to previous root
nodes.
• Path cost: the cost, from initial node node
(state) (state)
to goal state. The cost can be
related to speed (more speed node node node
(state)
means low cost), distance (state) (state)

(long distance means high node node


(state) (state)
cost), time (more time means
high cost), and so on.
dead point dead point dead point dead point
• Depth: number of steps along
the path from initial state.

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

Root (initial state) Dead point (leaf) Goal state 14


Solution Path
It is a collection of nodes from the root to the goal state.
S

a b c

g
d f

j
l
E
Solution path: S → b → g → E

Root (initial state) Dead point (leaf) Goal state 15


Measuring Searching Performance
• The output from a searching algorithm is either FAILURE or
SOLUTION.
• Searching algorithms are evaluated along the following dimensions:
– Completeness: does it always find a solution if one exists?
– Optimality: does it always find the shortest path solution? (optimal solution is the
solution with minimal cost)
– Time complexity: how long?
– Space complexity: how much memory? (maximum number of nodes in memory)
• Time and space complexity are measured in terms of:
– b: maximum branching factor of the search tree
– d: depth of the shortest path solution
– m: maximum depth of the state space (may be ∞)

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

Key question in search: Which of the generated nodes do we expand


next?
17
Searching Techniques
Search Tree
root

node node
(state) (state)

node node node


(state) (state) (state)

node node
(state) (state)

dead point dead point dead point dead point

18
Searching (Path finding) Techniques
Uninformed Search Informed Search

19
Uninformed search

• Given a state, we only know whether it is a goal state or not


• Cannot say one nongoal state looks better than another
nongoal state
• Can only traverse state space blindly in hope of somehow
hitting a goal state at some point
– Also called blind search
– Blind does not imply unsystematic!

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

• BFS is complete: if a solution exists, BFS will find it


• BFS finds a solution that guaranteed to be the shortest path
possible
• Is BFS optimal?
– Optimal means that the algorithm finds the best solution
– The best here can mean the solution is either the shortest, fastest, cheapest, …
– BFS is optimal if (1) the path cost is concerned with the shortest path, (2) the
actions of a single node have the same cost, and (3) costs increase by depth.
– Therefore, BFS is not necessarily an optimal solution
22
Breadth First Search (BFS)
Example

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

program is in execution. O(n)


• Space Complexity
– It is the amount of space or memory required for
getting a solution
– It depends on how many nodes can be in the 2 3 4 5 6 7 G n
queue (worst-case)

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

Best case: O(1)


– Best case is when the tree contains only 1 element at
3
each level.
– So BFS will store 1 node
so the space complexity is O(1), which means it is
constant regardless of any other parameter. G n
25
Breadth First Search (BFS)
Example
The following is an example of the breadth-first tree obtained by running a BFS on German cities
starting from Frankfurt:

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

Worst case: d=3


– the stack will contain d open nodes, where d is the maximum
depth of the tree d=4
– so the space complexity is O(d)

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

BFS finds the solution faster DFS


33
BFS or DFS
Solution
(b)

root root

1 1
2 3 4 5 6 7

8 2

G
G

BFS DFS finds the solution faster

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:

– It also has a disadvantage of incompleteness.


It is complete if the solution is above the
depth-limit.
– It may not be optimal if the problem has more
than one solution.
35
36

You might also like