Pong Lab

How a machine learns to play Pong

The blue paddle learns with Q-learning, a classic reinforcement-learning algorithm. It is steered by a table of numbers, the Q-table, that starts empty. After every move the agent gets a reward: +1 when it wins a point, −1 when it loses one, 0 otherwise. It uses that reward to adjust one number in the table. Repeat that tens of thousands of times and it learns to play.

Generation 100 after 100 training matches 0:0
  • Blue, left: the learning agent, steered by its Q-table.
  • Grey, right: a fixed opponent that never learns. It starts chasing the ball once the ball passes 60 % of the court, and sometimes hesitates.

Six generations, one serve

Each court is the same agent saved at a different point in its training, playing the same serve at the same time. Change a generation number (0 to 100) to compare any point in training, and click a court to watch it play, large, with its loop.

What training looks like

Training is 100 matches against the same opponent. Early on the agent explores, trying random moves 70 % of the time, and explores a little less after every match.

Blue: points won minus points lost per training match, averaged over the last 10. Grey: the chance of a random move. The marks along the bottom show when each generation was saved.

What generation 100 has learned Each square is one row of the table, showing its best move. Blank squares were never visited. The outlined square is the current state.

↑ Up ↓ Down • Stay

The four ideas

State

The agent never sees the screen. It sees a summary: where the ball will cross its line (wall bounces included), how far that is from the paddle's centre, and how soon it gets there. Similar moments share one row of the table, so there are only 65 rows.

Reward

+1 for a point won, −1 for a point lost, 0 for everything else. Nobody tells the agent to follow the ball. It discovers that moves which keep the ball in play lead to fewer −1s.

Value

Each number in the table estimates the reward still to come after a move. The update pulls it towards what actually happened next: the reward plus the best value of the following state. Good news travels backwards, one move at a time.

Exploration

An agent that only picks its current best move never finds a better one. During training it sometimes moves at random. The saved generations you watch never explore; they always take their best move.

About Pong Lab

Created by Tiago Tamagusko (tamagusko@gmail.com)

The Q-learning update and its settings (α 0.05, γ 0.9, exploration from 0.7 falling by 2 % per match) follow the Grupo Turing reinforcement-learning workshop (MIT, 2020); the workshop trained for 250 matches, this page for 100. The game engine is deterministic: the match shown is the same every time and on every computer.

Everything runs in this tab, from this site alone, and training takes a fraction of a second.