6CS4 AI Unit-2
6CS4 AI Unit-2
(6CS4-05)
Unit II
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
•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 –
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
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 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:
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
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.
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
•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.
1 2 3 1 2 3
4 6 4 5 6
7 5 8 7 8
solution
Instead of moving the tiles in the empty space we can visualize moving the
empty space in place of the tile.