Chapter 10 · Sutton & Barto

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:

Episodic semi-gradient one-step Sarsaw ← w + α [ Rt+1 + γ q̂(St+1, At+1, w) − q̂(St, At, w) ] ∇q̂(St, At, w)

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.

Dynamicsẋt+1 = bound[ ẋt + 0.001 At − 0.0025 cos(3xt) ],   xt+1 = bound[ xt + ẋt+1 ]

Semi-gradient Sarsa on Mountain Car

episode 1 · step 0 · last episode – steps
0.50

The car

Cost-to-go, −maxa q̂(x, ẋ, a)

Steps per episode (this run, log scale)

What to try

Step size comparison, averaged over 10 runs

Steps per episode (log scale)

α = 0.1/8α = 0.2/8α = 0.5/8

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:

Average reward of a policyr(π) = limh→∞ (1/h) Σt=1..h 𝔼[ Rt | A0:t−1 ~ π ]

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:

Differential semi-gradient Sarsaδ = R − R̄ + q̂(S′, A′, w) − q̂(S, A, w),   R̄ ← R̄ + β δ,   w ← w + α δ ∇q̂(S, A, w)

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.