Update a value table with TD(0) over an episode
Why it matters TD learning is how critics in actor-critic and value heads in PPO are trained.
Temporal difference learning updates a state's value right away, using the next state's current estimate instead of waiting for the episode to end.
Write td0_episode(V, transitions, alpha, gamma). V is a list or array of state values (do not change the input; work on a float copy). transitions is a list of (s, r, s_next, done). For each in order: target = r if done else r + gamma * V[s_next]; V[s] += alpha * (target - V[s]). Return the updated array.
def td0_episode(V, transitions, alpha, gamma)Example
td0_episode([0, 0, 0], [(0, 1, 1, False), (1, 2, 2, True)], 0.5, 0.9) gives array([0.5, 1., 0.]).
Understand the idea
TD(0) learns from guesses: after one step you know the reward you got and you have a guess for the next state, so you nudge your current guess toward "reward plus discounted next guess". It is like updating your arrival estimate at every station instead of only at the end of the trip.
Example with alpha 0.5, gamma 0.9 and all values 0: step from state 0 to 1 with reward 1, target 1 + 0.9 * 0 = 1, so V(0) becomes 0.5. Then from state 1 with reward 2 into a terminal state: target is just 2, so V(1) becomes 1.
Rule: delta = r + gamma * V(s_next) * (1 - done) - V(s); V(s) <- V(s) + alpha * delta.
The quantity delta, the TD error, is also the advantage estimate used by actor-critic methods and the building block of GAE in PPO.