1. Home
  2. Explore
  3. Computer Science
  4. CS 188 Lecture 6

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

CS 188 Lecture 6

Adversarial Search: Game Playing with Minimax and Pruning

This lecture covers the fundamentals of adversarial search, focusing on deterministic zero-sum games. The core ideas include formalizing games, the minimax algorithm, alpha-beta pruning, depth-limited search, and evaluation functions. These techniques enable agents to make optimal or near-optimal decisions in competitive environments such as chess, checkers, and Pac-Man.


Game Playing State of the Art

Games serve as benchmark problems for AI. Key milestones:

  • Checkers: In 1994, a computer became world champion, defeating Marion Tinsley who had dominated for 40 years. In 2007, checkers was solved — meaning the outcome is known if both sides play optimally (a draw with perfect play).

  • Chess: Deep Blue defeated Garry Kasparov in 1997 using sophisticated evaluation functions and deep search (up to 40-ply). Chess is not solved; strong consensus suggests it is a draw, but unproven.

  • Go: Traditionally very hard due to high branching factor (~300). In 2016, AlphaGo defeated the human champion using Monte Carlo tree search and learned evaluation functions of unprecedented quality.

  • Pac-Man: Still an open problem in this course, serving as a testbed for adversarial search techniques.

Types of Games

Games are categorized along several axes:

  • Deterministic vs. Stochastic: Deterministic (chess, checkers) have no randomness; stochastic (backgammon) involve dice rolls.

  • Number of Players: One (solitaire), two (chess, Go), or more (poker).

  • Zero-sum vs. Non-zero-sum: In zero-sum games, agents’ utilities are strictly opposite (one’s gain is another’s loss). In non-zero-sum, independent utilities allow win-win outcomes.

  • Perfect vs. Imperfect Information: Perfect information (chess, checkers) means the full state is known to all players; imperfect (poker) involves hidden cards.


Formalizing Games for AI

A unified interface for adversarial search consists of:

  • States: A set of states, including a start state s0s_0s0​.

  • Players: Usually two, taking turns.

  • Actions: Available actions per player per state.

  • Transition function: T(s,a)→s′T(s, a) \to s'T(s,a)→s′, like a successor function.

  • Terminal test: Checks if the game is over.

  • Terminal utilities: Scores assigned to terminal states (can be a range, not just win/lose/draw).

Solution: A policy that specifies the action to take in every reachable state. Unlike planning, a single action sequence is insufficient because the opponent’s moves dictate future states.

Zero-Sum Games

In zero-sum games, agents have completely opposing utilities. If Player 1’s utility is U(s)U(s)U(s), Player 2’s is −U(s)-U(s)−U(s). This reduces the problem to a single number per state, enabling straightforward adversarial search.


Adversarial Search and Minimax

Single-Agent Game Trees

In a single-agent setting (e.g., Pac-Man collecting food), the agent maximizes its own utility. The value of a state is the best achievable outcome from that state, computed as:

  • Terminal state: known utility.

  • Non-terminal state: V(s)=max⁡a∈Actions(s)V(Succ(s,a))V(s) = \max_{a \in \text{Actions}(s)} V(\text{Succ}(s, a))V(s)=maxa∈Actions(s)​V(Succ(s,a)).

Adversarial Game Trees

With two players alternating moves:

  • Maximizer: Pac-Man (wants high score).

  • Minimizer: The ghost (wants low score).

Given a tree where leaves have utilities, values propagate upward:

  • At maximizer nodes: V(s)=max⁡aV(Succ(s,a))V(s) = \max_{a} V(\text{Succ}(s, a))V(s)=maxa​V(Succ(s,a)).

  • At minimizer nodes: V(s)=min⁡aV(Succ(s,a))V(s) = \min_{a} V(\text{Succ}(s, a))V(s)=mina​V(Succ(s,a)).

The minimax value of the root is the outcome assuming both play optimally. Example: In a short two-move game, even if a leaf has +8, the minimizer will choose a move leading to -10 for the maximizer, so the root value is -8, not +8.

Tic-Tac-Toe

Tic-Tac-Toe is solved: the minimax value of the start state is 0 (draw). This is computed by expanding the full game tree (though pruning helps).

Minimax Algorithm (Basic)

function VALUE(state, player) return utility
    if TERMINAL(state) return UTILITY(state)
    if player == MAX
        v = -∞
        for a in ACTIONS(state)
            v = MAX(v, VALUE(RESULT(state, a), MIN))
        return v
    else  // MIN
        v = +∞
        for a in ACTIONS(state)
            v = MIN(v, VALUE(RESULT(state, a), MAX))
        return v

This is essentially depth-first search over the entire game tree.

Complexity of Minimax

  • Time: O(bm)O(b^m)O(bm), where bbb is the average branching factor and mmm is the maximum depth.

  • Space: O(bm)O(bm)O(bm).

For chess (b≈35b \approx 35b≈35, m≈100m \approx 100m≈100), exhaustive search is impossible. Real systems must prune and use depth limits.

Playing Against Suboptimal Opponents

Minimax assumes perfect play. Against a fallible opponent, a different strategy may be better: taking a risk for a large payoff if the opponent can make a mistake. This introduces probabilistic reasoning (covered in later lectures). Example: In a Pac-Man situation with no dots and only ghosts, the minimax solution may be to dive into a ghost immediately to minimize negative score. But if ghosts sometimes make errors, waiting or moving away could yield higher expected score.


Alpha-Beta Pruning

