Search

Search papers and lessons

Self-Play

Level 3 · 12 min read

Self-play trains an agent by letting it play against copies of itself, so the opponent improves as the agent does and no human examples are needed.

On this page

  • Colour = topic mix
  • Probability
  • Discrete math
  • Calculus
  • Linear algebra
  • Reinforcement learning
  • Stochastic processes
  • Sign in to see your progress

TL;DR

Self-play is a way to train a game-playing agent with no teacher and no dataset. The agent plays games against copies of itself, learns from the results, and then plays the improved version against itself again. Because the opponent gets better at the same pace as the agent, the difficulty rises automatically.

Self-play is a training recipe, not a single algorithm. It works on top of Reinforcement Learning and is most natural for two-player games where one side's gain is the other side's loss.

Where you'll see it: in game programs that learned chess, shogi and Go from the rules alone, and in training agents for competitive video games, where past versions of the agent serve as the opponents.

Intuition

Start from practice against a sparring partner

Imagine learning tennis with nobody to hit with. You could hit a wall, but the wall never returns a clever shot. Now imagine a partner who learns from every point you play and then plays back against you. You both get better, and each new skill you pick up is tested on the next rally.

Self-play is that partner. The agent's opponent is its own past or present self, so the practice is always matched to the current level. A fixed opponent, by contrast, can be beaten by a narrow trick and then teaches nothing more.

Why the idea exists

Learning from human games is expensive, and it caps the agent at the level of the humans who wrote the games. Hand-written opponents encode the designer's guesses about good play. Self-play removes both limits. The only input is the rules of the game and a way to score the result. The agent can discover strategies that no person has tried.

The picture to keep in mind is a loop:

flowchart LR
  A["Current policy"] --> B["Play games against a copy"]
  B --> C["Record moves and outcomes"]
  C --> D["Update the policy"]
  D --> A

The math

Self-play is most cleanly described for a two-player zero-sum game: two players alternate moves, and at the end one player's payoff is the negative of the other's. Rock-paper-scissors is one; chess and Go are others.

A strategy (or policy) π\pi gives, for each situation, a probability for each legal move. Write AA for the payoff matrix (a table of payoffs, one row per move of the first player and one column per move of the second): entry AijA_{ij} is the payoff to the first player when the first player uses pure move ii and the second uses pure move jj. If the first player uses the mixed strategy π\pi and the second uses σ\sigma, the expected payoff is

V(π,σ)=∑i∑jπi Aij σj.V(\pi, \sigma) = \sum_{i} \sum_{j} \pi_i \, A_{ij} \, \sigma_j.

Here πi\pi_i is the probability the first player picks move ii, and σj\sigma_j is the probability the second player picks move jj.

Definition: Minimax value and Nash equilibrium

The game value is V∗=max⁡πmin⁡σV(π,σ)V^\ast = \max_{\pi} \min_{\sigma} V(\pi, \sigma). A pair of strategies (π∗,σ∗)(\pi^\ast, \sigma^\ast) is a Nash equilibrium when neither player gains by changing alone. In a two-player zero-sum game, the minimax theorem says that the equilibrium value exists and equals the game value, and that the equilibrium strategies guarantee at least this payoff against any opponent.

The minimax idea is covered in Minimax Game, and the general solution concept in Nash Equilibrium. Self-play is a way of approaching that equilibrium without writing the matrix down.

Self-play as repeated best response

A simple self-play rule is fictitious play, proposed by George Brown in 1951. Each round, every player chooses the best response to the average of all the opponent's past moves. In a two-player zero-sum game, the empirical frequencies of play converge to an equilibrium, a result shown by Julia Robinson in 1951.

Modern self-play replaces the exact best response with a learned policy. The update is usually a reinforcement learning step such as a Policy Gradient method. The reward for the agent at the end of a game is z=+1z = +1 for a win, 00 for a draw and −1-1 for a loss. The policy moves toward moves that led to higher zz on average.

Two details that make it work

  1. Keep a pool of opponents. If the agent only plays its latest version, it can forget how to beat older strategies. Playing against a mix of past copies keeps earlier skills in the training signal.
  2. Keep exploring. A policy that becomes too sure of itself stops trying new moves. Add randomness to move choice, especially early in training, so that the games cover more of the game tree.

Worked examples

These examples use small numbers chosen to show the arithmetic. They are not records of real training runs.

Example
1. Tiny: fictitious play in rock-paper-scissors

Payoff to the first player: win +1+1, draw 00, lose −1-1. The only equilibrium is to play each move with probability 1/31/3, which has value 00.

True fictitious play answers the average of the opponent's whole history. To keep the arithmetic tiny, this example uses a simpler rule: each player answers only the opponent's previous move. Suppose both players start by playing rock, and each later round each player plays the move that beats the other's previous move.

  1. Round 1: both play rock.
  2. Round 2: each player's last move was rock, and paper beats rock. Both play paper.
  3. Round 3: each player's last move was paper, and scissors beats paper. Both play scissors.
  4. Round 4: each player's last move was scissors, and rock beats scissors. Both play rock again, and the cycle repeats.

Interpretation: over rounds 2 to 4 the agent plays paper, scissors and rock once each. The frequency of each move is 1/31/3, which matches the equilibrium, even though no single round looks balanced. Averaging over the history is what brings the mix to the equilibrium; the individual moves keep cycling. (With the full-history rule of fictitious play, the same averaging is built in.)

Example
2. The standard case: Nim, learned by self-play

Take 10 sticks. Each turn, a player removes 1, 2 or 3 sticks. The player who takes the last stick wins.

