Reward-to-Go for Every Timestep
Problem Statement
Return the discounted reward-to-go G_t for every timestep of an episode, not just the first:
Gt​=∑k=0T−t−1​γkRt+k+1​
Implement rewards_to_go(rewards, gamma) returning a list the same length as rewards, where element t is G_t.
Example:
rewards_to_go([1.0, 2.0, 3.0], 1.0)
[6.0, 5.0, 3.0]
- We compute the reward-to-go Gt​ in reverse order (from the last timestep to the first) because each Gt​ depends on the subsequent Gt+1​ via the recurrence Gt​=Rt+1​+γGt+1​.
- Starting at the final timestep t=2 with reward R3​=3.0 and no future rewards, the accumulated value is simply G2​=3.0+1.0⋅0=3.0.
- Moving to t=1 with reward R2​=2.0, we add the current reward to the discounted future value: G1​=2.0+1.0⋅3.0=5.0.
- At the initial timestep t=0 with reward R1​=1.0, we again combine the current reward with the discounted next value: G0​=1.0+1.0⋅5.0=6.0.
- The final output is [6.0, 5.0, 3.0]
Constraints:
0 <= len(rewards) <= 10000,0.0 <= gamma <= 1.0- Compute in a single backward pass (O(n)).
- Empty input returns an empty list.
1. Background Knowledge
In reinforcement learning, the return is the cumulative discounted reward an agent receives from a given state onward. The reward-to-go (also called the future return or cumulative discounted reward) at timestep t is defined as:
Gt​=k=0∑T−t−1​γkRt+k+1​Here, Rt+k+1​ is the reward received after taking an action at step t+k, and γ∈[0,1] is the discount factor that reduces the influence of distant rewards. When γ=1, the agent values all future rewards equally; when γ is small, the agent is myopic and prioritizes immediate rewards.
The reward-to-go is central to policy gradient methods (e.g., REINFORCE) and actor-critic algorithms, where it serves as a baseline-free estimate of the value of being in a particular state-action pair. Unlike the state-value function V(s), which is an expectation over all possible futures, Gt​ is a single-sample estimate computed from one observed trajectory.
A key insight is that Gt​ and Gt+1​ are related by a simple recurrence:
Gt​=Rt+1​+γGt+1​with the boundary condition GT​=0 (no rewards remain after the episode ends). This backward recurrence is the foundation of an efficient O(T) algorithm.
2. Algorithm Approach
Use a backward dynamic programming sweep. Instead of computing each Gt​ independently (which would be O(T2)), exploit the recurrence relation above:
- Start at the last timestep where GT​=0.
- Iterate backward from t=T−1 down to t=0.
- At each step, compute Gt​=Rt+1​+γ⋅Gt+1​.
This is analogous to computing a suffix sum with a multiplicative decay factor. The pattern is: maintain a running accumulator that you update as you walk backward through the reward sequence.
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.