The offline λ-return algorithm has to wait until the end of an episode before it can update anything. Truncated TD(λ), or TTD(λ), cuts the λ-return off at a horizon n steps ahead, so each state can be updated as soon as n more rewards have arrived.
Implement ttd_lambda_episode(V, states, rewards, n, lam, alpha, gamma) for tabular state values over one episode:
V: array of state values. Do not modify it in place; work on a float copy.
states: S0,S1,…,ST (length T+1). ST is terminal and its value is always 0, whatever V stores for it.
rewards: R1,…,RT, where rewards[k] is Rk+1.
For τ=0,1,…,T−1 in that order, with horizon h=min(τ+n,T):
- Form the k-step returns for k=1,…,h−τ, bootstrapping from the current table (including all earlier updates in this episode):
Gτ:τ+k=∑i=1kγi−1Rτ+i+γkV(Sτ+k)
- Combine them into the truncated λ-return:
Gτ:hλ=(1−λ)∑k=1h−τ−1λk−1Gτ:τ+k+λh−τ−1Gτ:h
- Update V(Sτ)←V(Sτ)+α[Gτ:hλ−V(Sτ)].
Return a tuple (V_new, targets) where targets is the list of the T truncated λ-returns in the order they were used.