PIXELBANKv9.1.0
Menu

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)\delta_t = R_{t+1} + \gamma V(s_{t+1}) - V(s_t) A^t=δt+γλ A^t+1,A^T=0\hat{A}_t = \delta_t + \gamma\lambda\, \hat{A}_{t+1}, \qquad \hat{A}_T = 0

Implement gae(rewards, values, gamma, lam) returning the list of T advantages.

Example:

Input:
gae([1.0, 1.0], [0.0, 0.0, 0.0], 0.9, 1.0)
Output:
[1.9, 1.0]
Reasoning:
  • Initialize the accumulated advantage to 00 and begin the backward pass at the final time step t=1t=1, where the future advantage is zero by definition.
  • Calculate the temporal difference error δ1\delta_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\delta_1 = R_1 + \gamma V(s_2) - V(s_1) = 1.0 + 0.9(0.0) - 0.0 = 1.0.
  • Update the accumulated advantage for t=1t=1 by adding the discounted future advantage: A^1=δ1+γλA^2=1.0+0.9(1.0)(0)=1.0\hat{A}_1 = \delta_1 + \gamma\lambda \hat{A}_2 = 1.0 + 0.9(1.0)(0) = 1.0.
  • Move to the previous time step t=0t=0 and calculate the temporal difference error δ0\delta_0: δ0=R0+γV(s1)−V(s0)=1.0+0.9(0.0)−0.0=1.0\delta_0 = R_0 + \gamma V(s_1) - V(s_0) = 1.0 + 0.9(0.0) - 0.0 = 1.0.
  • Update the accumulated advantage for t=0t=0 by incorporating the previously computed advantage: A^0=δ0+γλA^1=1.0+0.9(1.0)(1.0)=1.9\hat{A}_0 = \delta_0 + \gamma\lambda \hat{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 T floats.
🔒

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.

solution.py

Test Results

0/0
Run code to see test results.