GAE Over a Full Trajectory
Problem Statement
Compute GAE(gamma, lambda) advantages for a full trajectory. Given rewards R_1..R_T and values values of length T+1, the advantages satisfy the backward recursion:
δt​=Rt+1​+γV(st+1​)−V(st​) A^t​=δt​+γλA^t+1​,A^T​=0
Implement gae(rewards, values, gamma, lam) returning the list of T advantages.
Example:
gae([1.0, 1.0], [0.0, 0.0, 0.0], 0.9, 1.0)
[1.9, 1.0]
- Initialize the accumulated advantage to 0 and begin the backward pass at the final time step t=1, where the future advantage is zero by definition.
- Calculate the temporal difference error δ1​ using the reward and values at step 1: δ1​=R1​+γV(s2​)−V(s1​)=1.0+0.9(0.0)−0.0=1.0.
- Update the accumulated advantage for t=1 by adding the discounted future advantage: A^1​=δ1​+γλA^2​=1.0+0.9(1.0)(0)=1.0.
- Move to the previous time step t=0 and calculate the temporal difference error δ0​: δ0​=R0​+γV(s1​)−V(s0​)=1.0+0.9(0.0)−0.0=1.0.
- Update the accumulated advantage for t=0 by incorporating the previously computed advantage: A^0​=δ0​+γλA^1​=1.0+0.9(1.0)(1.0)=1.9.
- The final output is [1.9, 1.0]
Constraints:
len(values) == len(rewards) + 1.- Compute in a single backward pass.
- Return a list of
Tfloats.
1. Background Knowledge
Generalized Advantage Estimation (GAE) is a technique used in policy gradient reinforcement learning to balance the bias and variance of advantage estimates. The advantage function A^t​ measures how much better an action is compared to the baseline value estimate V(st​). A naive Monte Carlo return has low bias but high variance, while a single-step temporal difference (TD) error has high bias but low variance. GAE interpolates between these extremes using a decay parameter λ∈[0,1].
The core building block is the TD residual (or TD error) at time step t: δt​=Rt+1​+γV(st+1​)−V(st​) where γ is the discount factor, Rt+1​ is the reward received after taking an action in state st​, and V(st​), V(st+1​) are the value estimates for the current and next states. The GAE advantage is then a weighted sum of future TD residuals: A^t​=∑l=0T−t​(γλ)lδt+l​ This can be computed efficiently via the backward recursion A^t​=δt​+γλA^t+1​, with the boundary condition A^T​=0 (assuming the episode terminates at step T).
In practice, you are given a full trajectory of rewards and pre-computed value estimates. The values array has length T+1 because it includes the value of the terminal state V(sT+1​), which is typically 0 for episodic tasks but may be non-zero if the episode is truncated. The rewards array has length T, corresponding to rewards R1​,…,RT​.
2. Algorithm Approach
The problem is a classic backward dynamic programming or backward recursion pattern. Since A^t​ depends on A^t+1​, you must compute advantages from the end of the trajectory backward to the beginning.
The algorithm follows these principles:
- Initialize the advantage at the final step to 0.
- Iterate backward from t=T−1 down to t=0.
- At each step, compute the TD residual δt​ using the current reward, the next state's value, and the current state's value.
- Update the advantage using the recursion A^t​=δt​+γλA^t+1​.
- Store the computed advantage in the output list.
This approach avoids the O(T2) cost of directly summing the geometric series for each t by reusing the previously computed advantage, resulting in an O(T) solution.
3. Step-by-Step Strategy
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.