2.3 Policy, Value, and Return
2.2 defined the MDP tuple . But the MDP itself is just the "environment" — how does the agent actually make decisions inside it? This section introduces three core concepts: the policy (how the agent picks actions), the return (how we measure the quality of a trajectory), and the value function (how we evaluate the long-term payoff of a state or action). These three concepts are the foundation for every RL algorithm that follows.
Policies and Decision Rules
A policy is the agent's mapping from states to actions. It comes in two flavors:
- Deterministic policy: , which takes a state and outputs an action directly,
- Stochastic policy: , which takes a state and outputs a distribution over actions,
The stochastic policy is the more general of the two — a deterministic policy is just the special case where the distribution collapses onto a single point. RL almost always works with stochastic policies.
# A simple stochastic policy for CartPole
import torch
import torch.nn as nn
class CartPolePolicy(nn.Module):
def __init__(self):
super().__init__()
self.net = nn.Sequential(
nn.Linear(4, 32), nn.Tanh(),
nn.Linear(32, 2) # logits for 2 actions
)
def forward(self, state):
logits = self.net(state)
return torch.distributions.Categorical(logits=logits)
def act(self, state):
dist = self.forward(state)
action = dist.sample()
return action.item(), dist.log_prob(action)The Optimal Policy
The goal of RL is to find the optimal policy , the one that maximizes long-run cumulative reward:
Every algorithm covered later in this book — DQN, PPO, SAC — is, at bottom, an approximate solver for this optimization problem.
Return: Measuring a Trajectory
Over the course of an episode, the agent lives through a trajectory . The return is the cumulative reward from time onward:
What the Discount Factor γ Does
is the discount factor: it makes rewards further in the future count for less. It serves three purposes:
- Guarantees convergence mathematically: the infinite sum converges whenever
- Reflects uncertainty: rewards far in the future are inherently harder to predict, so they should be weighted less
- Stabilizes training: it keeps delayed rewards from blowing up the variance of the return
| γ value | Meaning | Typical use |
|---|---|---|
| 0 | Only the immediate step matters (greedy) | Rarely used |
| 0.9 | Short horizon (~10 steps) | Board games, recommender systems |
| 0.99 | Medium horizon (~100 steps) | Atari, CartPole |
| 0.999 | Long horizon (~1000 steps) | Long-horizon tasks, robot navigation |
| 1.0 | No discounting | Finite-horizon tasks |
Return in CartPole
In CartPole, every step gives a reward of 1 as long as the pole is still up. The episode ends when the pole falls or the cart goes out of bounds. The return is:
where is the episode length. With and , .
Value Functions: Long-Term Payoff
A value function measures how much return you can expect to collect from a given state or action while following policy . There are two kinds.
State Value V(s)
In words: starting from state and following policy from there, this is the expected cumulative return.
Action Value Q(s, a)
In words: starting from state , taking action first, then following afterward, this is the expected cumulative return.
How V and Q Relate
is just the expectation of under the action distribution that prescribes.
A Numerical Example: GridWorld
Consider a 4×4 GridWorld where the goal sits in the bottom-right corner (reward = +1, episode ends there) and every other step gives reward 0:
┌───┬───┬───┬───┐
│0.0│0.5│0.8│0.9│ ← V(s) values
├───┼───┼───┼───┤
│0.5│0.7│0.9│1.0│ ★ (goal)
├───┼───┼───┼───┤
│0.7│0.9│0.95│ │
├───┼───┼───┼───┤
│0.8│0.95│ │ │ ← unlabeled cells have lower V
└───┴───┴───┴───┘The closer a cell is to the goal, the higher its V — the reward is fewer steps away, so it gets discounted less.
The Advantage Function: Judging Actions
The advantage function measures how much better action is than the average action at state :
- : action beats the average
- : action falls short of the average
- : action is exactly average
The advantage function is central to policy gradient methods (Chapter 6) and Actor-Critic methods (Chapter 7).
A Preview of the Bellman Equation
Value functions satisfy the Bellman equation — a recursive relationship that writes as a function of :
This equation sits at the core of every RL algorithm. The next section, 2.4 Discounting, Trajectories, and POMDPs, works out more of the details of trajectories, and Chapter 3, Value Functions and the Bellman Equation will dig into the Bellman equation in full.
Section Summary
Policy, return, and value function are the three core concepts of the MDP:
- Policy : the agent's decision rule; the stochastic form is the most general version
- Return : the discounted cumulative reward from time onward,
- Value functions: for state value, for action value; the advantage measures relative quality
The next section, 3.3 Discounting, Trajectories, and POMDPs, formalizes trajectories and introduces the POMDP (partially observable MDP) extension.