# Solving efficient nonlinear Model Predictive Control problems with FalcOpt

FalcOpt stands for First-order Algorithm via Linearization of Constraints for OPTimization. It allows the user to solve nonlinear Model Predictive Control problems via a novel first-order algorithm presented in [1]. This tool works in Matlab and it allows one to automatically generate the C code to solve a custom-made control problem, together with the MEX interface. It heavily exploits sparsity of the problem and it is specifically indicated for time-critical applications.

The nonlinear Model Predictive Control problem that we aim to solve is:

where the index j spans the predicted horizon state x and input u in the horizon N. The bounds satisfy a < b componentwise and c > 0. are general nonlinear functions. The discrete-time dynamics, f, can be nonlinear and the nonlinear function n can be nonconvex. Besides the dynamics, all of the other constraints can be missing.

## Setting the problem

To solve the optimization problem presented above, first we run an offline code generation procedure which builds the required C (and possibly mex) functions. Then, the code is called in real-time at every time step k in a receding horizon fashion.

### Code generation

The code is generated via the following command:

`info = falcopt.generateCode(dynamics, N, nx, nu, objective);`

where:

• `dynamics` is a Matlab function handle containing the nonlinear discretized dynamics f;
• `N` is the prediction horizon
• `nx` is the state dimension
• `nu` is the input dimension
• `objective` is a Matlab structure with fields:
• `.Q` is the state cost weight
• `.R` is the input cost weight
• `.P` is the terminal state cost weight
2. general nonlinear cost function
• `.nonlinear` is the function
• `.nonlinearN` is the function
• `info` is a Matlab structure containing some general information about the problem (the expected number of FLOPS and the name or header of possible additional files).

The command `falcopt.generateCode` can be executed by specifying additional arguments, which allow one to consider e.g. additional constraints and their automatic differentiation, tolerances or other preferences. The syntax is:

`info = falcopt.generateCode(dynamics, N, nx, nu, objective, <option>, <value>, ..., <option>, <value>);`

where the additional options are given in Matlab by executing the command `help falcopt.generateCode`.

### Call the solver

Once that the solver is generated, the function is called in Matlab via the following command:

`[u, flag, info] = my_code(x0, u_sequence);`

where:

• 'my_code' or an user defined name is the mex function calling the homonym C file
• `x0` is the initial state
• `u_sequence` is a warm-start sequence of the N predicted inputs and additional arguments such as desired real-time references, terminal constraints parameters can be set in the code generation phase (check out Matlab `help falcopt.generateCode`).

The outputs are the following:

• `u` is the first input, result of the optimization problem, ready to be applied to the system
• `flag` is the exitflag of the optimization problem (>0 successful, 0 max number of iteration reached, <0 an error has occurred - the problem may be infeasible)
• `info` is a struct containing additional information, such as iterations, solve time, optimal value of the objective function and full predicted states and inputs. The amount of information can be selected during the code generation phase (check out Matlab `help falcopt.generateCode`).

#### Call the solver in Simulink

Another way to employ the solver is to generate a Simulink block. This is done by changing the option `buildTypes`:

`info = falcopt.generateCode(..., 'buildTypes', {'simulink'}, ...);`

FalcOpt builds automatically a Simulink block, interfacing the C file via S-Function. The block can be directly used in a simulation, for example:

## Credits

FalcOpt has been designed by Giampaolo Torrisi, Damian Frick and Tommaso Robbiani at the Automatic Control Laboratory, ETH Zurich, Switzerland. Scientific contributors to the work are Dr. Sergio Grammatico, Prof. Roy S. Smith and Prof. Manfred Morari. If you wish to use FalcOpt for your own research, please cite one of the following references:

@MISC{FalcOpt,
author = {Torrisi, Giampaolo and Frick, Damian and Robbiani, Tommaso and Grammatico, Sergio and Smith, Roy S. and Morari, Manfred},
title = {{FalcOpt: First-order Algorithm via Linearization of Constraints for OPTimization}},
howpublished = {{\nobreak{\url{https://github.com/torrisig/FalcOpt}}}},
month = {May},
year = {2017}
}

@article{FalcOpt_Theory,
author={Torrisi, Giampaolo and Grammatico, Sergio and Smith, Roy S. and Morari, Manfred},
title={A Projected Gradient and constraint linearization method for nonlinear model predictive control},
journal = {Under review with SIAM Journal on Control and Optimization (available online: \nobreak{\url{http://arxiv.org/abs/1610.06834}}) }
}

## FAQ

What compiler is required?

For the mex compilation, a C compiler must be installed and correctly interfaced to Matlab. Use the command `mex -setup` in Matlab to check your current compiler. The software was successfully tested in Matlab 2016b with the following compilers:

• Linux: GCC compiler
• Mac: clang compiler via XCode 7
• Windows: Microsoft SDK 7.1, Intel Parallel Studio XE 2016, Visual Studio C++ 2015
##### Clone this wiki locally
You can’t perform that action at this time.
Press h to open a hovercard with more details.