Alpha-beta pruning dramatically reduces the number of nodes examined without affecting the minimax value at the root.

Core Idea

While evaluating a node, if the current best option for one player is already worse than a guaranteed alternative for the opponent, the remaining children can be skipped. This is meta-reasoning: using computation to decide which computation to avoid.

  • Alpha (α\alphaα): The best value (largest) that the maximizer can guarantee so far along the path to the root.

  • Beta (β\betaβ): The best value (smallest) that the minimizer can guarantee so far along the path to the root.

Algorithm Sketch

  • At a max node: For each child, after evaluating it, update α=max⁡(α,child value)\alpha = \max(\alpha, \text{child value})α=max(α,child value). If α≥β\alpha \geq \betaα≥β, prune remaining children (since minimizer will avoid this path anyway).

  • At a min node: For each child, update β=min⁡(β,child value)\beta = \min(\beta, \text{child value})β=min(β,child value). If β≤α\beta \leq \alphaβ≤α, prune remaining children.

The algorithm passes α\alphaα and β\betaβ down from parent to child. When returning, only the actual value (or a bound that is sufficient for the parent) is returned.

Example Walkthrough

Consider a game tree where the root is a max node, left subtree yields a guaranteed 3, and the right subtree has a min node with children [2, ?, ?]. Once the first child (2) is seen, the min node's value is at most 2, which is less than the maximizer's guaranteed 3. Therefore, the remaining children under that min node can be pruned — they cannot affect the root decision.

Properties of Alpha-Beta Pruning

  • Exactness: The root value is identical to minimax’s. Intermediate node values may be inexact (bounds), but that does not affect the final decision.

  • Pruning effectiveness depends on child ordering. Evaluating promising moves first (e.g., using a heuristic) increases pruning.

  • With perfect ordering, time complexity drops to O(bm/2)O(b^{m/2})O(bm/2) — the square root of minimax, effectively doubling the search depth for the same compute budget.

  • Practical implication: Even with pruning, full search to game end is often infeasible (e.g., chess). Additional techniques are needed.

Quiz Example (Alpha-Beta)

A tree with values [10,8] on left (minimizer chooses 8) and [4, ...] on right: after finding 4, the minimizer's node value is at most 4, which is less than max's guaranteed 8. The remaining children under that min node can be pruned. Similarly, if a large subtree exists under a pruned branch, all that computation is saved.


Depth-Limited Search and Evaluation Functions

When the game tree is too deep to fully explore, we stop at a depth limit and use an evaluation function to estimate the utility of the resulting state.

Depth-Limited Minimax

Instead of terminal states, we treat nodes at the depth limit as pseudo-terminals and call EVAL(state). The algorithm then propagates these approximate values upward.

  • This loses the guarantee of optimal play.

  • Deeper search improves decision quality because the evaluation function is usually more accurate closer to the end of the game.

Evaluation Functions (Heuristics)

An evaluation function returns a score estimating how good a state is for the current player (maximizer). Design considerations:

  • Must be fast to compute — otherwise it eats into the search budget.

  • Should be correlated with true game outcome.

  • Often a weighted linear combination of features.

Chess example: Eval=w1⋅material advantage+w2⋅mobility+w3⋅king safety+…Eval = w_1 \cdot \text{material advantage} + w_2 \cdot \text{mobility} + w_3 \cdot \text{king safety} + \dotsEval=w1​⋅material advantage+w2​⋅mobility+w3​⋅king safety+…. Material typically uses standard piece values (queen=9, rook=5, bishop=3, knight=3, pawn=1).

Pac-Man example: Simple score (time penalty + pellet reward) may cause thrashing if depth is too shallow (e.g., depth 2 yields symmetric choices). Adding a bonus for being closer to the next pellet (proximity reward) resolves this. A good evaluation function can incorporate ghost distance, power pellet status, etc.

Iterative Deepening

When time is limited, run depth-limited search repeatedly with increasing depth. Once time runs out, use the result from the previous (shallower) search. This ensures a move is always available.

Trade-offs

  • More compute per evaluation = less depth explored.

  • A richer evaluation function may enable better decisions at shallow depth, but deeper search with a simple function can outperform.


Synergies: Evaluation Functions and Alpha-Beta Pruning

These two techniques are not orthogonal; they can enhance each other:

  • Ordering children: Use the evaluation function to rank successor states — evaluate the most promising (or least promising for the opponent) first. Better ordering leads to more pruning in alpha-beta.

  • Bounds for pruning: If the evaluation function provides an upper/lower bound on a node’s true value (e.g., a heuristic that is optimistic), it can trigger early pruning without expanding children. For instance, at a min node, if the evaluation of a state is already below alpha, no need to search deeper.

Understanding these interactions deepens intuition for adversarial search. In practice, most game-playing agents combine depth-limited search, alpha-beta pruning, and a carefully designed evaluation function.


Key Takeaways

  • Zero-sum games with perfect information are solved via minimax search, which computes the optimal policy assuming both players act optimally.

  • Alpha-beta pruning reduces the search space by skipping branches that cannot influence the final decision, ideally cutting time to O(bm/2)O(b^{m/2})O(bm/2).

  • Depth limits and evaluation functions are essential for games too large to search to completion. The quality of the evaluation function critically determines performance.

  • Iterative deepening allows agents to operate under time constraints.

  • Thoughtful integration of evaluation functions and alpha-beta pruning (e.g., child ordering, bound-based pruning) yields stronger game-playing agents.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent