Chapter 17

Sequential Decision Making and Dynamic Programming

In the referenced item, we introduced finite state machines as a structured and explicit way to model the logical flow of a robot’s behavior, allowing us to hand-design a set of rules that govern its actions in response to events. For well-defined tasks with a limited number of states, a carefully crafted finite state machine is an effective and interpretable way to implement a robot’s decision-making logic. However, the very structure that makes finite state machines clear also imposes fundamental limitations, especially as the complexity and uncertainty of the robot’s environment grow.

This chapter introduces a more general, optimization-based framework that directly addresses these limitations. Rather than manually specifying decision rules, we formulate decision-making as a mathematical optimization problem, allowing optimal actions to be computed automatically for complex, multi-step tasks, even under uncertainty. The central computational tool explored in this chapter is dynamic programming, a powerful algorithmic paradigm for solving sequential decision-making problems. Dynamic programming operates by decomposing complex, long-horizon problems into a sequence of simpler, nested subproblems that can be solved efficiently. As we will see, it also provides the theoretical foundation for modern learning-based approaches like reinforcement learning (the referenced item) and imitation learning (the referenced item).

We begin in Section 17.1 by applying dynamic programming to deterministic decision-making problems, where the robot’s actions have certain and predictable outcomes. We then extend this framework in Section 17.2 to stochastic decision-making problems, specifically Markov decision processes (MDPs), which explicitly account for uncertainty in the environment. Finally, in Section 17.3, we discuss the limitations of classical dynamic programming methods, thereby motivating the learning-based approaches introduced in later chapters.

17.1 Deterministic Sequential Decision Making

We begin our study of dynamic programming by introducing a simple, yet extremely general, formulation of sequential decision making. Despite its apparent simplicity, this formulation captures a wide range of problems arising in robotics, control, operations research, and artificial intelligence. We first consider the deterministic case, where the outcome of each action is fully predictable, before addressing stochasticity in the next section.

In this formulation, time is modeled as a sequence of discrete stages at which decisions are made. That is, we consider a discrete-time settingmargin: The continuous-time counterpart to dynamic programming is the Hamilton-Jacobi-Bellman (HJB) equation. While the HJB equation is beyond the scope of this chapter, we refer interested readers to Bertsekas (2000)11. Bertsekas, D. Dynamic Programming and Optimal Control. Athena Scientific, 2000. for a comprehensive treatment of continuous-time decision-making. , where the evolution of the system is described by a difference equation of the form:

𝒙t+1=ft(𝒙t,𝒖t),t=0,,T1, (17.1)

where 𝒙tn denotes the system state at time step t, 𝒖tm is the control applied at that step, and ft specifies how the state evolves. The integer T defines a finite planning horizon.

At each time step, not all controls may be available. We therefore associate with each state 𝒙t a set of admissible controls, denoted by 𝒰(𝒙t), and impose the constraint:

𝒖t𝒰(𝒙t),t=0,,T1. (17.2)

No particular structure is assumed for 𝒰(𝒙t). Depending on the application, it may be a finite set of discrete actions, a continuous region of allowable inputs, or a state-dependent subset encoding physical, logical, or resource limitations.

The objective of the decision making problem is specified through an additive cost function defined over the planning horizon:

J(𝒙0,𝒖0,,𝒖T1)=gT(𝒙T)+t=0T1gt(𝒙t,𝒖t), (17.3)

where gt represents the stage cost incurred at time t and gT is a terminal cost applied to the final state. The additivity of the cost over time is a central structural assumption: it is this property that enables the decomposition of the problem into simpler subproblems, which lies at the heart of dynamic programming. No assumptions are made regarding smoothness, convexity, or time invariance of the cost functions.

The deterministic sequential decision making problem can be formally defined as follows:

Definition 17.1 (Deterministic Sequential Decision Making Problem).

Given the discrete-time system (Equation 17.1), the control constraints (Equation 17.2), and the additive cost function (Equation 17.3), the deterministic sequential decision making problem can be stated as the following optimization problem:

J(𝒙0)=minimize𝒖t,t=0,,T1gT(𝒙T)+t=0T1gt(𝒙t,𝒖t),subject to𝒙t+1=ft(𝒙t,𝒖t),t=0,,T1,𝒖t𝒰(𝒙t),t=0,,T1. (17.4)

As we will see throughout the remainder of this chapter, the central goal of dynamic programming is to compute a solution to Problem (Equation 17.4) in the form of an optimal closed-loop control policy:

π={π0,,πT1},𝒖t=πt(𝒙t),t=0,,T1, (17.5)

where the policy π denotes a sequence of functions πt mapping any state 𝒙t into a control 𝒖tmargin: In the deterministic setting considered here, the system evolution is fully predictable, and closed-loop and open-loop optimal solutions therefore coincide. The true advantages of closed-loop policies emerge mainly in the stochastic setting, which we address in the next section. .

