Chapter 3 · Sutton & Barto

Finite Markov decision processes

Bandits had one situation. Real tasks have many, and actions change which situation comes next. The Markov decision process (MDP) is the formal model of that: states, actions, rewards and the probabilities that connect them. Every later method in this series estimates something defined here.

01The agent–environment loop

At each time step t = 0, 1, 2, … the agent observes a state St, picks an action At, and one step later receives a reward Rt+1 and finds itself in a new state St+1. The interaction produces a trajectory:

A trajectoryS0, A0, R1, S1, A1, R2, S2, A2, R3, …

The MDP is finite when the sets of states 𝒮, actions 𝒜 and rewards ℛ are all finite. The reward is indexed t+1 because it arrives together with the next state, as a consequence of At.

02Dynamics and the Markov property

Everything about how the environment behaves is captured by one function: the probability of each next state and reward given the current state and action.

The dynamics functionp(s′, r | s, a) = Pr{ St = s′, Rt = r | St−1 = s, At−1 = a },    Σs′ Σr p(s′, r | s, a) = 1

That this depends only on the current s and a, and not on anything earlier, is the Markov property. It is a demand on the state: the state must carry all the information from the past that matters for the future. Two useful quantities follow from p:

State-transition probabilities and expected rewardp(s′ | s, a) = Σr p(s′, r | s, a),    r(s, a) = Σr r Σs′ p(s′, r | s, a)

03Goals and returns

The agent's goal is to maximise the cumulative reward it receives, not the immediate one. That cumulative quantity is the return. In episodic tasks, which end at a final step T, it is a plain sum. In continuing tasks, which never end, it is discounted by γ ∈ [0, 1) so the sum stays finite:

Discounted return, and its recursive formGt = Rt+1 + γRt+2 + γ2Rt+3 + … = Σk≥0 γk Rt+k+1,    Gt = Rt+1 + γ Gt+1

γ sets how far ahead the agent looks. With γ = 0 it cares only about the next reward. As γ approaches 1 distant rewards count almost as much as near ones. A reward k steps away is worth γk of its face value, so with γ = 0.9 a reward 10 steps away counts for 0.35 of its value. The recursive form, one reward plus the discounted return from the next step, is what makes everything below work.

04Policies and value functions

A policy π(a|s) is the probability of taking action a in state s. Given a policy, every state has a value: the expected return when starting there and following π. So does every state–action pair.

State value and action value under πvπ(s) = 𝔼π[ Gt | St = s ],    qπ(s, a) = 𝔼π[ Gt | St = s, At = a ]

They are tied together: a state's value is the policy-weighted average of its action values, and an action's value is the expected immediate reward plus the discounted value of where it leads.

Linking v and qvπ(s) = Σa π(a|s) qπ(s, a),    qπ(s, a) = Σs′, r p(s′, r | s, a) [ r + γ vπ(s′) ]

05The Bellman equation

Substitute one equation into the other and you get a condition every vπ must satisfy: the value of a state equals the expected value of one step of reward plus the discounted value of the next state.

Bellman expectation equation for vπvπ(s) = Σa π(a|s) Σs′, r p(s′, r | s, a) [ r + γ vπ(s′) ]

For a finite MDP with n states this is a system of n linear equations in n unknowns, with exactly one solution. Read it as averaging over a small tree: from s, branch over actions by π, then over outcomes by p, and at each leaf take r + γvπ(s′). This one-step look-ahead is called a backup. Dynamic programming, Monte Carlo and TD all approximate this equation in different ways.

06Worked example: the recycling robot

A mobile robot collects empty cans (Sutton & Barto, Example 3.3). Its state is its battery level: high or low. In either state it can search for cans or wait for someone to bring one. When the battery is low it can also recharge. Searching earns more (rsearch) than waiting (rwait) but drains the battery. Searching from high leaves the battery high with probability α. Searching from low keeps it low with probability β; otherwise the battery runs flat, the robot must be rescued and recharged, and it gets −3. This is a continuing task, so returns are discounted.

The recycling robot as an MDP

vπ(high) – · vπ(low) –
0.80
0.60
2.0
1.0
0.90
Policy π:

Big circles are states, small dots are actions. Teal arrows from a state to its actions show the policy's probabilities. Each arrow out of an action is one outcome, labelled “probability, reward”.

The dynamics p(s′, r | s, a)

Every (s, a) row group sums to 1. Greyed rows are actions π never takes.

Solving for vπ

Check the Bellman equation for

Optimal values and policy

Sample a trajectory under π

What to try

07Optimal policies and the Bellman optimality equation

Policies can be compared: π ≥ π′ if vπ(s) ≥ vπ′(s) in every state. There is always at least one policy at least as good as all others, an optimal policy π*. All optimal policies share the same value functions v* and q*, which satisfy the Bellman optimality equation: the average over actions becomes a maximum.

Bellman optimality equationsv*(s) = maxa Σs′, r p(s′, r | s, a) [ r + γ v*(s′) ]
q*(s, a) = Σs′, r p(s′, r | s, a) [ r + γ maxa′ q*(s′, a′) ]

Once you have v*, acting greedily with a one-step look-ahead is optimal: the long-term consequences are already folded into v*. With q* you don't even need the look-ahead, or the model: just pick argmaxa q*(s, a). That is why Q-learning learns q*.

The optimality equations are nonlinear because of the max, so they are not solved by one matrix inversion. Solving them exactly needs three things that rarely hold together: an exact model p, enough computation, and the Markov property. The rest of this series is about getting around that. Dynamic programming assumes the model and computes. Monte Carlo and TD drop the model and learn from experience.