You are on page 1of 1

Nonlinear programming and code-generation in CasADi

Joris Gillis
MECO Research Team, Department Mechanical Engineering, KU Leuven
DMMS lab, Flanders Make, Leuven, Belgium
joris.gillis@kuleuven.be

1 Introduction 0 1 1

0.5 0.5
20
CasADi [1] (http://casadi.org) is an open-source

p2
0 0
symbolic software framework for nonlinear programming, 40
available in C++, Python, MATLAB and Octave. This ab- -0.5 -0.5

stract highlights (using ‘*‘) some recent advances included 60 -1 -1


0 50 -0.5 0 0.5
in CasADi 3.5 that enable a workflow from high-level mod- nz = 182 p1
eling to low-level use cases.
Fig. 1. Jacobian structure Fig. 2. Evaluation of M(p)
2 Modeling of an optimal control transcription

Consider a smooth nonlinear discretised system dynamics 3 Code-generation


F : R2 × R 7→ R2 . We study an optimal control task, imple-
A mapping from the initial state p to the optimal control
mented as multiple-shooting, that drives the states to zero
action u1 can be obtained* as a CasADi Function object:
starting from a parametric initial state p ∈ R2 .
M = o p t i . t o F u n c t i o n ({ p } ,{ u ( : , 1 ) } ) ;
minimize
x1 ,x2 ,...,xN+1 ,
∑N+1 2
i=1 xi ,
u1 ,u2 ,...,uN Function objects can be saved* to a file and loaded back
subject to xk+1 = F(xk , uk ), k = 1 . . . N (1) in, and can be exported to a self-contained C code. Code-
−1 ≤ uk ≤ 1, k = 1 . . . N generation now supports the implementation of the instanti-
x1 = p. ated NLP solver* .

These additions allow flexible workflows such as modeling


We use CasADi’s modeling language Opti to formulate the
system dynamics in Python-CasADi (e.g using JModelica),
problem symbolically:
using that model to construct an optimal control implemen-
o p t i = casadi . Opti ( ) ; tation in MATLAB-CasADi, and deploying the resulting al-
x = o p t i . v a r i a b l e ( 2 ,N+ 1 ) ; gorithm on an embedded system (e.g. using Simulink).
u = o p t i . v a r i a b l e ( 1 ,N ) ;
p = opti . parameter ( 2 ) ; Here, we merely demonstrate generating a mex file from M
o p t i . minimize ( sumsqr ( x ) ) ; to speed up production of Figure 2:
f o r k = 1 :N
M. g e n e r a t e ( ’Mc ’ , s t r u c t ( ’ mex ’ , t r u e ) ) ;
opti . subject to ( . . .
mex Mc . c −l a r g e A r r a y D i m s
x ( : , k +1)== F ( x ( : , k ) , u ( : , k ) ) ;
end
References
o p t i . s u b j e c t t o (−1<=u <=1); [1] Joel A. E. Andersson, Joris Gillis, Greg Horn, James B. Rawlings, and
opti . subject to (x (: ,1)== p ); Moritz Diehl. CasADi: a software framework for nonlinear optimization and opti-
mal control. Mathematical Programming Computation, Jul 2018.
[2] Joel A.E. Andersson and James B. Rawlings. Sensitivity Analysis for Non-
We indicate to use the sparsity-exploiting active-set solver linear Programming in CasADi. IFAC-PapersOnLine, 51(20):331 – 336, 2018. 6th
qrqp[2] for the subproblems of a sequential quadratic pro- IFAC Conference on Nonlinear Model Predictive Control NMPC 2018.

gramming solver:
Acknowledgement. This work has been carried out within the framework of projects
o pt s . qpsol = ’ qrqp ’ ; Flanders Make ICON: Physical and control co-design of electromechanical drivetrains
o p t i . s o l v e r ( ’ sqpmethod ’ , o p t s ) ; for machines and vehicles, and KU Leuven-BOF PFV/10/002 Centre of Excellence:
Optimization in Engineering (OPTEC). Flanders Make is the Flemish strategic re-
search centre for the manufacturing industry.
CasADi provides this solver with derivatives such as the
large-and-sparse constraint Jacobian (see Figure 1) through
algorithmic differentiation of expression graphs.

You might also like