In Chapter 3, we were able to derive closed-loop control laws primarily by exploiting strong structural assumptions, such as linear system dynamics, quadratic cost functions, or special system properties like differential flatness. While these assumptions yield elegant and computationally efficient solutions, they substantially limit the range of problems that can be addressed. Dynamic programming offers a fundamentally different approach. Rather than relying on restrictive structural properties, it exploits only the sequential nature of the decision-making process and the additive structure of the cost function, thereby providing a systematic framework for computing optimal closed-loop policies for a much broader class of systems, including those with nonlinear dynamics and non-quadratic costs.

In what follows, we will explore how dynamic programming can be employed to solve the deterministic decision making problem defined above through the so-called principle of optimality.

17.1.1 The Principle of Optimality

The dynamic programming approach to sequential decision making rests on a very simple, yet powerful, idea, known as the principle of optimalitymargin: Often referred to as Bellman’s principle of optimality. . Despite its simplicity, this principle is the key that transforms an otherwise intractable optimization problem into one that can be solved efficiently through recursive decomposition.

At a high level, the principle of optimality expresses the following insight. Suppose that a policy is optimal for a given decision making problem. Then, if we consider any intermediate time and the state reached at that time while following this policy, the remaining decisions prescribed by the policy must themselves be optimal for the subproblem that starts from that state. In other words, any tail segment of an optimal policy must itself be optimal for the corresponding tail subproblem.

The intuitive justification for this principle is straightforward. If the remaining decisions after some time were not optimal for the corresponding tail subproblem, then we could replace them with a better alternative and thereby reduce the total cost. This would contradict the assumption that the original policy was optimal. Thus, optimal policies must be composed of optimal solutions to all of their tail subproblems.

This idea is most easily visualized in shortest-path problems, as illustrated in Figure 17.1. If the optimal path from a starting point a to a destination e passes through an intermediate point b, then the portion of the path from b to e must itself be optimal among all paths that start at b. Otherwise, a shorter path from b to e could be substituted, yielding a shorter overall path from a to e.

Refer to caption
Figure 17.1: Illustration of the principle of optimality for a shortest-path problem. If the path abe is optimal from a to e, then the subpath be must be optimal when starting from b. Adapted from Kirk (2004)22. Kirk, D. E. Optimal Control Theory: An Introduction. Dover Publications, 2004..

We now state the principle of optimality formally for deterministic decision making problems.

Theorem 17.2 (Principle of Optimality (Deterministic Case)).

Let {𝐮0,𝐮1,,𝐮T1} be an optimal control sequence to the deterministic decision making problem defined by Problem 17.4 with initial condition 𝐱0, and let {𝐱0,𝐱1,,𝐱T} denote the corresponding optimal state trajectory. Then, for any time t{0,,T1}, the truncated control sequence {𝐮t,,𝐮T1} is optimal for the subproblem that starts from state 𝐱t at time t and minimizes the tail cost:

Jtail(𝒙t,𝒖t,,𝒖T1)=gT(𝒙T)+i=tT1gi(𝒙i,𝒖i),

subject to the same system dynamics and control constraints, over the horizon t to T.

The principle of optimality suggests that an optimal policy can be constructed by solving a sequence of smaller subproblems. One may first solve the tail subproblem involving only the final stage, then extend this solution to the tail subproblem involving the last two stages, and continue in this manner until an optimal policy for the entire horizon is obtained. The dynamic programming algorithm is based precisely on this idea: it proceeds backward in time, solving tail subproblems of increasing length by reusing solutions to shorter tail subproblems.

Example 17.1.1 (Shortest-Path Problem).

Consider the deterministic shortest-path problem shown in Figure 17.2, where the objective is to find an optimal path from point b to point f. Suppose that the optimal costs from points c, d, and e to f are already known.

Refer to caption
Figure 17.2: By leveraging optimal tail costs, the number of candidate paths that must be evaluated when searching from b to f is dramatically reduced. Adapted from Kirk (2004)33. Kirk, D. E. Optimal Control Theory: An Introduction. Dover Publications, 2004..

A brute-force approach would require evaluating all possible paths from b to f, including:

{bcf,bcdf,bcdef,bdcf,bdf,bdef,bedcf,bedf,bef}.

By exploiting the principle of optimality, we know that any optimal path from b to f must consist of an immediate step from b to one of its neighboring points (c, d, or e), followed by an optimal path from that neighboring point to f. Thus, only three candidate paths need to be evaluated:

bcf,bdf,bef,

where the optimal path can be found by selecting the minimum-cost path among:

{Jbc+Jcf,Jbd+Jdf,Jbe+Jef}.

