scispace - formally typeset
Open AccessJournal ArticleDOI

CasADi: a software framework for nonlinear optimization and optimal control

TLDR
This article gives an up-to-date and accessible introduction to the CasADi framework, which has undergone numerous design improvements over the last 7 years.
Abstract
We present CasADi, an open-source software framework for numerical optimization. CasADi is a general-purpose tool that can be used to model and solve optimization problems with a large degree of flexibility, larger than what is associated with popular algebraic modeling languages such as AMPL, GAMS, JuMP or Pyomo. Of special interest are problems constrained by differential equations, i.e. optimal control problems. CasADi is written in self-contained C++, but is most conveniently used via full-featured interfaces to Python, MATLAB or Octave. Since its inception in late 2009, it has been used successfully for academic teaching as well as in applications from multiple fields, including process control, robotics and aerospace. This article gives an up-to-date and accessible introduction to the CasADi framework, which has undergone numerous design improvements over the last 7 years.

read more

Content maybe subject to copyright    Report

Mathematical Programming Computation manuscript No.
(will be inserted by the editor)
CasADi A software framework for nonlinear optimization
and optimal control
Joel A. E. Andersson · Joris Gillis ·
Greg Horn · James B. Rawlings · Moritz Diehl
Received: date / Accepted: date
Abstract We present CasADi, an open-source software framework for numerical
optimization. CasADi is a general-purpose tool that can be used to model and solve
optimization problems with a large degree of flexibility, larger than what is associated
with popular algebraic modeling languages such as AMPL, GAMS, JuMP or Pyomo.
Of special interest are problems constrained by differential equations, i.e. optimal
control problems. CasADi is written in self-contained C++, but is most conveniently
used via full-featured interfaces to Python, MATLAB or Octave. Since its inception
in late 2009, it has been used successfully for academic teaching as well as in appli-
cations from multiple fields, including process control, robotics and aerospace. This
article gives an up-to-date and accessible introduction to the CasADi framework,
which has undergone numerous design improvements over the last seven years.
Keywords Optimization · Optimal control · Open source optimization software
Mathematics Subject Classification (2000) 90C99 · 93A30 · 97A01
J. A. E. Andersson
Department of Chemical and Biological Engineering, University of Wisconsin-Madison, USA
E-mail: jandersson@wisc.edu
J. Gillis
Production Engineering, Machine Design and Automation (PMA) Section, KU Leuven, Belgium
E-mail: joris.gillis@kuleuven.be
G. Horn
Kitty Hawk, Mountain View, USA
E-mail: greg@kittyhawk.aero
J. B. Rawlings
Department of Chemical and Biological Engineering, University of Wisconsin-Madison, USA
E-mail: james.rawlings@wisc.edu
M. Diehl
Department of Microsystems Engineering IMTEK, University of Freiburg, Germany
E-mail: moritz.diehl@imtek.uni-freiburg.de

2 Joel A. E. Andersson et al.
1 Introduction
CasADi is an open-source software for numerical optimization, offering an alterna-
tive to conventional algebraic modeling languages such as AMPL [50], Pyomo [69]
and JUMP [40]. Compared to these tools, the approach taken by CasADi, outlined
in this paper, is more flexible, but also lower-level, requiring an understanding of the
expression graphs the user is expected to construct as part of the modeling process.
This flexible approach is in particular valuable for problems constrained by dif-
ferential equations, introduced in the following.
1.1 Optimal control problems
Consider the f ollowing basic optimal control problem (OCP) in ordinary differential
equations (ODE):
minimize
x(·),u(·), p
Z
T
0
L(x(t),u(t), p)dt + E(x(T), p)
subject to
˙x(t) = f (x(t), u(t), p),
u(t) U , x(t) X ,
t [0,T ]
x(0) X
0
, x(T ) X
T
, p P,
(OCP)
where x(t) R
N
x
is the vector of (differential) states, u(t) R
N
u
is the vector of free
control signals and p R
N
p
is a vector of free parameters in the model. The OCP
here consists of a Lagrange term (L), a Mayer term (E), as well as an ODE with
initial (X
0
) and terminal (X
f
) conditions. Finally, there are admissible sets for the
states (X ), control (U ) and parameters (P). For simplicity, all sets can be assumed
to be simple intervals.
Problems of form (OCP) can be efficiently solved with the direct approach, where
(OCP) is transcribed into a nonlinear program (NLP):
minimize
w
J(w)
subject to g(w) = 0, w W ,
(NLP)
where w R
N
w
is the decision variable, J the objective function and W again taken
to be an interval set.
Popular direct methods include direct collocation [96, 97], which reached wide-
spread popularity through the work of Biegler and Cuthrell [21,31], and direct multi-
ple shooting by Bock and Plitt [23,22]. Both these methods exhibit good convergence
properties and can be easily parallelized.
Real-world optimal control problems are often significantly more general than
(OCP). Industrially relevant features include multi-stage formulations, i.e., different
dynamics in different parts of the time horizon, robust optimal control, problems
with integer variables, differential-algebraic equations (DAEs) instead of ODEs, and
multipoint constraints such as periodicity.

