5.1.9Reinforcement Learning Foundations

Dynamic programming (value - policy iteration)

3,280 words15 min readdifficulty · medium

Overview

Dynamic programming (DP) methods solve Markov Decision Processes (MDPs) by breaking them into subproblems and caching solutions. They compute optimal value functions and optimal policies through iterative updates that leverage the Bellman optimality equations.

Why DP matters: DP is theoretical foundation for modernRL algorithms. Even though pure DP requires a perfect model (transition probabilities and rewards), understanding it unlocks model-free methods like Q-learning and actor-critic, which approximate DP without a model.

Figure — Dynamic programming (value - policy iteration)

Core Concepts


The Two Bellman Equations

Bellman Expectation Equation (for a given policy)

Vπ(s)=aπ(as)s,rp(s,rs,a)[r+γVπ(s)]V^\pi(s) = \sum_a \pi(a|s) \sum_{s',r} p(s',r|s,a)[r + \gamma V^\pi(s')]

Derivation from first principles:

Start with the definition of value: Vπ(s)=Eπ[GtSt=s]V^\pi(s) = \mathbb{E}_\pi[G_t | S_t = s]

where Gt=Rt+1+γRt+2+γ2Rt+3+G_t = R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + \ldots is the return.

Separate the first reward from the rest: Vπ(s)=Eπ[Rt+1+γ(Rt+2+γRt+3+)St=s]V^\pi(s) = \mathbb{E}_\pi[R_{t+1} + \gamma(R_{t+2} + \gamma R_{t+3} + \ldots) | S_t = s]

The term in parentheses is just Gt+1G_{t+1}, the return from the next state: Vπ(s)=Eπ[Rt+1+γGt+1St=s]V^\pi(s) = \mathbb{E}_\pi[R_{t+1} + \gamma G_{t+1} | S_t = s]

By the definition of value function, E[Gt+1St+1=s]=Vπ(s)\mathbb{E}[G_{t+1}|S_{t+1}=s'] = V^\pi(s'): Vπ(s)=Eπ[Rt+1+γVπ(St+1)St=s]V^\pi(s) = \mathbb{E}_\pi[R_{t+1} + \gamma V^\pi(S_{t+1}) | S_t = s]

Expand the expectation over actions (from policy π\pi) and next states (from dynamics pp): Vπ(s)=aπ(as)s,rp(s,rs,a)[r+γVπ(s)]V^\pi(s) = \sum_a \pi(a|s) \sum_{s',r} p(s',r|s,a)[r + \gamma V^\pi(s')]

WHY each component:

  • π(as)\pi(a|s): Policy probability of taking action aa in state ss
  • p(s,rs,a)p(s',r|s,a): Environment dynamics (transition probability)
  • rr: Immediate reward we actually receive
  • γVπ(s)\gamma V^\pi(s'): Discounted value of where we end up

Bellman Optimality Equation

V(s)=maxasums,rp(s,rs,a)[r+γV(s)]V^*(s) = \max_asum_{s',r} p(s',r|s,a)[r + \gamma V^*(s')]

Derivation:

The optimal value is the maximum over all possible policies: V(s)=maxπVπ(s)V^*(s) = \max_\pi V^\pi(s)

For the optimal policy π\pi^*, it must choose the action that maximizes expected return: V(s)=maxaQ(s,a)V^*(s) = \max_a Q^*(s,a)

where Q(s,a)=E[Rt+1+γV(St+1)St=s,At=a]Q^*(s,a) = \mathbb{E}[R_{t+1} + \gamma V^*(S_{t+1}) | S_t=s, A_t=a]

Expand the expectation: V(s)=maxas,rp(s,rs,a)[r+γV(s)]V^*(s) = \max_a \sum_{s',r} p(s',r|s,a)[r + \gamma V^*(s')]

WHY the max operator: The optimal policy always picks the best action, so we maximize over actions rather than averaging over a policy distribution.


Algorithm1: Policy Iteration

Policy iteration alternates between two steps: policy evaluation (compute VπV^\pi for current policy) and policy improvement (make policy greedy).

Step 1: Policy Evaluation

Goal: Compute Vπ(s)V^\pi(s) for all states under current policy π\pi.

Algorithm:

  1. Initialize V(s)V(s) arbitrarily (e.g., 0 for all ss)
  2. Repeat until convergence (change < θ\theta):
    • For each state ss: Vnew(s)aπ(as)s,rp(s,rs,a)[r+γV(s)]V_{\text{new}}(s) \leftarrow \sum_a \pi(a|s) \sum_{s',r} p(s',r|s,a)[r + \gamma V(s')]
    • VVnewV \leftarrow V_{\text{new}}

WHY this works: Each iteration applies the Bellman expectation equation. Since it's a contraction mapping (due to γ<1\gamma < 1), repeated applications converge to the unique fixed point VπV^\pi.

WHY this step: "The update shrinks the distance to the true value function by a factor of γ\gamma with each sweep, so it must converge."

Step 2: Policy Improvement

Goal: Make policy better by acting greedily w.r.t. current value function.

Algorithm: For each state ss: πnew(s)=argmaxas,rp(s,rs,a)[r+γV(s)]\pi_{\text{new}}(s) = \arg\max_a \sum_{s',r} p(s',r|s,a)[r + \gamma V(s')]

WHY this improves the policy (Policy Improvement Theorem):

For the gredy action a=argmaxaQπ(s,a)a' = \arg\max_a Q^\pi(s,a): Qπ(s,a)Qπ(s,π(s))=Vπ(s)Q^\pi(s, a') \geq Q^\pi(s, \pi(s)) = V^\pi(s)

If we follow aa' then continue with π\pi, we get at least as much value: Vπ(s)Qπ(s,π(s))=Vπ(s)V^\pi(s) \leq Q^\pi(s, \pi'(s)) = V^{\pi'}(s)

Since this holds for all states, π\pi' is at least as good as π\pi. If π\pi' is not strictly better, then π\pi is already optimal.

Full Policy Iteration Loop

1. Initialize π arbitrarily
2. Repeat:
   a. V ← PolicyEvaluate(π)  // converge to V^π
   b. π' ← Gredy(V)
   c. If π' = π, stop (optimal!)
   d. π ← π'

Convergence: Policy iteration converges in a finite number of steps (at most AS|\mathcal{A}|^{|\mathcal{S}|} policies, but usually much faster). Each iteration strictly improves the policy until optimality.


Algorithm 2: Value Iteration

Value iteration combines policy evaluation and improvement into a single update step. Instead of fully evaluating a policy, we do one backup per state and immediately use it for the next iteration.

Algorithm:

  1. Initialize V(s)V(s) arbitrarily (e.g., 0 for all ss)
  2. Repeat until convergence (change < θ\theta):
    • For each state ss: Vnew(s)maxas,rp(s,rs,a)[r+γV(s)]V_{\text{new}}(s) \leftarrow \max_a \sum_{s',r} p(s',r|s,a)[r + \gamma V(s')]
    • VVnewV \leftarrow V_{\text{new}}
  3. Extract optimal policy: π(s)=argmaxas,rp(s,rs,a)[r+γV(s)]\pi^*(s) = \arg\max_a \sum_{s',r} p(s',r|s,a)[r + \gamma V(s')]

WHY this works: Each update applies the Bellman optimality equation. We're directly computing VV^* without maintaining an explicit policy during iteration. The max operator implicitly improves the policy every step.

Key insight: Value iteration is policy iteration with truncated policy evaluation (just one sweep instead of converging fully). This is often more efficient because the policy might improve before VπV^\pi fully converges.


Policy vs Value Iteration: The Tradeoff

| Aspect | Policy Iteration | Value Iteration | |--------|------------------| | Update | Full policy evaluation + improvement | Single optimality backup | | Iterations | Fewer policy changes (3-5 typically) | More backups (10-100s) but cheaper | | Per-iteration cost | High (many sweps to converge VπV^\pi) | Low (one sweep) | | Total cost | Often similar or better for small γ\gamma | Often better for large γ\gamma | | Intermediate policy | Always have a well-defined policy | No explicit policy until end |

When to use which:

  • Policy iteration: When evaluating a policy is fast (small state space, high γ\gamma), or when you want intermediate policies during computation.
  • Value iteration: When policy evaluation is expensive, or when you only care about the final optimal policy.
  • Asynchronous DP: Update states in any order (focus on high-error states), converges faster in practice.

Computational Complexity

Per iteration:

  • Policy evaluation: O(S2A)O(|\mathcal{S}|^2 |\mathcal{A}|) per sweep (for each state, sum over actions and next states)
  • Value iteration backup: O(S2A)O(|\mathcal{S}|^2 |\mathcal{A}|) per sweep

Total iterations:

  • Policy iteration: O(AS)O(|\mathcal{A}|^{|\mathcal{S}|}) worst case (every policy), but typically3-10 in practice
  • Value iteration: O(log(1/θ)log(1/γ))O\left(\frac{\log(1/\theta)}{\log(1/\gamma)}\right) to reach error threshold θ\theta

WHY γ\gamma affects convergence: The contraction factor is γ\gamma. Each iteration reduces error by at least factor γ\gamma, so smaller γ\gamma (less far-sighted) converges faster.

Space complexity: O(S)O(|\mathcal{S}|) to store VV and π\pi.


Connections to Other RL Methods

Relationship to temporal-difference learning:

  • DP: Model-based (needs p(s,rs,a)p(s',r|s,a)), sweps all states
  • TD learning: Model-free (learns from samples), updates one state at a time
  • TD is essentially DP with sampled backups instead of full expectation

Generalized Policy Iteration (GPI): All RL methods can be viewed as GPI: interleaving policy evaluation (making value function consistent with policy) and policy improvement (making policy greedy w.r.t. value). DP does this with exact computation; TD/Q-learning approximate it with samples.

Monte Carlo methods:

  • DP: Bootstrap (use estimated V(s)V(s') in updates)
  • Monte Carlo: No bootstrap (wait for actual return GtG_t)
  • Tradeoff: DP has lower variance but needs a model

Modern deep RL:

  • AlphaZero: Policy iteration with neural networks for VV and π\pi, Monte Carlo tree search for evaluation
  • Actor-critic: Policy gradient (improvement) + TD learning (evaluation)

Connections


Recall Explain to a 12-year-old

Imagine you're playing a board game where you want to reach the finish as fast as possible. You don't know the best strategy yet, so you try this: Plan 1 (Policy Iteration):

  1. Start with a random strategy: "I'll just roll the dice and move however I feel."
  2. Now pretend you follow that strategy for many games and calculate "How many turns does it take on average from each square?"
  3. Look at your strategy again: "Wait, if I'm on square 10, my strategy says go left, but if I go right instead, I reach the finish faster!"
  4. Update your strategy to always pick the move that gets to lower-turn squares.
  5. Repeat steps 2-4 until your strategy stops changing.

Plan 2 (Value Iteration):

  1. Start by guessing "0 turns from every square."
  2. Update each square: "If I'm here, what's the fastest I could finish?" Look at all possible moves and pick the best.
  3. Repeat step 2 until the numbers stop changing.
  4. Your final strategy: always move to the square with the lowest number.

Both plans find the perfect strategy! The first one is like trying a strategy, testing it, then fixing it. The second is like calculating the best possible score for each square and following that.


Flashcards

What are the two main DP algorithms for solving MDPs? :: Policy iteration and value iteration.

What is the key difference between policy evaluation and value iteration updates?
Policy evaluation uses expectation over policy: aπ(as)\sum_a \pi(a|s). Value iteration uses max over actions: maxa\max_a.
What is the Bellman expectation equation?
Vπ(s)=aπ(as)s,rp(s,rs,a)[r+γVπ(s)]V^\pi(s) = \sum_a \pi(a|s) \sum_{s',r} p(s',r|s,a)[r + \gamma V^\pi(s')]
What is the Bellman optimality equation?
V(s)=maxas,rp(s,rs,a)[r+γV(s)]V^*(s) = \max_a \sum_{s',r} p(s',r|s,a)[r + \gamma V^*(s')]
Why does policy improvement theorem guarantee that gredy policy is better?
Because Qπ(s,π(s))Qπ(s,π(s))=Vπ(s)Q^\pi(s, \pi'(s)) \geq Q^\pi(s, \pi(s)) = V^\pi(s) for all states, so VπVπV^{\pi'} \geq V^\pi.
What is the contraction factor inDP convergence?
γ\gamma (discount factor). Error shrinks by factor γ\gamma each iteration.
What is the stopping criterion for value iteration given tolerance θ\theta?
Vk+1Vk<θ||V_{k+1} - V_k||_\infty < \theta implies VkV<2θγ1γ||V_k - V^*||_\infty < \frac{2\theta\gamma}{1-\gamma}.
What is generalized policy iteration (GPI)?
The framework of interleaving policy evaluation and policy improvement, which underlies all RL algorithms.
Why does value iteration often converge faster than policy iteration per wall-clock time?
Each iteration is cheaper (one backup vs full convergence), and truncated evaluation is often sufficient for policy improvement.
What is the key requirement that makes DP infeasible for large real-world RL problems?
DP requires the full MDP model: transition probabilities p(s,rs,a)p(s',r|s,a) for all states and actions.
When should you prefer policy iteration over value iteration?
When the state space is small and policy evaluation is fast, or when γ\gamma is high (closer to 1).
What is in-place/asynchronous DP and why use it?
Updating states in any order (even focusing on high-error states) using latest values. Converges faster than synchronous sweps in practice.

Concept Map

solves

leverages

defines

related to

computes

feeds into

greedy w.r.t.

loops back to

converges to

yields

foundation for

approximates without model

Markov Decision Process

Dynamic Programming

Bellman Equations

Policy Evaluation

Policy Improvement

State Value V-pi

Action Value Q-pi

Optimal Value V-star

Optimal Policy pi-star

Model-Free Methods

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Chalo is topic ko simple tarike se samajhte hain. Dynamic Programming ka core idea yeh hai ki bade problem ko chote-chote subproblems mein tod do, aur unke solutions ko cache (save) kar lo taaki baar-baar calculate na karna pade. Socho ki tum ek maze mein shortest path dhundh rahe ho — instead of har route try karna, tum ulta soch ke chalte ho: "Agar main exit se ek step door hoon, to best move kya hai?" Phir "Do step door hoon to?" Is tarah tum chote answers build karte ho aur unhe reuse karte ho bade problems ke liye. Yahi kaam DP karta hai MDPs (Markov Decision Processes) ke saath, aur iska dil hai Bellman equation — jo kehti hai ki kisi state ka value equals immediate reward plus aage jahan pahunchoge uska discounted value.

Ab yahan do important equations aati hain. Pehli hai Bellman Expectation Equation, jo ek given policy ke liye value nikalti hai — yaani agar tum ek fixed strategy follow kar rahe ho to har state ka expected return kya hoga. Dusri hai Bellman Optimality Equation, jismein max operator lagta hai — yeh best possible action choose karti hai har state par, na ki average leti hai. Yeh max hi difference hai: expectation equation average nikalti hai policy ke according, jabki optimality equation hamesha best action pick karti hai. In dono ke saath humein V (state value), Q (action value), aur optimal policy π* milti hai, jo har state mein sabse acchi action batati hai.

Yeh cheez isliye important hai kyunki DP modern Reinforcement Learning ka theoretical foundation hai. Sach hai ki pure DP ko ek perfect model chahiye (transition probabilities aur rewards ka poora pata hona chahiye), jo real world mein aksar nahi milta. Lekin agar tum DP ache se samajh loge, to tumhe Q-learning aur actor-critic jaise model-free methods bhi easily samajh aayenge — yeh saare methods basically DP ko approximate karte hain bina model ke. To yeh topic ek building block hai jispe aage ki puri RL ki knowledge tiki hui hai, isliye ise strong karna zaroori hai.

Go deeper — visual, from zero

Test yourself — Reinforcement Learning Foundations

Connections