Lably
Skip to content

Dynamic programming

Problem optimization method

  • 33 min read
  • 3 chapters

Related on Lably

Through Mathematical optimization, Economics and Recursion

In both contexts, it refers to simplifying a complicated problem by breaking it down into simpler sub-problems in a recursive manner. While some decision problems cannot be taken apart this way, decisions that span several points in time do often break apart recursively. Likewise, in computer science, if a problem can be solved optimally by breaking it into sub-problems and then recursively finding the optimal solutions to the sub-problems, then it is said to have optimal substructure.

If sub-problems can be nested recursively inside larger problems, so that dynamic programming methods are applicable, then there is a relation between the value of the larger problem and the values of the sub-problems. In the optimization literature, this relationship is called the Bellman equation.

Overview

Mathematical optimization

In terms of mathematical optimization, dynamic programming usually refers to simplifying a decision by breaking it down into a sequence of decision steps over time.

This is done by defining a sequence of value functions V1, V2, ..., Vn taking y as an argument representing the state of the system at times i from 1 to n.

The definition of Vn(y) is the value obtained in state y at the last time n.

The values Vi at earlier times i = n −1, n − 2, ..., 2, 1 can be found by working backwards, using a recursive relationship called the Bellman equation.

For i = 2, ..., n, Vi−1 at any state y is calculated from Vi by maximizing a simple function (usually the sum) of the gain from a decision at time i − 1 and the function Vi at the new state of the system if this decision is made.

Since Vi has already been calculated for the needed states, the above operation yields Vi−1 for those states.

Finally, V1 at the initial state of the system is the value of the optimal solution. The optimal values of the decision variables can be recovered, one by one, by tracking back the calculations already performed.

Control theory

In control theory, a typical problem is to find an admissible control u∗ which causes the system x˙(t)=g(x(t),u(t),t) to follow an admissible trajectory x∗ on a continuous time interval t0≤t≤t1 that minimizes a cost function

J=b(x(t1),t1)+∫t0t1f(x(t),u(t),t)dt

The solution to this problem is an optimal control law or policy u∗=h(x(t),t), which produces an optimal trajectory x∗ and a cost-to-go function J∗. The latter obeys the fundamental equation of dynamic programming:

−Jt∗=minu{f(x(t),u(t),t)+Jx∗Tg(x(t),u(t),t)}

which is a partial differential equation known as the Hamilton–Jacobi–Bellman equation, in which Jx∗=∂J∗∂x=[∂J∗∂x1∂J∗∂x2…∂J∗∂xn]T and Jt∗=∂J∗∂t.

One finds that minimizing u in terms of t, x, and the unknown function Jx∗ and then substitutes the result into the Hamilton–Jacobi–Bellman equation to get the partial differential equation to be solved with boundary condition J(t1)=b(x(t1),t1). In practice, this generally requires numerical techniques for some discrete approximation to the exact optimization relationship.

Alternatively, the continuous process can be approximated by a discrete system, which leads to a following recurrence relation analog to the Hamilton–Jacobi–Bellman equation:

Jk∗(xn−k)=minun−k{f^(xn−k,un−k)+Jk−1∗(g^(xn−k,un−k))}

at the k-th stage of n equally spaced discrete time intervals, and where f^ and g^ denote discrete approximations to f and g. This functional equation is known as the Bellman equation, which can be solved for an exact solution of the discrete approximation of the optimization equation.

Example from economics: Ramsey's problem of optimal saving

In economics, the objective is generally to maximize (rather than minimize) some dynamic social welfare function. In Ramsey's problem, this function relates amounts of consumption to levels of utility. Loosely speaking, the planner faces the trade-off between contemporaneous consumption and future consumption (via investment in capital stock that is used in production), known as intertemporal choice. Future consumption is discounted at a constant rate β∈(0,1). A discrete approximation to the transition equation of capital is given by

kt+1=g^(kt,ct)=f(kt)−ct

where c is consumption, k is capital, and f is a production function satisfying the Inada conditions. An initial capital stock k0>0 is assumed.

Let ct be consumption in period t, and assume consumption yields utility u(ct)=ln⁡(ct) as long as the consumer lives. Assume the consumer is impatient, so that he discounts future utility by a factor b each period, where 0<b<1. Let kt be capital in period t. Assume initial capital is a given amount k0>0, and suppose that this period's capital and consumption determine next period's capital as kt+1=Akta−ct, where A is a positive constant and 0<a<1. Assume capital cannot be negative. Then the consumer's decision problem can be written as follows:

max∑t=0Tbtln⁡(ct) subject to kt+1=Akta−ct≥0 for all t=0,1,2,…,T

