labs / lab-c-bellman-equations
Next task →
Practical Labs/Lab: Markov Decision Processes and Bellman Equations
Level 3 · reinforcement-learning

Lab: Markov Decision Processes and Bellman Equations

Build a minimal, verifiable implementation that demonstrates Markov Decision Processes and Bellman Equations. Connect the code to the governing equation, inspect its output numerically, and explain one failure case.

Mathematical basis

For Markov Decision Processes and Bellman Equations, verify the Bellman update Q(s,a)←Q(s,a)+α[r+γmax⁡a′Q(s′,a′)−Q(s,a)]Q(s,a)\leftarrow Q(s,a)+\alpha[r+\gamma\max_{a'}Q(s',a')-Q(s,a)] on a hand-built transition.

Instructions

  1. 01Read section 7 of the Markov Decision Processes and Bellman Equations learning page and write its governing equation on paper.
  2. 02Implement the core operation in Gymnasium / PyTorch on CartPole or a 4×4 gridworld. Do not call a library function that solves the whole task.
  3. 03Verify the implementation on a hand-computed three-element or 3×3 example before scaling up.
  4. 04Run the experiment with discount γ or exploration ε and record episodic return.
  5. 05Inspect the output, identify one discrepancy with the theory, and write a one-paragraph explanation.

Starter code

import numpy as np

def q_update(q, s, a, r, s2, alpha=.5, gamma=.9):
    q = np.array(q, float, copy=True)
    q[s,a] += alpha*(r + gamma*q[s2].max() - q[s,a])
    return q

q = np.zeros((2,2)); q = q_update(q,0,1,1.,1)
assert q[0,1] > 0
print(np.round(q,3))