nicolapiccinelli / libmpc

libmpc++ is a C++ header-only library to solve linear and non-linear MPC
https://altairlab.gitlab.io/optcontrol/libmpc/
MIT License
137 stars 21 forks source link

Use with Unicycle Model #24

Closed nocholasrift closed 9 months ago

nocholasrift commented 9 months ago

Greetings! Great package, looks very interesting. I'm curious to know how easy it would be to setup this MPC to work with a unicycle model platform. Have you all tried with such a model? If so, was the MPC able to run at 10-15Hz? Is any linearization required?

Thanks!

nicolapiccinelli commented 9 months ago

Hello @nocholasrift, I’ve published a paper about motion planning of mobile robots using the library: https://ieeexplore.ieee.org/abstract/document/10178219

The implementation I did was using the non-linear MPC, and I was able to keep around 10/15hz. Of course, it depends on the number of constraints, the computation effort required, etc.

nocholasrift commented 9 months ago

Thanks for the follow up, Nicola, very helpful! Out of curiosity, is the source code for that non-linear mpc formulation with the gradients available publicly anywhere, or is it private?

nicolapiccinelli commented 9 months ago

The gradients in the non-linear MPC are automatically estimated using finite differences. Regarding the implementation, I have it on my private repository. If it makes sense, I can add a simplified version as an example.

nocholasrift commented 9 months ago

No worries, you have a pretty clear example of the NLMPC setup in the test folder, so I can go from there! I am curious though. From the NLOPT documentation: "If you find yourself computing the gradient by a finite-difference approximation (e.g. ∂f/∂x≈[f(x+Δx)−f(x−Δx)]/2Δx in one dimension), then you should probably use a derivative-free algorithm instead." I realize this library is developed as a general use MPC so it would not be feasible to implement in that way, but have you played around at all with using analytical gradients in the MPC formulations / noticed any speedups from doing so?

nicolapiccinelli commented 9 months ago

I played around with the analytical gradients but did not find a particular improvement in performance. The library uses Eigen extensively, and all the computations are already pretty optimized. I suggest using the static allocation and activating the optimization flags during the compilation (it makes a difference up to 4/5x in my experiments).

In general, I can tell you that, in my case, SLSQP works way better than any other gradient-free algos available in NLOPT. If you need, you can change the solver and see if it works better in your case.

I've been considering improving the gradient estimation for the nonlinear mpc and supporting more personalization (custom solvers, analytical gradient, etc.). I still haven't received any request or a particular need for my research.

nocholasrift commented 9 months ago

Gotcha, I'm surprised that is the case, but makes sense. SLSQP definitely seems like the way to go, from my own experience, none of the other algorithms compare in this use-case. Thanks for the discussion and quick follow-ups!