Formulate and generate OCP in Matlab. Load this OCP in Python

Hi :wave:

  • What interface of acados are you using (C/ Matlab/ Python/ Octave)?
    I am using both Matlab and Python interfaces.

  • What do you want to achieve?
    Is it possible to generate the C code of an OCP in Matlab then load the ocp solver in Python using the create_cython_solver_method

  • If there is a specific thing you tried that you are not sure of, add a formatted
    I need to use an Acados OCP within Python. This Acados OCP is already developed / tested in Matlab. I want to load this Acados OCP so that I can use this with Python.

Thank you :pray:

Hi,

this is not officially supported yet.
However, it is possible without too many modifications.
I made a minimal example here:

It would be better to actually load all (or most) information from the json file and set up the AcadosOcp as well.
A contribution in this direction is very welcome.
I am not sure when I will continue to work on this.

Best,
Jonathan

Hi Jonathan,

Many thanks for making a pull request so fast!

I came up with my own solution yesterday that helped me load the OCP solver in python:

  • To create the required Cython bindings we need the acados_solver.pxd file.
  • I used the corresponding tera template and the function (acados.utils.render_template) to generate this file
  • I then compiled the code using the already available make file. This create the requisite python bindings that can be loaded using AcadosOcpSolver.create_cython_solver(json_file=json_path)

I am able to load the OCP solver now. While it is working in some of the test scenarios, it is failing in others. I am therefore not sure if this is indeed the correct method to do this.

I will try to do this using your pull request.

The entire code to generate the .pyd file is below:

from acados_template.utils import render_template

tup = ('acados_solver.in.pxd', 'acados_solver.pxd')
out_dir = '/home/agilicious/catkin_ws/c_generated_code_num_drones_is_3'
json_path = '/home/agilicious/catkin_ws/acados_ocp_nlp.json'
render_template(tup[0], tup[1], out_dir, json_path, None)

Thanks!

1 Like

While it is working in some of the test scenarios, it is failing in others. I am therefore not sure if this is indeed the correct method to do this.

Is there something specific that goes wrong?
I worked on proper code reuse between MATLAB and python in Export OCP solver in Python and use it in Matlab by FreyJo ยท Pull Request #1472 ยท acados/acados ยท GitHub
Let me know, if you have any further issues in this regard.

Best,
Jonathan

Hi Jonathon,

Thanks a lot for adding this functionality!

I can confirm that both your and my methods are working equally well for me.
(The issue I mentioned in my last post was some bug in my code and not due to Acados)