Introduction
In the previous part, we laid the foundations of reinforcement learning: you now have the tools to describe a decision problem and to say what an optimal solution to it is.
This second part is about planning with a model: evaluating a policy, or finding the best one, in an environment with rules we know. This applies to problems such as video games, chess or Go, and it is a crucial step before learning from experience, without a model, which is the real goal of reinforcement learning.
Where Part 1 left us
Part 1, Modeling the World gave us the basics: an agent in state takes an action , receives a reward and lands in , according to the dynamics of the Markov Decision Process (MDP), .
The agent wants to maximize the return, recursively defined by the relation , or in full, as the discounted sum of all the rewards to come:
To evaluate how good a state or a state-action pair is, we defined helper functions: the value functions score states and actions under a policy ,
and their optimal counterparts and . The state values satisfy the Bellman equations, one per state:
On the four-state altitude map, Part 1 solved these by hand, as a system of equations.
This page teaches you how to solve them on any finite MDP, with algorithms that only need the model and the recursive structure of these equations. We will study:
- Dynamic programming and policy evaluation: computing the value of a policy.
- Policy iteration: finding the best policy in three steps: evaluate, improve, repeat.
- Value iteration: a shortcut to the optimal behavior: optimize directly, one sweep at a time.
- Taking a step back: the pattern behind all three, generalized policy iteration, and why the optimal policy is not unique.
Part 3 will remove the model: Part 3.1, Learning from Experience, with Monte Carlo methods and temporal-difference learning, then Part 3.2, Learning to Act, with SARSA and Q-learning, learn the same values from sampled interactions only. Everything they do is an approximation at scale of what this page computes exactly.
1. Evaluating a policy via dynamic programming
To be able to find an optimal policy, we first need to be able to compare policies. Policy evaluation is the computation of the value function of a policy, in every state. When a model is available, the main technique to do so is to use dynamic programming.
1.1 What dynamic programming is
Dynamic programming (DP) solves a complex problem by breaking it into simpler subproblems, storing their solutions, and reusing them instead of computing them again. Here, “dynamic” refers to the sequential, temporal side of the problem. It applies when a problem has two properties:
- optimal substructure: an optimal solution is made of optimal solutions of subproblems;
- overlapping subproblems: the same subproblems come back many times, so their solutions are worth caching.
MDPs have both. The Bellman equation is the decomposition link: the value of a state is one step plus the value of the next state. And the value function is the cache: one number per state, computed once, reused by every state that leads there.
The first property has a name in this setting, the principle of optimality:
A policy is optimal from a state if and only if it is optimal from every state it can reach from .
Once the hiker of Part 1 has reached , whatever happened before, the best the hiker can do is to behave optimally from . The problem splits into a first step and a smaller problem from the next state, which is exactly the Bellman optimality equation.
DP assumes full knowledge of the MDP: the states, the actions, the dynamics and . It is a planning method, used for both problems of section 4.3 of Part 1:
- Prediction/evaluation: given the MDP and a policy , compute ;
- Control: given the MDP, compute and an optimal policy .
1.2 Iterative policy evaluation
The Bellman expectation equation gives linear equations for the values . We could solve the system directly, but that costs about operations, too much for large state spaces.
Instead, we turn the equation into an update rule, and apply it again and again. Starting from any , for instance everywhere, and computing, for every state,
One pass over all the states is a sweep. Each sweep replaces the value of every state by a one-step look-ahead on the current values, a Bellman backup, and the sequence converges to . In practice, we stop when the largest change of a sweep, , falls below a small threshold :
Input: a policy , a threshold
- for every state
- repeat
- for each state
- until
One sweep costs about operations: every state, every action, every next state, but we don’t know how many sweeps are required to converge.
Main example of this lesson: A grid with states to , numbered row by row; the corners and are terminal. Four actions, up, down, left, right, deterministic; an action that would leave the grid leaves the agent in place. Every step costs , , and the policy we study is the equiprobable random one, .
Starting the policy evaluation from : after one sweep, every non-terminal state is worth , the reward of one step, whatever happens later. For state , next to the terminal corner:
After two sweeps, the states next to a terminal corner are worth (one of their four moves ends the episode) and the others . The values keep spreading from the corners, sweep after sweep, up to convergence:
To what values does converge? Take a few seconds to think about it.
Answer Open once you have a guess
is minus the expected number of steps a random walker needs to fall into a corner: steps on average from the far corners, although they are only steps from a terminal state.
The values take a long time to fully settle: about sweeps to get within of the true , the far corners going from after sweeps to . However, note that the comparisons between states settle much faster: from , every state that is better than another under is already better under . Keep this in mind for sections 2 and 3.
These large negative values say little about the task: they measure the cost of acting at random. Finding the optimal cost is the job of the next step: improving the policy.
2. Policy iteration: evaluate, improve, repeat
Evaluating a policy is a great step forward, but our true goal is to find the best policy, or one of the best: this is what policy iteration is about.
2.1 Improving a policy
Once we know , we can measure what happens if, in one state , we make a different choice and take a different action than the policy would, then follow again. That is exactly , and it follows from in one step:
On the gridworld of section 1.2, under the random policy, moves are deterministic so the sum shrinks and , where is the cell reached by . Moving down from state ends the episode, and a terminal state is worth : . Moving down from state lands in state : .
Same first action, but very different values, because scores one step of your choice followed by the random policy: from , that one step ends the episode; from , it hands the agent back to more random steps, on average.
The figure below starts with a simpler policy than the random one, up, then left, whose path is easy to follow: there, . Switch it to random to find the above, as the average of many episodes.
The figure already holds the idea of this section. Under the random policy, state is worth ; going down first, then acting at random, is worth .
One better choice, made once, saves five steps on average. In general, if , taking in is better than following there (both are expected returns from ). Changing the policy in that one state should help, but is this always the case, in any order we proceed? The policy improvement theorem says that it is, even when we change it in every state at once.
Let and be two deterministic policies such that, for every state ,
Then is at least as good as : for every .
The natural candidate here is what we call the greedy policy, which picks in every state the action with the best return one step ahead:
It satisfies the hypothesis of the theorem by construction. In every state, is an average of the action values, weighted by the policy, and a max is at least an average:
So greedifying never hurts.
When does it stop helping? Only when the policy is already optimal:
Optional A stable policy is optimal: why greedifying changes nothing only at the optimum Math needed Compute: arithmetic, a small linear system, applying a formula of the pageend of high school Reason: expectations, inequalities, an argument in words: no new tools, but a chain of reasoningfirst year of university, with a first course in probability Prove: induction, limits, norms, fixed points: a proof, which can be skipped on a first readingabout two years of university math The level says which math is needed, not how long it takes.
If is only as good as , , write the Bellman equation of , one step with , then replace by :
In every state, : satisfies the Bellman optimality equation, so , and was already optimal.
Exercise 2.1 Proof of the policy improvement theorem Math needed Compute: arithmetic, a small linear system, applying a formula of the pageend of high school Reason: expectations, inequalities, an argument in words: no new tools, but a chain of reasoningfirst year of university, with a first course in probability Prove: induction, limits, norms, fixed points: a proof, which can be skipped on a first readingabout two years of university math The level says which math is needed, not how long it takes.
Let and be two deterministic policies with in every state. The idea is to unroll the hypothesis: apply it at , then at the next state, then at the one after, each time taking one more step with instead of .
-
Show that . Why can the expectation be taken under , although is defined with ?
-
Apply the hypothesis at , and show that .
-
Show by induction that, for every ,
-
Let and conclude that . What is needed for the last term to vanish?
Hint
Question 1: fixes the first action, then follows . and only depend on that first action, and drawing it from the deterministic means taking .
Hint
Question 2: in the expectation of question 1, bound by , and use question 1 again, at . The inequality survives because .
Hint
Question 4: the tail is . When is bounded? What if ?
Checkpoint what you should get
Each application of the hypothesis hands one more step to ; when , the right-hand side tends to .
Solution
The whole chain, one line per application of the hypothesis:
- The second line is where enters: conditions on the action , which is the same as drawing the first action from . After that first step, still follows .
- The third line applies the hypothesis at , and multiplying by keeps the inequality. The fourth line is question 1 at : the second action is now drawn from too.
- Each repetition of questions 1 and 2 replaces by , with one more action drawn from : this is the induction step.
- The sum of the rewards tends to the return of , whose expectation is , if the tail vanishes: true for with bounded rewards, since is then bounded, or, with , when ends the episode with probability 1, since the value of a terminal state is .
What to notice the take-away
Each line hands one more step from to , and the hypothesis guarantees that each hand-over can only help. Nothing requires to be greedy: any policy that does at least as well as one step ahead, in every state, does at least as well for good.
2.2 Policy iteration
Policy iteration alternates the two steps until the policy stops changing:
- Evaluate the current policy: compute , with the sweeps of section 1.2.
- Improve it: make it greedy with respect to .
- Repeat until the policy is stable: .
- initialize arbitrarily
- repeat
- policy evaluation
- repeat for each state
- until convergence
- repeat for each state
- policy improvement
- for each state
- policy evaluation
- until
In words: each policy is strictly better than the previous one, unless it is already optimal. And policy iteration always stops: a finite MDP has a finite number of deterministic policies, so policy iteration reaches an optimal one in a finite number of iterations: , which is unique, and an optimal policy, which may not be.
On the gridworld of section 1.2, it goes fast. The greedy policy with respect to the values of the random policy is already optimal: walk to the nearest corner. Better still, it is optimal long before the evaluation has converged: the greedy policy of already is, although is far from . The greedy step only reads the ranking of the neighbors of each state, not the values themselves, nor the order of all the states: that is why it settles at , one sweep before the full order of section 1.2.
Exercise 2.2 One improvement step on a 3 × 3 grid Math needed Compute: arithmetic, a small linear system, applying a formula of the pageend of high school Reason: expectations, inequalities, an argument in words: no new tools, but a chain of reasoningfirst year of university, with a first course in probability Prove: induction, limits, norms, fixed points: a proof, which can be skipped on a first readingabout two years of university math The level says which math is needed, not how long it takes.
A grid, the terminal state in the top-left corner, the other states numbered to row by row:
Actions up, down, left, right, deterministic; a move off the grid leaves the agent in place; per step; . The policy is the equiprobable random one.
- Evaluation. Write the Bellman equation of . Use the symmetry of the grid to reduce the number of unknowns, and solve the system.
- Action values. Compute for every state, or at least for states , , and .
- Improvement. Compute the greedy policy . How does it differ from ?
- Without computation, give , and check that everywhere.
Hint
With : , where when the move hits a wall.
Hint
The grid and the random policy are symmetric about the diagonal through , and . Set , , , , : five unknowns.
Hint
Start from state , which only sees , and itself: , so . Then work your way up.
Checkpoint what you should get
, , ; the greedy policy walks straight to , and .
Solution
-
With the symmetry, reading the neighbors of each representative state (up, down, left, right):
- state : (up bounces, down to , left to , right to );
- state : (up and right bounce);
- state : ;
- state : (right bounces);
- state : (down and right bounce).
The last one gives ; then state gives , state gives , and states and give . So , , , , :
-
, in the order up, down, left, right:
- state : , , , ;
- state : , , , ;
- state : , , , ;
- state : , , , ;
- state : , , , ;
- state : , , , ;
- state : , , , ;
- state : , , , .
-
The greedy actions are in bold: left at , and ; up at , and ; up or left (a tie) at and . Where was uniform over four actions everywhere, is deterministic, up to two ties: it walks to along a shortest path.
-
Each step of costs and gets one cell closer to , so is minus the distance to :
Better than in every non-terminal state, as the theorem says. In fact : on this grid, one improvement step is enough.
What to notice the take-away
is better than , although both are two steps from : the center has four useful moves, while state wastes half of its moves against the walls. Under a random policy, the value measures how hard it is to escape, not the distance. One improvement step is not always enough: on Jack’s car rental, below, it takes four.
2.3 A larger example: Jack’s car rental
Let’s take as an example a problem that is not a grid, with random dynamics, and too many states to read as a table.
Jack manages two car rental locations. Each day, customers come to each location to rent cars, and other customers return cars, in random numbers that follow Poisson distributions with known rates (3 and 4 requests a day, 3 and 2 returns). Jack earns $10 per car rented, if he has one. Overnight, he can move up to 5 cars between the two locations, at $2 per car moved. Each location holds at most 20 cars, and .
The state is the number of cars at each location, states; the action is the number of cars moved overnight, from to . The dynamics are random but fully known, so can be computed exactly: this is a planning problem. Starting from the policy that never moves any car, policy iteration converges in four improvements, . With 441 states, tables of numbers are no longer readable: the policy becomes a map, and the value function a surface.
Policy iteration already handles problems with hundreds of states. And with that many states, the value function looks less like a table than like a smooth surface: approximating it by a continuous function, instead of storing one number per state, is the idea of Part 4, From Discrete to Continuous.
3. Value iteration: optimize directly
Can we go faster than policy iteration? Are there shortcuts to its cumbersome full evaluations? Value iteration is one of them.
3.1 One sweep of the optimality backup
Policy iteration evaluates each policy to convergence before improving it, and section 2.2 showed that this is often wasted work: the greedy policy settles long before the values do.
Value iteration goes to the extreme, and converges directly to the optimal values . It skips the explicit policy, and applies the Bellman optimality equation as an update rule:
Same sweeps as policy evaluation, with a max over the actions instead of an average under . This has the consequence to converge to the optimal value policy (because of the properties of the Bellman equations). When the values have converged, one last greedy sweep extracts the policy:
Input: a threshold
- for every state
- repeat
- for each state
- until
- return
A sweep costs the same as a sweep of policy evaluation, about operations. Each one is an improvement and a partial evaluation at once. This method is often preferred to Policy Iteration, but its number of sweeps can somletimes be higher.
Exercise 3.1 Value iteration on the 3 × 3 grid Math needed Compute: arithmetic, a small linear system, applying a formula of the pageend of high school Reason: expectations, inequalities, an argument in words: no new tools, but a chain of reasoningfirst year of university, with a first course in probability Prove: induction, limits, norms, fixed points: a proof, which can be skipped on a first readingabout two years of university math The level says which math is needed, not how long it takes.
The grid of Exercise 2.2: terminal in the top-left corner, states to , per step, . This time, solve the optimality equation directly, from in every state, with and .
- Compute . Are all eight values equal? Why does the shape of the grid not matter yet?
- Compute . What appears?
- Without computing further, what does converge to? After how many sweeps?
- Deduce an optimal policy. Which states have several optimal actions? How many deterministic optimal policies are there?
Hint
After one sweep, every successor still has , including : nothing yet tells apart from the other cells.
Hint
In , only the neighbors of can use ; every other state only sees cells worth .
Checkpoint what you should get
everywhere; at states and , elsewhere; , so , exact after 4 sweeps; ties at states , , , , so deterministic optimal policies.
Solution
-
Every action gives , so for all eight states. After one sweep, the only information available is the immediate reward, the same everywhere; is not distinguishable yet, since .
-
States and are next to : their best move gives . Every other state can only reach cells worth , hence :
-
Each sweep makes one more ring exact: , with dist the number of moves to . So converges to , exactly, after sweeps:
-
An optimal action is any move that gets one cell closer to : left at and , up at and , and up or left at , , and , the states off the first row and the first column. That makes deterministic optimal policies, plus every random mixture of the tied actions.
What to notice the take-away
In Exercise 2.2, the greedy policy of had ties at states and only; the greedy policy of has ties at , , and . Both policies are optimal: they just break the ties differently. is unique, is not, so algorithms must be compared on the values they reach, not on the actions they pick.
3.2 Why does it converge?
On our grids, value iteration stops after a few sweeps. With and random dynamics, as in Jack’s car rental, never reaches exactly: it only gets closer at every sweep. That it always converges, and to only, comes from one property of the update, folded below for those who want the math.
Optional Why do these algorithms converge?: the Bellman operator is a contraction Math needed Compute: arithmetic, a small linear system, applying a formula of the pageend of high school Reason: expectations, inequalities, an argument in words: no new tools, but a chain of reasoningfirst year of university, with a first course in probability Prove: induction, limits, norms, fixed points: a proof, which can be skipped on a first readingabout two years of university math The level says which math is needed, not how long it takes.
Write one sweep of value iteration as an operator that maps a value function to another:
For , it is a -contraction in the max norm, :
Proof in two steps. For a fixed , the rewards cancel in the difference, and the weights are nonnegative and sum to : . Then, for any two functions of the action, , which carries the bound through the max.
The Banach fixed-point theorem does the rest: a contraction has a unique fixed point, the solution of , which is the Bellman optimality equation, so that point is . And iterating from any converges to it exponentially fast, . The same argument without the max shows that policy evaluation converges to .
With , as in our gridworlds, the operator is no longer a contraction, and the argument fails. The iterations still converge there because every step costs : a policy that never reaches the terminal state is worth and is never the max, and a policy that reaches it exists. The “Trap” preset of the gridworld Space shows what happens when some states cannot reach it.
4. Taking a step back
Policy iteration and value iteration look like two different algorithms. Before wrapping everything up, let’s take a step back on what they share: the same two processes underneath, and the same answer at the end, a unique optimal value function, with possibly different optimal policies.
4.1 Evaluate and improve: generalized policy iteration
Side by side, the two algorithms that find :
- Policy iteration: each iteration is a full evaluation followed by a greedy step. It reaches in a finite number of iterations: few iterations, each one expensive.
- Value iteration: each iteration is one sweep of the optimality backup. It reaches in the limit (exactly, on our grids): many sweeps, each one cheap.
Both are instances of one idea, generalized policy iteration (GPI): two processes that interact, an evaluation that makes the values consistent with the current policy, and an improvement that makes the policy greedy with respect to the current values. Together they converge to the only point that both accept, and . Almost every method of Part 3 is GPI with an approximate evaluation.
4.2 One optimal value function, many optimal policies
Exercise 3.1 ended on a fact worth a second example: the optimal value function is unique, the optimal policy is not.
In the gambler’s problem of the extra exercises (E.1 and E.2), the ties are not an accident of symmetry: with a fair coin, every policy is optimal.
4.3 Beyond full sweeps
Every algorithm of this page sweeps the whole state set, again and again: fine for 16 states, hopeless for a Rubik’s cube. Updating only some states, in any order, still works, and is the first step toward Part 3.
Optional Asynchronous dynamic programming: sweeps don't scale
Every algorithm so far sweeps the whole state set at each iteration. A Rubik’s cube has about states and a fully known model; at a million state updates per second, a single sweep would take more than a million years.
The first step away from sweeps is the in-place update of section 1.2: one array, and each update immediately visible to the next ones. Asynchronous DP goes further: update the states in any order, some more often than others, using whatever values are available. It still converges, under one condition: no state may be ignored forever.
This freedom lets us spend the computation where it helps: order the updates so that information propagates fast (backward from the goal, for instance), skip states that are irrelevant to good behavior, or run the updates while an agent interacts with the environment, on the states it actually visits. But every update still computes : the experience decides where to update, the model is still needed to decide what the update is. Part 3 removes that last requirement.
5. Wrap-up
To wrap up, back to the hike of Part 1, solved with the two algorithms of this page.
Policy iteration needs two cycles, and the first improvement already gives the optimal policy . Value iteration needs 36 sweeps, yet its greedy policy is optimal on the hiker’s path, at and , after 5. The long tail is spent on , a state the optimal hiker never visits: a case for the asynchronous updates of section 4.3.
All three algorithms of this page, on the gridworld and on harder grids, in one Space:
What to try in the Space above
- Policy evaluation. Click state 1, then step from to : the backup reads the four neighbors, with the walls sending the agent back to itself. Switch between two arrays and in place, and compare the number of sweeps.
- Policy iteration. Step from to : one improvement turns the random walk into the shortest-path policy. From to , the values do not change any more, but two states still widen their set of greedy actions: there, all four moves are equally good. Change the number of sweeps to get modified policy iteration.
- Value iteration. Compare the sweep counts in the status line: 4 sweeps on this grid (the values are exact after 3, the 4th detects that nothing changes), against more than a hundred for policy iteration, whose cycles each evaluate a policy to convergence. Then try the Walls preset, and watch the values flow around the walls, one ring per sweep.
In a few lines:
- Dynamic programming solves the Bellman equations when the model is known, one sweep over the states at a time, with the value function as the cache of the subproblems.
- Policy evaluation iterates the expectation backup and converges to ; the order between the states settles long before the values.
- Policy improvement: acting greedily with respect to never makes the policy worse, and changes nothing only when is already optimal.
- Policy iteration alternates full evaluations and greedy improvements, and stops after a finite number of them: few iterations, each one expensive.
- Value iteration iterates the optimality backup: many sweeps, each one cheap. Modified policy iteration sits in between, and all three are generalized policy iteration.
- is unique; the optimal policy need not be.
The next move, from exact and exhaustive (this page) to sampled, starts in Part 3.1: Learning from Experience and continues in Part 3.2: Learning to Act. There, the agent no longer knows : Monte Carlo methods, temporal-difference learning, SARSA and Q-learning estimate the same values from the transitions they experience. The last step, from tables to approximate functions, is Part 4: From Discrete to Continuous, where these methods start solving real problems.
Extra exercises
Three exercises to go further, from the lightest to the most mathematical:
- E.1, the gambler’s problem on three states: the optimality equation, checked by hand.
- E.2, the full gambler’s problem: reasoning about an optimal policy, no new math.
- E.3, why a deterministic optimal policy always exists: a proof, built on the contraction of section 3.2.
Exercise E.1 The gambler's problem, reduced Math needed Compute: arithmetic, a small linear system, applying a formula of the pageend of high school Reason: expectations, inequalities, an argument in words: no new tools, but a chain of reasoningfirst year of university, with a first course in probability Prove: induction, limits, norms, fixed points: a proof, which can be skipped on a first readingabout two years of university math The level says which math is needed, not how long it takes.
A gambler bets on a sequence of fair coin flips, . Her capital is ; she wins when it reaches and loses when it drops to . At each flip she stakes an integer amount : heads, her capital becomes ; tails, . The reward is on reaching and otherwise, and , so is the probability of winning from when playing optimally.
- Write the Bellman optimality equation for , , with (the is counted on the transition that reaches ).
- Check that satisfies the equation for every possible stake in every state.
- What does this say about the optimal policy? How many deterministic optimal policies are there?
- Without computing: how should and the best stakes change when the coin is unfavorable, , or favorable, ?
Hint
The stakes allowed are , , .
Hint
For question 4: with an unfavorable coin, is it better to flip many times or few times?
Checkpoint what you should get
, every stake attains the max, and there are deterministic optimal policies.
Solution
-
, with the max over .
-
With , , :
- , : ;
- , : ;
- , : ;
- , : .
Every stake gives back : the max is over equal numbers, is the fixed point, and .
-
Every action attains the max, so every policy is optimal: deterministic ones, and any random mixture at . The reason: with a fair coin, the expected capital after a flip equals the capital before, whatever the stake. The probability of reaching before is then , however you bet.
-
With , each flip is a losing bet on average, so and the gambler should flip as few times as possible: stake big (with , the optimal stake at is , and ). With , each flip is a winning bet, , and she should stake small and play many flips (with , the optimal stake at is ).
What to notice the take-away
With , the ties disappear and the optimal stakes depend on the coin. In the full problem of Sutton and Barto (Example 4.3, capital up to , ), the plotted optimal policy has a strange sawtooth shape, with an all-in bet at but a stake of at . Part of that shape comes from how ties are broken: many stakes are optimal, and the plot shows one choice among them.
Exercise E.2 The gambler's curious optimal policy Math needed Compute: arithmetic, a small linear system, applying a formula of the pageend of high school Reason: expectations, inequalities, an argument in words: no new tools, but a chain of reasoningfirst year of university, with a first course in probability Prove: induction, limits, norms, fixed points: a proof, which can be skipped on a first readingabout two years of university math The level says which math is needed, not how long it takes.
The full problem of Sutton and Barto (Example 4.3): the gambler of Exercise E.1, with a capital , the goal at , stakes , and an unfavorable coin, . The optimal policy plotted in the book has an odd shape: at capital it bets everything, but at capital it stakes only .
- Give an intuitive argument for why staking everything is optimal at .
- At , the largest stake allowed is . Why does a stake of lose nothing there?
- Value iteration gives , attained by both stakes and . What does this say about the plot in the book?
Hint
With , each flip is a losing bet on average: does the gambler want to play many flips, or few?
Hint
At , with a stake of : where is the gambler if the flip is lost? If it is won?
Checkpoint what you should get
, attained only by the all-in bet; at , stakes and tie.
Solution
- With , every flip has a negative expected value: the gambler wants to reach in as few flips as possible. At , the largest stake allowed is , and it reaches the goal in a single flip, with probability . Any smaller stake buys at least one more flip, each a losing bet. So , attained by the all-in bet only.
- If the flip is lost, the gambler is at , from which the one-flip shot at the goal is still available; if it is won, she is at , better still. The small stake keeps the all-in option at instead of spending it.
- The two stakes are equally good: the optimal policy is not unique at , and the plot shows one tie-break among many, the smallest maximizing stake. Another tie-break draws a different picture, just as optimal. The plot of is one answer; is the answer.
What to notice the take-away
Exercise E.1 (fair coin) had every stake tied; with , many ties remain. Before reading meaning into the shape of an optimal policy, check whether the algorithm had to break ties to draw it.
Exercise E.3 Why a deterministic optimal policy exists Math needed Compute: arithmetic, a small linear system, applying a formula of the pageend of high school Reason: expectations, inequalities, an argument in words: no new tools, but a chain of reasoningfirst year of university, with a first course in probability Prove: induction, limits, norms, fixed points: a proof, which can be skipped on a first readingabout two years of university math The level says which math is needed, not how long it takes.
Part 1 (section 4.1) stated a theorem: a finite MDP has a deterministic optimal policy, greedy with respect to . This exercise proves it, for and bounded rewards. Besides the operator of section 3.2, define, for a policy ,
Both are -contractions: the unique fixed point of is , that of is .
- Let , a deterministic policy. Show that , and deduce .
- Show that is monotone: if in every state, then . Show also that for every .
- Deduce that for every policy , so that is optimal.
- Two actions tie for the argmax in some state. Why do both choices give an optimal policy, and why does not depend on the choice? Compare with the ties of Exercise 3.1.
- Where is it used that each is finite?
Hint
Question 1: is an action that attains the max in .
Hint
Question 3: start from , and apply to both sides, again and again.
Checkpoint what you should get
, so ; and .
Solution
- By definition of , the action attains the max: . So is a fixed point of , whose only fixed point is : .
- If , then for every , , because and . Averaging over with the nonnegative weights keeps the inequality. And an average is at most a max: .
- By question 2, . Applying the monotone again and again: , and this sequence converges to , the fixed point of the contraction . So for every , and with question 1, : is optimal, and deterministic by construction.
- Either tied action attains the max, so questions 1 to 3 apply to both choices: each gives a deterministic optimal policy. is the fixed point of , which does not involve any tie-break. This is what Exercise 3.1 showed: several optimal policies, one .
- In the definition of : over a finite set of actions, the max is always attained by some action. Over an infinite set, the supremum may not be reached, and no greedy action may exist.
What to notice the take-away
The proof never compares policies one by one, there are too many of them: it compares operators. The max of the Bellman optimality equation is attained by a single action in each state, and that is the whole reason a deterministic policy can be optimal.