CasADi A software framework for nonlinear optimization and optimal control 3
1.2 Scope of CasADi
CasADi started out as a tool for algorithmic differentiation (AD) using a syntax sim-
ilar to a computer-algebra system (CAS), explaining its name. While state-of-the-art
AD is still a key feature of CasADi, the focus has since shifted towards optimization.
In its current form, CasADi provides a set of general-purpose building blocks that
drastically decreases the effort needed to implement a large set of algorithms for nu-
merical optimal control, without sacrificing efficiency. This “toolkit design” makes
CasADi suitable for teaching optimal control to graduate-level students and allows
researchers and industrial practitioners to write codes, with a modest programming
effort, customized to a particular application or problem structure.
1.3 Organization of the paper
The remainder of the paper is organized as follows. We start by introducing the reader
to CasADi’s symbolic core in Section 2. The key property of the symbolic core is
a state-of-the-art implementation of AD. Some unique features of CasADi are in-
troduced, including how sparse matrix-valued atomic operations can be used in a
source-code-transformation AD framework.
In Section 3, we show how systems of linear or nonlinear equations, as well as
initial-value problems in ODEs or DAEs, can be embedded into symbolic expres-
sions, while maintaining differentiability to arbitrary order. This automatic sensitiv-
ity analysis for ODEs and DAEs is, to the best knowledge of the authors, a unique
feature of CasADi.
Section 4 outlines how certain optimization problems in canonical form can be
solved with CasADi, including nonlinear programs (NLPs), linear programs (LPs)
and quadratic programs (QPs), potentially with a subset of the variables confined to
integer values, i.e. mixed-integer formulations. CasADi provides a common interface
for formulating such problems, while delegating the actual numerical solution to a
third-party solver, either free or commercial, or to an in-house solver, distributed
with CasADi.
Section 5 consists of a tutorial, showing some basic use cases of CasADi. The
tutorial mainly serves to illustrate the syntax, scope and usage of the tool.
Finally, Section 6 gives an overview of applications where CasADi has been suc-
cessfully used to date before Section 7 wraps up the paper.
2 Symbolic framework
The core of CasADi consists of a symbolic framework that allows users to construct
expressions and use these to define automatically differentiable functions. These
general-purpose expressions have no notion of optimization and are best likened with
expressions in e.g. MATLAB’s Symbolic Toolbox or Python’s SymPy package. Once
the expressions have been created, they can be used to efficiently obtain new expres-
sions for derivatives using AD or be evaluated efficiently, either in CasADi’s virtual
machines or by using CasADi to generate self-contained C code.

4 Joel A. E. Andersson et al.
We detail the symbolic framework in the following sections, where we also pro-
vide MATLAB/Octave and Python code snippets
1
corresponding to CasADi 3.1 be-
low in order to illustrate the f unctionality. For a self-contained and up-to-date walk-
through of CasADi’s syntax, we recommend the user guide [14].
2.1 Syntax and usage
CasADi uses a MATLAB inspired “everything-is-a-matrix” type syntax, i.e., scalars
are treated as 1-by-1 matrices and vectors as n-by-1 matrices. Furthermore, all ma-
trices are sparse and stored in the compressed column format. For a symbolic frame-
work like CasADi, working with a single sparse data type makes the tool easier to
learn and maintain. Since the linear algebra operations are typically called only once,
to construct symbolic expressions rather than to evaluate them numerically, the extra
overhead of e.g. treating a scalar as a 1-by-1 sparse matrix is negligible.
The following code demonstrates loading CasADi into the workspace, creating
two symbolic primitives x R
2
and A R
2×2
and finally the creation of an expression
for e := A sin(x):
% MATLAB / O c t ave
i m p o r t c a s a d i .
x = SX . sym ( x , 2 ) ;
A = SX . sym ( A , 2 , 2 ) ;
e = A s i n ( x ) ;
d i s p ( e )
# Pyth o n
from c a s a d i import
x = SX . sym ( x , 2 )
A = SX . sym ( A , 2 , 2 )
e = mtimes (A, s i n ( x ) )
p r i n t ( e )
Output: @1=sin(x_0), @2=sin(x_1),
[((A_0*@1)+(A_2*@2)), ((A_1*@1)+(A_3*@2))]
The output should be interpreted as the definition of two shared subexpressions,
@1 := sin(x
0
) and @2 := sin(x
1
) followed by an expression for the resulting col-
umn vector (2-by-1 matrix). The fact that CasADi expressions are allowed to contain
shared subexpressions is essential to be able to solve large-scale problems and for
CasADi to be able to implement AD as described in Section 2.6.
2.2 Graph representation Scalar expression type
In the code snippet above, we used CasADi’s scalar expression type - SX to con-
struct a symbolic expression. Scalar in this context does not refer to the type its elf
SX is a general sparse matrix type but the fact that each nonzero element is de-
fined by a sequence of scalar-valued operations. CasADi uses the compressed column
storage (CCS) [12] format to store matrices, the same used to represent sparse ma-
trices in MATLAB, but with the difference that in CasADi entries are allowed to be
structurally nonzero but numerically zero as illustr ated by the following:
1
One may run these snippets on a Windows, Linux or Mac platform by following the install instructions
for CasADi 3.1 at http://install31.casadi.org/

