← Algorithms & ML

Reinforcement Learning

An agent with no labeled examples can still learn from the rewards its actions earn. Here Q-learning finds a safe path through a small grid world by trial and error.

Learning your way around an unfamiliar city, you try routes, keep the ones that work and back out of dead ends; nobody tells you the answer. Reinforcement learning works the same way. This part follows Q-learning as it learns a small grid world from rewards alone.

States, Actions and Rewards

Consider a grid world. An agent starts in one cell, its state, and must reach a goal. Some cells are pits with heavy penalties, and every other step costs a little. In each cell the agent chooses an action (up, down, left or right) and collects a reward for the move. Try to find the shortest safe path yourself.

Steps: 0
Total reward: 0
Figure 1. A 7x7 grid world. Blue is the start, green the goal (+10), red the pits (-10). Every other move costs -1, so the 12-step shortest route totals 11 × (-1) + 10 = -1. Click or tab to the grid and use the arrow keys, or the buttons.

With the whole map in view the route is easy to see. A thousand-cell grid with hidden rewards or slippery moves calls for an algorithm that learns from experience.

Q-Learning

Q-learning assigns a value to each (state, action) pair. The Q-value for "being in cell (3,2) and moving right" is the agent's current estimate of how good that action is from that position. All values start at zero.

In each episode the agent starts at the beginning and acts until it reaches the goal or a pit. After taking action a in state s, receiving reward r and landing in s', it moves the Q-value a step toward a new estimate, in proportion to the TD error:

The step size α sets how far each update moves; the discount factor γ, how much future rewards count.

Episode: 0
Steps last ep: –
Greedy route: –
Figure 2. Q-learning on the grid (α = 0.2, γ = 0.9, ε = 0.15). Each cell's four triangles show its action values: green high, red low, gray untried. Arrows mark the best action. "Greedy route" follows the arrows from the start; "Steps last ep" includes the random moves, so it keeps varying.

The green spreads backward from the goal one episode at a time. Knowing the map, you could instead apply the same backup to every cell at once, with the known moves and rewards in place of samples. That is value iteration (Figure 3).

Figure 3. Value iteration on the same grid: each cell's value after the chosen number of sweeps, green above zero and red below, and the largest error against the exact values on a log scale, beside the guarantee γk times the starting error. Slip sends a move sideways.

Each sweep carries value one cell farther, so the values are exact after 12 sweeps, the longest shortest route here. The general guarantee, an error shrinking by a factor γ per sweep, is loose because episodes end. Even the exact values are positive only within seven steps of the goal.

Exploration vs. Exploitation

At each step the agent either takes the action it believes best (exploiting) or tries something else that might reveal a better path (exploring). Too little exploring can lock it onto the first decent route, but here that danger is mild: values start at zero and every step costs -1, so untried actions look better than tried ones.

The parameter ε (epsilon) controls this tradeoff. With probability ε the agent picks a random action; otherwise it picks its current best according to the Q-table.

Agent A · Ep: 0
Greedy path: –
Agent B · Ep: 0
Greedy path: –
Figure 4. Two agents learn the same grid with different exploration rates (α = 0.2). Below: reward per episode, averaged over the last 10. "Greedy path" follows each agent's best actions from the start with no random moves; it reads "loops" until the values point a way to the goal.

Both greedy paths settle at 12 steps, yet the agent with the larger ε keeps collecting less reward. The update uses maxa' Q(s', a'), the best next action, whatever the agent actually does next, so the Q-values estimate what the greedy policy would earn; random moves only decide which values get updated. That makes Q-learning an off-policy method. Figure 5 repeats the race over 20 seeds.

Episodes until the greedy route settles
Mean reward per episode
Figure 5. Q-learning for 20 seeds at each exploration rate ε (rows) and step size α (columns), 300 episodes per run, γ = 0.9. First table: median episode at which the greedy route settles at 12 steps ("k/20": only k runs settled). Second: median reward per episode. Stronger color is better.

With α of at least 0.1 and ε up to 0.5, every seed settles on the 12-step route within 300 episodes. How soon depends mostly on α, while reward falls steeply with ε at every α. Since moves and rewards here are deterministic, α = 1 is fastest.

SARSA replaces the max with the Q-value of the action the agent really takes next, so it learns the value of its own exploratory behavior: it is on-policy. Where the shortest route runs beside a row of pits, SARSA learns a longer, safer route, while Q-learning learns the edge route and keeps falling in during training. This is the cliff-walking example in Sutton and Barto (Example 6.6).

The Learned Policy

After learning, the Q-values give a direction for every cell: the policy. Figure 6 shows it as arrows, bold where one action is clearly better and faint where the agent is nearly indifferent, and as a heatmap of each cell's value, its maximum Q-value.

Optimal path length: –
Total reward: –
Figure 6. The learned policy. "Show optimal path" traces the greedy route from start to goal, labeling the running total of reward after each step (-1 per move, then +10 on the last, for a final -1).

Value radiates outward from the goal. The pits barely show in the heatmap, because the best action beside a pit steps away from it, so that cell is worth as much as any other at its distance from the goal. The pits show up in the arrows instead.

When the Max Misleads

The max in the update is optimistic when rewards are noisy: if several equally bad actions have scattered estimates, the largest is too high, and Q-learning treats it as fact. In Example 6.7 of Sutton and Barto, going right from state A ends the episode with reward 0, and going left leads to B, where every action ends with a reward drawn from a normal distribution with mean -0.1 and variance 1. Double Q-learning keeps two tables, picks the best action with one and scores it with the other, so one table's lucky draws cannot inflate its own target.

Figure 7. How often each learner goes left from A, by episode, averaged over 1,000 simulated runs (ε = 0.1, α = 0.1, γ = 1). Going left is worse on average; the dotted line at 5% is what ε-greedy exploration alone would produce.

Beyond the Table

The Q-table here has 49 × 4 = 196 entries, each updated only when the agent takes that action in that cell. Most problems have far too many states for a table. Deep Q-networks (Mnih et al., 2015) replace the table with a neural network that maps a state, such as a game screen, to one Q-value per action, trained toward the same target r + γ max Q(s', a'). Learning in one state then shifts the estimates for similar states, so the target moves as the network changes; DQN steadied it with a replay buffer of past transitions and a separate copy of the network for the targets, refreshed only once every fixed number of updates.