Worked notebook · Foundations
One gridworld for everything
0. The practice grid
We will use this world for every calculation on this page.
- \(G\): goal terminal state
- \(P\): penalty terminal state
- Actions: \(\mathcal{A}=\{\uparrow,\downarrow,\leftarrow,\rightarrow\}\)
- Movement is deterministic
- Hitting a wall means staying in the same state
- Every normal movement gives \(r=-1\)
- Entering \(G\) gives \(+5\); entering \(P\) gives \(-5\)
- Discount: \(\gamma=0.9\)
- After reaching \(G\) or \(P\), the episode ends
1. Representing it as an MDP
An MDP is the tuple
State space:
From \(B\), for example:
| Action | Next state | Reward |
|---|---|---|
| ↑ | \(B\) (wall) | \(-1\) |
| ↓ | \(D\) | \(-1\) |
| ← | \(A\) | \(-1\) |
| → | \(G\) | \(+5\) |
Because movement is deterministic,
meaning a \(100\%\) probability of reaching \(G\) when we take right from \(B\).
2. One trajectory and return
Suppose the agent follows
Rewards along the way:
The return from \(C\) is
\(G_0 = 2.15\)
Remember this number. It will reappear as the optimal value of \(C\).
3. Policy evaluation
Start with a random (uniform) policy:
Initialize non-terminal values to zero:
After termination no further rewards occur, so the expected remaining return is \(0\). We hold \(V(G)=V(P)=0\) forever.
The synchronous Bellman expectation update (deterministic \(P\)) is
Because transitions are deterministic we do not need an extra sum over \(s'\).
Iteration 1
State \(A\)
From \(A\):
- ↑ — stay at \(A\), reward \(-1\)
- ↓ — move to \(C\), reward \(-1\)
- ← — stay at \(A\), reward \(-1\)
- → — move to \(B\), reward \(-1\)
Since all initial values are zero:
\(V_1(A)=-1\)
State \(B\)
\(V_1(B)=0.5\)
The positive value appears because one action reaches the goal.
State \(C\)
All four actions currently produce reward \(-1\) into zero-valued successors:
\(V_1(C)=-1\)
State \(D\)
Three normal actions give \(-1\); right enters the penalty \(-5\):
\(V_1(D)=-2\)
| State | \(V_1\) |
|---|---|
| \(A\) | \(-1\) |
| \(B\) | \(0.5\) |
| \(C\) | \(-1\) |
| \(D\) | \(-2\) |
Iteration 2 — example for \(A\)
Now use the previous values:
\(V_2(A)=-1.5625\)
| State | \(V_1\) | \(V_2\) |
|---|---|---|
| \(A\) | \(-1\) | \(-1.5625\) |
| \(B\) | \(0.5\) | \(-0.0625\) |
| \(C\) | \(-1\) | \(-2.125\) |
| \(D\) | \(-2\) | \(-2.5625\) |
Repeating until convergence under the random policy gives approximately
These values are poor: the random policy frequently hits walls and sometimes walks into the penalty. Evaluation did its job — it told the truth about a bad policy.
4. Policy improvement
Now calculate one-step action values under the converged \(V^{\pi}\):
At state \(B\)
Therefore
Doing this for every state yields the greedy policy
5. Evaluate the improved policy
The policy is now deterministic, so evaluation collapses to a short chain of equations.
State \(B\)
\(B\) goes directly to the goal:
\(V(B)=5\)
State \(A\)
\(V(A)=3.5\)
State \(D\)
\(V(D)=3.5\)
State \(C\)
\(V(C)=2.15\)
Same number as the trajectory return in Section 2 — because that path is what the improved policy does from \(C\).
6. Policy Iteration
Policy Iteration alternates
Therefore the final policy is optimal. At \(C\), two paths are equally good:
So \(\pi^*(C)\) may be either \(\rightarrow\) or \(\uparrow\) — both are optimal.
7. Value Iteration
Value Iteration folds evaluation and improvement into one update:
Instead of averaging actions, we immediately take the best one. Initialize \(V_0(s)=0\) for non-terminals.
Iteration 1
For \(B\):
For \(A\), every action currently looks like \(-1\). Same for \(C\). For \(D\), the best action avoids the penalty and also yields \(-1\):
| State | \(V_1\) |
|---|---|
| \(A\) | \(-1\) |
| \(B\) | \(5\) |
| \(C\) | \(-1\) |
| \(D\) | \(-1\) |
Iteration 2
But \(C\) still does not yet “see” the goal clearly:
| State | \(V_2\) |
|---|---|
| \(A\) | \(3.5\) |
| \(B\) | \(5\) |
| \(C\) | \(-1.9\) |
| \(D\) | \(3.5\) |
Iteration 3
Now \(C\) sees that both \(A\) and \(D\) are valuable:
\(V_3(C)=2.15\)
| State | \(V^*\) |
|---|---|
| \(A\) | \(3.5\) |
| \(B\) | \(5\) |
| \(C\) | \(2.15\) |
| \(D\) | \(3.5\) |
8. Policy Iteration vs Value Iteration
Policy Iteration
Fully evaluate the current policy,
then improve it.
evaluate → improve → repeat
Value Iteration
One optimal Bellman update,
then repeat.
\(V\leftarrow\mathcal{T}^*V\) until fixed
Both produce the same optimal policy:
The goal value spreads backward:
- Iteration 1: \(B\) discovers the goal
- Iteration 2: \(A\) and \(D\) discover \(B\)
- Iteration 3: \(C\) discovers \(A\) and \(D\)
That backward propagation of reward information is the core mechanism behind every Bellman-based reinforcement learning algorithm — DP, TD, and Q-learning all live inside this picture.
See also
- Chapter 02 — \(V^{\pi}\), \(Q^{\pi}\), Bellman equations.
- Chapter 03 — Policy / Value Iteration as algorithms.
- DP laboratory — run the same ideas on a larger grid.
Back to theory
Dynamic Programming
You just did a full PI and VI by hand. The chapter formalizes it.