Tutorial
OCPID-DAE1
Optimal Control and Parameter Identification with
Differential-Algebraic Equations of Index 1
May 16, 2013
Address of the Author: Prof. Dr. Matthias Gerdts
Institut f¨ur Mathematik und Rechneranwendung Fakult¨at f¨ur Luft- und Raumfahrttechnik
Universit¨at der Bundeswehr M¨unchen
Werner-Heisenberg-Weg 39, 85577 Neubiberg, Germany Email : [email protected]
WWW : www.unibw.de/lrt1/gerdts
1
Problem Formulation
The package OCPID-DAE1 with a Fortran 90 interface is designed for the numerical solution of optimal control problems and parameter identification problems of the subsequent form. Let [t0, tf] ⊂ R be a non-empty and bounded interval with fixed time points t0 < tf. Let
ϕ : Rnx× Rnx× R × Rnp → R, F : [t0, tf] × Rnx× Rnx × Rnu× Rnp → Rnx, f : [t0, tf] × Rnx× Rnu× Rnp → Rnx, H : [t0, tf] × Rnx× Rnu× Rnp → R, ψ : R × R × Rnx × Rnx× Rnu× Rnu× Rnp → Rnψ, g : [t0, tf] × Rnx× Rnu× Rnp → Rng, M : [t0, tf] × Rnx× Rnu× Rnp → Rnx×nx be mappings. We consider
Problem 1 (Optimal Control Problem (OCP))
Find a state variable x(·) : [t0, tf] → Rnx, an essentially bounded control variable u(·) :
[t0, tf] → Rnu, and a parameter vector p ∈ Rnp such that the objective function
ϕ(x(t0), x(tf), tf, p) + L
X
i=1
H(ξi, x(ξi), u(ξi), p) (1)
is minimized subject to the implicit differential equation (DAE)
0 = F (t, x(t), ˙x(t), u(t), p), a.e. in [t0, tf], (2)
the boundary conditions
ψ≤ ψ(t0, tf, x(t0), x(tf), u(t0), u(tf), p) ≤ ψ, (3)
the state constraints
g≤ g(t, x(t), u(t), p) ≤ g, a.e. in [t0, tf], (4)
and the box constraints
u ≤ u(t) ≤ u, a.e. in [t0, tf] (5)
and
p ≤ p ≤ p. (6)
Herein, ξi ∈ [t0, tf], i = 1, . . . , L, L ∈ N0, are optionally given time points, which may denote
time points at which measurements of the dynamic process are available. The function H can be used to model parameter identification problems.
The algorithm is particularly designed for the following two subclasses of the general DAE (2): • ODEs:
0 = M (t, x(t), u(t), p) · ˙x(t) − f (t, x(t), u(t), p), a.e. in [t0, tf], (7)
with non-singular matrix M (·). • Index-1 DAEs:
0 = F (t, xd(t), y(t), ˙xd(t), u(t), p) a.e. in [t0, tf], (8)
where the state x = (xd, y) ∈ Rnx is separated into differential variables xd∈ Rnx−ny and
algebraic variables y ∈ Rny. The matrix
F0 y | Fx0˙d
is assumed to be non-singular a.e. in [t0, tf]. In this case, the DAE (8) has differential
index 1. Notice, that no derivatives of the algebraic variables y occur.
2
Setting up the Minimum Energy Problem in ODE
Formula-tion
The minimum energy problem reads as
Problem 2 (Minimum Energy, ODE Formulation) Minimize 1 2 Z 1 0 u(t)2dt subject to the constraints
˙ x1(t) = x2(t), x1(0) = 0, x1(1) = 0, ˙ x2(t) = u(t), x2(0) = 1, x2(1) = −1, x1(t) ≤ 1 9
In order to fit the problem into problem class 1, we introduce a new state x3 with
˙ x3(t) = 1 2u(t) 2, x 3(0) = 0. Note that x3(1) = 1 2 Z 1 0
Problem 3 (Minimum Energy, ODE Formulation) Minimize x3(1) o ←→ ϕ(x(t0), x(tf), tf, p) + L X i=1 H(ξi, x(ξi), u(ξi), p)
subject to the constraints ˙ x1(t) = x2(t) ˙ x2(t) = u(t) ˙ x3(t) = 12u(t)2 ←→ M (t, x(t), u(t), p) ˙x(t) = f (t, x(t), u(t), p) 0 ≤ x1(0) ≤ 0 1 ≤ x2(0) ≤ 1 0 ≤ x3(0) ≤ 0 0 ≤ x1(1) ≤ 0 −1 ≤ x2(1) ≤ −1 ←→ ψ ≤ ψ(t0, tf, x(t0), x(tf), u(t0), u(tf), p) ≤ ψ −∞ ≤ x1(t) ≤ 19 o ←→ g ≤ g(t, x(t), u(t), p) ≤ g −∞ ≤ u(t) ≤ ∞ o ←→ u≤ u(t) ≤ u
Problem 3 fits into the format of Problem 1 with x = (x1, x2, x3)>, t0= 0, tf = 1,
ϕ(x(t0), x(tf)) = x3(1), H(ξ, x, u, p) = 0, M (t, x, u, p) = I3×3, f (t, x, u, p) = (x2, u, 1 2u 2)>, ψ = (0, 1, 0, 0, −1)>, ψ = (0, 1, 0, 0, −1)>, ψ(t0, tf, x(t0), x(tf), u(t0), u(tf), p) = (x1(0), x2(0), x3(0), x1(1), x2(1))>, g = −∞, g = 1 9, g(t, x, u, p) = x1, u = −∞, u = ∞,
and p is not present in this problem. With this, we can now start to enter the data in OCPID-DAE1. We use the template file ProblemTemplate.f90. Below only the relevant entries for Problem 3 are described.
Define dimensions of the problem:
DIM(1) = 3 ! number NX of states
DIM(2) = 1 ! number NU of controls
DIM(3) = 0 ! number NP of optimization parameters
DIM(4) = 1 ! number NG of state constraints
DIM(5) = 2 ! number NBC of boundary conditions in BDCOND
DIM(6) = 28 ! number NGITU of control grid points
DIM(7) = 1 ! number NGITX of shooting nodes
! (one for single shooting, >1 for multiple shooting)
DIM(8) = 0 ! number NROOT of switching functions
! (zero for standard optimal control problems)
DIM(9) = 0 ! number NY of algebraic variables in x
DIM(10) = 0 ! number NMEASURE of measure points xi_i in function H ! (zero for standard optimal control problems)
Note that DIM(5)=2, because we will merely enter the terminal constraints 0 ≤ x1(1) ≤ 0 and
−1 ≤ x2(1) ≤ −1 in the subroutine BDCOND, which contains the function ψ. The remaining
boundary conditions, which fix the initial state, will be entered directly in the arrays XL and XU, in which lower and upper bounds for the initial state are provided. This is more convenient than defining the latter as nonlinear constraints through the subroutine BDCOND, which would be possible as well.
Next, we define control parameters of the optimal control algorithm:
INFO(1) = 0 ! flag equidistant grids (=0) or non-equidistant grids (=1)
INFO(2) = 11 ! integrator (11=classic RK), see user’s guide for a list
INFO(3) = 2 ! control approximation by B-splines
! (1=piecewise constant, 2=continuous, piecewise linear, ...)
INFO(4) = 1 ! 0 = integration mode (no optimization) ! >0 = optimization mode
INFO(5) = 1 ! method for gradient calculation (1= sensitivity DAE)
INFO(6) = 0 ! structure of matrix F’_{x’} and M, respectively ! (0 = constant and diagonal)
INFO(7) = 0 ! flag for iteration matrix (not relevant for this problem)
INFO(8) = 0 ! flag for jacobian of state constraints ! (0 = approximation by finite differences,
! 1 = provide jacobian matrix in subroutine JACNLC by user)
INFO(9) = 6 ! output will be printed to output file with this number (6=screen)
INFO(10)= 1 ! flag for finite differences
! (1 = forward differences are used,
! 0 = central finite differences are used,
INFO(11)= 0 ! computation of consistent initial values for index-1 DAEs ! (not relevant for this problem)
Next we can control how the initial guess will be provided and how the results will be stored:
IINMODE = 0 ! 0 = initial guess for states at shooting nodes are provided
! in subroutine INESTX, initial guess for controls is provided in
subroutine INESTU
! <>0 = initial guess is provided in array SOL
IOUTMODE = 3 ! 1 = output of solution will be written to files
! 2 = output of solution will be written to arrays
! TIME,STATE,CONTROL,PARAMETERS,SCONSTRAINTS,DSOLREALTIME
! 3 = output of solution will be written to both, files and variables
If we like to perform a parametric sensitivity analysis w.r.t. to some model parameters, which are stored in USER(IUSER(I)) for I=1,...,NREALTIME, we could set appropriate data through IREALTIME, NREALTIME, HREALTIME, but in this example, we don’t do that and set
IREALTIME = 0 NREALTIME = 2 HREALTIME = 1.D-3
We switch on adjoint estimation by setting
IADJOINT = 1
Next we define the initial time t0= 0 and the length of the time interval tf − t0= 1:
T(0) = 0.0D0 ! provide initial time t0 of optimal control problem
T(1) = 1.0D0 ! provide length tf-t0 of optimal control problem
! provide lower and upper bounds for initial state x(t0) XL(0,1) = 0.0D0 XL(0,2) = 1.0D0 XL(0,3) = 0.0D0 XU(0,1) = 0.0D0 XU(0,2) = 1.0D0 XU(0,3) = 0.0D0
! provide lower and upper bounds for states at multiple shooting nodes in (t0,tf)
XL(1,1:DIM(1)) = -1.0D+20 XU(1,1:DIM(1)) = 1.0D+20
Note that XL(0,.) and XU(0,.) refer to the lower and upper bounds at t0 and XL(1,.) and
XU(1,.) refer to lower and upper bounds for the state at the shooting nodes. The latter only become relevant if DIM(7)>1.
Next we define u and u at t0, t0 < t < tf and tf:
! provide lower and upper bounds for initial control u(t0)
UL(-1,1:DIM(2)) = -1.0D+20 UU(-1,1:DIM(2)) = 1.0D+20
! provide lower and upper bounds for controls u(t), t0<t<tf
UL( 0,1:DIM(2)) = -1.0D+20 UU( 0,1:DIM(2)) = 1.0D+20
! provide lower and upper bounds for terminal control u(tf)
UL( 1,1:DIM(2)) = -1.0D+20 UU( 1,1:DIM(2)) = 1.0D+20
Lower and upper bounds g and g are defined by:
G(0,1) = -1.0D+20 ! lower bound g_l for state constraint g_l <= g(t,x(t),u(t),p) <= g_u
G(1,1) = 1.0D0/9.0D0 ! upper bound g_u for state constraint g_l <= g(t,x(t),u(t),p) <= g_u
Likewise, lower and upper bounds ψ and ψ are defined by the following. Please note that we merely enter the terminal constraints 0 ≤ x1(1) ≤ 0 and −1 ≤ x2(1) ≤ −1 in the subroutine
BDCOND, which contains the function ψ. Only for these constraints the lower and upper bounds are defined below. The remaining initial conditions have been considered in XL and XU.
BC(0,1) = 0.0D0 ! lower bound in 0 <= x_1(1) <= 0
BC(0,2) = -1.0D0 ! lower bound in -1 <= x_2(1) <= -1
BC(1,1) = 0.0D0 ! upper bound in 0 <= x_1(1) <= 0
BC(1,2) = -1.0D0 ! upper bound in -1 <= x_2(1) <= -1
This completes the entries for the static components in the optimal control problem. Now we need to define the dynamic components. We start with the objective function ϕ:
SUBROUTINE OBJ( X0, XF, TF, P, V, IUSER, USER )
IMPLICIT NONE
INTEGER, DIMENSION(*), INTENT(INOUT) :: IUSER
DOUBLEPRECISION, DIMENSION(*), INTENT(IN) :: X0,XF,P
DOUBLEPRECISION, DIMENSION(*), INTENT(INOUT) :: USER
DOUBLEPRECISION, INTENT(IN) :: TF
DOUBLEPRECISION, INTENT(OUT) :: V V = XF(3)
RETURN
END SUBROUTINE OBJ
The function H is not present in the minimum energy problem, hence we set:
SUBROUTINE HFUNC( I,T,X,U,P,HVAL,IUSER,USER )
IMPLICIT NONE
INTEGER, INTENT(IN) :: I
INTEGER, DIMENSION(*), INTENT(INOUT) :: IUSER
DOUBLEPRECISION, DIMENSION(*), INTENT(IN) :: X,U,P
DOUBLEPRECISION, DIMENSION(*), INTENT(INOUT) :: USER
DOUBLEPRECISION, INTENT(IN) :: T
DOUBLEPRECISION, INTENT(OUT) :: HVAL HVAL = 0.0D0
RETURN
END SUBROUTINE HFUNC
The right handside f is defined in DAE as follows:
SUBROUTINE DAE( T, X, XP, U, P, F, IFLAG, IUSER, USER )
INTEGER, INTENT(IN) :: IFLAG
INTEGER, DIMENSION(*), INTENT(INOUT) :: IUSER
DOUBLEPRECISION, DIMENSION(*), INTENT(IN) :: X,XP,U,P
DOUBLEPRECISION, DIMENSION(*), INTENT(INOUT) :: USER
DOUBLEPRECISION, INTENT(IN) :: T
DOUBLEPRECISION, DIMENSION(*), INTENT(OUT) :: F F(1) = X(2)
F(2) = U(1)
F(3) = 0.5D0*U(1)**2
RETURN
END SUBROUTINE DAE
The function g is defined in NLCSTR as follows:
SUBROUTINE NLCSTR( T, X, U, P, G, IUSER, USER )
IMPLICIT NONE
INTEGER, DIMENSION(*), INTENT(INOUT) :: IUSER
DOUBLEPRECISION, DIMENSION(*), INTENT(IN) :: X,U,P
DOUBLEPRECISION, DIMENSION(*), INTENT(INOUT) :: USER
DOUBLEPRECISION, INTENT(IN) :: T
DOUBLEPRECISION, DIMENSION(*), INTENT(OUT) :: G G(1) = X(1)
RETURN
END SUBROUTINE NLCSTR
The boundary conditions ψ are provided through BDCOND:
SUBROUTINE BDCOND( T0,TF,X0,XF,U0,UF,P,PSI,IUSER,USER )
IMPLICIT NONE
INTEGER, DIMENSION(*), INTENT(INOUT) :: IUSER
DOUBLEPRECISION, DIMENSION(*), INTENT(IN) :: X0,XF,U0,UF,P
DOUBLEPRECISION, DIMENSION(*), INTENT(INOUT) :: USER
DOUBLEPRECISION, INTENT(IN) :: T0,TF
DOUBLEPRECISION, DIMENSION(*), INTENT(OUT) :: PSI PSI(1) = XF(1)
PSI(2) = XF(2)
RETURN
An initial guess for the initial state x(0) can be provided in INESTX. In our case this is not relevant since the initial state is fixed. But if the initial state contains free components, then an initial guess has to be provided.
SUBROUTINE INESTX( T, X, IUSER, USER )
IMPLICIT NONE
INTEGER, DIMENSION(*), INTENT(INOUT) :: IUSER
DOUBLEPRECISION, DIMENSION(*), INTENT(INOUT) :: USER
DOUBLEPRECISION, INTENT(IN) :: T
DOUBLEPRECISION, DIMENSION(*), INTENT(OUT) :: X X(1) = 0.0D0
X(2) = 1.0D0 X(3) = 0.0D0
RETURN
END SUBROUTINE INESTX
Likewise an initial guess for the control u(t) needs to be provided in INESTU. Note that INESTU will be called at each grid point, so a time dependent initial guess can be provided if appropriate.
SUBROUTINE INESTU( T, U, IBOOR, IUSER, USER )
IMPLICIT NONE
INTEGER, INTENT(IN) :: IBOOR
INTEGER, DIMENSION(*), INTENT(INOUT) :: IUSER
DOUBLEPRECISION, DIMENSION(*), INTENT(INOUT) :: USER
DOUBLEPRECISION, INTENT(IN) :: T
DOUBLEPRECISION, DIMENSION(*), INTENT(OUT) :: U U(1)= -2.0D0
RETURN
END SUBROUTINE INESTU
Finally, we need to define the mass matrix M , which in our case is the identity matrix. The structure of M was defined by INFO(6).
SUBROUTINE MASS( NX,T,X,XP,U,P,MMASS,IUSER,USER )
IMPLICIT NONE
INTEGER, INTENT(IN) :: NX
INTEGER, DIMENSION(*), INTENT(INOUT) :: IUSER
DOUBLEPRECISION, DIMENSION(*), INTENT(IN) :: X,XP,U,P
DOUBLEPRECISION, DIMENSION(*), INTENT(INOUT) :: USER
DOUBLEPRECISION, DIMENSION(NX,*), INTENT(OUT) :: MMASS MMASS(1:NX,1)=1.0D0
RETURN
END SUBROUTINE MASS
We have completed entering the relevant data to solve the minimum energy problem. The remaining functions ITMAT,ROOT,DJUMP,JACNLC are not relevant for our problem and need to be assigned.
Compiling with
gfortran ProblemTemplate.f90 libocpiddae1.a libfgssqp.a -o minen and executing (don’t forget to copy param sqp.txt to the local directory)
./minen
will solve the problem and creates among others the data file OCODE01, which contains the solution in the following format: Each line of OCODE01 contains in the first column the time t and in the subsequent columns the state vector x(t), the control vector u(t), the parameter vector p, and the state constraints g(t, x(t), u(t), p). If the adjoint estimation option was chosen (IADJOINT = 1), then the file ADJOINT contains adjoint estimates for the solution in the following format: Each line of ADJOINT contains in the first column the time t and in the subsequent columns the state vector x(t), the adjoint estimates λ(t), and the Lagrange multipliers for the discretized state constraint.
Postscript files of the solution will be generated by
gnuplot OCPIDDAE1.gnu . 0 0.02 0.04 0.06 0.08 0.1 0.12 0 0.2 0.4 0.6 0.8 1 state 1 t State 1 vs time -1 -0.5 0 0.5 1 0 0.2 0.4 0.6 0.8 1 state 2 t State 2 vs time 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 0 0.2 0.4 0.6 0.8 1 state 3 t State 3 vs time -20 -15 -10 -5 0 5 10 15 20 0 0.2 0.4 0.6 0.8 1 adjoint 1 t Adjoint 1 vs time -1 0 1 2 3 4 5 6 0 0.2 0.4 0.6 0.8 1 adjoint 2 t Adjoint 2 vs time 0.985 0.99 0.995 1 1.005 1.01 0 0.2 0.4 0.6 0.8 1 adjoint 3 t Adjoint 3 vs time
-6 -5 -4 -3 -2 -1 0 1 0 0.2 0.4 0.6 0.8 1 control 1 t Control 1 vs time 0 2 4 6 8 10 12 14 16 18 0 0.2 0.4 0.6 0.8 1 multiplier 1 t Multiplier 1 vs time
Note for advanced users: In addition to the files OCODE01 and ADJOINT, the file OCODE02 will be created and it contains the sensitivity matrices S(t) of the state x(t; z) with respect to the vector z of optimization variables of the discretized optimal control problem, that is S(t) = ∂x(t;z)∂z . Each line of OCODE02 contains in the first column the time and in the subsequent columns the matrix S(t), which is stored columnwise.