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

6CS4 AI Unit-2

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

6CS4 AI Unit-2

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 77

ARTIFICIAL INTELLIGENCE

(6CS4-05)

Unit II
Game Playing

By: Er. Shweta Saraswat


Game playing

Game Playing is an important domain of artificial


intelligence. Games don’t require much knowledge;
the only knowledge we need to provide is the rules,
legal moves and the conditions of winning or losing the
game.
Game playing

Both players try to win the game. So, both of them try to
make the best move possible at each turn. Searching
techniques like BFS(Breadth First Search) are not
accurate for this as the branching factor is very high, so
searching will take a lot of time. So, we need another
search procedures that improve –
MINIMAX
MINIMAX

The most common search technique in game playing


is Minimax search procedure. It is depth-first depth-
limited search procedure.
Min-Max algorithm is mostly used for game playing in
AI. Such as Chess, Checkers, tic-tac-toe, go, and various
tow-players game. This Algorithm computes the minimax
decision for the current state.
MINIMAX

•Mini-max algorithm is a recursive or backtracking algorithm


which is used in decision-making and game theory.
•It provides an optimal move for the player assuming that
opponent is also playing optimally.
•Mini-Max algorithm uses recursion to search through the
game-tree.
•In this algorithm two players play the game, one is called
MAX and other is called MIN.
MINIMAX

•Both the players fight it as the opponent player gets the minimum
benefit while they get the maximum benefit.
•Both Players of the game are opponent of each other, where MAX
will select the maximized value and MIN will select the minimized
value.
•The minimax algorithm performs a depth-first search algorithm for
the exploration of the complete game tree.
•The minimax algorithm proceeds all the way down to the terminal
node of the tree, then backtrack the tree as the recursion.
Minimax algorithm uses two functions –

MOVEGEN : It generates all the possible moves that


can be generated from the current position.
STATICEVALUATION : It returns a value depending
upon the goodness from the viewpoint Of two-player
Working of Min-Max Algorithm:
The working of the minimax algorithm can be easily described using an
example. Below we have taken an example of game-tree which is
representing the two-player game.
In this example, there are two players one is called Maximizer and other is
called Minimizer.
Maximizer will try to get the Maximum possible score, and Minimizer will
try to get the minimum possible score.
This algorithm applies DFS, so in this game-tree, we have to go all the way
through the leaves to reach the terminal nodes.
At the terminal node, the terminal values are given so we will compare
those value and backtrack the tree until the initial state occurs. Following
are the main steps involved in solving the two-player game tree:
Example
Example

Step-1: In the first step, the algorithm generates the entire


game-tree and apply the utility function to get the utility
values for the terminal states. In the below tree diagram,
let's take A is the initial state of the tree. Suppose
maximizer takes first turn which has worst-case initial
value =- infinity, and minimizer will take next turn which
has worst-case initial value = +infinity.
Example

Step 2: Now, first we find the utilities value for the Maximizer,
its initial value is -∞, so we will compare each value in terminal
state with initial value of Maximizer and determines the higher
nodes values. It will find the maximum among the all.
For node D         max(-1,- -∞) => max(-1,4)= 4
For Node E         max(2, -∞) => max(2, 6)= 6
For Node F         max(-3, -∞) => max(-3,-5) = -3
For node G         max(0, -∞) = max(0, 7) = 7
Step 2
example

Step 3: In the next step, it's a turn for minimizer, so it will
compare all nodes value with +∞, and will find the
3rd layer node values.
For node B= min(4,6) = 4
For node C= min (-3, 7) = -3
Step 3
example

Step 4: Now it's a turn for Maximizer, and it will again


choose the maximum of all nodes value and find the
maximum value for the root node. In this game tree, there
are only 4 layers, hence we reach immediately to the root
node, but in real games, there will be more than 4 layers.
For node A max(4, -3)= 4
Step 4
Properties of Mini-Max algorithm:

Complete- Min-Max algorithm is Complete. It will definitely find a


solution (if exist), in the finite search tree.
Optimal- Min-Max algorithm is optimal if both opponents are playing
optimally.
Time complexity- As it performs DFS for the game-tree, so the time
complexity of Min-Max algorithm is O(bm), where b is branching
factor of the game-tree, and m is the maximum depth of the tree.
Space Complexity- Space complexity of Mini-max algorithm is also
similar to DFS which is O(bm).
Limitation of the minimax Algorithm

The main drawback of the minimax algorithm is that it


gets really slow for complex games such as Chess, go,
etc. This type of games has a huge branching factor, and
the player has lots of choices to decide. This limitation of
the minimax algorithm can be improved from alpha-beta
pruning which we will discuss in the next topic.
Example 2:

