Game Playing in AI

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Game playing in AI is an active area of research and has many practical

applications, including game development, education, and military training.


By simulating game playing scenarios, AI algorithms can be used to develop
more effective decision-making systems for real-world applications.
The most common search technique in game playing is Minimax search
procedure. It is depth-first depth-limited search procedure. It is used for
games like chess and tic-tac-toe.
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
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.

Figure 1: Before backing-up of values


Figure 2: After backing-up of values We assume that PLAYER1 will start the
game.
4 levels are generated. The value to nodes H, I, J, K, L, M, N, O is provided by
STATICEVALUATION function. Level 3 is maximizing level, so all nodes of
level 3 will take maximum values of their children. Level 2 is minimizing
level, so all its nodes will take minimum values of their children. This process
continues. The value of A is 23. That means A should choose C move to win.

Mini-Max Algorithm in Artificial Intelligence

 In decision-making and game theory, the mini-max algorithm is a


recursive or backtracking method. It suggests the best move for the
player, provided that the opponent is likewise playing well.
 In AI, the Min-Max algorithm is mostly employed for game play. Chess,
checkers, tic-tac-toe, go, and other two-player games are examples. This
Algorithm calculates the current state's minimax choice.
 The game is played by two players, one named MAX and the other
named MIN, in this algorithm.
 Both players FIGHT it, since the opponent player receives the smallest
benefit while they receive the greatest profit.
 Both players in the game are adversaries, with MAX selecting the
maximum value and MIN selecting the minimum value.
 For the exploration of the entire game tree, the minimax method uses a
depth-first search strategy.
 For the exploration of the entire game tree, the minimax method uses a
depth-first search strategy.
 The minimax algorithm descends all the way to the tree's terminal node,
then recursively backtracks the tree.
Initial call:

minimax(node, 3, true)

Working of Min-Max Algorithm:

 A simple example can be used to explain how the minimax algorithm


works. We've included an example of a game-tree below, which
represents a two-player game.
 There are two players in this scenario, one named Maximizer and the
other named Minimizer.
 Maximizer will strive for the highest possible score, while Minimizer will
strive for the lowest possible score.
 Because this algorithm uses DFS, we must go all the way through the
leaves to reach the terminal nodes in this game-tree.
 The terminal values are given at the terminal node, so we'll compare them
and retrace the tree till we reach the original state. The essential phases in
solving the two-player game tree are as follows:

Step 1: The method constructs the whole game-tree and applies the utility
function to obtain utility values for the terminal states in the first step. Let's
assume A is the tree's initial state in the diagram below. Assume that the
maximizer takes the first turn with a worst-case initial value of -infinity, and the
minimizer takes the second turn with a worst-case initial value of +infinity.
Step 2: Next, we'll locate the Maximizer's utilities value, which is -, and
compare each value in the terminal state to the Maximizer's initial value to
determine the upper nodes' values. It will select the best option from all of them.

 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 3: Now it's the minimizer's time, thus it'll compare all nodes' values with +
and determine the 3rd layer node values.

 For node B = min(4,6) = 4


 For node C = min (-3, 7) = -3
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: Now it's Maximizer's turn, and it'll choose the maximum value of all
nodes and locate the root node's maximum value. There are only four layers in
this game tree, so we can go to the root node right away, but there will be more
layers in real games.
For node A max(4, -3)= 4
That was the complete workflow of the minimax two player game.

Properties of Mini-Max algorithm:

 Complete –The Min-Max algorithm is finished. In the finite search tree,


it will undoubtedly locate a solution (if one exists).
 Optimal- If both opponents are playing optimally, the Min-Max
algorithm is optimal.
 Time complexity- Because it executes DFS for the game-tree, the time
complexity of the Min-Max algorithm is O(bm), where b is the game-
branching tree's factor and m is the tree's maximum depth.
 Space Complexity- Mini-max method has a space complexity that is
similar to DFS, which is O(bm).

Limitation of the minimax Algorithm:

The biggest disadvantage of the minimax algorithm is that it becomes extremely


