Dynopt - Dynamic Optimisation Code For
Dynopt - Dynamic Optimisation Code For
MATLAB
zniar , D. Salhi , M. Fikar , M.A. Latifi
M. Ci
Introduction
The objective of dynamic optimisation is to determine, in open loop control, a set of decision variable time profiles (pressure, temperature, flow rate, current, heat duty, . . . ) for a
dynamic system that optimise a given performance index (or cost functional or optimisation criterion)(cost, time, energy, selectivity, . . . ) subject to specified constraints (safety, environmental
and operating constraints). Optimal control refers to the determination of the best time-varying
profiles in closed loop control.
The numerical methods used to find a deterministic solution of dynamic optimisation
problems can be grouped into two categories: indirect and direct methods. In this work only
direct methods are considered. In this category, there are two strategies: sequential method
and simultaneous method. The sequential strategy, often called control vector parameterisation
(CVP), consists in an approximation of the control trajectory by a function of only a few
parameters and leaving the state equations in the form of the original differential algebraic
equation (DAE) system [7]. In the simultaneous strategy, both the control and state variables
are discretised using polynomials (e.g., Lagrange polynomials) of which the coefficients become
the decision variables in a much larger Nonlinear Programming problem (NLP) [2].
In this paper, the method of orthogonal collocation on finite elements is developed based
on [2; 9]. It is implemented purely in MATLAB as a collection of M files without any MEX/DLL
interface. For the solution of NLP, standard Optimisation Toolbox is used. Its aim is to provide
a simple interface to dynamic optimisation. Is is suitable for typical problems in chemical and
biochemical industries. When compared to lower (compilator based) programming languages, a
typical solution time is longer. However, the total time of learning and simplicity makes rapid
development and integration with other packages possible. The source code of the package is
available free at the web pages of authors.
The outline of the paper is as follows. In the next section we sketch a general NLP
formulation for optimal control problems using orthogonal collocation on finite elements method,
which is implemented in the dynamic optimisation package (dynopt). Section 3 shows how to
define a given control problem with dynopt. In section 4, we present some known examples from
literature dealing with chemical reactors which are then solved and discussed in section 5.
Consider the following general dynamic optimisation problem for t [0, tf ] which comprises a
Meyer-type cost functional, a process dynamic model described by a set of ordinary differential
equations (ODE), and equality and inequality constraints.
xi1,0
ui1,1
ui1,2
xi1,1
xi1,2
xi,0
i1
ui,1
ui,2
xi,1
xi,2
xi+1,0
i+1
ui+1,1
ui+1,2
xi+1,1
xi+1,2
xi+2,0
i+2
i
Figure 1: Finite-element collocation discretisation for state profiles, control profiles and element
lengths
min J[x(tf )]
(1)
u(t)
such that
x(t)
x(0) = x0
In order to convert problem (1) into NLP problem, the orthogonal collocation on finite
elements is used (Fig. 1). The optimal control problem (1) is then solved by complete parameterisation of both, the control and the state vectors [3; 9; 10]. That means that the control
and state profiles on one time interval are approximated by linear combination of some basis
functions (Lagrange polynomials (2), (3)).
xK+1 (t) =
K
X
j=0
K
Y
(t tik )
(tij tik )
(2)
k=0,j
in element i, i = 1, . . . , NE
uK (t) =
K
X
j=1
K
Y
(t tik )
uij j (t); j (t) =
(tij tik )
k=1,j
in element i, i = 1, . . . , NE
(3)
Here k = 0, j means that k starts from 0 and k 6= j, NE is the number of elements. Also xK+1 (t)
is a (K + 1)th order (deg < K + 1) piecewise polynomial and uK (t) is Kth order (deg < K)
piecewise polynomial. The difference in orders is due to the existence of the initial conditions
for x(t), for each element i. The times tij are given as roots of the Legendre polynomials on
interval [0, 1].
The problem (1) now becomes:
min
xij ,uij ,i
J(xf )
(4)
such that
x10 x0 = 0, tf
NE
X
i = 0
i=1
j = 1, . . . , K
xi0 xi1
K+1 (i ) = 0,
i = 2, . . . , NE
E
xf xN
K+1 (N E+1 ) = 0
U
i
i = 1, . . . , NE
uL
i uK (i ) ui ,
L
i
U
ui uK (i+1 ) ui , i = 1, . . . , NE
iL i iU i = 1, . . . , NE
Tutorial
3.1
Problem Definition
min J =
tf
u(t)
(5)
such that
x 1 = x2
x1 (0) = 0
x 2 = x2 + u
x2 (0) = 1
2
tf = 1
where
x1 (t), x2 (t) states,
u(t) control vector.
As the objective function is not in the Meyer form needed by dynopt, we define an additional
differential equation
(6)
x 3 = x21 + x22 + 0.005u2 , x3 (0) = 0
and rewrite the cost as
min J = x3 (tf )
(7)
u(t)
3.2
Step1: Process
function sys = process(t,x,flag,u)
switch flag,
case 0
sys = [x(2);
-x(2)+u;
x(1)^2+x(2)^2+0.005*u^2];
case 1
sys = [0 0 2*x(1);
1 -1 2*x(2);
0 0 0];
case 2
sys = [0 1 0.01*u];
case 3
sys = [0 0 0];
case 4
sys = [0;-1;0];
otherwise
error([unhandled flag = ,num2str(flag)]);
end
dynopt optimises a given performance index evaluated at the final conditions subject to the
constraints which can be evaluated at the initial conditions or over the full time interval or at
the final conditions. Thus the input arguments of objfun and confun are as follows: t - scalar
value representing tij , x - state vector evaluated at corresponding time tij , u - control vector
evaluated at corresponding time tij . objfun should be defined as follows:
Step2: Objective Function
function [f,Dft,Dfx,Dfu] = objfun(t,x,u)
f=x(3);
Dft=[];
Dfx=[0;0;1];
Dfu=[];
The given constraints should be written in M-file confun.m as follows:
Step3: Constraints
function [c,ceq,Dct,Dcx,Dcu,Dceqt,Dceqx,Dcequ] = confun(t,x,u)
c=x(2)-8*(t-0.5)^2+0.5;
ceq=[];
Dct=[-16*t+8];
Dcx=[0;1;0];
Dcu=[];
Dceqt=[];
Dceqx=[];
Dcequ=[];
Step4: Optimisation After the problem has been defined by the above mentioned functions,
user invokes the dynopt function as follows:
opt
opt
opt
opt
opt
=
=
=
=
=
optimset(LargeScale,off,Display,iter);
optimset(opt,GradObj, on,GradConstr,on);
optimset(opt,TolFun,1e-5);
optimset(opt,TolCon,1e-5);
optimset(opt,TolX,1e-5);
0.5
14
12
10
0.5
4
2
1.5
2
0
0.2
0.4
0.6
0.8
2.5
0.2
0.4
0.6
0.8
Figure 2: Optimal trajectory found for tu- Figure 3: Optimal trajectory found for tutorial problem - control
torial problem - constraint
Optimal control and the constraint are shown in Fig. 2 and Fig. 3, respectively. Note, that the
constraint is satisfied in collocation points only this is given by the orthogonal collocation
approach.
Case Studies
4.1
Car Optimisation
(8)
u(t)
such that
x 1 = u
x1 (0) = 0
x1 (tf ) = 0
x 2 = x1
x2 (0) = 0
x2 (tf ) = 300
u [2, 1]
where
x1 (t) velocity,
x2 (t) distance,
u(t) control variable (acceleration).
4.2
Tubular Reactor
(9)
such that
x 1 = (u + 0.5u2 )x1
x1 (0) = 1
x 2 = ux1
x2 (0) = 0
u [0, 5]
tf = 1
where
x1 (t) dimensionless concentration of A,
x2 (t) dimensionless concentration of B,
u(t) control variable.
4.3
Batch Reactor
(10)
u(t)
such that
x 1 = k1 x21
x 2 =
k1 x21
x1 (0) = 1
k2 x2
( 2500
)
T
x2 (0) = 0
k1 = 4000e
k2 = 620000e(
T [298, 398]
tf = 1
5000
)
T
where
x1 (t) concentration of A,
x2 (t) concentration of B,
T temperature (control variable).
4.4
Consider a catalytic plug flow reactor [4; 12] with the following reactions:
ABC
max J = 1 x1 (tf ) x2 (tf )
u(t)
such that
x 1 = u(10x2 x1 )
x1 (0) = 1
x 2 = u(10x2 x1 ) (1 u)x2
x2 (0) = 0
u [0, 1]
where
x1 (t) mole fraction of A,
x2 (t) mole fraction of B,
u(t) fraction of type 1 catalyst.
tf = 12
(11)
4.5
CSTR
Consider the following problem of continuously stirred tank reactor (CSTR) [1; 6; 11]
max J =
u(t)
0.2
(12)
such that
x 1 = u4 qx1 17.6x1 x2 23x1 x6 u3
x 2 = u1 qx2 17.6x1 x2 146x2 x3
x 3 = u2 qx3 73x2 x3
x 4 = qx4 + 35.2x1 x2 51.3x4 x5
x 5 = qx5 + 219x2 x3 51.3x4 x5
x 6 = qx6 + 102.6x4 x5 23x1 x6 u3
x 7 = qx7 + 46x1 x6 u3
x(0) = [0.1883 0.2507 0.0467 0.0899 0.1804 0.1394 0.1046]T
q = u 1 + u 2 + u4
0 u1 20
0 u2 6
0 u3 4
0 u4 20
tf = 0.2
where
x1 (t) x7 (t) states,
u1 (t) u4 (t) controls.
Again, the cost function can be rewritten to the Meyer form by introducing a new state defined
by the integral function with its initial value equal to zero.
For all the examples, a piecewise linear profile for the control variable was used. We have used
2 time elements (example 1), 4 time elements (examples 24), and 10 elements (examples 5, 6)
and precision 105 (if not stated otherwise).
The first problem (Section 4.1) consists of starting and stoping a car in minimum for a fixed
distance (300 units). This problem was treated by [2; 9; 10] and the optimal value of 30 time
units was reported. By using 4 collocation points for state variables and 2 collocation points
for control variable we obtained the same value of performance index as from the literature.
Optimal control and state profiles are shown in Fig. 4, Fig. 5, and Fig. 6.
The second problem (Section 4.2) is a tubular reactor control problem where the state
variable x2 at final time has to be maximised. This problem was treated by [4; 9; 12] and the
optimal value (0.57353) was reported by [4; 9] and optimal value (0.57284) was given by [12].
20
1
0.5
15
0.5
10
5
1.5
2
0
10
15
time
20
25
30
10
15
time
20
25
30
Figure 4: Optimal trajectory found for Ex- Figure 5: Optimal trajectory found for Example 4.1 - control (acceleration)
ample 4.1 - velocity
5
250
200
3
u
x2
300
150
2
100
1
50
0
0
0
10
15
time
20
25
30
0.2
0.4
0.6
0.8
time
Figure 6: Optimal trajectory found for Ex- Figure 7: Optimal trajectory found for Example 4.1 - distance
ample 4.1 - control
By using 4 collocation points for state variables and 2 collocation points for control variable
we obtained the value of performance index 0.5729. Optimal control trajectory is presented
by Fig. 7.
The objective in problem described in Section 4.3 is to obtain the optimal temperature
profile that maximizes x2 at the end of a specified time. The problem was solved by [9; 12] and
the reported optimum (0.610775) was found by [9] and (0.61045) obtained by [12]. We were
able to obtain the value of 0.6107 by applying 3 collocation ponits for state variables and 2
collocation points for control variable. Optimal temperature profile is presented by Fig. 8.
Optimisation of problem in Section 4.4 has also been analysed. This problem was solved
by [9; 12] and the optima (0.476946, 0.47615) were found. Value of the performance index
obtained for this example using dynopt is 0.4770. In this case 4 collocation points for state
variables and 2 collocation points for control variables were used. Optimal control trajectory is
presented by Fig. 9.
Maximisation problem in Section 4.5 was treated by [1; 6; 11]. Four control variables
of a chemical reactor are optimised in order to obtain maximum economic benefit. Reported
optimal value (21.757) was obtained using CVP method inplemented in DYNO [6] and also in
other references. For this example, 4 collocation points for state variables and 2 collocation
points for control variables were choosen and an optimum was found at value 21.8003. Note
however, that better value can be explained by the use of linear control profiles compared to
piece-wise constant approximations in references.
0.7
390
0.6
380
370
0.5
0.4
350
360
340
0.3
330
0.2
320
0.1
310
300
0
0.2
0.4
0.6
time
0.8
6
time
10
12
Figure 8: Optimal trajectory found for Ex- Figure 9: Optimal trajectory found for Example 4.2 - control
ample 4.3 - control
Note, that all the results obtained by orthogonal collocation on finite elements method
implemented within MATLAB-dynopt are only local in nature, since NLP solvers are only based
on necessary conditions for optimality.
Conclusion
The orthogonal collocation on finite elements has been developed and implemented within MATLAB environment. It has been tested on a few examples from the literature dealing with chemical
reactors. The examples were chosen to illustrate the ability of the dynopt package to treat the
problems of varying levels of difficulty.
The package dynopt is able to obtain results in a good agreement with the referenced
works. From the user point of view, it is implemented in MATLAB and thus aimed for a large
base of its users. Its main purpose is to make possible a rapid development and testing of
dynamic optimisation problems and incorporation to higher level problems.
It must be noted that optima obtained are only local in nature. Our future work will
be devoted to global optimisation problems where the package can provide local results for the
global problems.
Acknowledgments
The authors are pleased to acknowledge the financial support of the Scientific Grant Agency of
the Slovak Republic under grant No. 1/0135/03. Part of the work has been completed under the
French Slovak program Stefanik (Dynamic and Global Optimisation of Processes) and within
the framework of the European Social Fund (Development of Human Resources for Research
and Progress in Automation, JPD 3 2004/1-056).
References
[1] M-S. G. E. Balsa-Canto, J. R. Banga, A. A. Alonso, and V. S. Vassiliadis. Dynamic optimization of chemical and biochemical processes using restricted second-order information.
Computers chem. Engng., 25(4-6):539546, 2001. 8, 9
[2] J. E. Cuthrell and L. T. Biegler. On the optimization of differential-algebraic process
systems. AIChE Journal, 33:12571270, 1987. 1, 8
20
5
15
u2
10
3
2
1
0
0.05
0.1
time
0.15
0.2
0.05
0.1
time
0.15
0.2
Figure 10: Optimal trajectory found for Figure 11: Optimal trajectory found for
Example 4.5 - control 1
Example 4.5 - control 2
20
2
15
1.5
10
0.5
0.05
0.1
time
0.15
0.2
0.05
0.1
time
0.15
0.2
Figure 12: Optimal trajectory found for Figure 13: Optimal trajectory found for
Example 4.5 - control 3
Example 4.5 - control 4
[3] J. E. Cuthrell and L. T. Biegler. Simultaneous optimization and solution methods for batch
reactor control profiles. Computers chem. Engng., 13(1/2):4962, 1989. 2
[4] S.A. Dadebo and K.B. McAuley. Dynamic optimization of constrained chemical engineering
problems using dinamic programming. Computers chem. Engng., 19:513525, 1995. 6, 7, 8
[5] W. F. Feehery. Dynamic Optimisation with Path Constraints. PhD thesis, MIT, 1998. 4
[6] M. Fikar and M. A. Latifi. Users guide for FORTRAN dynamic optimisation code DYNO.
Technical Report mf0201, LSGC CNRS, Nancy, France; STU Bratislava, Slovak Republic,
2002. 4, 8, 9
[7] C. J. Goh and K. L. Teo. Control parametrization: A unified approach to optimal control
problems with general constraints. Automatica, 24:318, 01 1988. 1
[8] D. Jacobson and M. Lele. A transformation technique for optimal control problems with a
state variable inequality constraint. IEEE Trans. Automatic Control, 5:457464, 1969. 4
[9] J. S. Logsdon and L. T. Biegler. Accurate solution of differential-algebraic optimization
problems. Chem. Eng. Sci., (28):16281639, 1989. 1, 2, 6, 8, 9
[10] J. S. Logsdon and L. T. Biegler. Decomposition strategies for large-scale dynamic optimization problems. Chem. Eng. Sci., 47(4):851864, 1992. 2, 8