Guide To AI Logo
Unit 20

Reinforcement Learning

learning optimal decision-making through interactions between agents and environments

Agent-Environment Reinforcement Loop
AGENTBrain / PolicyENVWorld / PhysicsAction (a_t)State & Reward (s_t, r_t)
Read diagram labels
  • Agent: brain / policy
  • Environment: world / physics
  • Action (aₜ)
  • State and reward (sₜ, rₜ)

Core Concepts Covered

  • Markov Decision Processes (MDPs) and Bellman equations
  • Value-based (Q-learning, DQN) and Policy-based methods
  • Actor-critic models and reinforcement learning feedback
Local Setup Recommendation

To execute and experiment with the code cells below on your local machine, ensure you have set up your isolated virtual environments and scientific libraries by following the detailed protocols in Unit 03: Environment Setup or run them in Google Colab.

1. Markov Decision Processes, Trajectories, and Returns

An agent interacts with an environment through a trajectory τ=(S0,A0,R1,S1,)\tau=(S_0,A_0,R_1,S_1,\ldots). A Markov Decision Process is (S,A,P,R,γ)(\mathcal{S},\mathcal{A},P,R,\gamma): states, actions, transition probabilities, rewards, and discount factor. The Markov property says the current state contains the information needed to predict the next transition given an action; Unit 19 develops the stochastic-process foundation.

The return after time tt is Gt=Rt+1+γRt+2+γ2Rt+3+.G_t=R_{t+1}+\gamma R_{t+2}+\gamma^2R_{t+3}+\cdots. A smaller γ\gamma favors immediate reward; a value near one gives longer-horizon consequences more weight. In finite episodes γ\gamma may equal one, while continuing tasks usually use γ<1\gamma<1 to keep returns finite.

A policy π(as)\pi(a\mid s) may be deterministic or stochastic. RL differs from supervised learning because actions affect which future observations and rewards become available.

MDP Transitions: State, Action, Reward, and Next State

s0s1s2a, r=+1a, r=+5P(s' | s,a) can branch stochastically
Read diagram labels
  • s0
  • s1
  • s2
  • a, r=+1
  • a, r=+5
  • P(s' | s,a) can branch stochastically
Worked Example 1

Discounted Trajectory Return

Problem

Rewards after a state are (2,1,4)(2,-1,4) and γ=0.5\gamma=0.5. Find GtG_t and compare it with the undiscounted finite return.

Step-by-step solution

1.Gt=2+0.5(1)+0.52(4)=20.5+1=2.5G_t=2+0.5(-1)+0.5^2(4)=2-0.5+1=2.5.

2.The undiscounted return is 21+4=52-1+4=5.

Final answer and interpretation

Discounting makes the reward two steps away contribute only 11 instead of 44.

Worked Example 2

Expected One-Step Reward

Problem

Action aa gives reward 55 with probability 0.70.7 and 2-2 with probability 0.30.3. Find its expected immediate reward.

Step-by-step solution

1.E[Rs,a]=0.7(5)+0.3(2)=2.9E[R\mid s,a]=0.7(5)+0.3(-2)=2.9.

Final answer and interpretation

This expectation alone is not the action value; future state values must also be included.

2. Value Functions and Bellman Backups

The state value is Vπ(s)=Eπ[GtSt=s]V^\pi(s)=\mathbb{E}_\pi[G_t\mid S_t=s] and the action value is Qπ(s,a)=Eπ[GtSt=s,At=a]Q^\pi(s,a)=\mathbb{E}_\pi[G_t\mid S_t=s,A_t=a]. The Bellman expectation equation decomposes value into one-step reward plus discounted value under the same policy: Vπ(s)=aπ(as)s,rp(s,rs,a)[r+γVπ(s)].V^\pi(s)=\sum_a\pi(a\mid s)\sum_{s',r}p(s',r\mid s,a)[r+\gamma V^\pi(s')].

Optimality replaces the policy average with the best next choice: V(s)=maxas,rp(s,rs,a)[r+γV(s)].V^*(s)=\max_a\sum_{s',r}p(s',r\mid s,a)[r+\gamma V^*(s')]. A Bellman backup applies one of these right-hand sides to update an estimate.

A Bellman Backup Combines Reward and Future Value

ss'₁s'₂p=.7, r=2p=.3, r=0Q(s,a)= Σ p(r + γV)one-step backup
Read diagram labels
  • s
  • s'₁
  • s'₂
  • p=.7, r=2
  • p=.3, r=0
  • Q(s,a)
  • = Σ p(r + γV)
  • one-step backup
Worked Example 1

Bellman Expectation Backup

Problem

A fixed policy takes an action that reaches s1s_1 with probability 0.750.75, reward 22, value 44; or s2s_2 with probability 0.250.25, reward 1-1, value 88. Let γ=0.9\gamma=0.9. Find the backup.

Step-by-step solution

1.Branch returns are 2+0.9(4)=5.62+0.9(4)=5.6 and 1+0.9(8)=6.2-1+0.9(8)=6.2.

Final answer and interpretation

Vπ(s)=0.75(5.6)+0.25(6.2)=5.75V^\pi(s)=0.75(5.6)+0.25(6.2)=5.75.

Worked Example 2

Bellman Optimality Choice

Problem

At state ss, action Left has expected backup 3+0.8(5)3+0.8(5) and Right has 1+0.8(8)1+0.8(8). Which is optimal?

Step-by-step solution

1.Left gives 77; Right gives 7.47.4.

Final answer and interpretation

V(s)=max(7,7.4)=7.4V^*(s)=\max(7,7.4)=7.4, so the greedy action is Right.

3. Policy Evaluation and Value Iteration

Policy evaluation repeatedly applies the Bellman expectation backup until values stabilize for a fixed policy. Policy improvement then acts greedily with respect to those values. Alternating the two gives policy iteration. Value iteration combines partial evaluation and improvement by repeatedly applying the Bellman optimality operator.

For a finite discounted MDP, the optimality operator is a contraction, so repeated exact backups converge to VV^*. In larger problems, sampling and function approximation replace exhaustive state sweeps.

Worked Example 1

Two Value-Iteration Sweeps

Problem

A state ss can Exit for reward 22, or Wait for reward 11 and deterministically return to ss. With γ=0.5\gamma=0.5 and V0(s)=0V_0(s)=0, compute V1,V2V_1,V_2.

Step-by-step solution

1.V1(s)=max(2,1+0.5(0))=2V_1(s)=\max(2,1+0.5(0))=2.

2.V2(s)=max(2,1+0.5(2))=2V_2(s)=\max(2,1+0.5(2))=2.

Final answer and interpretation

The estimate has stabilized at 22; Exit and one-step Wait are tied under this value.

Value iteration compares complete backed-up action returns, not only immediate rewards.

4. Q-Learning, Exploration, and Deep Q-Networks

Model-free Q-learning updates from an observed transition: Q(s,a)Q(s,a)+α[r+γmaxaQ(s,a)Q(s,a)].Q(s,a)\leftarrow Q(s,a)+\alpha\big[r+\gamma\max_{a'}Q(s',a')-Q(s,a)\big]. The bracketed quantity is the temporal-difference (TD) error. It is off-policy because its target uses the greedy next action even when behavior explores.

ϵ\epsilon-greedy selects a random action with probability ϵ\epsilon and otherwise a greedy action. With mm actions and a unique greedy action, that action's total probability is 1ϵ+ϵ/m1-\epsilon+\epsilon/m. A stochastic policy instead learns a full probability distribution, which is useful for naturally uncertain or continuous control.

A DQN replaces the table with Q(s,a;θ)Q(s,a;\theta). An experience replay buffer breaks short-range correlations and reuses transitions in random mini-batches. A separate target network supplies slowly changing bootstrap targets. Without these devices, moving targets, correlated data, and maximization bias can destabilize learning; monitor seeds and learning curves rather than trusting one run.

Q-Table Learning and Random Experience Replay

Q(s,a) table(s, a, r, s')(s, a, r, s')(s, a, r, s')(s, a, r, s')(s, a, r, s')random mini-batch breaks correlation
Read diagram labels
  • Q(s,a) table
  • (s, a, r, s')
  • random mini-batch breaks correlation
Worked Example 1

Complete Q-Learning Update

Problem

Given Q(s,a)=2Q(s,a)=2, reward r=3r=3, γ=0.9\gamma=0.9, maxaQ(s,a)=4\max_{a'}Q(s',a')=4, and α=0.5\alpha=0.5, update Q(s,a)Q(s,a).

Step-by-step solution

1.TD target is 3+0.9(4)=6.63+0.9(4)=6.6.

2.TD error is 6.62=4.66.6-2=4.6.

Final answer and interpretation

Updated value is 2+0.5(4.6)=4.32+0.5(4.6)=4.3.

Worked Example 2

ϵ\epsilon-Greedy Probabilities

Problem

There are four actions, one uniquely greedy, and ϵ=0.2\epsilon=0.2. What probability does each action receive?

Step-by-step solution

1.Exploration assigns 0.2/4=0.050.2/4=0.05 to every action.

2.The greedy action also receives exploitation mass 0.80.8, totaling 0.850.85.

Final answer and interpretation

Each non-greedy action receives 0.050.05.

5. Policy Gradients, Advantages, and Actor–Critic

Policy gradients directly maximize J(θ)=Eτπθ[G0]J(\theta)=\mathbb{E}_{\tau\sim\pi_\theta}[G_0]. The score-function estimator weights log-policy gradients by returns: θJ(θ)=E[θlogπθ(AtSt)Gt].\nabla_\theta J(\theta)=\mathbb{E}[\nabla_\theta\log\pi_\theta(A_t\mid S_t)G_t]. High-return actions become more probable; a baseline can reduce variance without changing the expected gradient.

The advantage A(s,a)=Q(s,a)V(s)A(s,a)=Q(s,a)-V(s) says whether an action is better than the state's baseline. Actor–critic methods use an actor for πθ\pi_\theta and a critic for VϕV_\phi or QϕQ_\phi. A one-step TD error δt=Rt+1+γV(St+1)V(St)\delta_t=R_{t+1}+\gamma V(S_{t+1})-V(S_t) is a practical advantage estimate.

PPO constrains policy changes through a clipped probability-ratio objective, improving reliability compared with unconstrained large policy steps. It remains sensitive to reward design, normalization, rollout length, and implementation details.

Actor–Critic Learning Loop

ACTORπθ(a|s)ENVr, s'CRITICVφ(s)advantage / TD error guides policy update
Read diagram labels
  • ACTOR
  • πθ(a|s)
  • ENV
  • r, s'
  • CRITIC
  • Vφ(s)
  • advantage / TD error guides policy update
Worked Example 1

TD Error and Actor Direction

Problem

The critic has V(s)=5V(s)=5, observes reward 22, next value V(s)=6V(s')=6, and γ=0.9\gamma=0.9. Find the TD error and interpret the actor update.

Step-by-step solution

1.δ=2+0.9(6)5=2.4\delta=2+0.9(6)-5=2.4.

2.The positive TD error means the action performed better than the critic expected.

Final answer and interpretation

An actor update weighted by 2.42.4 increases the log-probability of that sampled action.

Worked Example 2

Return-Weighted Policy Gradient

Problem

Two sampled log-policy gradients are g1=(1,0)g_1=(1,0) and g2=(0,2)g_2=(0,2) with returns 33 and 1-1. Estimate their unnormalized average direction.

Step-by-step solution

1.Weight and sum: 3g1+(1)g2=(3,2)3g_1+(-1)g_2=(3,-2).

2.Average across two samples: (1.5,1)(1.5,-1).

Final answer and interpretation

The positive-return action is reinforced while the negative-return action is suppressed.

Worked Example 3

Cumulative Algorithm Choice

Problem

Choose a starting family for a tiny known MDP, an Atari-like pixel environment with discrete actions, and a continuous robot controller.

Step-by-step solution

1.Use value or policy iteration for the tiny known transition model.

2.Use a DQN-family method for high-dimensional observations with a small discrete action set.

Final answer and interpretation

Use a stochastic actor–critic policy for continuous actions.

Interactive Practice Quiz

Test your understanding with instant feedback

QUESTION 01

What does the discount factor γ\gamma control?

QUESTION 02

What does a Bellman expectation backup use for the next action?

QUESTION 03

What is the Q-learning TD target?

QUESTION 04

Why use an experience replay buffer in DQN?

QUESTION 05

What does the critic estimate in an actor–critic method?

QUESTION 06

If V(s)=4V(s)=4, r=1r=1, V(s)=5V(s')=5, and γ=0.8\gamma=0.8, what is the one-step TD error?

QUESTION 07

With three actions, a unique greedy action, and ϵ=0.3\epsilon=0.3, what is the greedy action's total probability?

QUESTION 08

What is the main role of a DQN target network?