TL;DR
An agent acting in a Markov Decision Process receives a reward after each step, and the question is how much those future rewards are worth now. The discounted return is the sum of all future rewards, where each reward is multiplied by a discount factor raised to the number of steps until it arrives. With below , a reward soon is worth more than the same reward later, and the infinite sum stays finite.
Builds on: Markov Decision Process, Geometric Series · Leads to: Value Function
Where it's used
Game-playing agents. An agent that learns to play Atari games from the screen, such as the deep Q-network of 2015, is trained to maximise a return built from the in-game score, discounted by a factor close to , such as . Board-game agents often use a single final reward, a win or a loss, and discount it lightly or not at all, because the outcome is what matters.
Warehouse and charging robots. A robot that must drive to a charger pays a small cost at every step and gets a large reward on arrival. The discounted return tells the planner whether a long route to the charger is worth its accumulated cost, and a discount factor sets how much an early cost matters compared with a distant reward.
Choosing a discount in practice. Engineers use the discount to set the planning horizon. A factor of makes rewards about steps away still count for roughly a third of their value, while a factor of makes the agent look only a few steps ahead. The choice changes which behaviour the agent learns, so it is set on purpose, not left at a default.
How it works
Take an episode in which an agent receives rewards , and , after which the episode ends. Use discount , and compute the return from the start, time .
- List the rewards in the order they arrive. The reward after the first step is , after the second is , and after the third is . Check: three rewards, one per step.
- Assign each reward a weight. The first reward is one step away, so its weight is . The second is one step later, weight . The third is two steps later, weight .
- gamma = 0.9
- gamma = 0.5
- Multiply and add. The return is . Check: the two nonzero contributions are and , which add to .
- Check with the recursion. The return from time is just its one reward, . The return from time is the next reward plus the discounted return after it, . Then . Check: the recursion gives the same as the direct sum.
- Read the answer. The return of is smaller than the undiscounted total of , because the reward of arrives two steps away and counts only a quarter as much.
flowchart LR A[Rewards r1, r2, r3 in order] --> B[Weights 1, gamma, gamma squared] B --> C[Multiply each reward by its weight] C --> D[Sum to get G0] D --> E[Check: G0 = r1 + gamma times G1]
The math
Definition
Let be the rewards received after time , and let be the discount factor. The discounted return from time is
The definition holds for a process that never ends. For an episode that stops at a fixed time , the sum runs only up to , and is allowed because the sum is finite.
Step 1 in symbols: the rewards
The reward is the one received after the action taken at time . The index says that the reward steps after that is . Rewards are random because the environment is random, so is a random variable.
Step 2 in symbols: the weights
The weight on the reward steps away is . Weights decline geometrically, which is why the sum has a closed form. The plot above shows the two weights in the walkthrough, as curves of .
Step 3 in symbols: the recursion
Split off the first term of the sum and shift the index in the rest:
This is the identity used in step 4 of the walkthrough. It says that the return now is the next reward plus times the return one step later, so an algorithm can compute returns backwards from the end of an episode.
Step 4 in symbols: a constant reward
If every reward equals , the geometric series gives (see Geometric Series), so
For a finite horizon of rewards, the same argument gives .
Worked examples
Three rewards of each, with .
- The weights are , and .
- The return is .
- Check with the finite closed form: .
- Interpretation: three rewards of are worth today, because the later two are shrunk.
Rewards of , and at steps 1, 2 and 3, then nothing. The discount is .
- The weight on the reward at step 3 is . Its contribution is .
- The zero rewards contribute nothing, whatever their weights.
- The return is . Check: the undiscounted total is , and , as expected.
- Compare: with the return would be , because the agent cares only about the immediate reward. With it would be .
A robot pays a cost of at each of the first four steps while driving to a charger, then receives a reward of on the fifth step, when it plugs in. The discount is . The rewards are , , , , .
- Weights: , , , for the four costs, and for the reward.
- Discounted costs: .
- Discounted reward: .
- Return: , about .
- Interpretation: the trip is worth doing, because the reward outweighs the discounted costs.
- Try it: suppose is instead, with the same rewards. What is ? Work the weights the same way, with for the reward. Answer: the costs sum to , the reward gives , so . With heavy discounting the robot's future charging is not worth its early costs.
Common mistakes and tradeoffs
It is tempting to think that the return is the reward at the next step. In fact it is the whole future stream of rewards, each one weighted by its distance. An agent that optimises only the next reward ignores the long-run cost of a bad move.
It is tempting to think that discounting is only for money. In fact is a modelling choice about the agent's horizon. It also makes an infinite sum finite when rewards are bounded, which is why the definition uses .
It is tempting to think that two runs starting in the same state have the same return. In fact the return is random, because the environment and the agent's own choices are random. The Value Function is the expected return, and that is the quantity that the agent learns.
Use a discounted return when the task has no natural end, or when near rewards really are worth more than distant ones. A discount near values long-term consequences but makes learning slower and the estimates noisier. A small discount makes the agent short-sighted. For episodic tasks with a natural end, an undiscounted sum () is often the honest choice, and for tasks measured by long-run rate, an average-reward objective is used instead.
History
The idea of a discounted sum of future values is old. Economists used it to value future income and utility long before computers existed. In the 1950s Richard Bellman showed that the recursion above, now called the Bellman equation, lets a sequential decision problem be solved by working backwards through time, and he described this approach in his 1957 book on dynamic programming. Reinforcement learning joined the two ideas in the 1980s, and Sutton and Barto's 1998 textbook made the discounted return the standard objective for learning agents. Deep reinforcement learning from pixels, such as the 2015 Atari agent, kept the same definition and combined it with neural networks.
Recap
- An agent collects a stream of rewards, and the discounted return adds them with weights .
- The return satisfies , so it can be computed backwards.
- A constant reward has return , from the geometric series.
- The discount sets how far ahead the agent looks, and it changes the behaviour the agent learns.
- Read next: Value Function, which is the expected return from each state, and Discount Rate, the economic idea this factor borrows its name from.
Check yourself
QuestionRecall: what is the weight on a reward that arrives four steps after the current time, in terms of the discount factor ?Show answer
It is . The reward has index , so , and it is weighted by . The first reward after the current action, , has weight .
QuestionComputation: rewards of on every step for three steps, then nothing, with . What is ?Show answer
The weights are , and , so . Check: the closed form for three equal rewards is .
QuestionWhy: why must be less than when the process never ends?Show answer
With and a constant positive reward, the sum of the rewards grows without bound, so is infinite and cannot be compared or learned from. With and bounded rewards, the weights decay fast enough for the sum to converge.
QuestionApplied: an agent has and a reward of every step forever. What is its return, and why does that number matter?Show answer
The return is . It matters because it shows that a discount of gives the agent an effective horizon of about 100 steps, so the agent behaves as if it looks about that far ahead.