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:
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.
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:
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:
γ 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.
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.
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.
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
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
- The starting policy (wait in high, search in low) is not optimal. Change the two policy menus until the optimal box says your policy is optimal, and watch vπ rise in both states as you go.
- Raise β to 1. Searching on a low battery can no longer run it flat, so the optimal action in low switches from recharge to search.
- Set γ to 0. Values become one-step expected rewards, and the optimal policy just maximises the next reward: search in high, wait in low. Raise γ and recharging, which pays nothing now but leads to the high state, becomes the better choice in low.
- Press Average 2,000 returns. The average of sampled returns approaches vπ(high). That is Monte Carlo estimation in a nutshell.
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.
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.