This simple example illustrates the central idea underlying dynamic programming, where optimal tail costs are reused to efficiently construct optimal solutions to larger problems. We now formalize this idea into a systematic algorithm for solving sequential decision making problems.

17.1.2 The Dynamic Programming Algorithm

The principle of optimality provides a powerful structural property of optimal solutions, but by itself it does not specify how such solutions should be computed. Dynamic programming turns this structural insight into a concrete computational procedure to find optimal control policies.

Rather than attempting to find an optimal control sequence over the entire horizon at once, dynamic programming proceeds by solving a sequence of smaller subproblems backward in time. This motivates the introduction of the so-called cost-to-go function. For each time t{0,,T} and each state 𝒙t, define the optimal cost-to-go Jt(𝒙t) as the minimum achievable cost when the system starts from state 𝒙t at time t and evolves optimally until the terminal time T. By definition, the cost-to-go at time T coincides with the terminal cost:

JT(𝒙T)=gT(𝒙T),𝒙T𝒳,

since no further decisions remain to be made after time T.

The principle of optimality implies that the optimal cost-to-go functions satisfy a recursive relationship, where the cost-to-go at time t can be expressed in terms of the stage cost at time t and the cost-to-go at time t+1:

Jt(𝒙t)=min𝒖t𝒰(𝒙t)[gt(𝒙t,𝒖t)+Jt+1(ft(𝒙t,𝒖t))],t=0,,T1. (17.6)

This equation, often referred to as the Bellman equation, expresses the global optimization problem in terms of a local minimization combined with the optimal solution of a shorter-horizon problem.

In practice, the dynamic programming algorithm leverages the Bellman equation to compute the optimal cost-to-go functions via a backward-in-time recursion, as summarized in Algorithm 1. Starting from the known terminal cost JT, one computes JT1, then JT2, and so on, until J0 is obtained. At each stage of this backward recursion, the optimal tail cost is computed for every state in the state space. The result is a collection of functions {Jt()}t=0T that completely characterize the optimal performance of the system from any state and time.

JT(𝒙T)=gT(𝒙T), for all 𝒙T𝒳
for t=T1 to 0 do
      Jt(𝒙t)=min𝒖t𝒰(𝒙t)[gt(𝒙t,𝒖t)+Jt+1(ft(𝒙t,𝒖t))], for all 𝒙t𝒳
 
return J0(),,JT()
Algorithm 1 Dynamic Programming (Deterministic)

Once the cost-to-go functions have been computed, the optimal control at each time step is obtained by minimizing the sum of the immediate cost and the optimal cost-to-go of the resulting next state:

𝒖t=argmin𝒖t𝒰(𝒙t)[gt(𝒙t,𝒖t)+Jt+1(ft(𝒙t,𝒖t))].

The system is then propagated to the next state 𝒙t+1=ft(𝒙t,𝒖t), and the process is repeated until the terminal time T is reached. Conceptually, the backward pass computes the optimal cost-to-go functions of every tail subproblem, while the forward pass uses these functions to compute the actions that realize these optimal costs.

Although dynamic programming yields an exact solution to the deterministic decision making problem, its direct application is often limited by computational considerations. The backward recursion requires evaluating the Bellman equation for every possible state at every time step, which may be infeasible when the state space is continuous or very large. Discretization of the state space may render the algorithm implementable, but even then the computational burden can grow rapidly with the dimension of the state. These challenges motivate the development of approximate methods that retain the conceptual framework of dynamic programming while relaxing its computational demands.

Despite these practical limitations, dynamic programming occupies a central role in sequential decision making. It provides the canonical solution method for finite-horizon problems, offers a precise interpretation of optimality through cost-to-go functions, and serves as the conceptual foundation for a wide range of learning-based control algorithms.

Example 17.1.2 (Grid Navigation).

Consider the environment shown in Figure 17.3, where the objective is to move from point a to point h while incurring the minimum possible cost. The state 𝒙={a,b,c,d,e,f,g,h} corresponds to the agent’s current location on the grid, and the available control actions at each state are encoded by the arrows indicating allowable directions of travel. For example, at point c the agent may move either right or up, but not left or down. Each directed edge is associated with a nonnegative traversal cost, as shown in the figure.

Refer to caption
Figure 17.3: A deterministic decision making problem where the goal is to move from point a to point h while incurring the minimal amount of cost. The path adefgh is the optimal path. We solve this problem by dynamic programming in Example 17.1.2. Adapted from Kirk (2004)44. Kirk, D. E. Optimal Control Theory: An Introduction. Dover Publications, 2004..

We treat point h as a terminal state with zero terminal cost, so that:

JT(h)=0,

and we allow the agent to remain at h at zero cost once it is reached. The dynamic programming recursion is initialized at this terminal condition and proceeds backward in time, successively computing the optimal cost-to-go for states that can reach h within an increasing number of steps.

