Dynamic programming (value - policy iteration)
5.1.9· AI-ML › Reinforcement Learning Foundations
Overview
Dynamic programming (DP) methods Markov Decision Processes (MDPs) ko subproblems mein todkar aur solutions cache karke solve karte hain. Ye optimal value functions aur optimal policies ko iterative updates ke zariye compute karte hain jo Bellman optimality equations ka use karte hain.
DP kyun important hai: DP modern RL algorithms ki theoretical foundation hai. Bhaleki pure DP ke liye ek perfect model chahiye (transition probabilities aur rewards), ise samajhna model-free methods jaise Q-learning aur actor-critic ko unlock karta hai, jo model ke bina DP ko approximate karte hain.

Core Concepts
The Two Bellman Equations
Bellman Expectation Equation (ek given policy ke liye)
First principles se derivation:
Value ki definition se shuru karo:
jahan return hai.
Pehle reward ko baaki se alag karo:
Parentheses wala term bas hai, yaani next state se return:
Value function ki definition se, :
Actions (policy se) aur next states (dynamics se) par expectation expand karo:
WHY har component:
- : State mein action lene ki policy probability
- : Environment dynamics (transition probability)
- : Immediate reward jo hum actually receive karte hain
- : Jahan hum pahunchte hain uska discounted value
Bellman Optimality Equation
Derivation:
Optimal value sabhi possible policies par maximum hai:
Optimal policy ke liye, use wo action choose karna chahiye jo expected return maximize kare:
jahan
Expectation expand karo:
WHY max operator: Optimal policy hamesha best action pick karti hai, isliye hum policy distribution par average lene ki jagah actions par maximize karte hain.
Algorithm 1: Policy Iteration
Policy iteration do steps ke beech alternate karta hai: policy evaluation (current policy ke liye compute karo) aur policy improvement (policy ko greedy banao).
Step 1: Policy Evaluation
Goal: Current policy ke under sabhi states ke liye compute karo.
Algorithm:
- ko arbitrarily initialize karo (e.g., sabhi ke liye 0)
- Convergence tak repeat karo (change < ):
- Har state ke liye:
WHY this works: Har iteration Bellman expectation equation apply karta hai. Kyunki yeh ek contraction mapping hai ( ki wajah se), repeated applications unique fixed point par converge hoti hain.
WHY this step: "Yeh update true value function se distance ko har sweep mein factor se shrink karta hai, isliye yeh zaroor converge hoga."
Step 2: Policy Improvement
Goal: Current value function ke w.r.t. greedily act karke policy ko better banao.
Algorithm: Har state ke liye:
WHY this policy improve karta hai (Policy Improvement Theorem):
Greedy action ke liye:
Agar hum follow karein phir ke saath continue karein, toh hume kam se kam utni hi value milegi:
Kyunki yeh sabhi states ke liye hold karta hai, kam se kam jitni acchi hai. Agar strictly better nahi hai, toh already optimal hai.
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 finite number of steps mein converge hoti hai (zyada se zyada policies, lekin usually bahut faster). Har iteration policy ko strictly improve karti hai jab tak optimality na aa jaaye.
Algorithm 2: Value Iteration
Value iteration policy evaluation aur improvement ko ek single update step mein combine karta hai. Kisi policy ko fully evaluate karne ki jagah, hum ek backup per state karte hain aur use immediately next iteration ke liye use karte hain.
Algorithm:
- ko arbitrarily initialize karo (e.g., sabhi ke liye 0)
- Convergence tak repeat karo (change < ):
- Har state ke liye:
- Optimal policy extract karo:
WHY this works: Har update Bellman optimality equation apply karta hai. Hum directly compute kar rahe hain bina iteration ke dauran explicit policy maintain kiye. Max operator implicitly har step policy improve karta hai.
Key insight: Value iteration, policy iteration hai jisme truncated policy evaluation hai (fully converge karne ki jagah bas ek sweep). Yeh aksar zyada efficient hota hai kyunki policy fully converge hone se pehle improve ho sakti hai.
Policy vs Value Iteration: The Tradeoff
| Aspect | Policy Iteration | Value Iteration |
|---|---|---|
| Update | Full policy evaluation + improvement | Single optimality backup |
| Iterations | Kam policy changes (typically 3-5) | Zyada backups (10-100s) lekin sasta |
| Per-iteration cost | High (many sweeps to converge ) | Low (ek sweep) |
| Total cost | Chhote ke liye aksar similar ya better | Bade ke liye aksar better |
| Intermediate policy | Hamesha well-defined policy hoti hai | End tak koi explicit policy nahi |
Kab kya use karein:
- Policy iteration: Jab policy evaluate karna fast ho (chhota state space, high ), ya jab computation ke dauran intermediate policies chahiye hon.
- Value iteration: Jab policy evaluation expensive ho, ya jab sirf final optimal policy chahiye.
- Asynchronous DP: States ko kisi bhi order mein update karo (high-error states par focus karo), practice mein faster converge hoti hai.
Computational Complexity
Per iteration:
- Policy evaluation: per sweep (har state ke liye, actions aur next states par sum)
- Value iteration backup: per sweep
Total iterations:
- Policy iteration: worst case (har policy), lekin typically 3-10 in practice
- Value iteration: error threshold tak pahunchne ke liye
WHY convergence affect karta hai: Contraction factor hai. Har iteration error ko kam se kam factor se reduce karta hai, isliye chhota (kam far-sighted) faster converge karta hai.
Space complexity: aur store karne ke liye .
Connections to Other RL Methods
Temporal-difference learning se relationship:
- DP: Model-based (needs ), sabhi states ko sweep karta hai
- TD learning: Model-free (samples se seekhta hai), ek state at a time update karta hai
- TD essentially DP hai jisme full expectation ki jagah sampled backups hain
Generalized Policy Iteration (GPI): Sabhi RL methods ko GPI ke roop mein dekha ja sakta hai: policy evaluation (value function ko policy ke consistent banana) aur policy improvement (policy ko value ke w.r.t. greedy banana) ko interleave karna. DP yeh exact computation se karta hai; TD/Q-learning ise samples se approximate karte hain.
Monte Carlo methods:
- DP: Bootstrap karta hai (updates mein estimated use karta hai)
- Monte Carlo: Bootstrap nahi (actual return ka wait karta hai)
- Tradeoff: DP ki variance lower hai lekin model chahiye
Modern deep RL:
- AlphaZero: aur ke liye neural networks ke saath policy iteration, evaluation ke liye Monte Carlo tree search
- Actor-critic: Policy gradient (improvement) + TD learning (evaluation)
Connections
- 5.108-Markov-Decision-Process-(MDP) — DP MDPs solve karta hai
- 5.1.10-Temporal-difference-learning — DP ka model-free version
- 5.1.11-Q-learning — Model ke bina value iteration
- 5.1.12-SARSA — Model ke bina policy iteration
- 5.1.15-Policy-gradient-methods — Direct policy search (bina value function ke)
- Bellman-equations — DP ki foundation
- Contraction-mapping-theorem — DP kyun converge karta hai
Recall Ek 12-saal ke bachche ko samjhao
Socho tum ek board game khel rahe ho jahan finish tak jitni jaldi ho sake pahunchna chahte ho. Abhi best strategy pata nahi hai, toh tum yeh try karte ho:
Plan 1 (Policy Iteration):
- Random strategy se shuru karo: "Main bas dice roll karunga aur jo mann kare move karunga."
- Ab pretend karo ki tum us strategy ko kai games ke liye follow karte ho aur calculate karo "Har square se average mein kitne turns lagte hain?"
- Apni strategy dobara dekho: "Ruko, agar main square 10 par hun, meri strategy kehti hai left jao, lekin agar main right jaaun, toh finish zyada jaldi pahunchunga!"
- Apni strategy update karo ki hamesha wo move choose karo jo kam-turns wale squares par le jaaye.
- Steps 2-4 repeat karo jab tak strategy change karna band na ho jaaye.
Plan 2 (Value Iteration):
- Guess karke shuru karo "Har square se 0 turns."
- Har square update karo: "Agar main yahan hun, toh main kitni jaldi finish kar sakta hun?" Sabhi possible moves dekho aur best pick karo.
- Step 2 repeat karo jab tak numbers change karna band na ho jaayein.
- Teri final strategy: hamesha sabse kam number wale square ki taraf move karo.
Dono plans perfect strategy dhundh lete hain! Pehla wala ek strategy try karne, use test karne, phir fix karne jaisa hai. Doosra wala har square ke liye best possible score calculate karne aur use follow karne jaisa hai.
Flashcards
MDPs solve karne ke liye do main DP algorithms kaun se hain? :: Policy iteration aur value iteration.