Generic Dynamic Programming Function in Matlab
Generic Dynamic Programming Function in Matlab
Abstract— This paper introduces a generic dynamic pro- The class of optimal control problems that can be solved
gramming function for Matlab. This function solves discrete- using the proposed Matlab function can be written as:
time optimal-control problems using Bellman’s dynamic pro-
gramming algorithm. The function is implemented such that the min J(u(t)) (1)
user only needs to provide the objective function and the model u(t)
equations. The function includes several options for solving s.t.
optimal-control problems. The model equations can include
several state variables and input variables. Furthermore, the ẋ(t) = F (x(t), u(t), t) (2)
model equations can be time-variant and include time-variant x(0) = x0 (3)
x(tf ) ∈ [xf,min , xf,max ]
state and input constraints. The syntax of the function is
explained using two examples. The first is the well-known (4)
Lotka-Volterra fishery problem and the second is a parallel x(t) ∈ X (t) ⊂ R n
(5)
u(t) ∈ U(t) ⊂ Rm
hybrid-electric vehicle optimization problem.
(6)
I. INTRODUCTION
where
When developing causal suboptimal controllers it is an
J(u(t)) = G(x(tf )) + ∫
tf
advantage if the optimal controller is known, even if this con- H(x(t), u(t), t)dt (7)
troller is not causal. In many cases, such optimal controllers 0
can be found using the deterministic dynamic programming is the cost functional. The important characteristics of the
(DP) algorithm introduced in [1]. Of course, this optimal considered optimal-control problems are the time-variant
controller can be found only if all future disturbance and constraints on the input and the state, the constrained final
reference inputs are known. In this sense, this solution is state, and the time-variant model equation. These problems
not causal. Nevertheless, this optimal solution is very useful, are in general difficult to solve. However, these problems
because it can be used as a benchmark to which all other can be solved using Bellman’s DP [1], provided that the
causal controllers can be compared to. conditions stated in the next section are satisfied.
Many excellent text books have been published on the
II. DYNAMIC PROGRAMMING ALGORITHM
subject of DP theory, among them [2] and [3]. An overview
of the history and development of dynamic programming This section gives a brief overview of the deterministic DP
is shown in [4]. Interested readers are referred to these algorithm as it is implemented in the dpm function. Since
references for a detailed discussion of the basic ideas of DP is used here to solve a continuous-time control problem,
DP. When implementing the deterministic DP algorithm on the continuous-time model (2) must be discretized in time
a computer there are many numerical issues that arise that first. Let the discrete-time model be given by
have, so far, not yet received sufficient attention. Also, since xk+1 = Fk (xk , uk ), k = 0, 1, . . . , N − 1 (8)
the computational complexity of every DP algorithm is ex-
ponential in the number of states and inputs, special attention with the state variable xk ∈ Xk and the control signal uk ∈ Uk .
must be given to minimizing the overall computational cost. A. Basic Algorithm
Let π = {µ0 , µ1 , . . . µN −1 } be a control policy. Further let
The implementation of suitable numerical algorithms that
efficiently solve a given DP problem is, therefore, a nontrivial
part of a design process. the discretized cost of (7) using π with the initial state
This paper presents a Matlab function that efficiently x(0) = x0 be
solves deterministic DP problems. The focus lies on the opti- Jπ (x0 ) =gN (xN ) + φN (xN ) . . .
mal control of non-linear, time-variant, constrained, discrete- N −1
time approximations of continuous-time dynamic models. + ∑ hk (xk , µk (xk )) + φk (xk ), (9)
One area where such DP tools have been used successfully is k=0
the energy management problem in hybrid-electric vehicles where gN (xN ) + φN (xN ) is the final cost. The first term
[5], [6]. gN (xN ) represents the final cost in (7). The second term
O. Sundström, Department of Mechanical and Process Engineering ETH is an additional penalty function φN (xN ) that can be used
Zurich, 8092 Zurich, Switzerland and Empa, Swiss Federal Laboratories to enforce a constraint on the final state (4). The function
for Materials Testing and Research, 8600 Dübendorf, Switzerland, sund- hk (xk , µk (xk )) is the cost of applying the control µk (xk )
stroem@[Link]
at xk , according to H(x(t), u(t), t) in (7). The state con-
straints (5) are enforced by the penalty function φk (xk )
L. Guzzella, Department of Mechanical and Process Engineering ETH
Zurich, 8092 Zurich, Switzerland, guzzella@[Link]
where Π is the set of all admissible policies. When solving discrete-time optimal control
Based on the principle of optimality [1], the DP algorithm problems the dpm function is normally called using
evaluates the optimal cost-to-go1 function Jk (xi ) at every
[res dyn] = dpm(fun,par,grd,prb,options);
node in the discretized state-time space2 by proceeding
backward in time: where fun is the model function handle, par is any user
1) End cost calculation step defined parameter structure that is forwarded to the model,
grd is the grid structure, prb is the problem structure, and
JN (xi ) = gN (xi ) + φN (xi ) (11) options is the option structure. The output of the dpm
2) Intermediate calculation step for k = N − 1 to 0 function are normally two structures representing the DP-
output and the signals from forward simulation of the model
Jk (xi ) = min { hk (xi , uk ) + φk (xi ) . . . using the optimal control input map.
uk ∈Uk
Since the DP algorithm is often time consuming,
+Jk+1 (Fk (xi , uk ))} (12) the dpm function can also be used only for for-
The optimal control is given by the argument that minimizes ward simulation when the DP output structure dyn is
the right-hand side of equation (12) for each xi at time index precalculated. This can be very useful when chang-
k of the discretized state-time space. ing the initial condition or when increasing the start-
The cost-to-go function Jk+1 (x) used in (12) is evaluated ing time N0 of the problem. To call the dpm function
only on discretized points in the state space. Furthermore, when the DP output structure is already calculated use
the output of the model function Fk (xi , uk ) is a continuous
res = dpm(dyn,fun,par,grd,prb,options);
variable in the state space which can be between the nodes
of the state grid. Consequently, the last term in (12), namely All the structures in the code above are further explained in
Jk+1 (Fk (xi , uk )) must be evaluated appropriately. There the remainder of this section.
exist several methods of finding the appropriate cost-to-go
Jk+1 (Fk (xi , uk )) such as using a nearest-neighbor approxi- A. Problem
mation or using more advanced interpolation schemes. In the In the problem structure all necessary parameters that
dpm function introduced in this paper, linear interpolation define the problem are given. The important parameters
of the cost-to-go Jk+1 is used. Since the state and the are the time step Ts of the model description and the
input grids are equally spaced, the computational cost of problem length N. Moreover, in the problem structure an
this interpolation is low compared to the cost induced by the optional cell array can be defined, which contains time-
model evaluations. variant information relevant for the problem description. For
The output of the algorithm (11)–(12) is an optimal control example, if the model explicitly depends on the time the cell
signal map. This map is used to find the optimal control array W{1} would contain a time vector with N elements.
signal during a forward simulation of the model (8), starting The corresponding elements in these time-variant vectors are
from a given initial state x0 , to generate the optimal state forwarded to the model function throughout the problem.
trajectory. In the optimal control signal map the control The problem structure can also contain a starting time index
signal is only given for the discrete points in the state space where the forward simulation starts. This can be helpful
grid. The control signal, therefore, must be interpolated when when searching for a time optimal solution. An overview
the actual state does not coincide with the points in the of the problem structure is shown in Table I.
state grid. In general, the complexity of the DP algorithm
TABLE I
is exponential in the number of state and input variables.
PROBLEM - STRUCTURE ( PRB )
III. DPM-FUNCTION
Ts time step (is passed to the model function)
The dpm function solves the discretized version of the N number of time steps in problem (integer that defines
optimal control problem (1)–(7) using the dynamic pro- the problem length)
gramming algorithm introduced in Section II-A. This section N0 (optional) start time index (only used in forward
shows the syntax and commands for solving such problems. simulation)
W{.} (optional) vectors with length N containing time-
1
The terms cost-to-go and optimal cost-to-go are used equivalently variant data for the model
throughout this paper referring to optimal cost-to-go. It is important to note
that the term optimal is used in the sense of optimality achievable under
the numeric errors. B. State/Input Grids and Constraints
2
The following notation is used: xik denotes the state variable x in the
discretized state-time space at the node with time-index k and state-index The grid structure grd contains all the information about
i. xk denotes a (state-)continuous state-variable at time k. the state and input grids and constraints. An overview of the
1626
Authorized licensed use limited to: INDIAN INSTITUTE OF TECHNOLOGY DELHI. Downloaded on June 19,2025 at 10:47:09 UTC from IEEE Xplore. Restrictions apply.
TABLE III
grd structure is shown in Table II. The grd structure is com-
OPTIONS - STRUCTURE ( OPTIONS )
posed by cell arrays, where there is a cell for each state vari-
able and each input variable. For example, for a problem with HideWaitbar hide waitbars (0/1)
two state variables the grd structure contains grd.X{1}, Warnings show warnings (0/1)
SaveMap save cost-to-go map (0/1)
grd.X{2}, [Link]{1}.lo, [Link]{2}.lo, and so UseLine use boundary line method (0/1)
on. The input grid is used in a similar way depending on FixedGrid (used if UseLine=1) using the original grid as
the number of input variables of the problem. specified in grd or adjust the grid to the boundary
lines (0/1)
Iter (used if UseLine=1) maximum number of itera-
TABLE II
tions when inverting model
GRID - STRUCTURE ( GRD ) Tol (used if UseLine=1) minimum tolerance when in-
verting model
Nx{.} number of grid points in state grid InfCost a large cost for infeasible states (I=1)
Xn{.}.lo lower limits for each state (vector for time-variant or
scalar for fixed) Minimize (optional) minimizing (or maximizing) cost function
Xn{.}.hi upper limits for each state (vector for time-variant or (0/1) default is minimizing
scalar for fixed) InputType (optional) string with the same number of characters
XN{.}.lo final state lower constraints as number of inputs. Contains the character ’c’ if
XN{.}.hi final state upper constraints input is continuous or ’d’ if discrete (default is all
X0{.} inital value (only used in forward sim) continuous).
gN{1} (optional) Cost matrix at the final time (must
Nu{.} number of grid points in input grid be of size([Link]{1}) = [[Link]{1}
Un{.}.lo (optional) upper limits for each input (vector for time- [Link]{2} ... [Link]{.}])
variant or scalar for fixed)
Un{.}.hi (optional) upper limits for each input (vector for time-
variant or scalar for fixed)
the forward simulation of the model when applying the opti-
mal control input map. The dyn structure is associated with
the dynamic programming algorithm, the optimal cost-to-go,
C. Options
and the optimal control input map. When the boundary line
The DP approach can be used for many different prob- method is used the dyn structure also contains the boundary
lem settings and the options structure defines how to use lines (with the states, inputs, and costs). An overview of the
the algorithm. An overview of the options that can be two structures are shown in Tables IV and V.
specified in the options structure is shown in Table III.
TABLE IV
The HideWaitbar options decides if waitbars are shown
DP OUTPUT- STRUCTURE ( DYN )
or not when running the DP algorithm. The SaveMap
option determines if the optimal cost-to-go is saved and [Link] Xo,Uo{.},Jo contains the cost, input, and state for
returned. Note that the memory requirements increase when the upper boundary line
[Link] Xo,Uo{.},Jo contains the cost, input, and state for
SaveMap=1. the lower boundary line
An important option is the UseLine option, which de- Jo{.,.} optimal cost-to-go (indexed by input number and
cides if the boundary line method, introduced in [8], is used time index)
Uo{.,.} optimal control input (indexed by input number and
or not. The boundary line method is very useful for increas- time index)
ing the accuracy of problems with final state constraints. For
more information about the boundary line method readers are
referred to [8]. Note in the actual version of the dpm function TABLE V
it can only be used when there is only one state variable. If RESULTS - STRUCTURE ( RES )
D. Output dpm('sample_model',Nx,Nu);
The outputs of the dpm function are two structures, namely This command will save an m-function as
res and dyn. The res structure contains the results from sample_model.m with a random model of Nx state
1627
Authorized licensed use limited to: INDIAN INSTITUTE OF TECHNOLOGY DELHI. Downloaded on June 19,2025 at 10:47:09 UTC from IEEE Xplore. Restrictions apply.
variables and Nu input variables, suitable for usage with In the first example of the Lotka-Volterra fishery problem
the dpm function, which can be used as a template when the entire code necessary to use the dpm function is shown.
developing a new problem description. In the hybrid-electric vehicle example, however, the model
In general the model function should have the format: function contains far too many lines to be included in this
paper. Interested readers can download the complete model
function [X, C, I, signals] = mymodel(inp,par) equations at [7].
where the model input structure inp is generated by the A. Lotka-Volterra Fishery
dpm-function and contains the elements in Table VI. The In order to evaluate the optimal solution by means of DP
structure par can contain any user defined parameters a discrete-time approximation of the continuous-time Lotka-
necessary in the model function. It is important that the Volterra model is used. Using an Euler forward approxima-
model function preserves the size of the inputs to the outputs. tion with a time step Ts = 0.2 days, the discrete-time model
Consequently, the elements inp.X{.}, inp.U{.} and the is
outputs X{.}, C{.}, and I must have the same size. The
structure signals can contain any user defined internal xk+1 = f (xk , uk ) + xk , k = 0, 1, . . . , N − 1 (13)
signals in model. These signals are stored during the forward
where
simulation and returned in the res structure when calling
x2
f (xk , uk ) = Ts ⋅ ( ⋅ (xk − k ) − uk ) .
the dpm-function, Table V. 2
(14)
100 1000
TABLE VI
INPUT- STRUCTURE ( INP ) The state xk ∈ [0, 1000] is the amount of fishes in a lake, the
control signal uk ∈ [0, 10] is the constant fishing rate during
X{.} current states (n+m dimensional matrix form de- one time step. The discrete-time optimal control problem of
pending on the number of inputs and state variables)
U{.} current inputs (n+m dimensional matrix form de- maximizing the amount of fishes caught over a fixed period
pending on the number of inputs and state variables) of time can be formulated as follows:
W{.} current time-variant data (scalar) N −1
∑ −uk ⋅ Ts
Ts time step
min (15)
uk ∈[0, 10] k=0
s.t.
xk+1 = f (xk , uk ) + xk
TABLE VII
MODEL OUTPUTS
(16)
x0 = 250 (17)
xN ≥ 750 (= xf,min )
X{.} resulting states after applying inp.U{.} at
inp.X{.} (same size as inp.X{.}) (18)
C{.} resulting cost of applying inp.U{.} at inp.X{.} xk ∈ [0, 1000] (19)
(same size as inp.X{.})
I set with infeasible combinations (feasible=0, infeasi- 200
ble=1) (same size as inp.X{.}) N= + 1. (20)
Ts
signals structure with user defined signals (same size as
inp.X{.}) To solve this optimal control problem the model function
(13) is implemented in Matlab as:
1628
Authorized licensed use limited to: INDIAN INSTITUTE OF TECHNOLOGY DELHI. Downloaded on June 19,2025 at 10:47:09 UTC from IEEE Xplore. Restrictions apply.
for an initial state of x0 = 250, is shown in Fig. 1 by the
1000
black solid line.
900
B. Hybrid-Electric Vehicle Example
800
o
u = 10 xf The energy consumption of hybrid-electric vehicles can
700
be described well using a quasi-static discrete-time model.
state variable x [-]
1629
Authorized licensed use limited to: INDIAN INSTITUTE OF TECHNOLOGY DELHI. Downloaded on June 19,2025 at 10:47:09 UTC from IEEE Xplore. Restrictions apply.
two examples. This dpm function together with the model
functions introduced in this paper can be downloaded at [7].
0.65 The computational time3 required for backward calculation
0.6
for the two examples, without using the boundary line, is
state variable x [-]
Torq
Rec
Stfeasib
Th e
In
Elec
Thanks to Daniel Ambühl and Moritz Oetiker for their
ha
rma
trica
u
rechl
e as
constructive inputs during the development of the code and
rge
l
arge
sist
for testing and evaluating the dpm function.
Fig. 2. Optimal input map obtained using the DP algorithm for a full R EFERENCES
parallel hybrid-electric vehicle driving the Japanese 10-15 drive cycle. The
[1] R. E. Bellman, Dynamic programming. Princeton - N.J.: Princeton
black curve shows the optimal state-of-charge trajectory when the battery
University Press, 1957.
is 55% charged at the start.
[2] D. Bertsekas, Dynamic programming and optimal control, 3rd ed.
Belmont, Massachusetts: Athena Scientific, 2005.
0.8
[points x106 /s]
1630
Authorized licensed use limited to: INDIAN INSTITUTE OF TECHNOLOGY DELHI. Downloaded on June 19,2025 at 10:47:09 UTC from IEEE Xplore. Restrictions apply.