Written this way, the problem looks complicated, because it involves solving for all the choice variables c0,c1,c2,…,cT. (The capital k0 is not a choice variable—the consumer's initial capital is taken as given.)

The dynamic programming approach to solve this problem involves breaking it apart into a sequence of smaller decisions. To do so, we define a sequence of value functions Vt(k), for t=0,1,2,…,T,T+1 which represent the value of having any amount of capital k at each time t. There is (by assumption) no utility from having capital after death, VT+1(k)=0.

The value of any quantity of capital at any previous time can be calculated by backward induction using the Bellman equation. In this problem, for each t=0,1,2,…,T, the Bellman equation is

Vt(kt)=max(ln⁡(ct)+bVt+1(kt+1)) subject to kt+1=Akta−ct≥0

This problem is much simpler than the one we wrote down before, because it involves only two decision variables, ct and kt+1. Intuitively, instead of choosing his whole lifetime plan at birth, the consumer can take things one step at a time. At time t, his current capital kt is given, and he only needs to choose current consumption ct and saving kt+1.

To actually solve this problem, we work backwards. For simplicity, the current level of capital is denoted as k. VT+1(k) is already known, so using the Bellman equation once we can calculate VT(k), and so on until we get to V0(k), which is the value of the initial decision problem for the whole lifetime. In other words, once we know VT−j+1(k), we can calculate VT−j(k), which is the maximum of ln⁡(cT−j)+bVT−j+1(Aka−cT−j), where cT−j is the choice variable and Aka−cT−j≥0.

Working backwards, it can be shown that the value function at time t=T−j is

VT−j(k)=a∑i=0jaibiln⁡k+vT−j

where each vT−j is a constant, and the optimal amount to consume at time t=T−j is

cT−j(k)=1∑i=0jaibiAka

which can be simplified to

cT(k)=AkacT−1(k)=Aka1+abcT−2(k)=Aka1+ab+a2b2…c2(k)=Aka1+ab+a2b2+…+aT−2bT−2c1(k)=Aka1+ab+a2b2+…+aT−2bT−2+aT−1bT−1c0(k)=Aka1+ab+a2b2+…+aT−2bT−2+aT−1bT−1+aTbT

We see that it is optimal to consume a larger fraction of current wealth as one gets older, finally consuming all remaining wealth in period T, the last period of life.

Computer science

There are two key attributes that a problem must have in order for dynamic programming to be applicable: optimal substructure and overlapping sub-problems. If a problem can be solved by combining optimal solutions to non-overlapping sub-problems, the strategy is called "divide and conquer" instead. This is why merge sort and quick sort are not classified as dynamic programming problems.

Optimal substructure means that the solution to a given optimization problem can be obtained by the combination of optimal solutions to its sub-problems. Such optimal substructures are usually described by means of recursion. For example, given a graph G=(V,E), the shortest path p from a vertex u to a vertex v exhibits optimal substructure: take any intermediate vertex w on this shortest path p. If p is truly the shortest path, then it can be split into sub-paths p1 from u to w and p2 from w to v such that these, in turn, are indeed the shortest paths between the corresponding vertices (by the simple cut-and-paste argument described in Introduction to Algorithms). Hence, one can easily formulate the solution for finding shortest paths in a recursive manner, which is what the Bellman–Ford algorithm or the Floyd–Warshall algorithm does.

Overlapping sub-problems means that the space of sub-problems must be small, that is, any recursive algorithm solving the problem should solve the same sub-problems over and over, rather than generating new sub-problems. For example, consider the recursive formulation for generating the Fibonacci sequence: Fi = Fi−1 + Fi−2, with base case F1 = F2 = 1. Then F43 = F42 + F41, and F42 = F41 + F40. Now F41 is being solved in the recursive sub-trees of both F43 as well as F42. Even though the total number of sub-problems is actually small (only 43 of them), we end up solving the same problems over and over if we adopt a naive recursive solution such as this. Dynamic programming takes account of this fact and solves each sub-problem only once.

This can be achieved in either of two ways:

  • Top-down approach: This is the direct fall-out of the recursive formulation of any problem. If the solution to any problem can be formulated recursively using the solution to its sub-problems, and if its sub-problems are overlapping, then one can easily memoize or store the solutions to the sub-problems in a table (often an array or hashtable in practice). Whenever we attempt to solve a new sub-problem, we first check the table to see if it is already solved. If a solution has been recorded, we can use it directly, otherwise we solve the sub-problem and add its solution to the table.
  • Bottom-up approach: Once we formulate the solution to a problem recursively as in terms of its sub-problems, we can try reformulating the problem in a bottom-up fashion: try solving the sub-problems first and use their solutions to build-on and arrive at solutions to bigger sub-problems. This is also usually done in a tabular form by iteratively generating solutions to bigger and bigger sub-problems by using the solutions to small sub-problems. For example, if we already know the values of F41 and F40, we can directly calculate the value of F42.

Some programming languages can automatically memoize the result of a function call with a particular set of arguments, in order to speed up call-by-name evaluation (this mechanism is referred to as call-by-need). Some languages make it possible portably (e.g. Scheme, Common Lisp, Perl or D). Some languages have automatic memoization built in, such as tabled Prolog and J, which supports memoization with the M. adverb. In any case, this is only possible for a referentially transparent function. Memoization is also encountered as an easily accessible design pattern within term-rewrite based languages such as Wolfram Language.

Bioinformatics

Dynamic programming is widely used in bioinformatics for tasks such as sequence alignment, protein folding, RNA structure prediction and protein-DNA binding. The first dynamic programming algorithms for protein-DNA binding were developed in the 1970s independently by Charles DeLisi in the US and by Georgii Gurskii and Alexander Zasedatelev in the Soviet Union. Recently these algorithms have become very popular in bioinformatics and computational biology, particularly in the studies of nucleosome positioning and transcription factor binding.

Sign in to keep reading

2 more chapters are free with a Lably account.

More in Applied mathematics