Rewards, policies and episodes
What reinforcement learning learns
Reinforcement learning (RL) studies decisions whose consequences can unfold over many steps. An agent acts in an environment and receives feedback as rewards. Unlike a supervised label, a reward does not directly tell the agent which action was correct. A warehouse simulator might reward reaching a delivery square but penalize every move. The agent must discover a useful sequence, not merely predict a label. If the correct route is already known, ordinary graph search may be simpler.
A finite Markov decision process
A Markov decision process (MDP) specifies states, legal actions, a transition distribution, rewards and an objective (often discounted return). The Markov assumption means the conditional distribution of the next state and reward depends on current state and action, not the earlier history once those are given. If a hidden battery level affects movement, position alone may not be a Markov state. Add the relevant battery information or consider a partially observable model. Deterministic transitions are a special case with probability one. A policy maps states to actions or action probabilities; a policy is not the environment itself.
Calculate discounted return
For a finite episode with rewards r1 through rT, G = r1 + gamma*r2 + ... + gamma^(T-1)rT, with 0 <= gamma <= 1. Gamma controls the relative weight of later rewards. For [2,4] and gamma=.5, G=2+.54=4. Gamma=0 keeps only the first reward; gamma=1 sums this finite sequence. Empty reward lists have return zero. The reverse recurrence avoids repeated exponentiation. Infinite-horizon objectives require additional assumptions; for example, bounded rewards and gamma<1 ensure a finite discounted sum. The following function assumes small finite numeric rewards.
def discounted_return(rewards, gamma): if not 0 <= gamma <= 1: raise ValueError("gamma must be in [0, 1]") total = 0 for reward in reversed(rewards): total = reward + gamma * total return total
print(discounted_return([2, 4], 0.5))print(discounted_return([-1, 5], 0.9))print(discounted_return([], 1))Read the result
The outputs are 4.0, 3.5 and 0. The second is -1+0.9*5. A value function V^pi(s) is the expected return from state s under policy pi, while Q^pi(s,a) starts with a specified action and then follows pi. A single observed return is a sample, not automatically the expected value. Compare several trials under a common environment and discount.
Trace a bounded grid episode
Consider a one-row, three-cell grid: [0 start] [1 middle] [2 goal]. Left from 0 stays at 0. All nonterminal transitions cost one point; reaching 2 gives +5 instead and ends the episode. State 2 has no legal actions. The simulator starts at 0 and caps the number of moves. The return and success are kept distinct. The cap is external to this MDP: hitting it without reaching the goal truncates the rollout, not a terminal transition. A true finite-horizon MDP would include remaining time in the state when needed.
def grid_step(state, action): if state not in (0, 1) or action not in ("left", "right"): raise ValueError("invalid state or action") # Positions 0, 1, 2; 2 is terminal. next_state = max(0, state + (1 if action == "right" else -1)) return next_state, (5 if next_state == 2 else -1), next_state == 2
def rollout(policy, limit): if type(limit) is not int or limit < 0: raise ValueError("nonnegative integer step limit required") state, rewards = 0, [] for _ in range(limit): state, reward, terminated = grid_step(state, policy(state)) rewards.append(reward) if terminated: return rewards, True return rewards, False
print(rollout(lambda s: "right", 8))print(rollout(lambda s: "left", 3))print(rollout(lambda s: "right", 1))Interpret the trace
Moving right twice gives rewards [-1,5] and success True; always moving left for three steps gives [-1,-1,-1] and False. A one-step budget gives [-1] and False. The Boolean means goal reached, not whether the loop stopped. The rollout is safe even if a policy never reaches the goal. The state transition is deterministic here; uncertain movement would require specifying its distribution.
Common mistakes and practice
Do not count the reward for entering a terminal state twice. Do not equate immediate reward with long-term value. Calculate the discounted return for [-1,-1,5] at gamma=.5 and compare with [-1,5]. The answers are -0.25 and 1.5, so the shorter route is better under this reward rule. Change the step budget to zero and predict the result: an empty reward list and False. Next: use sampled transitions to improve a policy.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Rewards, policies and episodes quiz
Reason about returns and bounded decisions.
How does an RL reward differ from a supervised target label?
In the lesson’s MDP, what does the Markov assumption require?
What is the discounted return of [2,4] with gamma=0.5?
What does V^pi(s) represent in the lesson?
Starting at cell 0, the rollout stops after one move because its external step budget is exhausted. Which outcome occurred?