When another agent works against us, as in chess, the environment changes in response to our moves. Adversarial search plans for the opponent's replies.

Two-Player Zero-Sum Games

Games are a classic AI domain: the rules are simple, the world is fully known, and the opponent is clever. We consider two-player, zero-sum, perfect-information, deterministic games such as chess, checkers and tic-tac-toe. Zero-sum means one player's gain is exactly the other's loss, so the utilities sum to zero (or a constant). Players are called MAX (who wants to maximise the value) and MIN (who wants to minimise it). A game is defined by the initial state, the player to move, the actions allowed, the transition model, a terminal test (game over) and a utility function giving the final payoff, for example +1 win, 0 draw, −1 loss. The game tree has the possible moves as edges, with MAX and MIN moves alternating; each level of one move is a ply.

Definition: Zero-sum game. A game in which the total payoff to all players is constant, so any gain for one player is an equal loss for the opponent.

The Minimax Algorithm

Minimax assumes both players play optimally. The minimax value of a node is the utility of reaching it for MAX if both play perfectly from there: for a terminal node it is the utility; for a MAX node it is the maximum of the values of its children; for a MIN node it is the minimum. The algorithm is a depth-first recursion down to the leaves, backing values up. At the root, MAX picks the move with the highest minimax value. It is complete for finite trees and optimal against an optimal opponent. Time is O(bᵐ) and space O(b·m), where b is the branching factor and m the maximum depth; for chess (b ≈ 35, m ≈ 100) this is impossible, so practical programs cut off the search at a depth limit and use an evaluation function estimating the value of non-terminal positions, such as material count plus position.

Practical Issues

Evaluation functions are often a weighted sum of features (pawn = 1, knight = 3, rook = 5, queen = 9). Programs use iterative deepening, transposition tables (to avoid re-searching the same position) and opening books. Games with chance (backgammon) add chance nodes and use expected values (expectiminimax); games with hidden information (poker) need probabilistic methods.

Table 2.6: Minimax node values

Node type Chooses Value
Terminal None Utility of the state
MAX Best move for MAX Maximum of children
MIN Best move for MIN Minimum of children

Example 2.6: Minimax on a 3-ply tree

Problem: Root MAX has two MIN children, each with two MAX children, each with two leaves. Leaves in order: 3, 5, 6, 9, 1, 2, 0, −1. Find the value of the root.

Solution:

  1. Bottom MAX nodes: max(3, 5) = 5; max(6, 9) = 9; max(1, 2) = 2; max(0, −1) = 0.
  2. MIN nodes: min(5, 9) = 5; min(2, 0) = 0.
  3. Root MAX: max(5, 0) = 5.

Result: The minimax value of the root is 5; MAX should play the first move.

leaves = [3, 5, 6, 9, 1, 2, 0, -1]
def minimax(depth, i, is_max):
    if depth == 3:
        return leaves[i]
    vals = [minimax(depth + 1, 2 * i + k, not is_max) for k in (0, 1)]
    return max(vals) if is_max else min(vals)
print("minimax value of root:", minimax(0, 0, True))

Output

minimax value of root: 5

Common mistakes

  • Using max at every level instead of alternating max and min.
  • Assuming the opponent plays randomly; minimax assumes optimal play.
  • Searching to the end of a game like chess; a cut-off and evaluation function are needed.
  • Forgetting that utilities are measured from MAX's point of view.

Key takeaways

  • Zero-sum games: gain of one is loss of the other; MAX maximises, MIN minimises.
  • Minimax value: utility at leaves; max at MAX nodes; min at MIN nodes.
  • Time O(bᵐ), space O(b·m); real games need depth cut-off and evaluation functions.
  • One ply is one player's move.

Practice questions

  1. What does zero-sum mean?
  2. How is a MIN node valued in minimax?
  3. What is the time complexity of minimax?

Answers to the practice questions

  1. The players' payoffs add to a constant, so one's gain equals the other's loss.
  2. By the minimum of its children's values.
  3. O(bᵐ).

Sources

  • Official BCA syllabus Artificial Intelligence, course code 26BCA404DS04, Unit II