At the first backward step, corresponding to one step from the terminal time, only states that can transition directly to h are relevant. These are the points e and g, together with h itself. The optimal costs-to-go are obtained by adding the immediate transition cost to the terminal cost:

JT1(h)=0+JT(h)=0,uT1(h)=stay,JT1(e)=8+JT(h)=8,uT1(e)=right,JT1(g)=2+JT(h)=2,uT1(g)=up.

At the next step of the recursion, states such as d and f become relevant, since they can reach h in two steps. Their optimal costs-to-go are computed by adding the immediate cost of moving to e or g and then using the previously computed one-step tail costs:

JT2(d)=3+JT1(e)=11,uT2(d)=right,JT2(f)=3+JT1(g)=5,uT2(f)=right.

At this point, the cost-to-go values represent the optimal cost of reaching h in at most two steps from each of the states {d,e,f,g}.

As the recursion continues, additional states enter the computation, and some states acquire multiple feasible paths to the terminal state. For example, at the next backward step, state e may either move directly to h or move downward to f and then proceed optimally from there. The Bellman equation automatically selects the cheaper of these alternatives:

JT3(e)=min{8+JT2(h), 2+JT2(f)}=7,uT3(e)=down.

Similarly, the other relevant states at this step are updated as follows:

JT3(g)=2,uT3(g)=up,JT3(d)=3+JT2(e)=11,uT3(d)=right,JT3(f)=5,uT3(f)=right,JT3(a)=8+JT2(d)=19,uT3(a)=right,JT3(c)=min{5+JT2(d), 3+JT2(f)}=8,uT3(c)=right.

At this stage, we see that the goal h is reachable from a in three time steps on path adeh, and that we would incur a cost of 19.

Extending the horizon further allows the algorithm to discover lower-cost paths that take advantage of additional intermediate states. In particular, the recursion continues one more step to yield:

JT4(e)=7,uT4(e)=down,JT4(g)=2,uT4(g)=up,JT4(d)=3+JT3(e)=10,uT4(d)=right,JT4(f)=5,uT4(f)=right,JT4(a)=8+JT3(d)=19,uT4(a)=right,JT4(c)=min{5+JT3(d), 3+JT3(f)}=8,uT4(c)=right,JT4(b)=9+JT3(c)=17,uT4(b)=right.

Finally, extending the horizon one last time allows the algorithm to find the optimal path from the initial state a to the goal state h, corresponding to the path adefgh, with total cost 18:

JT5(e)=7,uT5(e)=down,JT5(g)=2,uT5(g)=up,JT5(d)=10,uT5(d)=right,JT5(f)=5,uT5(f)=right,JT5(a)=min{8+JT4(d), 5+JT4(b)}=18,uT5(a)=right,JT5(c)=min{5+JT4(d), 3+JT4(f)}=8,uT5(c)=right,JT5(b)=9+JT4(c)=17,uT5(b)=right.

Several important features of dynamic programming are illustrated by this example. First, the algorithm does not search over complete paths from a to h. Instead, it incrementally builds optimal solutions by reusing previously computed tail costs. Second, the algorithm computes optimal costs and controls for all states, not just the initial state of interest. As a result, once the cost-to-go functions have been computed, optimal paths can be generated immediately from any starting point and for any horizon length. For example, starting from point c with a horizon of three steps, the optimal path cfgh and its associated cost of 8 can be read off directly, without any additional computation.

17.2 Decision Making Under Uncertainty: Markov Decision Processes

The deterministic decision making framework developed in Section 17.1 provides a clean and powerful lens through which sequential decision making problems can be understood and solved. In realistic robotic systems, however, the environment is never perfectly known, and uncertainty is an intrinsic feature of physical interaction and perception. Sensor measurements are noisy, actuation is imperfect, and the environment may evolve in ways that cannot be modeled exactly or anticipated in advance. As a result, the evolution of the system state cannot be described deterministically, and decision quality must be evaluated in a statistical sense.

The goal of this section is to extend the deterministic sequential decision making problem in Definition 17.1 to explicitly account for such uncertainty. We begin by deriving a stochastic formulation of the sequential decision making problem that incorporates uncertainty into both the system dynamics and the cost structure, thereby introducing the framework of Markov decision processes (MDPs). This framework is a cornerstone of modern decision making under uncertainty, and will serve as a bridge to the learning-based methods developed in the referenced item and the referenced item. We then adapt the principle of optimality and the dynamic programming algorithm to this stochastic setting.

17.2.1 Problem Formulation

We begin by modifying the state transition model in (Equation 17.1) to include a stochastic disturbance:

𝒙t+1=ft(𝒙t,𝒖t,𝒘t),t=0,,T1, (17.7)

