Run minimal_example_mhe from C++


I would like to use the Python auto generated code for a MHE solver directly from C++. As it was stated in MHE using algebraic variables, I was able to solve my problem using the Python interface thanks to the help provided here. However, I couldn’t use the auto generated solver from C++. So, I started from scratch with to see if I could achieve that task.

After digging around with the code, I was able to create a CMake project where the mhe_pendulum_ode_test.cpp file uses the auto generated code to solve the same problem that was solved using Python. One detail that is bugging me is that the errors between the true and the estimated states are a little different from the Python version.

The code is in this repository mhe_acados_cpp. You can try it by doing the following

  1. Edit CMakeLists.txt to fit your acados installation.
  2. Go to solver/mhe folder and run python3 to get the auto generated code.
  3. Go back to the project root, create a build directory and run cmake and make to get an executable
mkdir build
cd build
cmake ..
  1. After runing the executable you should have a text file called simXest.txt inside the build directory that contains the values of the MHE estimated states obtained using C++.
  2. Go into the src directory and run python3 to compare.

Could anyone point me if I’m missing something in the C++ code? It took me forever to realize how to set the parameters p (hint: it was the acados_update_params function).

Thanks in advance!


I had a quick look.

Was this your main issue?
I reproduced the following:

Native Python:

difference |x0_est - x0_bar| 6.475980809298076e-12
difference |x_est - x_true| 1.5647168350951527e-09

Exported CPP:

difference |x0_est - x0_bar| 2.3906595541638575e-07
difference |x_est - x_true| 2.8829698023581986e-07

Exported CPP with more accuracy -> 1 more iteration.

difference |x0_est - x0_bar| 1.1366692365338366e-15
difference |x_est - x_true| 7.431582678349519e-15

I guess some initialization of the solvers is different here, but in general this seems fine to me.