Ai Exp 07
Ai Exp 07
Experiment No: 07
Theory:
There are two main approaches to game playing in AI, rule-based systems and machine
learning-based systems.
1. Rule-based systems use a set of fixed rules to play the game.
2. Machine learning-based systems use algorithms to learn from experience and
make decisions based on that experience.
Min-Max Algorithm
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.
Complete- Min-Max algorithm is Complete. It will definitely find a solution (if exist), in
the finite search tree.
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.
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:
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.
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.
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.
import java.io.*;
class GFG {
// Returns the optimal value a maximizer can obtain.
// depth is current depth in game tree.
// nodeIndex is index of current node in scores[].
// isMax is true if current move is of maximizer, else false
// scores[] stores leaves of Game tree.
// h is maximum height of Game tree
static int minimax(int depth, int nodeIndex, boolean isMax,
int scores[], int h)
{
// Terminating condition. i.e leaf node is reached
if (depth == h)
return scores[nodeIndex];
}
}
Output:
The optimal value is: 12