where 𝒘t denotes a stochastic disturbance at time t. The disturbance 𝒘t is assumed to be drawn from a known conditional probability distribution:

𝒘tpt(𝒘t𝒙t,𝒖t). (17.8)

This assumption implies that the distribution of the next state depends only on the current state and action, and not on the full history of the system. This conditional independence assumption is an instance of the Markov propertymargin: Which we previously encountered in Chapter 11 in the context of Bayesian filtering. , which states that, given the present state and action, the future evolution of the system is independent of the past. Accordingly, the state 𝒙t can be interpreted as a sufficient summary of all past information relevant for future decision making.

The admissible control constraints remain unchanged. At each time step, the control must satisfy:

𝒖t𝒰(𝒙t),t=0,,T1, (17.9)

where 𝒰(𝒙t) may encode physical limitations, logical constraints, or discrete action choices.

We also allow the instantaneous cost to depend explicitly on the disturbance:

gt:𝒳×𝒰×𝒲,t=0,,T1, (17.10)

where 𝒲 denotes the space of possible disturbance values.

Because the system evolution is now stochastic, performance can no longer be evaluated along a single trajectory. Instead, we measure performance in expectation. Specifically, given a policy π={π0,,πT1}, we define the associated expected cost starting from an initial state 𝒙0 as:

Jπ(𝒙0)=𝔼𝒘[gT(𝒙T)+t=0T1gt(𝒙t,πt(𝒙t),𝒘t)], (17.11)

where the expectation is taken with respect to the joint distribution of the disturbance sequence {𝒘0,,𝒘T1} induced by the policy π and the stochastic dynamics (Equation 17.7). This formulation corresponds to a risk-neutral objective, where policies are compared based on their average performancemargin: While the expected cost formulation is the most common in the literature, alternative risk measures—such as worst-case performance or risk-sensitive criteria—can also be considered, but are beyond the scope of this chapter. .

We can now formally state the stochastic sequential decision making problem.

Definition 17.1 (Stochastic Sequential Decision Making Problem).

Given the stochastic dynamics (Equation 17.7), the control constraints (Equation 17.9), and the expected cost (Equation 17.11), the stochastic sequential decision making problem consists of computing an optimal policy:

π={π0,,πT1},

that solves:

J(𝒙0)=minimize[π]𝔼𝒘[gT(𝒙T)+t=0T1gt(𝒙t,πt(𝒙t),𝒘t)],subject to𝒙t+1=ft(𝒙t,𝒖t,𝒘t),t=0,,T1,𝒖t=πt(𝒙t)𝒰(𝒙t),t=0,,T1. (17.12)

This formulation is also known as a finite-horizon Markov decision process. It mirrors the deterministic problem in structure, differing only in the introduction of random disturbances and the use of expected cost as the performance criterion. As we will see next, this similarity allows us to extend the principle of optimality and dynamic programming methods to this stochastic settingmargin: This problem formulation lies at the core of several disciplines, including optimal control, operations research, robotics, economics, and machine learning. As a result, it is common to encounter substantially different notation and terminology across communities, even when describing essentially the same underlying problem. In this chapter, we adopt notation that is standard in optimal control. In the next chapter, we will reintroduce the same problem using notation that is more common in the machine learning and reinforcement learning literature. For a broader discussion of the connections, overlaps, and distinctions among these perspectives, we refer the reader to Powell (2012)55. Powell, W. B. “AI, OR and control theory: A Rosetta Stone for stochastic optimization.” In Princeton University, 2012.. .

17.2.2 Stochastic Decision Making with Dynamic Programming

As in the deterministic case, the key property that enables efficient solution methods for the stochastic sequential decision making problem is the principle of optimality. In this subsection, we first state the principle of optimality for the stochastic setting, and then show how it leads to a dynamic programming recursion for computing optimal policies.

Principle of Optimality.

In the stochastic setting, the core intuition behind the principle of optimality—that optimal policies can be constructed by composing optimal solutions to tail subproblems—remains valid. However, it must be formulated in terms of expected future cost. Because state transitions are random, it is no longer meaningful to reason in terms of a single “optimal trajectory”. Instead, policies are evaluated by the expected cumulative cost they induce under the stochastic elements in the system.

The crucial assumption that enables a recursive decomposition is the Markov property. Specifically, as introduced in Chapter 11, the Markov property ensures that the future evolution of the system depends only on the current state and action, and not on the full history leading up to that state. Consequently, the expected cost incurred from time t onward depends only on 𝒙t and the future actions selected by the policy.

Under this assumption, if a policy is optimal from the initial condition, then after reaching any intermediate state 𝒙t, the remaining portion of that policy must still be optimal for the tail problem that starts at 𝒙t. If this were not the case, the tail could be replaced by an alternative policy with strictly lower expected cost, thereby reducing the overall expected cost and contradicting the optimality of the original policy.