This algorithm is a two player game, so we call the first player as


PLAYER1 and second player as PLAYER2. The value of each node is
backed-up from its children. For PLAYER1 the backed-up value is the
maximum value of its children and for PLAYER2 the backed-up value
is the minimum value of its children. It provides most promising move
to PLAYER1, assuming that the PLAYER2 has make the best move. It
is a recursive algorithm, as same procedure occurs at each level.
Example 2
Figure 1: Before backing-up of values
Example

Figure 2: After backing-up of values


ALPHA-BETA
PRUNING
Alpha-Beta Pruning

•Alpha-beta pruning is a modified version of the minimax algorithm. It is an


optimization technique for the minimax algorithm.
•As we have seen in the minimax search algorithm that the number of game
states it has to examine are exponential in depth of the tree. Since we cannot
eliminate the exponent, but we can cut it to half. Hence there is a technique
by which without checking each node of the game tree we can compute the
correct minimax decision, and this technique is called pruning.
•This involves two threshold parameter Alpha and beta for future expansion,
so it is called alpha-beta pruning. It is also called as Alpha-Beta
Algorithm.
Alpha-Beta Pruning

Alpha-beta pruning can be applied at any depth of a tree, and sometimes it not only
prune the tree leaves but also entire sub-tree.
The two-parameter can be defined as:
Alpha: The best (highest-value) choice we have found so far at any point along the
path of Maximizer. The initial value of alpha is -∞.
Beta: The best (lowest-value) choice we have found so far at any point along the
path of Minimizer. The initial value of beta is +∞.
The Alpha-beta pruning to a standard minimax algorithm returns the same move as
the standard algorithm does, but it removes all the nodes which are not really
affecting the final decision but making algorithm slow. Hence by pruning these
nodes, it makes the algorithm fast.
Condition for Alpha-beta pruning:

The main condition which required for alpha-beta pruning


is:
α>=β  
Key points about alpha-beta pruning:

•The Max player will only update the value of alpha.


•The Min player will only update the value of beta.
•While backtracking the tree, the node values will be
passed to upper nodes instead of values of alpha and beta.
•We will only pass the alpha, beta values to the child
nodes.
Working of Alpha-Beta Pruning:

Let's take an example of two-player search tree to


understand the working of Alpha-beta pruning
Step 1: At the first step the, Max player will start first
move from node A where α= -∞ and β= +∞, these value
of alpha and beta passed down to node B where again α=
-∞ and β= +∞, and Node B passes the same value to its
child D.
Step 1
example

Step 2: At Node D, the value of α will be calculated as its


turn for Max. The value of α is compared with firstly 2
and then 3, and the max (2, 3) = 3 will be the value of α at
node D and node value will also 3.
example

Step 3: Now algorithm backtrack to node B, where the


value of β will change as this is a turn of Min, Now β=
+∞, will compare with the available subsequent nodes
value, i.e. min (∞, 3) = 3, hence at node B now α= -∞, and
β= 3.
step3
example

In the next step, algorithm traverse the next successor of Node


B which is node E, and the values of α= -∞, and β= 3 will also
be passed.
Step 4: At node E, Max will take its turn, and the value of
alpha will change. The current value of alpha will be compared
with 5, so max (-∞, 5) = 5, hence at node E α= 5 and β= 3,
where α>=β, so the right successor of E will be pruned, and
algorithm will not traverse it, and the value at node E will be 5.
step4
step5

Step 5: At next step, algorithm again backtrack the tree,


from node B to node A. At node A, the value of alpha will
be changed the maximum available value is 3 as max (-∞,
3)= 3, and β= +∞, these two values now passes to right
successor of A which is Node C.
At node C, α=3 and β= +∞, and the same values will be
passed on to node F.
example

Step 6: At node F, again the value of α will be compared


with left child which is 0, and max(3,0)= 3, and then
compared with right child which is 1, and max(3,1)= 3
still α remains 3, but the node value of F will become 1.
step6
example

Step 7: Node F returns the node value 1 to node C, at C


α= 3 and β= +∞, here the value of beta will be changed, it
will compare with 1 so min (∞, 1) = 1. Now at C, α=3 and
β= 1, and again it satisfies the condition α>=β, so the next
child of C which is G will be pruned, and the algorithm
will not compute the entire sub-tree G.
step7
example

Step 8: C now returns the value of 1 to A here the best


value for A is max (3, 1) = 3. Following is the final game
tree which is the showing the nodes which are computed
and nodes which has never computed. Hence the optimal
value for the maximizer is 3 for this example.
step8
Move Ordering in Alpha-Beta pruning:

