Professional Documents
Culture Documents
knowledge)
Tags Unit - 2
Games are an important part of AI. Games don’t require much knowledge.
We only need to know the rules of the game, legal moves and all the conditions and
constraints and the conditions of losing or winning the game.
Both players want to win the game so we need to make the best possible move at
each step. Since each move can create a lot of possible branches, algorithms like
BFS are not always very effective since they can take a lot of time to give a viable
solution.
Adversarial search
It is the type of search where we examine the problem which arises when we try to
plan ahead of the world and other agents that are playing against us.
Searches in which two or more players with conflicting goals are trying to
explore the same search space for the solution, are called adversarial
searches or Games.
Types of games
Perfect information: A game in which the agents can look into the complete board
and see the positions of their opponent agents as well. Agents have all the
information about the game and can see each other’s moves as well. Example:
Chess, Go etc.
Imperfect information: The game in which the agents don’t have the complete
information of the game and are not aware about the opponent’s positions.
Deterministic games: Games which follow a strict pattern and a set of rules for the
games, and there is no randomness associated with them. Example: Chess
Zero-sum game
Adversarial searches which are pure competition.
Each agent’s win or loss of utility is exactly balanced by the losses or gains of the
utility of the other agent.
One player tries to maximize one single value while the other player tries to
minimize it.
Formalization of a problem
A game can be defined as a type of search in AI which can be formalized with the
following elements:
Results(s, a): Transition model which specifies the result of moves in the state
space.
Utility(s, p): A utility function returns a numeric value for the game that ends in
terminal states s for player p. Also called payoff function.
Game tree
A game tree is a tree where nodes of the tree are the game states and the edges of
the tree are the moves by the players. It involves the initial state, actions, and the
result function.
Example: Tic-tac-toe
The following figure is showing part of the game-tree for tic-tac-toe game. Following are
some key points of the game:
From the initial state, MAX has 9 possible moves as he starts first. MAX place x and
MIN place o, and both player plays alternatively until
we reach a leaf node where one player has three in a row or all squares
are filled.
Both players will compute each node, minimax, the minimax value
which is the best achievable utility against an optimal adversary.
Suppose both the players are well aware of the tic-tac-toe and
playing the best play. Each player is doing his best to prevent another
one from winning. MIN is acting against Max in the game.
So in the game tree, we have a layer of Max, a layer of MIN, and each layer is
called as Ply. Max place x, then MIN puts o to prevent Max from winning, and this
game continues until the terminal node.
In this either MIN wins, MAX wins, or it's a draw. This game-tree is the whole search
space of possibilities that MIN and MAX are playing
tic-tac-toe and taking turns alternately.
It aims to find the optimal strategy for MAX to win the game.
In the game tree, optimal leaf node could appear at any depth of the tree.
Propagate the minimax values up to the tree until the terminal node discovered.
In a given game tree, the optimal strategy can be determined from the minimax value of
each node, which can be written as MINIMAX(n). MAX prefer to move to a state of
maximum value and MIN prefer to move to a
state of minimum value then:
Only for two players. Has two players, MAX and MIN.
Both the players fight it as the opponent gets the minimum benefit while they get
maximum benefit.
Proceeds all the way down to the terminal node of the tree and then backtracks the
three as recursion.
There are two players, maximiser and minimiser. Maximiser will try to get the max
possible score and minimiser will do the opposite.
Steps:
1. Algorithm first generates the entire game tree and applies the utility function to get
the utility values for the terminal states. Let A be the initial state of the tree.
Suppose maximizer takes first turn which has worst-case initial value = −∞, and
minimizer will take next turn which has worst-case initial value = +∞
2. We first find utility values for maximizer, initial value being −∞, so we will compare
each value in terminal state with initial value and determine the higher node values.
3. Now we check for minimizer. It will compare values with +∞ and will find the third
layer node values.
b. For C, min(-3, 7) = -3
Properties of minmax:
Optimal: Optimal if both players play optimally
Limitations
Gets slow for really complex games due to the huge branching in games like chess.
We can check the correct minmax decision without checking all the nodes. This is
called pruning.
It can be applied to any depth of the tree and it can sometimes also prune entire
subtrees making it quite faster.
Parameters:
Alpha (α): The best (highest-value) choice we have come across so far at any
point along the path. Initial value is −∞.
Beta (β ): The best (lowest-value) we have come across so far at any point
along the path. Initial value is +∞.
Key points:
While backtracking, the node values will be passed to upper nodes instead of
values of α and β .
Steps:
1. At first, max will start first move from node A, where α = −∞ and β = +∞
and these values are passed to B where these values are the same.
3. Now we backtrack to B, where β value will change since it is the Min’s turn. β =
+∞ and we compare max(∞, 3) = 3, thus at B, α = −∞ and β = 3.
At node C, α=3 and β= +∞, and the same values will be passed on to node F.
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.
Worst ordering: In some cases, the algo does not prune any leaves of the tree
and works exactly like minmax. Though, here it consumes more time because
of the deciding factors. In this case, the best move occurs in the right side of the
tree. Time complexity is: O(bM ).
Ideal ordering: Occurs when a lot of pruning occurs and the best move occurs
in the left side of the tree. We apply DFS and it goes twice as deep compared to
minmax in the same amount of time. Complexity is: O(bm/2 ).