Formally, we can state the principle of optimality for the stochastic decision making problem as follows:

Theorem 17.2 (Principle of Optimality (Stochastic Case)).

Let π={π0,π1,,πT1} be an optimal policy for the stochastic decision making problem defined in Problem 17.12. For any time t and any state 𝐱t that is reachable under π, the tail policy {πt,,πT1} is an optimal policy for the tail subproblem that starts at time t from state 𝐱t and minimizes the expected cost:

Jπ(𝒙t)=𝔼𝒘[gT(𝒙T)+i=tT1gi(𝒙i,πi(𝒙i),𝒘i)].

As in the deterministic case, this result implies that optimal policies can be constructed by solving a sequence of nested tail subproblems, with dynamic programming providing a systematic procedure for carrying out this backward construction, as we describe next.

Dynamic Programming.

The dynamic programming algorithm for the stochastic case closely mirrors its deterministic counterpart. It proceeds backward in time, starting from the terminal cost:

JT(𝒙T)=gT(𝒙T),𝒙T𝒳,

and successively computing the optimal cost-to-go for earlier stages. At each step, the algorithm evaluates, for every state, the expected cost associated with each admissible control and selects the minimizing one. Similarly, the cost-to-go can be expressed recursively, yielding the (stochastic) Bellman equation:

Jt(𝒙t)=min𝒖t𝒰(𝒙t)𝔼𝒘[gt(𝒙t,𝒖t,𝒘t)+Jt+1(ft(𝒙t,𝒖t,𝒘t))],t=0,,T1. (17.13)

The resulting dynamic programming algorithm is summarized in Algorithm 2.

JT(𝒙)=gT(𝒙), for all 𝒙𝒳
for t=T1 to 0 do
      Jt(𝒙)=min𝒖𝒰(𝒙)𝔼𝒘[gt(𝒙,𝒖,𝒘)+Jt+1(ft(𝒙,𝒖,𝒘))], for all 𝒙𝒳
 
return J0(),,JT()
Algorithm 2 Dynamic Programming (Stochastic Case)

Once the cost-to-go functions have been computed, an optimal policy is obtained by selecting:

πt(𝒙t)=argmin𝒖t𝒰(𝒙t)𝔼𝒘[gt(𝒙t,𝒖t,𝒘t)+Jt+1(ft(𝒙t,𝒖t,𝒘t))]. (17.14)
Example 17.2.1 (Inventory Control).

Consider a simple inventory control problem in which the state xt denotes the available stock of an item at time t. At each step, the decision maker chooses how many items to order, ut, before facing a random demand wt.

The system dynamics are given by:

xt+1=max{0,xt+utwt},

which captures the fact that demand reduces inventory, restocking increases it, and inventory cannot go below zero. We impose the constraint:

xt+ut2,

so that the inventory capacity is limited to at most two units.

Demand is modeled as a discrete random variable with the following probability distribution:

p(wt=0)=0.1,p(wt=1)=0.7,p(wt=2)=0.2.

We consider a finite horizon of T=3 steps and define the expected cost:

𝔼w[t=02(ut+(xt+utwt)2)],

which penalizes both ordering costs and the squared deviation between inventory and demand.

To solve this problem using dynamic programming, we first identify the state and control spaces:

𝒳={0,1,2},𝒰(xt)={0,1,2xt}.

Following Algorithm 2, we initialize the terminal cost-to-go function at time t=3 as:

J3(x3)=0,x3{0,1,2},

since no costs are incurred after the final stage. Working backward, we compute the cost-to-go at time t=2 by minimizing the expected one-step cost:

J2(0)=minimizeu2{0,1,2}𝔼w[u2+(u2w2)2],=minimizeu2{0,1,2}u2+0.1u22+0.7(u21)2+0.2(u22)2=1.3,J2(1)=minimizeu2{0,1}𝔼w[u2+(1+u2w2)2],=minimizeu2{0,1}u2+0.1(1+u2)2+0.7(u2)2+0.2(u21)2=0.3,J2(2)=𝔼w[(2w2)2]=0.1(2)2+0.7(1)2+0.2(0)2=1.1.

Note that for x2=2, the only admissible action is u2=0 due to the inventory capacity constraint, and hence the minimum is achieved at u2=0.

From these computations, we directly obtain the optimal actions at time t=2:

π2(0)=1,π2(1)=0,π2(2)=0.

Continuing this process, we compute the cost-to-go at time t=1:

J1(0)=minimizeu1{0,1,2}𝔼w[u1+(u1w1)2+J2(max{0,u1w1})]=2.5,J1(1)=minimizeu1{0,1}𝔼w[u1+(1+u1w1)2+J2(max{0,1+u1w1})]=1.5,J1(2)=𝔼w[(2w1)2+J2(max{0,2w1})]=1.68,

