What a policy is
A policy is the agent's strategy: a rule that maps each state to an action (or to a distribution over actions). It's the answer to the MDP — once you have a good policy, the agent simply follows it. Everything the agent has learned is compressed into this one object. In broader ML the same word, policy, means exactly this decision rule.
Diagram of the reinforcement-learning loop: the agent takes an action, and the environment returns a reward and the next state.
Policies come in two flavours. A deterministic policy always picks the same action in a given state — clean and easy to read. A stochastic policy returns probabilities over the action space, so the same state can lead to different choices on different visits.
Why on earth would a policy be random?
It sounds odd to deliberately act randomly, but a stochastic policy earns its keep:
- Exploration. Trying varied actions is the only way to discover whether an untested choice is secretly better — the engine behind learning at all.
- Games of bluff. In rock-paper-scissors, any predictable rule loses. The optimal play is genuinely random.
- Hidden state. When two situations look identical but call for different actions, mixing them probabilistically is the best you can do.
Trajectories: the trail an agent leaves
Let a policy loose in an MDP and it produces a trajectory: the sequence of states, actions, and rewards it actually experiences — s₀, a₀, r₁, s₁, a₁, r₂, and so on. A trajectory is one story of what happened: the agent's lived experience for a single run.
Generating a trajectory by stepping the policy and environment forward is called a rollout. Rollouts are how learning algorithms get their data: you can't read the reward function directly, so you sample the world by acting in it and watching what comes back.
Interactive Q-learning gridworld in which an agent moves cell to cell, tracing trajectories and collecting rewards.
def rollout(env, policy, max_steps=1000):
s = env.reset()
trajectory = []
for _ in range(max_steps):
a = policy.sample(s) # stochastic: draw an action
s_next, r, done = env.step(a) # the MDP's transition + reward
trajectory.append((s, a, r))
s = s_next
if done: # hit an absorbing state
break
return trajectoryFrom trajectories back to the objective
Each trajectory has a return — the discounted sum of its rewards. Because a stochastic policy and a random environment make every rollout a little different, the same policy yields a spread of returns. The agent's true goal is the expected return: the average return over all the trajectories the policy could generate.
The objective in one line: find the policy whose expected discounted return is largest.
That single line is the north star of the entire field: *find the policy whose expected return is largest.* Every algorithm you'll meet later — value-based, policy-gradient, model-based — is a different route to that one summit. They all begin by gathering trajectories.