Optimal Control
Optimal Control
Optimal Control
Chapter 3:
Optimal Control
Master's degree in Automatic Control and Robotics
Conceptual Idea
• Optimal control utilizes optimization to generate the control action considering the reference
and measurements
• The practical implementation of this control is done using a computer, for this reason this
type of control is typically implemented in discrete-time.
Master's degree in Automatic Control and Robotics
(1) The control goals are transformed into an objective function named J that is function of the
states and controls in a time horizon N
– The objective function can contain several objectives: reference tracking is the main one, but also other objectives as
minimizing the control effort (energy) required by the controller, smooth control actions, ...
(2) The model and the physical limitations of the states and the control are the constraints of
the optimization problem
x fc ( x(t ),u(t ))
Ts
x(k 1) f ( x(k),u(k))
Master's degree in Automatic Control and Robotics
• There are many methods for discretizing a numerical system (as e.g., Euler, Runge-Kutta, etc.)
x(k 1) x(k)
x(t )
Ts
• Then, the non-linear model of the system in continuous-time can be expressed in discrete-
time as follows
x fc ( x(t ),u(t ))
Ts
x(k 1) f ( x(k),u(k))
Master's degree in Automatic Control and Robotics
(1) The control goals are transformed into an objective function named J that is function of the
states and controls in a time horizon N
(2) The model and the physical limitations of the states and the control are the constraints of
the optimization problem
N N 1
min e Je (k) u Ju (k)
u( 0 ), ,u( N 1 )
k 1 k 0
suject to :
x(k 1) f ( x(k),u(k)) k 0 , ,N
x( 0 ) xo known
x(k) [ x ,x ] k 1, ,N
u(k) [u,u] k 0 , ,N 1
Master's degree in Automatic Control and Robotics
Problem Solution
• To solve the optimization problem associated to the optimal control problem, there are two
procedures
(1) Analytically using the theory learned in the first part of the course.
(2) Numerically using numerical solvers as the ones available in Matlab.
• For linear systems, both solutions are possible when neglecting the physical constraints
affecting states and inputs.
• For non-linear systems, the analytical solutions is almost impossible, so only the numerical
one is possible.
Master's degree in Automatic Control and Robotics
• When the system to be controlled can be formulated or approximated using a linear model,
the system can be represented in the standard linear form that after discretising can be
expressed in the following form:
x Ac x(t ) Bcu(t )
Ts
x(k 1) Ax(k) Bu(k)
• Then, the previous optimization problem can reformulated in the following way
N N 1
min e Je (k) u Ju (k)
u( 0 ), ,u( N 1 )
k 1 k 0
suject to :
x(k 1) Ax(k) Bu(k) k 0 , ,N
x( 0 ) xo known
x(k) [ x ,x ] k 1, ,N
u(k) [u,u] k 0 , ,N 1
Master's degree in Automatic Control and Robotics
Environment Software/Toolbox/Package
Standalone AMPL, GAMS (~1990)
User Matlab YALMIP, CVX (~ 2000)
Python PuLP, CVXPy
Julia JuMP, Convex.jl
YALMIP
NAG OOQP
Gurobi, CPLEX
LINPROG QUADPROG
MOSEK QSOPT
PENBMI CPLEX
LMIRANK GLPK
Master's degree in Automatic Control and Robotics
• The numerical solution of the optimal control problem can be obtained using an optimization
language as Yalmip
u = sdpvar((nu,1,N),(1,1,N));
x = sdpvar((nx,1,N+1),(1,1,N+1));
constraints = [];
objective = 0;
for k = 1:N
objective = objective + x’{k}*Q*x{k} + u’{k}*R*u{k};
constraints = [constraints, x{k+1} == A*x{k} + B*u{k}];
end
• The analytical solution of the optimal control problem can be done using several approaches:
• To obtain the analytical solution, physical constraints are neglected and the objective
function is expressed in vector/matrix form as follows
1 T 1 N 1 T
min x (N)Sx(N) x (k)Qx(k) uT (k)Ru(k)
u( 0 ), ,u( N 1 ) 2 2 k 0
suject to :
x(k 1) Ax(k) Bu(k) k 0 , ,N
x( 0 ) xo known
Master's degree in Automatic Control and Robotics
• To solve the LQR using the Langrange method a new objective function is created as follows
N 1
L J T (k 1) x(k 1) Ax(k) Bu(k)
k 0
1 T 1 N 1 T
where: J x (N)Sx(N) x (k)Qx(k) uT (k)Ru(k)
2 2 k 0
L
0 k 1 , 2 , ,N
x(k )
L
0 k 0 , 2 , ,N 1
u(k )
L
0 k 1 , 2 , ,N
(k )
Master's degree in Automatic Control and Robotics
Optimal Control
The optimal control obtained from the analytical solution is a state feedback- control:
Ricatti Equation
where P(N) S
1
Jopt x T ( 0 )P( 0 )x( 0 )
2
Master's degree in Automatic Control and Robotics
If the horizon N is long enough, the Ricatti Equation reach a steady state solution:
Optimal Control
The optimal control obtained from the analytical solution is a state feedback- control:
u(k ) K ss x(k )
where
K ss R 1BT ( AT )1 [Pss Q]
Ricatti Equation
1 2 1 9 2
min x ( 10 ) [ x (k ) u 2 (k )]
2 2 k 0
suject to :
x(k 1) 0.3679 x(k ) 0.6321u(k )
x( 0 ) 1
P(N) P( 10 ) S 1
P( 9 ) 1 01354
. 1( 1 0.3996 1) 1 10967
.
P( 8 ) 1 01354
. 10967
. ( 1 0.3996 10967
. ) 1 11032
.
P(7 ) 1 01354
. 11032
. ( 1 0.3996 11032
. ) 1 10036
.
P( 6 ) 1 01354
. 10036
. ( 1 0.3996 10036
. ) 1 11037
.
P(k) 11037
. , k 5 , 4 , ,0
• This equation could be solved approximately using the steady state approximation
Pss 11037
.
Pss 2.2674
Master's degree in Automatic Control and Robotics
• After solving the Ricatti equation, we can determine the optimal controller gain:
K(N) K( 10 ) 1 06321
. 0.3679 1 [P( 10 ) 1] 0
K( 9 ) 1 06321
. 0.3679 1 [P( 9 ) 1] 11662
.
K( 8 ) 1 06321
. 0.3679 1 [P( 8 ) 1] 11773
.
K(7 ) 1 06321
. 0.3679 1 [P(7 ) 1] 01781
.
K(k) 01781
. , k 5 , 4 , ,0
• This gain can be calculated approximately using the steady state approximation
1
Jopt x T ( 0 )P( 0 )x( 0 ) 0.5518
2
• If we use instead the steady state value of the matrix P(0) in the previous formula, we get a
suboptimal value:
1
ss
Jopt x T ( 0 )Pss x( 0 ) 0.5518
2
Master's degree in Automatic Control and Robotics
u(k) K ss x(k)
such that
x
k 0
T
(k)Qx(k) uT (k)Ru(k) x T ( 0 )Pss x T ( 0 )
x(k 1) ( A BK ss )x(k)
it follows
YAT AY BW W T BT YH T HY W T RW 0
Master's degree in Automatic Control and Robotics
• Finally, after some algebraic manipulations the following LMI can be obtained
Y YAT W T BT YH T WT
AY BW Y 0 0
0
HY 0 I 0
W 0 0 R 1
Y P 1 I I
• The value K ss WY 1 can be obtained considering that J opt xoT Pxo 0
• Leading to the following optimization problem I Y
min
W ,Y
I I
subject to: I Y 0
Y YAT W T BT YH T WT
AY BW Y 0 0
0
HY 0 I 0
W 0 0 R 1
Master's degree in Automatic Control and Robotics
• This LMI problem can be solved with Yalmip and SeDuMi solver as follows:
Y = sdpvar(nx,nx);
W = sdpvar(nu,nx,'full');
gamma = sdpvar(1,1)
constraints=[Y>=0];
constraints=[constraints, [gamma*I I;I gamma];
constraints= [constraints, [-Y Y*A’-W’*B’ Y*H’ W’; A*Y-B*W –Y 0 0; H*Y 0 –I 0; W 0 0 –
R* -1]] <= 0];
optimize(F,gamma)
Kss= value(W)*inv(value(Y));
Master's degree in Automatic Control and Robotics
N 1
min x (N)Sx(N) x T (k)Qx(k) uT (k)Ru(k)
T
u( 0 ), ,u( N 1 )
k 0
suject to :
x(k 1) Ax(k) Bu(k) k 0 , ,N
x( 0 ) xo known
can be solved using dynamic programming as presented in the first part of the course.
• Dynamic programming is a recursive approach that is based on the principle of decomposing
the optimization in subproblems.
• Defining
F(k ) x T (k )Qx(k ) uT (k )Ru(k )
then:
N
J(N) F(k )
k 0
Master's degree in Automatic Control and Robotics
S( 1)
0 uopt (N) 0 Sopt ( 1) x T (N)Qx(N)
u(N)
Master's degree in Automatic Control and Robotics
S( 2 )
0 uopt (N 1) K(N 1)x(N 1) Sopt ( 2 ) x T (N 1)P(N 1)x(N 1)
u(N 1)
where:
function [x,u,P,K]=dynamic_lqr(k,x,u,P,K,A,B,R,Q)
if k==1
x(:,,k)=[1,1]; % initial conditions
K(:,:,k)=inv(R+B'*P(:,:,k+1)*B)*B'*P(:,:,k+1)*A;
P(:,:,k)=Q+A'*P(:,:,k+1)*inv(eye(2)+B*inv(R)*B'*P(:,:,k+1))*A;
u(k)=-K(:,:,k)*x(:,:,k);
else
K(:,:,k)=inv(R+B'*P(:,:,k+1)*B)*B'*P(:,:,k+1)*A;
P(:,:,k)=Q+A'*P(:,:,k+1)*inv(eye(2)+B*inv(R)*B'*P(:,:,k+1))*A;
S( 1)
2u( 2 ) 0 uopt ( 2 ) 0 Sopt ( 1) x 2 ( 2 )
u( 2 )
S( 2 )
2 [ 2 x( 1) u( 1)] 2u( 1) 0 uopt ( 1) x( 1) Sopt ( 2 ) 3 x 2 ( 1)
u( 1)
Master's degree in Automatic Control and Robotics
S( 3 )
6 [ 2 x( 1) u( 1)] 2u( 0 ) 0 uopt ( 0 ) 15
. x( 0 ) Sopt ( 3 ) 4 x 2 ( 0 )
u( 0 )
• Then: xopt ( 1) 0.5 uopt ( 1) xopt ( 1) 0.5 xopt ( 2 ) 2 xopt ( 1) uopt ( 1) 0.5