with optimal stage actions:

π1(0)=1,π1(1)=0,π1(2)=0.

Finally, in the last step:

J0(0)=minimizeu0{0,1,2}𝔼w[u0+(u0w0)2+J1(max{0,u0w0})]=3.7,J0(1)=minimizeu0{0,1}𝔼w[u0+(1+u0w0)2+J1(max{0,1+u0w0})]=2.7,J0(2)=𝔼w[(2w0)2+J1(max{0,2w0})]=2.818,

with:

π0(0)=1,π0(1)=0,π0(2)=0.

For this example, the optimal policy is time-invariant: order one unit when the inventory is empty, and order nothing otherwise. This policy balances the risk of unmet demand against the cost of carrying inventory, and it emerges naturally from the dynamic programming recursion.

17.3 Limitations of Dynamic Programming

Dynamic programming is a powerful algorithmic framework that underlies a wide range of methods for solving sequential decision-making problems. However, despite its generality and conceptual elegance, it suffers from several important practical limitations.

First, in its standard form, dynamic programming requires perfect knowledge of the environment. This includes an accurate model of the system dynamics—whether deterministic or stochastic—as well as a known cost or reward function. In many real-world applications, particularly in robotics, this requirement can be highly restrictive, as physical interactions involving friction, contact dynamics, or complex nonlinear effects are often difficult to model accurately. Moreover, the algorithms presented in this chapter assume that the full system state is known and directly observable, which is often not the case in practice.

A second major limitation of dynamic programming is the so-called curse of dimensionality. The computational and storage requirements of dynamic programming grow exponentially with the dimension of the state space. Concretely, if the state is n-dimensional and each state variable can take on M discrete values, then the Bellman equation must be evaluated Mn times at each stage of the algorithm. While this may be tractable for low-dimensional problems, it quickly becomes infeasible as the dimensionality increases. This issue is especially relevant in robotics, where the state and action spaces can be very high-dimensional due to the presence of multiple degrees of freedom, sensors, and actuators.

Together, these limitations motivate the development of alternative approaches that relax some of the assumptions underlying classical dynamic programming. In particular, they have led to the study of learning-based methods that trade exact optimality for computational tractability.

In the next chapter, we first introduce reinforcement learning, which can be viewed as a natural extension of the ideas developed here. At a high level, reinforcement learning may be viewed as a form of approximate dynamic programming, as it retains the central concepts of value functions, Bellman recursions, and policy improvement, while addressing the key limitations of classical dynamic programming by allowing policies to be learned from interaction rather than computed from a perfectly known model over an explicitly enumerated state space. For this reason, reinforcement learning provides the most direct next step after the present chapter.

We then turn to imitation learning, which offers a broader and highly practical framework for robot learning from demonstrations. Rather than learning solely through trial-and-error interaction with the environment, imitation learning leverages expert behavior to acquire control policies, and has become one of the most widely used paradigms for training robotic systems in practice. Taken together, reinforcement learning and imitation learning provide two complementary responses to the limitations of classical dynamic programming, and they will be the focus of the referenced item and the referenced item, respectively.

17.4 Summary

In this chapter, we introduced a general, optimization-based framework for sequential decision-making that allows us to compute optimal policies for complex tasks, even under uncertainty. We began in Section 17.1 by framing deterministic sequential decision-making as a discrete-time optimal control problem, introducing dynamic programming as the primary computational tool for solving such problems. Grounded in the principle of optimality, dynamic programming allows long-horizon problems to be decomposed into a sequence of simpler, recursively defined subproblems. In Section 17.2, we extended this framework to the stochastic setting, where uncertainty in system dynamics and outcomes must be explicitly accounted for. This led to the formulation of Markov decision processes and the use of dynamic programming to compute policies that optimize expected performance under known sources of randomness. Finally, in Section 17.3, we discussed the practical limitations of dynamic programming, most notably the curse of dimensionality and the reliance on accurate models of the environment. These challenges motivate the development of learning-based approaches, which are the focus of the next chapter.

To learn more.

For a foundational and comprehensive treatment of dynamic programming and sequential decision-making, Dynamic Programming and Optimal Control by Bertsekas (2000)66. Bertsekas, D. Dynamic Programming and Optimal Control. Athena Scientific, 2000. is an indispensable resource, providing rigorous derivations for both deterministic and stochastic problems. For an introduction to Markov decision processes with a focus on their role in modern artificial intelligence and robotics, Reinforcement Learning: An Introduction by Sutton and Barto (2018)77. Sutton, R., Barto, A. Reinforcement learning: An introduction. MIT Press, 2018. is the standard reference. A more in-depth mathematical treatment of MDPs can be found in Markov Decision Processes: Discrete Stochastic Dynamic Programming by Puterman (2014)88. Puterman, M. Markov Decision Processes: Discrete Stochastic Dynamic Programming. Wiley, 2014..

