You are on page 1of 34

Numerical Software Verification Workshop 2016

Model Based Code Generation for


Nonlinear Model Predictive Control
Behzad Samadi
Department of Research and Development, Maplesoft
Outline

I Control theory
I Modeling
I Model predictive control
I Model based code generation
Control Theory
Control Theory is Rocket Science
Feedback Control
Modeling
Causal Modeling

Lorenz Attractor

Ordinary differential equations (ODE):

d
x (t) = −a(y (t) − x (t))
dt
d
y (t) = (b − z(t))x (t) − y (t)
dt
d
z(t) = x (t)y (t) − cz(t)
dt
Causal Modeling

Lorenz Attractor
Causal Modeling

Lorenz Attractor
Acausal Modeling

Simple DC Motor
Acausal Modeling

Manipuator Robot with 5 Degrees of Freedom


Acausal Modeling

Manipuator Robot with 5 Degrees of Freedom


Model Predictive Control (MPC)
Model Predictive Control (MPC)

I What is MPC?
I MPC is a closed loop implementation of optimal control.
I Why not use a PID?
I PID controllers do not perform well for highly nonlinear systems.
I To control a multi-input multi-output (MIMO) system, several
PID controllers should be connected in a non-trivial
configuration.
I Tuning PID controllers is not an easy task, especially given
state and input constraints.
Why MPC?

I MPC can handle constraints on inputs and states.


I MPC can be designed for MIMO nonlinear systems.
I Tuning an MPC controller is much more intuitive comparing to
a PID.
I MPC is a systematic model based approach.
I There are tools for generating code for MPC controllers
automatically.

Nonlinear Automatic MPC Controller


System Model Code Generation Code

Figure: Automatice Code Generation


Example
I A race driver needs to look forward! (prediction)
I Minimize a cost function at each time instant depending on the
current situation (closed loop optimal control)
I Optimization constraints:
I Vehicle dynamics behavior
I Limited power
I No skidding
I Following the road
I Avoiding collision

Figure: Autonomous Driving


MPC Applications

I Chemical process control


I Oil refineries
I Pulp and paper
I Mechatronics
I Robotics
I Washing machines
I Automotive
I Engine controllers
I Aerospace
I Aircraft control
I Spacecraft formation and attitude control
I Power generation
I Computational biology
Nonlinear Model

I Consider the following nonlinear system:

ẋ (t) =f (x (t), u(t))


x (t0 ) =x◦

where:
I x (t) is the state vector
I u(t) is the input vector
Optimal Control Problem

Z tf
minimize J(x0 , t0 ) = φ(x (tf )) + L(x (τ ), u(τ ))dτ
u t0
subject to ẋ (t) = f (x (t), u(t))
x (t0 ) = x◦
gi (x (t), u(t)) = 0, for i = 1, . . . , ng
hi (x (t), u(t)) ≤ 0, for i = 1, . . . , nh
Barrier Method
I Using a particular interior-point algorithm, the barrier method,
the inequality constraints are converted to equality constraints:

Z tf  
minimize φ(x (tf )) + L(x (τ ), u(τ )) − r T α(τ ) dτ
u,α t0
subject to ẋ (t) = f (x (t), u(t))
x (t0 ) = x◦
gi (x (t), u(t)) = 0, for i = 1, . . . , ng
hi (x (t), u(t)) + αi (t)2 = 0, for i = 1, . . . , nh

where α(t) ∈ Rnh is a vector slack variable and the entries of


r ∈ Rnh are small positive numbers.

(Boyd and Vandenberghe 2004)


(Diehl, Ferreau, and Haverbeke 2009)
Discretization
I Discretize the problem into N steps from t0 to tf :

N−1
X 
minimize φd (xN ) + L(xk , uk ) − r T αk
u,α
k=0
subject to xk+1 = fd (xk , uk )
x0 = x◦
gi (xk , uk ) = 0, for i = 1, . . . , ng
2
hi (xk , uk ) + αik = 0, for i = 1, . . . , nh

tf −t0
where ∆τ = N and:

φ(x (tf ), tf )
φ(xN , N) =
∆τ
fd (xk , uk ) = xk + f (xk , uk )∆τ
Optimization Problem

N−1
X 
minimize φd (xN ) + L(xk , uk ) − r T αk
u,α
k=0
subject to xk+1 = fd (xk , uk )
x0 = x◦
G(xk , uk , αk ) = 0

where:  
g1 (xk , uk )
 .. 

 . 

 gng (xk , uk ) 
