AI Unit3 Gameplaying
AI Unit3 Gameplaying
ITT451
UNIT III
GAME PLAYING
By
Ms.C.B.Thaokar
Assistant Professor
Department of Information Technology
RCOEM
SYLLABUS
Games
Minimax Algorithm
Alpha – Beta Pruning
Games that include an element of Chance
2
WHY STUDY GAME PLAYING?
Games allow us to experiment with easier versions of
real-world situations
Hostile agents act against our goals
Games have a finite set of moves
Games are fairly easy to represent
Good idea to decide about what to think
Perfection is unrealistic, must settle for good
One of the earliest areas of AI
Claude Shannon and Alan Turing wrote chess programs in
1950s
The opponent introduces uncertainty
The environment may contain uncertainty
(backgammon)
Search space too hard to consider exhaustively
Chess has about 1040 legal positions
Efficient and effective search strategies even more critical 3
Games are fun to target!
ASSUMPTIONS
Static or dynamic?
Fully or partially observable?
Discrete or continuous?
Deterministic or stochastic?
Episodic or sequential?
4
ZERO-SUM GAMES
Focus primarily on “adversarial games”
Two-player, zero-sum games
6
USING SEARCH
Search could be used to find a perfect sequence
of moves except the following problems arise:
There exists an adversary who is trying to minimize
your chance of winning every other move
You cannot control his/her move
Search trees can be very large, but you have finite
time to move
Chess has 1040 nodes in search space
With single-agent search, can afford to wait
Solution?
7
GAME TREES
Tic tac toe
Two players, MAX and MIN
8
MINIMAX ALGORITHM
Mini-max algorithm is a recursive or backtracking algorithm which is used
in decision-making and game theory
Mini-Max algorithm uses recursion to search through the game-tree.
Min-Max algorithm is mostly used for game playing in AI. Such as Chess,
Checkers, tic-tac-toe, go, and various two-players game.
In this algorithm two players play the game, one is called MAX and
other is called MIN.
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 9
PSEUDO CODE FOR MINIMAX ALGORITHM
function minimax(node, depth, maximizingPlayer) is
if depth ==0 or node is a terminal node then
return static evaluation of node
return minEva
WORKING OF MIN-MAX ALGORITHM:
The working of the minimax algorithm can be easily described
using an example.
In this example, there are two players one is called Maximizer
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:
11
MINIMAX 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.
12
MINIMAX 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) 13
=7
MINIMAX 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 3 rd layer node values.
For node B= min(4,6) = 4
For node C= min (-3, 7) = -3
14
MINIMAX 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
15
Example : 5 Stone NIM Game
Problem Statement :
5 stone Nim is a game played by 2 players and consists of pile
of 5 stones.
Each player can remove either one or two stones from the pile
on its turn
The player who removes the last stone wins the game
16
EXAMPLE : 5 STONE NIM GAME
17
MiniMax Theorem
Every two person zero sum game is forced
to win for one player or a forced draw for
either player, in principle these minimax
startegies can be computed.
Brute Force Approach
- 35 100 ᷈ 10 154
ISSUES IN PRACTICAL GAMES AND THEIR SOLUTIONS
? ? ? ?
CUTOFF AND MINIMAX
EXAMPLE
89 89
91 21 39 101 96 96 89 11 29 49 59 51 81 79
EVALUATION FUNCTIONS
Evaluation functions score non-terminals in
depth-limited search
playing optimally.
Time complexity- As it performs DFS for the game-tree, so
23
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
24
ALPHA-BETA PRUNING
Alpha-beta pruning is a modified version of the minimax algorithm.
It is an optimization technique for the minimax algorithm.
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.
3 5 2 ? ?
Eg2 :
minimax(root) = max( min(3,12,8), min(2,z,y), min(14,5,2))
= max ( 3, z , 2) ; z <=2 assume 26
= 3
ALPHA-BETA PRUNING
Alpha values
- A lower bound on exact minimax score
- True value might >=α
Beta values
- A upper bound on exact minimax score
- True value might <= β
28
CONDITION FOR ALPHA-BETA PRUNING:
β cutoff :
- It occurs at max node
- α >= β value of any Min ancestor
29
KEY POINTS ABOUT ALPHA-BETA
PRUNING:
beta.
While backtracking the tree, the node values
91 21 39 101 61 69 71 71 89 11 29 49 59 51 81 79
EX-2 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.
33
WORKING OF ALPHA-BETA PRUNING:
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.
34
WORKING OF ALPHA-BETA PRUNING:
35
WORKING OF ALPHA-BETA PRUNING:
38
WORKING OF ALPHA-BETA PRUNING:
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.
40
EG-2 ALPHA-BETA PRUNING
91 21 39 101 61 69 71 71 89 11 29 49 59 51 81 79
MOVE ORDERING IN ALPHA-BETA
PRUNING:
Itcan 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).
42
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:
43