The effectiveness of alpha-beta pruning is highly dependent on the order in which each node
is examined. Move order is an important aspect of alpha-beta pruning.
It can be of two types:
Worst ordering: In some cases, alpha-beta pruning algorithm does not prune any of the
leaves of the tree, and works exactly as minimax algorithm. In this case, it also consumes
more time because of alpha-beta factors, such a move of pruning is called worst ordering. In
this case, the best move occurs on the right side of the tree. The time complexity for such an
order is O(bm).
Ideal ordering: The ideal ordering for alpha-beta pruning occurs when lots of pruning
happens in the tree, and best moves occur at the left side of the tree. We apply DFS hence it
first search left of the tree and go deep twice as minimax algorithm in the same amount of
time. Complexity in ideal ordering is O(bm/2).
Rules to find good ordering:

Following are some rules to find good ordering in alpha-beta pruning:


Occur the best move from the shallowest node.
Order the nodes in the tree such that the best nodes are checked first.
Use domain knowledge while finding the best move. Ex: for Chess,
try order: captures first, then threats, then forward moves, backward
moves.
We can bookkeep the states, as there is a possibility that states may
repeat.
Alpha-beta pruning conclusion

Alpha Beta Pruning is a method that optimizes the Minimax


algorithm. The number of states to be visited by the minimax
algorithm are exponential, which shoots up the time complexity.
Some of the branches of the decision tree are useless, and the
same result can be achieved if they were never visited. Therefore,
Alpha Beta Pruning cuts off these useless branches and, in best-
case, cuts back the exponent to half.
Why it is named as……

Alpha Beta Pruning relieved its name because it uses two parameters called
alpha and beta to make the decision of pruning branches.
–> Alpha is used and updated only by the Maximizer and it represents the
maximum value found so far. The initial value is set to -∞. The value of alpha
is passed down to children node, but the actual node values are passed up
while backtracking.
–> Beta is used and updated only by the Minimizer and it represents the
minimum value found. The initial value is ∞. Again, the value of beta is passed
down to the children node, but actual node values are used to backtrack.
Alpha-beta pruning

The condition used by alpha beta pruning to prune the


useless branches is:

 alpha >= beta


Example

Let’s make above algorithm clear with an example. 


Example
The initial call starts from A. The value of alpha here is -INFINITY and the value of beta is +INFINITY. These values are passed down to subsequent nodes in
the tree. At A the maximizer must choose max of B and C, so A calls B first
At B it the minimizer must choose min of D and E and hence calls D first.
At D, it looks at its left child which is a leaf node. This node returns a value of 3. Now the value of alpha at D is max( -INF, 3) which is 3.
To decide whether its worth looking at its right node or not, it checks the condition beta<=alpha. This is false since beta = +INF and alpha = 3. So it continues
the search.
D now looks at its right child which returns a value of 5.At D, alpha = max(3, 5) which is 5. Now the value of node D is 5
D returns a value of 5 to B. At B, beta = min( +INF, 5) which is 5. The minimizer is now guaranteed a value of 5 or lesser. B now calls E to see if he can get a
lower value than 5.

At E the values of alpha and beta is not -INF and +INF but instead -INF and 5 respectively, because the value of beta was changed at B and that is
what B passed down to E

Now E looks at its left child which is 6. At E, alpha = max(-INF, 6) which is 6. Here the condition becomes true. beta is 5 and alpha is 6. So beta<=alpha is
true. Hence it breaks and E returns 6 to B

Note how it did not matter what the value of E‘s right child is. It could have been +INF or -INF, it still wouldn’t matter, We never even had to look at it
because the minimizer was guaranteed a value of 5 or lesser. So as soon as the maximizer saw the 6 he knew the minimizer would never come this way
because he can get a 5 on the left side of B. This way we dint have to look at that 9 and hence saved computation time.

E returns a value of 6 to B. At B, beta = min( 5, 6) which is 5.The value of node B is also 5
example

So far this is how our game tree looks. The 9 is crossed out because it was never computed. 
B returns 5 to A. At A, alpha = max( -INF, 5) which is 5. Now the maximizer is guaranteed a value of 5 or greater. A now
calls C to see if it can get a higher value than 5.
At C, alpha = 5 and beta = +INF. C calls F
At F, alpha = 5 and beta = +INF. F looks at its left child which is a 1. alpha = max( 5, 1) which is still 5.
F looks at its right child which is a 2. Hence the best value of this node is 2. Alpha still remains 5
F returns a value of 2 to C. At C, beta = min( +INF, 2). The condition beta <= alpha becomes true as beta = 2 and alpha
= 5. So it breaks and it does not even have to compute the entire sub-tree of G.
The intuition behind this break off is that, at C the minimizer was guaranteed a value of 2 or lesser. But the maximizer
was already guaranteed a value of 5 if he choose B. So why would the maximizer ever choose C and get a value less than
2 ? Again you can see that it did not matter what those last 2 values were. We also saved a lot of computation by
skipping a whole sub tree.

