🔬 Practical: RL Grid World
🎯 Practical Objectives
- Visualise value iteration converging on a 5×5 grid world
- Observe Q-learning episodes and how Q-values evolve
- See the optimal policy displayed as directional arrows
- Experiment with γ (discount factor) and α (learning rate)
1. Overview
This interactive demo implements two core RL algorithms on a simple 5×5 grid world:
- Value Iteration (model-based): uses known transition probabilities and rewards to compute V* and the optimal policy.
- Q-Learning (model-free): learns Q-values from episodes of exploration using the ε-greedy strategy.
💡 Grid World Setup
- S (green) = Start cell at position (0,0)
- G (gold) = Goal cell at position (4,4) with reward +10
- X (red) = Obstacle cells — impassable walls
- Each step costs −0.1 (encourages shortest path)
- Actions: Up, Down, Left, Right (deterministic transitions)
- Moving into a wall or obstacle keeps the agent in its current state
2. Interactive Grid World
Policy Visualisation
3. Value & Q-Value Display
State Values V(s)
Run an algorithm to see V-values.
Q-Learning Episode Count
Episodes run: 0
ε-greedy exploration: ε = 0.2
4. Algorithm Reference
Value Iteration (for the Grid World)
Initialize V(s) = 0 for all states
Repeat until convergence (Δ < θ):
Δ ← 0
For each state s:
v ← V(s)
V(s) ← max_a ∑_{s'} P(s'|s,a) [R(s,a,s') + γ V(s')]
Δ ← max(Δ, |v − V(s)|)
Output policy: π(s) = argmax_a ∑_{s'} P(s'|s,a) [R(s,a,s') + γ V(s')]
Q-Learning (for the Grid World)
Initialize Q(s,a) = 0 for all s, a
For each episode:
s ← start state
Repeat until s is terminal:
Choose a from s using ε-greedy on Q
Take action a, observe r, s'
Q(s,a) ← Q(s,a) + α [r + γ max_{a'} Q(s',a') − Q(s,a)]
s ← s'
5. Exercises
✍ Exercise 1: Policy Iteration
Modify the code below to implement policy iteration instead of value iteration. You will need two separate loops: one for policy evaluation (iterative) and one for policy improvement (greedy w.r.t. evaluated values). Compare the number of iterations needed for convergence with value iteration.
✍ Exercise 2: Stochastic Transitions
Change the transition model so that the intended action is executed with probability 0.8, and with probability 0.1 each the agent moves perpendicular (left or right of intended). How does this affect:
- The optimal policy (especially near obstacles)?
- The state values along the optimal path?
- The number of Q-learning episodes needed to converge?
✍ Exercise 3: Discount Factor Experiments
Use the γ slider to try values: 0.1, 0.5, 0.9, 0.99. For each, run value iteration and observe:
- How do V-values change as γ approaches 1?
- Does the optimal policy change for different γ values?
- What happens when γ is very small (e.g., 0.1) — does the agent become “myopic”?
✍ Exercise 4: SARSA vs Q-Learning
Implement SARSA alongside Q-learning. Run both algorithms for 500 episodes and compare:
- The learned Q-values near obstacles
- The resulting policies — does SARSA learn a “safer” path?
- Cumulative reward per episode over time