Implement a function that computes Generalized Advantage Estimation (GAE) advantages and corresponding returns for a trajectory, properly handling episode boundaries.
Your function should take:
rewards: a list of rewards [r_0, r_1, ..., r_{T-1}] of length Tvalues: a list of estimated state values [V(s_0), V(s_1), ..., V(s_T)] of length T+1 (the last value is the bootstrap value for the state after the final action)dones: a list of done flags [d_0, d_1, ..., d_{T-1}] of length T, where 1 indicates a terminal state and 0 indicates a non-terminal stategamma: the discount factorlam: the GAE lambda parameter that controls the bias-variance tradeoffWhen an episode terminates (done=1), the bootstrapped next-state value should be treated as zero, and the advantage accumulation should not propagate across episode boundaries.
The function should return a tuple of two lists:
advantages: the GAE advantages for each timestep, rounded to 4 decimal placesreturns: the target returns computed as advantages plus the corresponding state values, rounded to 4 decimal places