G(xk , uk , αk ) = 
 
 h1 (xk , uk ) + α1k2 


 .. 

 . 
hnh (xk , uk ) + αn2h k
Lagrange Multipliers
I Lagrangian:

L(x , u, α, λ, ν) = φd (xN , N) + (x◦ − x0 )T λ0


N−1
X
+ L(xk , uk ) − r T αk
k=0
+ (fd (xk , uk ) − xk+1 )T λk+1

+G(xk , uk , αk )T νk

I Optimality conditions:

Lxk = 0, Lλk = 0 for k = 0, . . . , N

Lαk = 0, Luk = 0, Lνk = 0 for k = 0, . . . , N − 1


Hamiltonian

I The Lagrangian can be rewritten as:

L(x , u, α, λ, ν) =φd (xN ) + x◦T λ0 − xNT λN


N−1
X 
+ H(xk , uk , αk , λk+1 ) − xkT λk
k=0

I Hamiltonian:

H(xk , uk , αk , λk+1 , νk ) =L(xk , uk ) − r T αk


+ fd (xk , uk )T λk+1 + G(xk , uk , αk )T νk
Pontryagin’s Maximum Principle

Optimality Conditions
Lλk+1 = 0 ?
xk+1 = fd (xk? , uk? )
L λ0 = 0 x0? = x◦
Lxk = 0 λk = Hx (xk? , uk? , αk? , λ?k+1 , νk? )
?

LxN = 0 λ?N = ∂x∂N φd (xN? )


Luk = 0 Hu (xk? , uk? , αk? , λ?k+1 , νk? ) = 0
L αk = 0 Hα (xk? , uk? , αk? , λ?k+1 , νk? ) = 0
Lνk = 0 G(xk? , uk? , αk? ) = 0

for k = 0, . . . , N − 1
Model Predictive Control

I I understand optimal control but what is MPC?


I MPC is the optimal controller in the loop:
1. Measure/estimate the current state xn .
2. Solve the optimal control problem to compute uk for
k = n, . . . , n + N − 1.
3. Return un as the value of the control input.
4. Update n.
5. Goto step 1.
I MPC is implemented in real time.
Continuation/GMRES Method

I Step 1: Compute xk and λk as a function of uk and αk , given


the following equations:

xk+1 = fd (xk , uk )
x0 = xn
λk = Hx (xk , uk , αk , λk+1 , νk )

λN = φd (xN )
∂xN

(Ohtsuka 2004)
Continuation/GMRES Method

I Step 2: Solve the equation F (xn , U) = 0, where:

Hu (x0 , u0 , α0 , λ1 , ν0 )
 

 Hα (x0 , u0 , α0 , λ1 , ν0 ) 


 G(x0 , u0 , α0 ) 


F (xn , U) =  .. 
 . 

 Hu (xN−1 , uN−1 , αN−1 , λN , νN−1 )
 

 
 Hα (xN−1 , uN−1 , αN−1 , λN , νN−1 ) 
G(xN−1 , uN−1 , αN−1 )

where U = [u0T , . . . , uN−1


T , αT , . . . , αT
0
T T
N−1 , ν0 , . . . , νN−1 ]
T

(Ohtsuka 2004)
Continuation/GMRES Method
I Continuation method: Instead of solving F (x , U) = 0, find U
such that:
Ḟ (x , U) = As F (x , U)
where As is a matrix with negative eigenvalues.
I Now, we have:

Fx ẋ + FU U̇ = As F (x , U)

I GMRES: To compute U̇ using the following equation, which is


linear in U̇, we use the generalized minimum residual (GMRES)
algorithm.
FU U̇ = As F (x , U) − Fx f (x , u)
I To compute U at any given time, we need to have an initial
value for U and then use the above U̇ to update it.

(Ohtsuka 2004)
Example

Electro Hydraulic Servo System


Electro Hydraulic Servo System
Automatic Code Generation
References

Boyd, S.P., and L. Vandenberghe. 2004. Convex Optimization.


Cambridge Univ Pr.
Diehl, Moritz, Hans Joachim Ferreau, and Niels Haverbeke. 2009.
“Efficient Numerical Methods for Nonlinear MPC and Moving
Horizon Estimation.” In Nonlinear Model Predictive Control,
391–417. Springer.
Ohtsuka, Toshiyuki. 2004. “A Continuation/GMRES Method for
Fast Computation of Nonlinear Receding Horizon Control.”
Automatica 40 (4). Elsevier: 563–74.

You might also like