Matlab interface overhaul to match the Python interface in solver creation

Dear acados Matlab & Octave users :wave:

We are excited to announce significant updates to the Matlab and Octave interface of acados aiming to closely align it with the Python interface.
For now those changes are available on the branch acados/matlab_interface_overhaul and are planned to be released in acados v0.4.0.

Key updates

With the recent update in PR #1193, the problem formulation objects closely match the Python correspondents.
We hope for feedback on that from the acados community :family_man_woman_girl_boy:

For now those changes are available on the branch acados/matlab_interface_overhaul.
The changes are detailed and motivated below.

OCP solver creation

An acados OCP solver can now be created in different ways.
Option 1) is planned to be deprecated in a future release, option 2) is the preferred one for which new features will be added and option 3) is a way to transition to the new interface, which only requires minimal changes to existing code and will be supported longer than 1).

1) old / legacy style

Previously, an acados OCP solver in Matlab was created by calling:

% create OCP 
ocp_model = acados_ocp_model();
ocp_opts = acados_ocp_opts();
% set fields of ocp_model, ocp_opts using ocp_model.set(...)
ocp_solver = acados_ocp(ocp_model, ocp_opts);

The classes acados_ocp_model, acados_ocp_opts and acados_ocp are part of the legacy Matlab interface of acados.

This will still work in acados v0.4.*.
However, we plan on deprecating it and suggest for users to transition to the new interface 2), or the explicit translation to the new formulation object 3), both detailed below.

The differences in behavior are that:

  1. acados_ocp returns an AcadosOcpSolver object, which can be interacted with similarly to the old acados_ocp class.
  2. The AcadosOcpSolver does not have fields model_struct and opts_struct corresponding to the legacy interface, instead there is a field ocp of type AcadosOcp from which information about the problem and solver can be deduced.

2) new interface for OCP solver description

The new Matlab interface to describe OCP solvers for their creation can be used with the following syntax:

% OCP formulation
ocp = AcadosOcp();
% provide OCP formulation and solver options by setting properties
ocp_solver = AcadosOcpSolver(ocp);

An example for this can be found in the file new_minimal_example_ocp.m

Interactions with AcadosOcpSolver follow the same syntax as before, .set(), .get(), with the acados_ocp class.

3) transition to new interface

The easiest way to update your code to change your code from

ocp_solver = acados_ocp(ocp_model, ocp_opts);

to

ocp = setup_AcadosOcp_from_legacy_ocp_description(ocp_model, ocp_opts)
ocp_solver = AcadosOcpSolver(ocp);

This makes the translation of the old formulation objects into the new one more explicit.
This also helps users who want to transition to the new interface. One can compare the AcadosOcp objects obtained with the new interface with the one obtained by the function setup_AcadosOcp_from_legacy_ocp_description.

Integrator / solver for initial value problems creation

The changes done to the integrator interface are pretty much analogous to the one of the OCP solver.
An example can be found in the file new_minimal_example_sim.m

Background and Motivation

The classes AcadosOcp and subclasses AcadosModel, AcadosOcpConstraints, AcadosOcpCost, AcadosOcpDims, AcadosOcpOptions now completely match their Python correspondents, which are extensively documented.
The mismatch between some names between the Matlab and Python interface was causing trouble for both users and developers of acados.

These classes have existed in Matlab for a long time now, but were primarily used in the background.
Internally, there has been a translation function, similar to setup_AcadosOcp_from_legacy_ocp_description for a long time, since the existence of an acados Simulink interface, and more prominently, since the whole Matlab interface uses the template based solver as a backend, introduced in PR #934.

Lastly, this cleanup, will enable an extension of the Matlab interface to acados OCP solvers based on multi-phase OCP formulations, which we plan for the near future.

Beta testing

Your insights are invaluable in ensuring the robustness and user-friendliness of these updates.
Please check out the branch acados/matlab_interface_overhaul, and let us know if you encounter any issues by opening a new topic in the forum.

We look forward to your feedback and contributions. :pray:

Best,
Jonathan & Katrin

2 Likes

Short update on this:
Matlab interface overhaul by FreyJo · Pull Request #1193 · acados/acados · GitHub is merged and the changes announced here are now in the acados master branch and will be in the next release, i.e. the one after Release v0.3.6 · acados/acados · GitHub
Feedback is still highly appreciated!

To everyone who is new to acados or considers to transition to the new interface:
Please checkout the this list which keeps track of those examples that already use the new interface.

Some more hints on how to port existing code to the new MATLAB interface:

  • solver options:

    • strings are now caps lock, e.g. 'sqp_rti''SQP_RTI'
    • boolean options now use booleans (instead of 'true'/'false' strings)
    • for a full list of available options and their defaults compare the python documentation and the definition of the class AcadosOcpOptions
  • dynamics:

    • explicit integrators: provide fields AcadosModel.f_expl_expr and set AcadosOcpOptions.integrator_type = 'ERK'

    • implicit integrators: provide fields AcadosModel.f_impl_expr and AcadosModel.xdot and set AcadosOcpOptions.integrator_type = 'IRK' (or 'GNSF' or 'LIFTED_IRK')

  • constraints: instead of providing matrices J* for specifying which indices are constrained, the new interfaces requires you to provide arrays idx* explcitly providing 0-based indices

  • slack penalties Z* have to be provided as vectors (previously they were provided as matrices from which only the diagonal was taken)