Skip to content

5 Dynamic Programming

5.1 Derivation of Bellman's PDE

5.1.1 Dynamic Programming

We begin with some mathematical wisdom:

“It is sometimes easier to solve a problem by embedding it within a larger class of problems and then solving the larger class all at once.”

A Calculus Example

Suppose we wish to calculate the value of the integral

0sinxxdx

This is pretty hard to do directly, so let us as follows add a parameter α into the integral:

I(α):=0eαxsinxxdx

We compute

I(α)=0(x)eαxsinxxdx=0sinxeαxdx=1α2+1

where we integrated by parts twice to find the last equality. Consequently

I(α)=arctanα+C

and we must compute the constant C. To do so, observe that

0=I()=arctan()+C=π2+C,

and so C=π2. Hence I(α)=arctanα+π2, and consequently

0sinxxdx=I(0)=π2

Come back to control

We want to adapt some version of this idea to the vastly more complicated setting of control theory. For this, fix a terminal time T>0 and then look at the controlled dynamics

(ODE){x˙(s)=f(x(s),α(s))x(0)=x0,

with the associated payoff functional

(P)P[α()]=0Tr(x(s),α(s))ds+g(x(T)).

We embed this into a larger family of similar problems, by varying the starting times and starting points:

(5.1){x˙(s)=f(x(s),α(s))(tsT)x(t)=x.

with

(5.2)Px,t[α()]=tTr(x(s),α(s))ds+g(x(T)).

Consider the above problems for all choices of starting times 0tT and all initial points xRn.

Definition: value function

For xRn,0tT, define the value function v(x,t) to be the greatest payoff possible if we start at xRn at time t. In other words,

(5.3)v(x,t):=supα()APx,t[α()](xRn,0tT).

Notice then that

(5.4)v(x,T)=g(x)(xRn).

5.1.2 Derivation of Hamilton-Jacobi-Bellman Equation

Our first task is to show that the value function v satisfies a certain nonlinear partial differential equation.

Our derivation will be based upon the reasonable principle that

"it's better to be smart from the beginning, than to be stupid for a time and then become smart".

We want to convert this philosophy of life into mathematics.

To simplify, we hereafter suppose that the set A of control parameter values is compact.

Theorem 5.1 (Hamilton-Jacobi-Bellman Equation)

Assume that the value function v is a C1 function of the variables ( x,t ). Then v solves the nonlinear partial differential equation

(HJB)vt(x,t)+maxaA{f(x,a)xv(x,t)+r(x,a)}=0(xRn,0t<T),

with the terminal condition

v(x,T)=g(x)(xRn)

Remark

We call (HJB) the Hamilton-Jacobi-Bellman equation, and can rewrite it as

(HJB)vt(x,t)+H(x,xv)=0(xRn,0t<T),

for the partial differential equations Hamiltonian

H(x,p):=maxaAH(x,p,a)=maxaA{f(x,a)p+r(x,a)}

where x,pRn.

Proof

  1. Let xRn,0t<T and let h>0 be given. As always
A={α():[0,)A measurable }.

Pick any parameter aA and use the constant control

α()a

for times tst+h. The dynamics then arrive at the point x(t+h), where t+h<T. Suppose now a time t+h, we switch to an optimal control and use it for the remaining times t+hsT.

What is the payoff of this procedure? Now for times tst+h, we have

{x˙(s)=f(x(s),a)x(t)=x

The payoff for this time period is tt+hr(x(s),a)ds. Furthermore, the payoff incurred from time t+h to T is v(x(t+h),t+h), according to the definition of the payoff function v. Hence the total payoff is

tt+hr(x(s),a)ds+v(x(t+h),t+h)

But the greatest possible payoff if we start from ( x,t ) is v(x,t). Therefore

(5.5)v(x,t)tt+hr(x(s),a)ds+v(x(t+h),t+h).
  1. We now want to convert this inequality into a differential form. So we rearrange (5.5) and divide by h>0 :
v(x(t+h),t+h)v(x,t)h+1htt+hr(x(s),a)ds0

Let h0 :

vt(x,t)+xv(x(t),t)x˙(t)+r(x(t),a)0

But x() solves the ODE

{x˙(s)=f(x(s),a)(tst+h)x(t)=x.

Employ this above, to discover:

vt(x,t)+f(x,a)xv(x,t)+r(x,a)0.

This inequality holds for all control parameters aA, and consequently

(5.6)maxaA{vt(x,t)+f(x,a)xv(x,t)+r(x,a)}0.
  1. We next demonstrate that in fact the maximum above equals zero. To see this, suppose α(),x() were optimal for the problem above. Let us utilize the optimal control α() for tst+h. The payoff is
tt+hr(x(s),α(s))ds

and the remaining payoff is v(x(t+h),t+h). Consequently, the total payoff is

tt+hr(x(s),α(s))ds+v(x(t+h),t+h)=v(x,t).

Rearrange and divide by h :

v(x(t+h),t+h)v(x,t)h+1htt+hr(x(s),α(s))ds=0.

Let h0 and suppose α(t)=aA. Then

vt(x,t)+xv(x,t)x˙(t)f(x,a)+r(x,a)=0;

and therefore

vt(x,t)+f(x,a)xv(x,t)+r(x,a)=0

for some parameter value aA. This proves (HJB).

5.1.2 The Dynamic Programming Method

Here is how to use the dynamic programming method to design optimal controls:

Step 1: Solve the Hamilton-Jacobi-Bellman equation, and thereby compute the value function v.

Step 2: Use the value function v and the Hamilton-Jacobi-Bellman PDE to design an optimal feedback control α(), as follows. Define for each point xRn and each time 0tT,

α(x,t)=aA

to be a parameter value where the maximum in (HJB) is attained. In other words, we select α(x,t) so that

vt(x,t)+f(x,α(x,t))xv(x,t)+r(x,α(x,t))=0.

Next we solve the following ODE, assuming α(,t) is sufficiently regular to let us do so:

(ODE){x˙(s)=f(x(s),α(x(s),s))(tsT)x(t)=x.

Finally, define the feedback control

(5.7)α(s):=α(x(s),s).

In summary, we design the optimal control this way: If the state of system is x at time t, use the control which at time t takes on the parameter value aA such that the minimum(fault, maximum?) in (HJB) is obtained.

We demonstrate next that this construction does indeed provide us with an optimal control.

Theorem 5.2 (Verification of Optimality)

The control α() defined by the construction (5.7) is optimal.

Proof of Theorem 5.2

We have

Px,t[α()]=tTr(x(s),α(s))ds+g(x(T)).

Furthermore according to the definition (5.7) of α() :

Px,t[α()]=tT(vt(x(s),s)f(x(s),α(s))xv(x(s),s))ds+g(x(T))=tTvt(x(s),s)+xv(x(s),s)x˙(s)ds+g(x(T))=tTddsv(x(s),s)ds+g(x(T))(ChainRule)=v(x(T),T)+v(x(t),t)+g(x(T))=g(x(T))+v(x(t),t)+g(x(T))=v(x,t)=supα()APx,t[α()].

That is,

Px,t[α()]=supα()APx,t[α()];

and so α() is optimal, as asserted.

5.2 Examples

5.2.1 Example 1: Dynamics with Three Velocities

Let us begin with a fairly easy problem:

(ODE){x˙(s)=α(s)(0ts1)x(t)=x

where our set of control parameters is

A={1,0,1}.

We want to minimize

t1|x(s)|ds

and so take for our payoff functional

(P)Px,t[α()]=t1|x(s)|ds.

As our first illustration of dynamic programming, we will compute the value function v(x,t) and confirm that it does indeed solve the appropriate Hamilton-Jacobi-Bellman equation. To do this, we first introduce the three regions:

3 regions
  • Region I={(x,t)x<t1,0t1}.
  • Region II={(x,t)t1<x<1t,0t1}.
  • Region III={(x,t)x>1t,0t1}.

We will consider the three cases as to which region the initial data ( x,t ) lie within.

  • Region III
optimal path in region3

In this case we should take α1, to steer as close to the origin 0 as quickly as possible. Then

v(x,t)=area under path taken=(1t)12(x+x+t1)=(1t)2(2x+t1).
  • Region I In this region, we should take α1, in which case we can similarly compute v(x,t)=(1t2)(2x+t1).

  • Region II In this region we take α±1, until we hit the origin, after which we take α0. We therefore calculate that v(x,t)=x22 in this region.

Checking the Hamilton-Jacobi-Bellman PDE

Now the Hamilton-JacobiBellman equation for our problem reads

(5.8)vt+maxaA{fxv+r}=0

for f=a,r=|x|. We rewrite this as

vt+maxa=±1,0{avx}|x|=0;

and so

(HJB)vt+|vx||x|=0.

We must check that the value function v, defined explicitly above in Regions I-III, does in fact solve this PDE, with the terminal condition that v(x,1)=g(x)=0.

Now in Region II v=x22,vt=0,vx=x. Hence

vt+|vx||x|=0+|x||x|=0 in Region II, 

in accordance with (HJB). In Region III we have

v(x,t)=(1t)2(2x+t1);

and therefore

vt=12(2x+t1)(1t)2=x1+t,vx=t1,|t1|=1t0.

Hence

vt+|vx||x|=x1+t+|t1||x|=0 in Region III, 

because x>0 there.

Likewise the Hamilton-Jacobi-Bellman PDE holds in Region I.

Remarks

  • In the example, v is not continuously differentiable on the borderlines between Regions II and I or III.
  • In general, it may not be possible actually to find the optimal feedback control. For example, reconsider the above problem, but now with A={1,1}. We still have α=sgn(vx), but there is no optimal control in Region II.

5.2.2 Example 2: Rocket Railroad Car

Recall that the equations of motion in this model are

(x˙1x˙2)=(0100)(x1x2)+(01)α,|α|1

and

P[α()]= time to reach (0,0)=0τ1dt=τ.

To use the method of dynamic programming, we define v(x1,x2) to be minus the least time it takes to get to the origin (0,0), given we start at the point (x1,x2).

What is the Hamilton-Jacobi-Bellman equation? Note v does not depend on t, and so we have

maxaA{fxv+r}=0

for

A=[1,1],f=(x2a),r=1

Hence our PDE reads

max|a|1{x2vx1+avx21}=0;

and consequently

(HJB){x2vx1+|vx2|1=0 in R2v(0,0)=0.

Checking the Hamilton-Jacobi-Bellman PDE

We now confirm that v really satisfies (HJB). For this, define the regions

I:={(x1,x2)|x112x2|x2|},II:={(x1,x2)|x112x2|x2|}.

A direct computation, the details of which we omit, reveals that

v(x)={x22(x1+12x22)12 in Region I x22(x1+12x22)12 in Region II. 

In Region I we compute

vx2=1(x1+x222)12x2,vx1=(x1+x222)12;

and therefore

x2vx1+|vx2|1=x2(x1+x222)12+[1+x2(x1+x222)12]1=0.

This confirms that our (HJB) equation holds in Region I, and a similar calculation holds in Region II.

Optimal control

Since

max|a|1{x2vx1+avx2+1}=0,

the optimal control is

α=sgnvx2.

5.2.3 Example 3: General Linear-Quadratic Regulator

For this important problem, we are given matrices M,B,DMn×n,NMn×m,CMm×m; and assume

B,C,D are symmetric and nonnegative definite, 

and

C is invertible. 

We take the linear dynamics

(ODE){x˙(s)=Mx(s)+Nα(s)(tsT)x(t)=x,

for which we want to minimize the quadratic cost functional

tTx(s)Bx(s)+α(s)Cα(s)ds+x(T)Dx(T).

So we must maximize the payoff

(P)Px,t[α()]=tTx(s)Bx(s)+α(s)Cα(s)dsx(T)dx(T).

The control values are unconstrained, meaning that the control parameter values can range over all of A=Rm.

We will solve by dynamic programming the problem of designing an optimal control. To carry out this plan, we first compute the Hamilton-Jacobi-Bellman equation

vt+maxaRm{fxv+r}=0,

where

{f=Mx+Nar=xBxaCag=xDx

Rewrite:

(HJB)vt+maxaRm{(v)NaaCa}+(v)MxxBx=0.

We also have the terminal condition

v(x,T)=xDx

Maximization

For what value of the control parameter a is the minimum in the original problem attained? To understand this, we define Q(a):=(v)NaaCa, and determine where Q has a maximum by computing the partial derivatives Qaj for j=1,,m and setting them equal to 0 . This gives the identitites

Qaj=i=1nvxinij2aicij=0.

Therefore (v)N=2aC, and then 2Ca=Nv. But C=C. Therefore

a=12C1Nxv.

This is the formula for the optimal feedback control: It will be very useful once we compute the value function v.

Finding the value function

We insert our formula a=12C1Nv into (HJB), and this PDE then reads

(HJB){vt+14(v)NC1Nv+(v)MxxBx=0v(x,T)=xDx.

Our next move is to guess the form of the solution, namely

v(x,t)=xK(t)x,

provided the symmetric n×n-matrix valued function K() is properly selected. Will this guess work?

Now, since xK(T)x=v(x,T)=xDx, we must have the terminal condition that

K(T)=D.

Next, compute that

vt=xK˙(t)x,xv=2K(t)x.

We insert our guess v=xK(t)x into (HJB), and discover that

x{K˙(t)+K(t)NC1NK(t)+2K(t)MB}x=0.

Look at the expression

2xKMx=xKMx+[xKMx]=xKMx+xMKx.

Then

x{K˙+KNC1NK+KM+MKB}x=0.

This identity will hold if K() satisfies the matrix Riccati equation

(R){K˙(t)+K(t)NC1NK(t)+K(t)M+MK(t)B=0(0t<T)K(T)=d

In summary, if we can solve the Riccati equation (R), we can construct an optimal feedback control

α(t)=C1NK(t)x(t)

Furthermore, (R) in fact does have a solution, as explained for instance in the book of Fleming-Rishel.

5.2.4 Example 4: More General Linear-Quadratic Regulator with Cross-Term and time-variant

Chapter 11, part 2 in Numerical Optimal Control by Moritz Diehl and Sebastien Gros.

For this important problem, we are given matrices M,B,DMn×n,N,QMn×m,CMm×m; and assume

B,C,D are symmetric and nonnegative definite, 

and

C is invertible. 

We take the linear dynamics

(ODE){x˙(s)=M(s)x(s)+N(s)α(s)(0sT)x(0)=x0,

for which we want to minimize the quadratic cost functional

tT[x(s)α(s)][B(s)Q(s)Q(s)C(s)][x(s)α(s)]ds+x(T)Dx(T).

So we must maximize the payoff

(P)Px,t[α()]=tT[x(s)α(s)][B(s)Q(s)Q(s)C(s)][x(s)α(s)]ds+x(T)Dx(T).

as

v(x,t)=supPx,t[α()]

Also, we start from the assumption that the value function is quadratic. In order to verify this statement, let us first observe that v(x,T)=xDx is quadratic. Thus, let us assume for now that v(x,t) is quadratic for all time, i.e. v(x,t)=xK(t)x for some symmetric matrix K(t). Under this assumption, the HJB equation reads as

vt=maxaRm{fxv+r}=maxaRm{2x(t)K(t)(M(t)x(t)+N(t)a(t))[x(t)a(t)][B(t)Q(t)Q(t)C(t)][x(t)a(t)]}=maxaRm{[xa][BKMMKQKNQNKC][xa]}.

Introduce Schur complement lemma below:

Lemma: Schur Complement

If a matrix R is positive definitem then

minu[xu][QSSR][xu]=x(QSR1S)x

and the minimizer u(x)=R1Sx

By Schur Complement Lemma, the above HJB eqatio yields

vt=x(BKMMK(QKN)C1(QNK))x,

which is again a quadratic term. Thus, as v is quadratic at a time T, it remains quadratic throughout the backwards evolution. The resulting matrix differential equation

K˙=BKMMK(QKN)C1(QNK)

with terminal condition

K(T)=D

is called the differential Riccati equation. Integrating it backwards allows us to compute the cost-to-go function for the above optimal control problem. The corresponding feedback law is by the Schur complement lemma given as:

α(x,t)=C(t)1(Q(t)N(t)K(t))x.

5.3 Dynamic Programming and the Pontryagin Maximum Principle

5.3.1 The Method of Characteristics.

Assume H:Rn×RnR and consider this initial-value problem for the Hamilton-Jacobi equation:

(HJB){ut(x,t)+H(x,xu(x,t))=0(xRn,0<t<T)u(x,0)=g(x)

A basic idea in PDE theory is to introduce some ordinary differential equations, the solution of which lets us compute the solution u. In particular, we want to find a curve x() along which we can, in principle at least, compute u(x,t).

This section discusses this method of characteristics, to make clearer the connections between PDE theory and the Pontryagin Maximum Principle.

Notaion

x(t)=(x1(t)xn(t)),p(t)=xu(x(t),t)=(p1(t)pn(t))

Derivation of characteristic equations

We have

pk(t)=uxk(x(t),t)

and therefore

p˙k(t)=uxkt(x(t),t)+i=1nuxkxi(x(t),t)x˙i

Now suppose u solves (HJ). We differentiate this PDE with respect to the variable xk :

utxk(x,t)=Hxk(x,u(x,t))i=1nHpi(x,u(x,t))uxkxi(x,t)

Let x=x(t) and substitute above:

p˙k(t)=Hxk(x(t),xu(x(t),t)p(t))+i=1n(x˙i(t)Hpi(x(t),xu(x,t)p(t))uxkxi(x(t),t).

We can simplify this expression if we select x() so that

x˙i(t)=Hpi(x(t),p(t)),(1in);

then

p˙k(t)=Hxk(x(t),p(t)),(1kn).

These are Hamilton's equations, already discussed in a different context in §4.1:

(H){x˙(t)=pH(x(t),p(t))p˙(t)=xH(x(t),p(t)).

We next demonstrate that if we can solve (H), then this gives a solution to PDE (HJ), satisfying the initial conditions u=g on t=0. Set p0=g(x0). We solve (H), with x(0)=x0 and p(0)=p0. Next, let us calculate

ddtu(x(t),t)=ut(x(t),t)+xu(x(t),t)x˙(t)=H(xu(x(t),t)p(t),x(t))+xu(x(t),t)p(t)pH(x(t),p(t))=H(x(t),p(t))+p(t)pH(x(t),p(t))

Note also u(x(0),0)=u(x0,0)=g(x0). Integrate, to compute u along the curve x() :

u(x(t),t)=0TH+pHpds+g(x0)

This gives us the solution, once we have calculated x() and p().

5.3.2 Connections between Dynamic Programming AND The Pontryagin Maximum Principle

Return now to our usual control theory problem, with dynamics

(ODE){x˙(s)=f(x(s),α(s))(tsT)x(t)=x

and payoff

(P)Px,t[α()]=tTr(x(s),α(s))ds+g(x(T)).

As above, the value function is

v(x,t)=supα()Px,t[α()].

The next theorem demonstrates that the costate in the Pontryagin Maximum Principle is in fact the gradient in x of the value function v, taken along an optimal trajectory:

Theorem 5.3 (Costates AND Gradients)

Assume α(),x() solve the control problem (ODE), (P).

If the value function v is C2, then the costate p() occuring in the Maximum Principle is given by

p(s)=xv(x(s),s)(tsT).

Proof

  1. As usual, suppress the superscript *. Define p(t):=xv(x(t),t). We claim that p() satisfies conditions (ADJ) and (M) of the Pontryagin Maximum Principle. To confirm this assertion, look at
p˙i(t)=ddtvxi(x(t),t)=vxit(x(t),t)+j=1nvxixj(x(t),t)x˙j(t).

We know v solves

vt(x,t)+maxaA{f(x,a)xv(x,t)+r(x,a)}=0;

and, applying the optimal control α(), we find:

vt(x(t),t)+f(x(t),α(t))xv(x(t),t)+r(x(t),α(t))=0.
  1. Now freeze the time t and define the function
h(x):=vt(x,t)+f(x,α(t))xv(x,t)+r(x,α(t))0.

Observe that h(x(t))=0. Consequently h() has a maximum at the point x=x(t); and therefore for i=1,,n,

0=hxi(x(t))=vtxi(x(t),t)+fxi(x(t),α(t))xv(x(t),t)+f(x(t),α(t))xvxi(x(t),t)+rxi(x(t),α(t)).

Substitute above:

p˙i(t)=vxit+i=1nvxixjfj=vxit+fxvxi=fxixvrxi.

Recalling that p(t)=xv(x(t),t), we deduce that

p˙(t)=(xf)pxr.

Recall also

H=fp+r,xH=(xf)p+xr.

Hence

p˙(t)=xH(p(t),x(t)),

which is (ADJ). 3. Now we must check condition (M). According to (HJB),

vt(x(t),t)+maxaA{f(x(t),a)v(x(t),t)p(t)+r(x(t),t)}=0,

and maximum occurs for a=α(t). Hence

maxaA{H(x(t),p(t),a)}=H(x(t),p(t),α(t));

and this is assertion (M) of the Maximum Principle.

Interpretations

The foregoing provides us with another way to look at transversality conditions:

  • Free endpoint problem: Recall that we stated earlier in Theorem 4.3 that for the free endpoint problem we have the condition
(T)p(T)=g(x(T))

for the payoff functional

tTr(x(s),α(s))ds+g(x(T)).

To understand this better, note p(s)=v(x(s),s). But v(x,t)=g(x), and hence the foregoing implies

p(T)=xv(x(T),T)=g(x(T)).
  • Constrained initial and target sets:

Recall that for this problem we stated in Theorem 4.5 the transversality conditions that

{p(0) is perpendicular to T0p(τ) is perpendicular to T1

when τ denotes the first time the optimal trajectory hits the target set X1. Now let v be the value function for this problem:

v(x)=supα()Px[α()],

with the constraint that we start at x0X0 and end at x1X1 But then v will be constant on the set X0 and also constant on X1. Since v is perpendicular to any level surface, v is therefore perpendicular to both X0 and X1. And since

p(t)=v(x(t))

this means that

{p is perpendicular to X0 at t=0p is perpendicular to X1 at t=τ

5.4 Infinite Time Optimal Control

Let us now regard an infinite time optimal control problem, as follows:

(ODE){x˙(s)=f(x(s),α(s)),s[0,]x(0)=x0,

with the associated payoff functional

(P)P[α()]=tr(x(s),α(s))ds.

The principle of optimality states that the value function of this problem, if it is finite and it exists, must be stationary, i.e. independent of time. Setting vt(x,t)=0 leads to the stationary HJB equation

0=maxaA{f(x,a)xv(x,t)+r(x,a)}

with stationary optimal feedback control law

α(x)=argmaxα{f(x,α)xv(x,t)+r(x,α)}

This equation is easily solvable in the linear quadratic case, i.e., in the case of an infinite horizon linear quadratic optimal control with time independent cost and system matrices. The solution is again quadratic and obtained by setting

K˙=0

and solving

0=BKMMK(QKN)C1(QNK).

This equation is called the algebraic Riccati equation in continuous time. Its feedback law is a static linear gain:

α(x)=C1(QNK)x.

Released under the MIT License.