slow while playing complex games like chess or go. This style of game contains
a lot of branching, and the player has a lot of options to choose from. The
minimax algorithm's drawback can be alleviated by using alpha-beta pruning
Artificial intelligence is a broad topic, and there are many algorithms involved
in it. One such algorithm is the minimax algorithm. It is a famous backtracking
algorithm used in decision-making. Alpha-beta pruning is an optimization
technique for the minimax algorithm. Pruning literally means cutting away dead
or overgrown branches or stems. In artificial intelligence, pruning means cutting
off the useless branches in the decision trees and random forests. Let us learn
more about alpha beta pruning in artificial intelligence.

What is Alpha Beta Pruning?


Alpha beta pruning in Artificial Intelligence is a way of improving the minimax
algorithm. In the minimax search method, the number of game states that it
must investigate grows exponentially with the depth of the tree. We cannot
remove the exponent, but we can reduce it by half. As a result, a technique
known as pruning allows us to compute the proper minimax choice without
inspecting each node of the game tree. Alpha-beta pruning may be done at any
level in a tree, and it can occasionally trim the entire sub-tree and the tree
leaves. It is named alpha-beta pruning in artificial intelligence because it
involves two parameters, alpha, and Beta.
Condition for Alpha Beta Pruning
The two parameters of alpha beta pruning in artificial intelligence are
Alpha: It is the best highest value choice that we have found in the path of the
maximizer. The starting value of alpha is -∞.
Beta: It is the best lowest value choice that we have found in the path of the
minimizer. The starting value of beta is +∞.
Key Points about Alpha Beta Pruning
 Alpha: At each point along the Maximizer path, Alpha is the best option or
the highest value we've discovered. –∞ is the initial value for alpha.

 Beta: At every point along the Minimizer route, Beta is the best option or
the lowest value we've identified. The value of beta is initially set to +∞.

 The condition for Alpha-beta Pruning is α >= β.

 Alpha is updated only when it's MAX's time, and Beta can only be updated
when it's MIN's turn.

 The MAX player will only update alpha values, whereas the MIN player
will only update beta values.
 During the reversal of the tree, node values will be transferred to upper
nodes instead of alpha and beta values.

 Only child nodes will get Alpha and Beta values.


Working of Alpha Beta Pruning
Let us consider a two-player search tree to understand further how Alpha-beta
pruning in artificial intelligence works.
Step 1
The Max player will start by traveling from node A to node B, where α = -
∞ and β = +∞, and delivering these alpha and beta values to node B, where = -
and = + once again, and Node B transmitting the identical value to its offspring
D.

Step 2
As Max's turn at Node D approaches, the value of α will be decided. When the
value of α is compared to 2, then 3, the value at node D is max (2, 3) = 3.
Hence, the node value is also 3.
Step 3
The algorithm returns to node B, where the value of β will change since this is a
turn of Min. Now β = +∞will be compared to the value of the available
subsequent nodes, i.e., min (∞, 3) = 3, resulting in node B now α = -∞, and β =
3. In the next phase, the algorithm will visit the next successor of Node B, Node
E, and pass the values of α = -∞ and β = 3.
Step 4
Max will take over at node E and change alpha's value. The current existing
value of alpha will be compared to 5, resulting in max (-∞, 5) = 5, and at node
E, where α>=β, the right successor of E will be pruned, and the algorithm will
not traverse it, resulting in the value at node E being 5.

Step 5
We now traverse the tree backward, from node B to node A. At node A, alpha
will be converted to the greatest feasible value of 3, as max (-∞, 3)= 3, and β =
+∞. These two values will now be passed on to Node C, A's right-hand
successor.
At node C, the values and β = +∞ and α =3 will be passed on to node F, and
node F will get the identical values.
Step 6
At node F, the value of α is compared with the left child 0, and max(3,0)= 3. It
is then compared with the right child, which is 1, and max(3,1)= 3.

Step 7
Node F sends the node value 1 to node C. The value of Beta is adjusted at C, α
= 3 and β= +∞, and it is compared to 1, resulting in min (∞, 1) = 1. Now, if α =
3 and β = 1, the condition α>=β is met, the algorithm will prune the next child
of C, which is G, rather than calculating the entire sub-tree G.

Step 8
C now returns the value of 1 to A, with max (3, 1) = 3 being the optimum result
for A. The final game tree is shown here, with nodes that have been calculated
and nodes that have never been computed. As a result, in this case, the ideal
value for the maximizer is 3.

You might also like