Overview

4 Perfect Knowledge, Optimal Policy: Dynamic Programming

This chapter introduces dynamic programming as the most direct way to solve a Markov decision process when the full environment model is known. It contrasts model-based and model-free approaches, then shows how dynamic programming fits among the main reinforcement learning paradigms: bandits, temporal difference learning, Monte Carlo methods, tree search, and planning with full knowledge. The central message is that dynamic programming is elegant because it exploits transition probabilities and rewards to reason about the future, but it is also expensive and therefore best understood as a foundation for later RL methods rather than a universally practical solution.

The core of the chapter is the Bellman equation, presented as a recursive “domino rule” that breaks a decision’s value into immediate reward plus discounted future value. The text distinguishes between state-value functions, action-value functions, and advantage, showing how each helps evaluate behavior from a different angle. Backup diagrams are used to build intuition for how value propagates backward through possible future states. With these concepts in place, generalized policy iteration is introduced as the main dynamic programming loop: evaluate the current policy, improve it greedily using the resulting values, and repeat until the policy stabilizes.

Two worked examples make the theory concrete. In the inventory problem, repeated policy evaluation and improvement gradually transform a naive “always order one” policy into an optimal state-dependent strategy that orders more when stock is low and stops ordering when inventory is sufficient. In the resource allocation example, the chapter gives code for defining states, actions, transition probabilities, rewards, policy evaluation, policy improvement, and a full generalized policy iteration routine. The resulting learned policy behaves like a smart portfolio manager, choosing safer projects when resources are scarce and riskier ones when the budget allows. The chapter closes by emphasizing dynamic programming’s main limitations: the explosion of state space and the unrealistic requirement for a perfect model of the environment.

Five different paradigms in solving Markov decision process.
Five different paradigms in solving Markov decision process.
State value function representation
State value function representation
Backup diagram for state value function.
Backup diagram for state value function.
Action value function representation.
Action value function representation.
Backup diagram for action value function.
Backup diagram for action value function.
General algorithm and schema of general policy iteration algorithm.
General algorithm and schema of general policy iteration algorithm.
Generalized policy iteration algorithm.
Generalized policy iteration algorithm.
Visualization of results for resource allocation problem.
Visualization of results for resource allocation problem.

Summary

  • Dynamic programming is a collection of model-based algorithms used to solve Markov decision processes by breaking a problem into a sequence of smaller, recursive subproblems.
  • Fundamental requirement of dynamic programming is the perfect model of the environment, meaning the state transition probabilities and reward function are completely and fully given and known.
  • The Bellman equations are the core mathematical tool, defining the value of a state or state-action pair recursively in terms of the values of successor states.
  • We use two key metrics: the state-value function which measures the long-term value of being in a state, and the action-value function which measures the value of taking an action from that state.
  • Generalized Policy Iteration is the central pattern for solving Markov decision process, involving an interaction between two processes:
  • Policy Evaluation: Calculating the value function for the current policy.
  • Policy Improvement: Greedily updating the policy based on the current values.
  • Value Iteration is an efficient dynamic programming algorithm that combines a single sweep of evaluation with an improvement step in each iteration, guaranteeing convergence to the optimal policy.
  • The hands-on resource allocation example showed dynamic programming uncovering a nuanced strategy—be conservative with low budgets, takes balanced actions with medium budgets, and acts opportunistically with high budgets.
  • Dynamic programming is severely limited by the curse of dimensionality, making it computationally and memory-wise intractable for problems with large state spaces.
  • The requirement of a perfect model is a second major weakness of dynamic programming as such models are unavailable or inaccurate for most real-world problems, motivating the need for model-free methods.

FAQ

What is dynamic programming in reinforcement learning?Dynamic programming in reinforcement learning is a model-based method for solving a Markov decision process when the transition probabilities and reward function are known. It uses Bellman equations to recursively evaluate and improve a policy until an optimal policy is found.
What are the two main steps of generalized policy iteration?Generalized policy iteration alternates between two processes: policy evaluation, which estimates how good the current policy is, and policy improvement, which updates the policy to choose better actions based on those value estimates.
What is the Bellman equation trying to compute?The Bellman equation computes the value of a state or state-action pair by combining the immediate reward with the discounted expected value of future states. It expresses long-term return recursively.
What is the difference between the state-value function Vπ(s) and the action-value function Qπ(s,a)?The state-value function Vπ(s) measures how good it is to be in a state under a policy, while the action-value function Qπ(s,a) measures how good it is to take a specific action in that state and then follow the policy afterward.
What is the advantage function?The advantage function is defined as Aπ(s,a) = Qπ(s,a) - Vπ(s). It tells us how much better or worse a specific action is compared to the average value of the state under the policy.
How does policy evaluation work in dynamic programming?Policy evaluation repeatedly applies the Bellman expectation equation to update the value of each state under the current policy until the values stop changing significantly. It averages over possible next states using their transition probabilities and rewards.
How does policy improvement choose a better policy?Policy improvement evaluates all valid actions in each state and selects the action with the highest expected return, using the current value function. This greedy update produces a new policy that should be better than the previous one.
What is value iteration?Value iteration is an efficient form of generalized policy iteration that performs one sweep of value updates and then immediately improves the policy. It repeats this process until the policy becomes stable.
Why was dynamic programming useful in the inventory management example?It allowed the optimal ordering policy to be computed exactly from the known model. In the example, the method found that the best policy was to order 2 units in state 0, 1 unit in state 1, and 0 units in states 2 and 3+.
What are the main limitations of dynamic programming?The two major limitations are the curse of dimensionality and the need for a perfect model. Dynamic programming becomes computationally expensive as the state space grows, and it is difficult to use when transition probabilities and rewards are unknown in real-world problems.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Applied Reinforcement Learning Applied Reinforcement Learning ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Applied Reinforcement Learning Applied Reinforcement Learning ebook for free