Markov Decision Processes
In §5.1 Bolt made one decision and was done. Real life is a sequence: each action puts Bolt in a new situation that needs another decision, and the consequences of today's choice reach far into the future. Taking a shortcut near the stairwell might save time now and cost everything three steps later.
The 4×3 world
If the world were deterministic, the answer would be a plan: "Up, Up, Right, Right, Right." But with slipping, the same plan reaches +1 only \(0.8^5 \approx 33\%\) of the time. Bolt needs instructions for every cell it might end up in, not just a fixed sequence. That's a policy.
The definition
A Markov decision process is a tuple \((S, A, P, R, \gamma)\):
- \(S\): a set of states (here, the 11 open cells), with a start state.
- \(A(s)\): the actions available in state \(s\) (N, S, E, W).
- \(P(s' \mid s, a)\): the transition model, the probability of reaching \(s'\) by doing \(a\) in \(s\).
- \(R(s)\) (or \(R(s,a,s')\)): the reward, an immediate and possibly negative payoff.
- \(\gamma \in [0,1]\): the discount factor.
The word "Markov" carries a big assumption:
$$ P(s_{t+1} \mid s_t, a_t, s_{t-1}, a_{t-1}, \dots, s_0) = P(s_{t+1} \mid s_t, a_t). $$The future depends only on the present state and action, not on the history. The state must carry everything relevant. If Bolt's slip probability depended on how long it had been driving (wheels heating up), we'd have to add "time driving" to the state to restore the Markov property. Choosing what goes into the state is a modelling decision, just like choosing features in Chapter 2.
Policies
A policy \(\pi\) maps every state to an action: \(\pi(s) = a\). It is a complete contingency plan: wherever Bolt finds itself, the policy says what to do. Because of the Markov property, the current state is all a policy needs. Remembering history doesn't help.
An optimal policy \(\pi^*\) maximizes the expected utility of the resulting sequence of states. What "utility of a sequence" means needs a careful definition.
Utilities over time: rewards and discounting
Bolt collects rewards \(R(s_0), R(s_1), R(s_2), \dots\). The standard way to score the whole sequence is the discounted sum:
$$ U([s_0, s_1, s_2, \dots]) = R(s_0) + \gamma R(s_1) + \gamma^2 R(s_2) + \dots = \sum_{t=0}^{\infty} \gamma^t R(s_t). $$Why discount?
- Preference for sooner: a reward now beats the same reward later. Hot maggi now beats hot maggi tomorrow.
- Uncertainty about the future: \(\gamma\) can be read as "each step, there's a \(1-\gamma\) chance the world ends (battery dies)."
- Mathematics: with \(\gamma < 1\) and rewards bounded by \(R_{\max}\), the sum is finite even over an infinite horizon:
A \(\gamma\) near 0 gives a myopic agent that only cares about immediate reward. A \(\gamma\) near 1 gives a far-sighted one. With \(\gamma = 1\) (as in AIMA's 4×3 example) we need terminal states that are reached with probability 1, or utilities could be infinite. Under certain assumptions (stationary preferences), additive discounted rewards are the only sensible way to combine rewards over time.
The utility of a state, and the Bellman equation
The utility of a state under policy \(\pi\) is the expected discounted reward from starting there and following \(\pi\):
$$ U^\pi(s) = \mathbb{E}\Big[\sum_{t=0}^\infty \gamma^t R(S_t) \;\Big|\; S_0 = s,\ \pi\Big]. $$Write \(U(s) = U^{\pi^*}(s)\) for the utility under the optimal policy. Here is the key insight. The utility of a state is the reward you get now, plus the discounted expected utility of wherever you land next, assuming you act optimally:
$$ \boxed{\;U(s) = R(s) + \gamma \max_{a \in A(s)} \sum_{s'} P(s' \mid s, a)\; U(s')\;} $$This is the Bellman equation (Richard Bellman, 1957). Once you know \(U\), the optimal policy is just one-step lookahead:
$$ \pi^*(s) = \arg\max_{a} \sum_{s'} P(s' \mid s, a)\, U(s'). $$The Bellman equation turns an infinite-horizon problem into a local consistency condition: every state's value must agree with its neighbours' values. It's like saying "the best way from Hostel C to the canteen is: take the best first step, then follow the best way from wherever that leaves you." This idea of breaking a big optimization into the same small one repeated is dynamic programming.
There is one Bellman equation per state, so \(n\) equations in \(n\) unknowns. But they're non-linear because of the max, so we can't just solve them with linear algebra. §5.3 gives two algorithms that do.
Also: Q-values
It's often handy to define the value of doing action \(a\) in state \(s\) and then acting optimally:
$$ Q(s,a) = R(s) + \gamma \sum_{s'} P(s' \mid s,a)\, U(s'), \qquad U(s) = \max_a Q(s,a), \qquad \pi^*(s) = \arg\max_a Q(s,a). $$Q-values are the foundation of Q-learning, the reinforcement-learning algorithm that learns them from experience without knowing \(P\).
How the reward shapes behaviour
The living reward \(R(s)\) for non-terminal cells completely changes Bolt's "personality." For the 4×3 world with \(\gamma=1\) (AIMA Fig. 17.2):
| Living reward \(R(s)\) | Optimal behaviour |
|---|---|
| \(R < -1.6284\) | Life is so painful that Bolt heads for the nearest exit, even diving into the −1 stairwell from next to it. |
| \(-1.6284 < R < -0.4278\) | Rushes to +1 by the shortest route, even going North from (3,0) right past the pit. |
| \(-0.4278 < R < -0.0850\) | Takes the shortest route to +1 but turns away from the pit at (3,0). Accepts some risk elsewhere. |
| \(-0.0850 < R < -0.0221\) | The standard answer (R = −0.04): takes the long, safe way round from the bottom row. |
| \(-0.0221 < R < 0\) | Very cautious: bumps into walls on purpose (e.g. at (2,1) and (3,0)) to avoid any chance of −1. |
| \(R > 0\) | Life is good, so Bolt avoids both exits forever and keeps collecting reward. With γ = 1 the utilities are infinite. |
You can check every row with the interactive grid world in §5.3. These thresholds were also verified by sweeping R in the §5.3 code.
Just like the loss in Chapter 2 and the fitness in Chapter 3, the reward is what the agent actually optimizes. With a positive living reward, Bolt never delivers anything. The objective was satisfied and the designer's intent was not. This is called reward hacking, and it is one of the central problems in AI safety.
- Define an MDP formally: the 5 components and the Markov property.
- Write the Bellman equation and explain each term.
- Compute a discounted return: rewards \(1, 1, 1, \dots\) forever with \(\gamma=0.9\) gives \(1/(1-0.9)=10\).
- Explain why a policy, and not a plan, is needed in stochastic environments.
- Given rewards, predict qualitatively what the optimal policy does.