S14 · Lesson 46 · Reinforcement learning
Learning to act from rewards, Markov decision processes with discounted returns, Bellman's equations, value and policy iteration, Q-learning, exploration in bandit problems with upper confidence bounds, and policy gradients
Every model so far learned from examples labeled with the right answer. A battery-powered sensor deciding when to transmit, a controller tuning a power converter or a program playing a game gets no labels, only rewards, often long after the action that earned them. Reinforcement learning turns that into an optimization over policies. Its central equation, Bellman's, also underlies the optimal control of S9: the linear–quadratic regulator is a reinforcement learning problem whose answer happens to have a closed form.
- An agent discounts a reward t steps away by . The solid curve is the share of a steady reward stream's total discounted value collected within the first T steps, .
- The dashed curve is , the factor by which T sweeps of value iteration shrink the error in a value estimate: the same discount sets both how far ahead the agent looks and how fast planning converges.
- With γ = 0.99 the agent effectively plans about 100 steps ahead, and value iteration needs hundreds of sweeps.
Predict
An agent's discount factor is 0.99. Roughly how far ahead does it effectively plan?
A steady reward r is worth = 100r in total, as if the agent counted the next = 100 steps fully and ignored the rest; 63% of that value comes within the first 100 steps. The discount is a horizon setting: 0.9 looks about 10 steps ahead, 0.999 about 1000.
Name it
Markov decision processes
At each step the agent sees a state s, takes an action a, receives a reward r and moves to a new state with probability . It seeks a policy π, a rule for choosing actions, that maximizes the expected return , with discount 0 ≤ γ < 1.
Values and Bellman's equations
A policy's value is its expected return from s; the action value is the return from taking a first and following π afterward. Values satisfy
a linear system, , for a fixed policy. The optimal values satisfy Bellman's optimality equation
and acting greedily with respect to is optimal.
Planning with a model
Value iteration applies the optimality equation as an update. Each sweep shrinks the largest error by at least γ, so reaching a fraction ε of the starting error takes sweeps. Policy iteration alternates solving for exactly and making π greedy; it usually converges in a handful of iterations.
Learning without a model
Q-learning updates after every step from what actually happened:
It converges to if every state–action pair keeps being tried, which needs exploration: the ε-greedy rule takes a random action a fraction ε of the time. With a neural network in place of the table, this is deep Q-learning.
Exploration in bandits
With one state and K actions of unknown mean reward (a bandit), the UCB1 rule picks the action maximizing
where is how often a has been tried in t steps. The bonus shrinks as an action is tried, so poorly known actions get tested, and the total shortfall against always choosing the best action (the regret) grows only as ln t.
Policy gradients
A parameterized policy can be improved directly by gradient ascent on its expected return: , estimated from sampled episodes (REINFORCE). Subtracting a baseline, such as an estimate of V(s), from G reduces the estimate's variance without biasing it.
Worked example
A sensor earns a reward of 1 per step while charged, and goes low with probability 0.2 per step; while low it earns nothing and recharges with probability 0.5. With γ = 0.9, what is each state worth?
Show the solution
- Bellman equations.
- Solve. The second gives ; put that in the first:
- Read it. The low state is worth almost as much as the charged one because it recharges quickly.
- Check. Over the long run the sensor is charged 0.5/0.7 = 71% of the time, and is the average of the two values, weighted by that share.
Answer the charged state is worth 7.53 and the low state 6.16.
Worked example
Q(s, a) = 2.0. The agent takes a, receives r = 1 and lands where the best action value is 3.0. With γ = 0.9 and α = 0.1, what is the update? And how many value-iteration sweeps shrink an initial error of 10 below 0.001?
Show the solution
- Target.
- Update. The error is 1.7, so
- Value-iteration sweeps. needs so 88 sweeps.
- Compare. Each Q-learning step moves only one entry a tenth of the way, which is why model-free learning needs far more experience than planning with a known model needs sweeps.
Answer Q becomes 2.17, and value iteration needs 88 sweeps.
Worked example
After 1000 pulls of a bandit, action A has averaged 0.60 over 100 pulls and action B 0.50 over 10. Which does UCB1 pick next?
Show the solution
- Bonuses.
- Indices. so UCB1 tries B again: with only 10 pulls, B's true mean could well exceed A's.
- Later on. As B's count grows its bonus falls, and if B really is worse, it gets tried only about ln t times more.
Answer UCB1 picks B, whose index of 1.68 beats A's 0.97.
Check
An agent with discount 0.95 receives a steady reward of 1 per step.
(a)
What is the stream's discounted value?
(b)
What is its effective horizon?
Check
Value iteration runs with discount 0.95.
(a)
How many sweeps shrink the error by a factor of 0.01?
Check
Q(s, a) = 0. The agent receives r = −1 and lands where the best action value is 10; γ = 0.99 and the learning rate is 0.05.
(a)
What is the temporal-difference error?
(b)
What does Q(s, a) become?
Check
A fixed policy earns 1 per step in state 1 and 0 in state 2. It moves from state 1 to 2 with probability 0.3 and from 2 to 1 with probability 0.6; γ = 0.8.
(a)
What is state 1 worth?
(b)
And state 2?
Check
A bandit action has averaged 0.3 over 500 of 100 pulls.
(a)
What is its UCB1 index?
Check
An ε-greedy agent with ε = 0.1 chooses among 2 actions.
(a)
How often does it take the greedy action?
Check
Why does Q-learning need exploration even though it learns the value of the greedy policy?
Check
A fixed policy earns r in state 1 and u in state 2, moving 1 → 2 with probability a and 2 → 1 with probability b; discount γ.
Step 1
What is ?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What is ?
Check
Write value_iteration(P, R, gamma, tol) for a finite MDP: P[s][a] is a list of (probability, next state) pairs and R[s][a] the reward. Iterate until no value changes by more than tol, and return (values, greedy policy) as lists.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does every action's value depend on what the agent will do afterward?
An action's worth isn't just its immediate reward: it also moves the agent to a new state, from which future rewards depend on the actions taken there. So its value is the reward plus the discounted value of where it leads, under whatever policy follows, which is Bellman's equation. That recursion is what lets a reward earned much later be credited to the early action that made it possible: values propagate backward through the states, one step per sweep of value iteration or per visit in Q-learning, until every state's value reflects the best reachable future.
Practice
Keep practicing
9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.