CasADi A software framework for nonlinear optimization and optimal control 5
% MATLAB / O c t a ve
C = SX . eye ( 2 ) ;
C ( 1 , 1 ) = SX . sym ( x ) ;
C ( 2 , 1 ) = 0 ;
d i s p (C )
# Pyth o n
C = SX . eye ( 2 )
C [ 0 , 0 ] = SX . sym ( x )
C [ 1 , 0 ] = 0
p r i n t ( C)
Output: [[x, 00],
[0, 1]]
Note the difference between structural zeros (denoted 00) and numerical zeros
(denoted 0). The fact that symbolic matrices are always sparse in CasADi stands in
contrast to e.g. MATLAB’s Symbolic Math Toolbox, where expressions are always
dense. Also note that CasADi has indices starting with 1 in MATLAB/Octave, but 0
in Python. In C++, CasADi also follows the index-0 convention.
When working with the SX type, expressions are stored as a directed acyclic graph
(DAG) where each node or atomic operation is either:
A symbolic primitive, created with SX.sym as above
A constant
A unary operation, e.g. sin
A binary operation, e.g. , +
This relatively simple graph representation is designed to allow numerical evalua-
tion with very little overhead, either in a virtual machine (Section 2.4) or in generated
C code (Section 2.5). Each operation also has a chain-rule that can efficiently be ex-
pressed with the other atomic operations.
2.3 Graph representation Matrix expression type
There is a second expression type in CasADi, the matrix expression type MX. For
this type, each operation is a matrix operation; an expression such as A + B where A
and B are n-by-m matrices would result in a single addition operation, in contrast to
up to mn scalar addition operations using the SX type. In the most general case, an MX
operation can have multiple matrix-valued inputs and return multiple matrix-valued
outputs.
The choice to implement two different expression types in CasADi and expose
them both to the end user is the result of a design compromise. It has proven difficult
to implement an expression type that works efficiently both for e.g. the right-hand-
side of an ODE, where minimal overhead is critical, and at the same time be able to
represent the very general symbolic expressions that make up the NLP resulting from
a direct multiple shooting discretization, which contains embedded ODE integrators.
The syntax of the MX type mirrors that of SX:

Citations
More filters
Posted Content

Enabling New Flexibility in the SUNDIALS Suite of Nonlinear and Differential/Algebraic Equation Solvers.

TL;DR: The SUNDIALS suite of nonlinear and DIfferential/ALgebraic equation solvers (SUNDIALs) as mentioned in this paper has been redesigned to better enable the use of application-specific and third-party algebraic solvers and data structures.
Posted Content

Neural Ordinary Differential Equations

TL;DR: In this paper, the authors introduce a new family of deep neural network models called continuous normalizing flows, which parameterize the derivative of the hidden state using a neural network, and the output of the network is computed using a black-box differential equation solver.
Proceedings ArticleDOI

Mini Cheetah: A Platform for Pushing the Limits of Dynamic Quadruped Control

TL;DR: The Mini Cheetah robot is used to execute 360° backflips, with trajectories generated using offline nonlinear optimization, and dynamic trot, trot-run, bounding, and pronking gaits on the robot to speeds of up to 2.45 meters per second using Convex Model-Predictive Control (cMPC).
Journal ArticleDOI

All you need to know about model predictive control for buildings

TL;DR: This paper provides a unified framework for model predictive building control technology with focus on the real-world applications and presents the essential components of a practical implementation of MPC such as different control architectures and nuances of communication infrastructures within supervisory control and data acquisition (SCADA) systems.
Journal ArticleDOI

Model Predictive Control of Power Electronic Systems: Methods, Results, and Challenges

TL;DR: This survey paper highlights the most relevant MPC techniques for power electronic systems and assesses the schemes in question in terms of design, and computational complexity, along with their performance, and applicability depending on the power electronic system at hand.
References
More filters
Book