17.5 Exercises

The starter code for the exercises provided below is available online through GitHub. To get started, download the code by running in a terminal window:

git clone https://github.com/StanfordASL/pora-exercises.git

We denote Problems requiring hand-written solutions and coding in Python with [Uncaptioned image] and [Uncaptioned image], respectively.

[Uncaptioned image] Problem 1: Shortest Path Through A Grid

Consider the graph shown in Figure 17.4, where it is only possible to move to the right and the numbers associated with each edge represent a cost to traverse that edge. The decision to be made at each node is whether to go “up” or “down”, and we can assume the state transitions are deterministic.

diagram

Figure 17.4: Simple grid for shortest-path problem.

For this exercise:

  1. 1.

    Use dynamic programming to find the shortest path from A to B.

  2. 2.

    Consider a generalized version of the shortest path problem in Figure 17.4 where the grid has n segments along each side. Find the number of computations required by an exhaustive search algorithm (i.e., the number of routes that such an algorithm would need to evaluate) and the number of computations required by a DP algorithm (i.e., the number of DP evaluations). For example, for the n=3 case shown in Figure 17.4, an exhaustive search algorithm requires 20 computations, while the DP algorithm requires only 15.

[Uncaptioned image] Problem 2: Machine Maintenance

Suppose we have a machine that is either running or is broken down. If it runs throughout one week, it makes a gross profit of $100. If it fails during the week, gross profit is zero. If it is running at the start of the week and we perform preventive maintenance, the probability that it will fail during the week is 0.4. If we do not perform maintenance, the probability of failure is 0.7. However, preventative maintenance will cost $20. When the machine is broken down at the start of the week, it may either be repaired at a cost of $40, in which case it will fail during the week with a probability of 0.4, or it may be replaced at a cost of $150 by a new machine that is guaranteed to run through its first week of operation. Find the optimal repair, replacement, and maintenance policy that maximizes total profit over four weeks, assuming a new machine at the start of the first week (that is guaranteed to run during the first week of operation).

[Uncaptioned image] Problem 3: Markovian Drone

In this problem, we will model the task of flying a drone to its destination through a storm as a Markov Decision Process (MDP), and solve for the optimal policy using dynamic programming. The world is represented as an n×n grid, so the state space is:

𝒳{(x1,x2)2x1,x2{0,1,,n1}}.

In these coordinates, (0,0) represents the bottom left corner of the map and (n1,n1) represents the top right corner of the map. From any location x=(x1,x2)𝒳, the drone has five possible controls it can apply:

𝒰{up,down,left,right,land}.

The corresponding state changes for each control input are:

  • up: (x1,x2)(x1,x2+1)

  • down: (x1,x2)(x1,x21)

  • left: (x1,x2)(x11,x2)

  • right: (x1,x2)(x1+1,x2)

  • land: (x1,x2)(x1,x2)

Additionally, there is a storm centered at xeye𝒳. The storm’s influence is strongest at its center and decays farther from the center according to the equation ω(x)=exp(xxeye222σ2). Given its current state x and control input u, the drone’s next state is determined as follows:

  • The control to land is deterministic.

  • With probability ω(x), the storm will cause the drone to move in a uniformly random direction (for non-landing controls).

  • With probability 1ω(x), the drone will move in the direction specified by the control (for non-landing controls).

  • If the resulting movement would cause the drone to leave 𝒳, then it will not move at all. For example, if the drone is on the right boundary of the map, then moving right will do nothing.

The quadrotor’s objective is to reach xgoal𝒳 as quickly as possible, so the cost function is the indicator function gt(xt)=1Ixgoal(xt). In other words, the drone will receive a cost of 1 whenever it is not at xgoal𝒳, and a cost of 0 at the goal. The drone has limited fuel capacity, so it must reach the goal in at most T timesteps. If the drone is not at the goal at the end of the horizon, it will crash and we will incur a cost of 100 to replace the drone, therefore gT(xT)=100(1Ixgoal(xT)).

In the notebook ch17/exercises/markovian_drone.ipynb, complete the following exercises:

  1. 1.

    Implement the functions of DroneMDP to compute the optimal cost-to-go values via dynamic programming (Algorithm 2) and extract the optimal policy using Equation 17.14.

  2. 2.

    Given T=100, n=20, σ=10, xeye=(15,15), and xgoal=(19,9), compute and plot a heatmap of the optimal cost-to-go for t=0 over the grid 𝒳. Then, using the code provided, simulate the MDP with the optimal policy for T=100 time steps with the state initialized at x=(0,19).

Practice · 1 notebooks