Control with function approximation
Chapter 9 approximated state values for a fixed policy. For control we approximate action values instead, act ε-greedily on them, and keep improving: Sarsa with a function in place of a table. This page runs it on Mountain Car, a continuous-state task, and then turns to continuing tasks, where function approximation changes how goals should be defined.
01Episodic semi-gradient Sarsa
Approximate qπ(s, a) by q̂(s, a, w) and apply the semi-gradient update to state–action pairs. With a Sarsa target:
At a terminal next state the target is just Rt+1. For control, act greedily or ε-greedily on q̂, so that improvement happens continuously alongside evaluation. With a linear q̂ and binary features, ∇q̂ is the feature vector x(s, a): each update adds α × δ to the weights of the features active for the state–action pair just taken.
02Worked example: Mountain Car
An underpowered car sits in a valley and must reach the top of the hill on the right (Sutton & Barto, Example 10.1). Its engine is too weak to drive straight up. It has to back up the left slope first and use gravity to build momentum. The state is position x ∈ [−1.2, 0.5] and velocity ẋ ∈ [−0.07, 0.07]. The actions are full throttle backward, zero, or forward. Each step costs −1 until the car passes x = 0.5. The values are represented with tile coding: 8 offset 8×8 grids over (x, ẋ), separately for each action, 1,944 weights in all. All weights start at 0, which is optimistic since every true value is negative, so the agent explores even with ε = 0.
Semi-gradient Sarsa on Mountain Car
The car
Cost-to-go, −maxa q̂(x, ẋ, a)
Steps per episode (this run, log scale)
What to try
- Play the first episode. The car rocks back and forth: every visited pair's value drops below zero, so the greedy action keeps switching to whatever hasn't been tried. That is optimism driving exploration.
- Watch the cost-to-go map during the first episode. A dark ridge grows along the states the car keeps revisiting, while unvisited states stay at 0, which looks attractive.
- Run +100 episodes. Episode length falls from hundreds or thousands of steps to roughly 100–150. The map fills in: the bright peak sits at the valley floor with little speed, the hardest place to start, and cost falls off toward states with enough momentum to coast out.
- Try α × 8 = 0.1 versus 0.5. A smaller step learns more slowly but more steadily, as the averaged chart below shows.
Step size comparison, averaged over 10 runs
Steps per episode (log scale)
03n-step semi-gradient Sarsa
The n-step return of chapter 7 carries over unchanged, with q̂ in the bootstrap term: Gt:t+n = Rt+1 + … + γn−1Rt+n + γnq̂(St+n, At+n, w). On Mountain Car an intermediate n, such as 8, learns faster and reaches better performance than one-step Sarsa, as it did with tables.
04Continuing tasks: the average-reward setting
For tasks that never end, Part I used discounting. With function approximation that becomes awkward. The agent can no longer represent the value of every state exactly, so we need a single number that ranks policies. The natural one is the average reward, the long-run reward per step:
Values are then defined relative to that average. The differential return sums how much each reward exceeds it, Gt = (Rt+1 − r(π)) + (Rt+2 − r(π)) + …, and the Bellman equations and TD errors keep their form with γ removed and r(π) subtracted from every reward:
R̄ is a running estimate of r(π), updated by the TD error. In an access-control queuing task, where an agent decides whether to accept customers of different priority onto a limited set of servers, this algorithm learns to reject low-priority customers when few servers are free and accept them when many are.
05Why not just discount?
In a continuing task with approximation, discounting turns out not to change which policy is best. Weight each state's discounted value by how often the policy visits it, and the total is exactly r(π)/(1−γ), the same ranking as the average reward for every γ. Worse, the policy improvement theorem, which made GPI work for tables, no longer holds once states share parameters: improving the policy in one state can make others worse. So in this setting γ is best understood as a parameter of the solution method, not of the problem, and the average-reward formulation is the principled one.