Given the sequence of rewards an agent collected during one episode, compute the discounted return from the first time step.
Reinforcement learning agents do not maximise the immediate reward, they maximise the return — the total reward accumulated from now until the end of the episode. Rewards that arrive later are worth less, and the discount factor gamma controls by how much:
Gt=Rt+1+γRt+2+γ2Rt+3+⋯=∑k=0T−t−1γkRt+k+1
Two special cases are worth internalising because they anchor the whole spectrum:
The recursive form G_t = R_{t+1} + gamma * G_{t+1} is the seed of every algorithm in this plan — the Bellman equations are nothing more than this identity with an expectation wrapped around it.
Implement:
def discounted_return(rewards, gamma):
...
Return the discounted return G0 as a float.
Input is a Python list of floats and a float. Output is a single float. The grader rounds it to 4 decimal places.
print(round(discounted_return([1.0, 2.0, 3.0], 0.9), 4))
Output:
5.23
Because 1.0 + 0.92.0 + 0.813.0 = 1.0 + 1.8 + 2.43 = 5.23.
discounted_return([1.0, 2.0, 3.0], 0.9)
5.23
G_0 = 1.0 + 0.92.0 + 0.9^23.0 = 1.0 + 1.8 + 2.43 = 5.23.
0 <= len(rewards) <= 10000.0 <= gamma <= 1.00.0.float; do not round inside the function.