Work out which positions are losing for the player to move. With 0 sticks, the player to move has already lost. Call a position losing when every move from it leads to a winning position for the opponent.

  1. With 1, 2 or 3 sticks, the player takes all of them and wins. These are winning positions.
  2. With 4 sticks, every move leaves 3, 2 or 1 sticks, all winning for the opponent. So 4 is a losing position.
  3. With 5, 6 or 7 sticks, the player can leave 4 and win. These are winning positions.
  4. With 8 sticks, every move leaves 7, 6 or 5, all winning for the opponent. So 8 is losing.
  5. The pattern continues: the losing positions are the multiples of 4.

Interpretation: with 10 sticks, the winning move is to take 2 and leave 8. A self-play learner that plays many games against itself should, with enough games, value the positions 4 and 8 as losing and learn to leave them to the opponent. It finds this rule without being told it.

Example
3. Realistic: a game program that replaces its champion

A training loop for a board game alternates two phases. In the first, the current network generates many self-play games. In the second, a new network is trained on the results. Before the new network replaces the old one, the two play a set of evaluation games, and the new one is kept only if it wins a clear majority. AlphaGo Zero used a 55 percent threshold over a set of 400 games.

Suppose the new network wins 231 of 400 games.

  1. Win rate: 231/400=0.5775231 / 400 = 0.5775, which is 57.7557.75 percent.
  2. Compare with the threshold: 57.75≥5557.75 \geq 55, so the new network replaces the old one.
  3. Suppose instead it wins 210 of 400. Then 210/400=0.525210 / 400 = 0.525, or 52.552.5 percent. This is below 5555, so the old network stays as the champion and generates the next round of games.
  4. Interpretation: the threshold stops training from chasing noise. A small edge in a few hundred games is not strong evidence of a better player, so the loop waits for a clearer improvement.

The numbers are illustrative. The point is the rule, not the exact counts.

Tradeoffs and failure modes

SituationProblemBetter choice
Two-player zero-sum game with a clear winnerSelf-play converges well toward an equilibriumUse self-play with a pool of past opponents
Agent plays only its latest versionIt forgets how to beat strategies it has moved away fromKeep a pool of past copies as opponents
Cyclic games such as rock-paper-scissorsThe agent can cycle through strategies without improvingAverage over history, or add a population of diverse opponents
Cooperative or general-sum gameTwo players may agree on a poor outcome, and there is no single equilibrium to aim forAdd reward shaping or other signals from outside the self-play loop
Hidden information, such as cards in handThe state the search sees is incompleteUse methods designed for imperfect information

Some failure modes come up again and again:

  • Narrow style. If all games come from one policy, the data covers only that policy's favourite positions. The agent can be strong in those positions and weak elsewhere.
  • Exploitability. A policy can beat its own copy and still lose badly to a different strategy. Winning against yourself is not a guarantee of strength against others.
  • Cycles. Two agents can keep trading wins without either getting stronger, as in rock-paper-scissors.
  • Compute. Self-play needs very large numbers of games. Results in published systems depend on this scale, and small reproductions usually learn much more slowly.

Self-play is often combined with Monte Carlo Tree Search during the game, as in AlphaZero, and with a Curriculum Learning schedule that starts from easier positions.

History

In the 1950s, Arthur Samuel's checkers program learned by playing against versions of itself, one of the first demonstrations that a machine could improve at a game without a human teacher. George Brown proposed fictitious play in 1951, and Julia Robinson showed in the same year that it converges for two-player zero-sum games.

In the early 1990s, Gerald Tesauro's backgammon program TD-Gammon learned by playing itself with a neural network and temporal-difference updates. It reached a strong level of play and showed that self-play and learned evaluation could work together.

In 2017, David Silver and colleagues at DeepMind published a Go program that learned from self-play alone, with no human game records, and then the same approach was extended to chess and shogi in the AlphaZero work. Later systems for real-time strategy games used a league of many past agents as opponents, which is the population idea in the definition above.

Recap

Self-play trains an agent against copies of itself, so the opponent's difficulty rises with the agent's skill. In two-player zero-sum games the target is the minimax equilibrium, which fictitious play approaches by averaging over history. In practice you pair a Reinforcement Learning update with a pool of past opponents, enough exploration, and a gate on replacing the champion. It leads on to AlphaZero, which adds Monte Carlo Tree Search, and to league training for harder, multi-player games.

Check yourself

QuestionIn one sentence, what is the main reason self-play does not need a dataset of human games?Show answer

The agent generates its own training data by playing against copies of itself. The opponent improves at the same pace, and each game's result provides a learning signal, so the only input required is the rules and a way to score the outcome.

QuestionIn rock-paper-scissors with payoffs +1+1, 00, −1-1, what mix does the equilibrium use, and what is the game's value?Show answer

Each move with probability 1/31/3. No player can gain by moving away from this mix, and its expected payoff against any opponent is 00, which is the game value.

QuestionIn Nim with 10 sticks, taking 1, 2 or 3 per turn, which move wins, and why?Show answer

Take 2, leaving 8 sticks. Positions with 4 or 8 sticks are losing for the player to move, because every move from them leaves a winning position for the opponent. Leaving 8 hands the opponent a losing position.

QuestionWhy might keeping a pool of past copies as opponents be better than always playing the newest version?Show answer

The newest version may have forgotten how to beat strategies it has moved away from, and it may cycle between strategies without improving. A pool of past copies keeps those earlier skills in the training signal and gives a more varied opponent set.

QuestionAn agent wins 205 of 400 evaluation games against the current champion, and the threshold is 55 percent. Should the champion be replaced?Show answer

No. 205/400=0.5125205 / 400 = 0.5125, or 51.2551.25 percent, which is below 5555 percent. The difference is too small to count as clear improvement over a few hundred games, so the champion stays.

Sources