C now returns a value of 2 to A. Therefore the best value at A is max( 5, 2) which is a 5.

Hence the optimal value that the maximizer can get is 5


Example

This is how our final game tree looks like. As you can
see G has been crossed out as it was never computed. 
JUG
PROBLEM
CHESS
PROBLEM
The problem of Playing Chess

To build a program that could play chess, we have to specify:

•The starting position of the chess board,


•The rules that define legal moves, and
•The board position that represent a win.
The starting position can be described by an 8 X 8 array
square in which each element square (X, Y), (x varying
from 1 to 8 & y varying from 1 to 8) describes the board
position of an appropriate piece in the official chess
opening position.
The goal is any board position in which the opponent
does not have a legal move and his or her “king” is under
attack.
The legal moves provide the way of getting from initial
state of final state.
The legal moves can be described as a set of rules
consisting of two parts: A left side that gives the current
position and the right side that describes the change to be
made to the board position.
Example

Current Position: While pawn at square ( 5, 2 ), AND Square ( 5, 3 )


is empty, AND Square ( 5, 4 ) is empty.

Changing Board Position: Move pawn from Square ( 5, 2 ) to


Square ( 5, 4 ).
•The current position of a chess coin on the board is its state and the set of all possible states
is state space.

•One or more states where the problem terminates are goal states.

•Chess has approximately 10¹²⁰ game paths. These positions comprise the problem search space.

•Using above formulation, the problem of playing chess is defined as a problem of moving
around in a state space, where each state corresponds to a legal position of the board.

•State space representation seems natural for playing chess problem because the set of states,
which corresponds to the set of board positions, is well organised.
It is a normal chess game. In a chess problem, the start is
the initial configuration of chessboard. The final state is
the any board configuration, which is a winning position
for any player. There may be multiple final positions and
each board configuration can be thought of as
representing a state of the game. Whenever any player
moves any piece, it leads to different state of game.
TILES
PROBLEM
Problem
N-puzzle that consists of N tiles (N+1 titles with an
empty tile) where N can be 8, 15, 24 and so on.
INTRODUCTION

N-puzzle that consists of N tiles (N+1 titles with an empty tile) where N can be 8, 15, 24 and so on.

Tiles puzzle or N-puzzle is also known as eight puzzle problem by the name of N puzzle
problem or sliding puzzle problem.
In our example N = 8. (that is square root of  (8+1) = 3 rows and 3 columns).

In the same way, if we have N = 15, 24 in this way, then they have Row and columns as follow (square
root of (N+1) rows  and square root of (N+1) columns).

That is if N=15 than number of rows and columns= 4, and if N= 24 number of rows and columns= 5.
So, basically in these types of problems we have given
a initial state or initial configuration (Start state) and a
Goal state or Goal Configuration.

Here We are solving a problem of 8 puzzle that is a 3x3


matrix.
Example : Consider a 8 puzzle problem i.e 3x3
matrix

Initial state                                                       Goal state

1 2 3 1 2 3

4 6 4 5 6

7 5 8 7 8
solution

The puzzle can be solved by moving the tiles one by one


in the single empty space and thus achieving the Goal
state.
Rules of solving puzzle

Instead of moving the tiles in the empty space we can visualize moving the
empty space in place of the tile.

The empty space can only move in four directions (Movement of empty space)


1. Up
2. Down
3. Right or
4. left
The empty space cannot move diagonally and can
take only one step at a time.
All possible move of a Empty tile
0 x o
x # X
0 x 0

o- Position total possible moves are (2),


 x - position total possible moves are (3) and 
#-position total possible moves are (4)
Let's solve the problem without Heuristic
Search that is Uninformed Search or Blind Search
( Breadth First Search and Depth First Search)

Breath First Search to solve Eight puzzle problem


example
Solved with bfs

Time complexity: In worst case time complexity in BFS


is O(b^d) know as order of b raise to power d.  In
this particular case it is (3^20). 
b-branch factor
d-depth factor
Let's solve the problem with Heuristic Search that is Informed Search (
A* , Best First Search (Greedy Search))

To solve the problem with Heuristic search or informed


search we have to calculate Heuristic values of each node
to calculate cost function. (f=g+h)
           
Note: See the initial state and goal state carefully all
values except (4,5 and 8) are at their respective places. so,
the heuristic value for first node is 3.(Three values are
misplaced to reach the goal). And let's take actual cost
(g) according to depth.
Note: Because of quick solution time complexity is less
than that of Uninformed search but optimal solution
not possible. 
THANK
YOU

You might also like