Convex Optimization

TL;DR: In this article, the focus is on recognizing convex optimization problems and then finding the most appropriate technique for solving them, and a comprehensive introduction to the subject is given. But the focus of this book is not on the optimization problem itself, but on the problem of finding the appropriate technique to solve it.
Journal ArticleDOI

On the implementation of an interior-point filter line-search algorithm for large-scale nonlinear programming

TL;DR: A comprehensive description of the primal-dual interior-point algorithm with a filter line-search method for nonlinear programming is provided, including the feasibility restoration phase for the filter method, second-order corrections, and inertia correction of the KKT matrix.
Proceedings ArticleDOI

LLVM: a compilation framework for lifelong program analysis & transformation

TL;DR: The design of the LLVM representation and compiler framework is evaluated in three ways: the size and effectiveness of the representation, including the type information it provides; compiler performance for several interprocedural problems; and illustrative examples of the benefits LLVM provides for several challenging compiler problems.
Book

AMPL: A Modeling Language for Mathematical Programming

TL;DR: An efficient translator is implemented that takes as input a linear AMPL model and associated data, and produces output suitable for standard linear programming optimizers.
Book

Lapack Users' Guide

Ed Anderson
TL;DR: The third edition of LAPACK provided a guide to troubleshooting and installation of Routines, as well as providing examples of how to convert from LINPACK or EISPACK to BLAS.
Related Papers (5)
Frequently Asked Questions (16)
Q1. What have the authors contributed in "Casadi – a software framework for nonlinear optimization and optimal control" ?

The authors present CasADi, an open-source software framework for numerical optimization. This article gives an up-to-date and accessible introduction to the CasADi framework, which has undergone numerous design improvements over the last seven years. 

Conditional expressions, which include switches and if-statements, can be expressed using a dedicated Switch function object class in CasADi. 

In a framework such as CasADi, which is frequently used for rapid prototyping with many design iterations, fast VMs are important not only for numerical evaluation, but also for symbolic processing, which can make up a significant portion of the total solution time. 

A potential drawback, which is inherent with this so-called variational approach to sensitivity analysis, is that when used inside a gradient-based optimization code, the calculated derivatives may not be consistent with the nondifferentiated function evaluation, due to differences in time discretization. 

How a function class calculates directional derivatives is class-specific; e.g., an integrator node typically generates functions for directional derivatives by augmenting the ODE/DAE integration with its sensitivity equations. 

The linear solver class is a plugin class in CasADi, and leaves to the derived class – typically an interface to a third-party linear solver – to actually perform the factorization and solution. 

Once the expressions have been created, they can be used to efficiently obtain new expressions for derivatives using AD or be evaluated efficiently, either in CasADi’s virtual machines or by using CasADi to generate self-contained C code. 

sym ( ’ x ’ , 2 ) z=SX . sym ( ’ z ’ ) u=SX . sym ( ’ u ’ ) f = v e r t c a t ( z∗x [0]−x [ 1 ] + u , \\ x [ 0 ] ) g=x [1 ]∗∗2+ z−1 h=x [0 ]∗∗2+ x [1 ]∗∗2+ u∗∗2 dae= d i c t ( x=x , p=u , ode=f , \\z=z , a l g =g , quad=h )# C r e a t e s o l v e r i n s t a n c e T = 1 0 . 

To exploit this block sparse structure, CasADi implements a hierarchical sparsity pattern calculation algorithm based on graph coloring. 

Encapsulating subexpressions used multiple times into separate function objects allows expression graphs to stay small and has implications for the memory use in the context of algorithmic differentiation, cf. Section 2.6. 

For the type of Jacobians typically encountered in CasADi, both in simulation and optimization, the nonzeros are unlikely to be encountered in random locations. 

CasADi uses the 64-bit unsigned long long datatype for this, meaning that up to 64 rows or columns can be calculated in a single sparsity propagation sweep. 

sym ( ’ x ’ , 2 ) p=SX . sym ( ’ p ’ ) z=1−x [ 1 ]∗∗2 f = v e r t c a t ( z∗x [0]−x [ 1 ] + p , \\x [ 0 ] )dae= d i c t ( x=x , p=p , ode= f )# 

The linear solver instances are embedded into CasADi’s MX expression graphs using a dedicated linear solve node, which is similar to the more generic function call node introduced in Section 2.3. 

For this example, the sparsity pattern is thus recovered in 7 sweeps, which can be compared with ceil(100,000/64) = 1,563 sweeps needed for the naive algorithm, where 64 rows or columns are calculated in each sweep. 

Algorithm 1 Heuristic to calculate complete Jacobians in CasADiCalculate the Jacobian sparsity pattern if J is symmetric (typically a Hessian) thenRun a star-coloring algorithm.elseif