On-Policy Control with Approximation
Prediction learned a value function from features; control learns to act. We carry semi-gradient methods over to action values , giving episodic semi-gradient Sarsa and its n-step form, and solve Mountain Car by descending a cost-to-go surface.
╌╌╌╌
The previous lesson learned a value function from features: replace the table with a parameterized , and update the weight vector by semi-gradient descent on the prediction error. That solves evaluation — how good is a fixed policy — but not control, where the policy itself is what we are trying to improve. Control needs action values, because a greedy step compares actions, and to compare actions from a value function you must be able to evaluate each one.
So the move is the same one that took tabular TD to Sarsa: approximate the action-value function instead of the state-value function. Write for a differentiable function of the weights, generalize the semi-gradient update to it, and interleave policy improvement by acting greedily (or -greedily) with respect to the current .1 Everything from prediction carries over; what is new is that once the state space is too large to enumerate, the whole idea of discounting the future starts to come apart — and the second half of this lesson replaces it.
Episodic semi-gradient Sarsa
In prediction the target for was some return , and the semi-gradient update nudged toward reducing the squared error between and :
The action-value form is the identical statement with in place of and a state–action argument in place of a state. For a general target estimating ,
Take to be the one-step Sarsa return, one real reward plus the discounted estimate of the next state–action pair actually taken, and this becomes episodic semi-gradient one-step Sarsa:
It is called semi-gradient for the same reason as in prediction: the target itself depends on , but we treat it as a fixed number and differentiate only the estimate that we are correcting. The gradient of the target is dropped. For a constant policy this method converges the same way TD(0) does, with the same kind of error bound.
To turn evaluation into control we need action selection and improvement on top of it. If the action set is discrete and not too large, both are immediate: in state compute for every action , then take the greedy action . Policy improvement is done by making the estimation policy a soft — say -greedy — approximation of that greedy policy, and actions are selected according to the same policy. (Continuous or very large action sets are a research topic; here the action set is small.)
- 1input: a differentiable action-value function
- 2parameters: step size ; small
- 3initialize arbitrarily (e.g. )
- 4for each episode do
- 5initial state and action of episode (e.g. -greedy)
- 6repeat
- 7take action , observe
- 8if is terminal then
- 9
- 10breakgo to next episode
- 11choose as a function of (e.g. -greedy)
- 12
- 13
- 14
- 15until is terminal
The terminal step drops the bootstrap: with of a terminal state defined to be zero, the target is just the final reward . Otherwise every step is a single semi-gradient correction toward a Sarsa return computed from the action the policy actually selected next — which is what makes it on-policy.
Mountain Car
The running example for the rest of this lesson is Mountain Car: drive an underpowered car up a steep one-dimensional hill. The engine is weaker than gravity, so from the bottom of the valley full throttle is not enough to climb the goal slope directly. The only solution is counterintuitive — reverse up the opposite hill first, then use the accumulated momentum to carry past the goal. It is a clean example of a continuous control task where things must get worse (farther from the goal) before they can get better, and undirected methods struggle with it.
The physics is a simplified two-variable system. Position and velocity update by
where enforces and . The action is full-reverse, zero, or full-forward throttle; the term is gravity along the slope. The reward is on every step until the car passes the goal at , which ends the episode. Minimizing total penalty therefore means reaching the goal in as few steps as possible. Each episode starts from a random position in with zero velocity.
Trace two steps by hand to see the dynamics. Start at the valley near , , and apply full forward throttle . The gravity term is ; with the cosine argument in radians, , so gravity contributes about . The velocity update is , and the position becomes . The engine's barely outpaces gravity here because the car sits near the flat valley bottom, where the slope is shallow. Push forward again from : gravity is still small, , so . Velocity accumulates, but slowly — and once the car climbs the right slope, where turns strongly negative, gravity's pull grows until full throttle alone stalls the climb. That stall forces the policy to reverse first: driving left up the opposite hill lets gravity there add to the engine, and the car returns through the valley with enough speed to clear the right slope on the next pass.
The two continuous state variables are turned into binary features by tile coding: eight overlapping grid-tilings, each partitioning the (position, velocity) plane, with a feature set to when the state falls in that tile and otherwise. Because the features are binary indicators, the action-value function is linear in the weights,
so and the semi-gradient update simply adds a scaled feature vector to the weights. The feature vector depends on the action as well as the state — one block of tile features per action — so the same tilings give a separate value surface for each of the three throttle settings.
The cost-to-go surface
A useful way to visualize learning is to plot the cost-to-go function, , over the (position, velocity) plane. Since every reward is , is negative and roughly counts the steps still needed to reach the goal; negating it gives a positive surface whose height approximates the number of steps remaining from each state. Learning is the process of this surface settling into the true cost-to-go.
Sutton and Barto's figure shows exactly this. The action values were initialized to zero, which is optimistic (all true values are strongly negative), so even with the agent explores widely: every state it visits turns out worse than the unrealistically high zero it started at, which continually drives it away from wherever it has been toward unexplored states. The step-428 panel, before even one episode has finished, shows the car oscillating in the valley, building the momentum it needs; by episode 9000 the surface is a clean bowl. This optimistic initialization substitutes for explicit exploration on this task.
Semi-gradient n-step Sarsa
One-step Sarsa bootstraps immediately; as in the tabular case, an intermediate level of bootstrapping usually learns faster. The n-step return generalizes to the function-approximation setting exactly as it did to state values — string real rewards, then bootstrap from at the state–action pair reached steps out:
with when , as usual. The -step update is the same semi-gradient step with this return as its target:
The same lag as tabular -step Sarsa applies — the return for step is not computable until wall-clock time — so the pseudocode updates the state steps in the past.
- 1input: a differentiable ; a policy (if estimating )
- 2parameters: step size ; small ; a positive integer
- 3initialize arbitrarily (e.g. )
- 4all store and access operations can take their index mod
- 5for each episode do
- 6initialize and store terminal
- 7select and store or -greedy in
- 8
- 9for do
- 10if then
- 11take action , observe and store and
- 12if is terminal then
- 13
- 14else
- 15select and store or -greedy in
- 16= time whose estimate is updated now
- 17if then
- 18
- 19if then
- 20
- 21
- 22until
On Mountain Car the effect is the one bootstrapping always has: an intermediate learns faster and reaches a lower steps-per-episode floor than . With well-chosen step sizes, clearly beats ; sweeping both and shows the familiar tradeoff, with a moderate (around ) sitting at the bottom of the family of U-shaped curves.
Control at scale
Episodic semi-gradient Sarsa is the on-policy, value-based core that modern control methods extend. The most important extension past Sutton & Barto addresses a limitation of this lesson: the greedy step is only tractable when the actions can be enumerated.
On-policy actor-critic and the continuous-action gap. A large or continuous
action set makes intractable — the actions cannot be enumerated
to pick the greedy one. The standard solution is to parameterize the policy directly
and learn it alongside a value estimate, the actor-critic idea that the
policy-gradient lesson
develops. Its deep, on-policy instances are A3C (Mnih et al., 2016, Asynchronous methods for deep reinforcement learning
, ICML), which runs many actor-learners in
parallel to decorrelate on-policy data without a replay buffer, and PPO (Schulman et
al., 2017, Proximal policy optimization algorithms
, arXiv:1707.06347), which
constrains each policy update with a clipped surrogate objective so the on-policy
step never moves the policy too far.2 Both keep the semi-gradient
value critic of this lesson and add a policy that handles continuous throttle
directly, rather than three discrete settings.
What carried over, and what comes next
The mechanical part of the jump from prediction to control is small. Swap for , add -greedy action selection, and the semi-gradient update is unchanged — episodic semi-gradient Sarsa and its -step form are the prediction methods with an action argument added, and Mountain Car shows them descending a cost-to-go surface into a working policy.
The subtle part is the setting, and it is large enough to need its own lesson. Once the state space is too big to enumerate, function approximation undermines discounting, and the replacement is the average-reward formulation. That continues in average-reward control for continuing tasks. Further out, relaxing the on-policy assumption exposes the sharpest instability in approximate RL — the deadly triad.
Footnotes
- Sutton & Barto, Reinforcement Learning: An Introduction (2nd ed.), Ch. 10 — On-policy Control with Approximation. §10.1 — Episodic Semi-gradient Control: the action-value semi-gradient update (10.1), episodic semi-gradient one-step Sarsa (10.2), and Example 10.1, the Mountain Car task with tile-coded features (10.3) and its cost-to-go figure (Figure 10.1). §10.2 — Semi-gradient n-step Sarsa: the n-step return (10.4) and update (10.5). ↩
- Mnih, V. et al. (2016),
Asynchronous methods for deep reinforcement learning
, ICML — A3C, parallel on-policy actor-learners. Schulman, J. et al. (2017),Proximal policy optimization algorithms
, arXiv:1707.06347 — PPO's clipped surrogate objective for stable on-policy updates. ↩
╌╌ END ╌╌