Tim-Salzmann / l4casadi

Use PyTorch Models with CasADi for data-driven optimization or learning-based optimal control. Supports Acados.
MIT License
368 stars 28 forks source link

Encounter issue when transfer l4c.L4CasADi to l4c.RealTimeL4CasADi #18

Closed LilHu7 closed 1 year ago

LilHu7 commented 1 year ago

Hi, Tim

Sorry for bother again.

I have been always using the l4c.L4CasADi(MultiLayerPerceptron()) to transfer torch.Tensor model to casadi symbolic one for SQP_RTI NLP solving. For accelerating the code computing, i want to transfer 2-order Taylor approximation to 1-order one. Therefore, the only change of the code is change 'learned_dyn_model = l4c.L4CasADi(nn, model_expects_batch_dim = True, name = 'learned_dyn')' to 'learned_dyn_model = l4c.RealTimeL4CasADi(nn, approximation_order=1)'.

The error reports are as follow: RuntimeError: Error in Function::Function for 'RealTime_nn_mpc_expl_ode_fun' [MXFunction] at .../casadi/core/function.cpp:249: .../casadi/core/function_internal.cpp:145: Error calling MXFunction::init for 'RealTime_nn_mpc_expl_ode_fun': .../casadi/core/mx_function.cpp:406: RealTime_nn_mpc_expl_ode_fun::init: Initialization failed since variables [f_a, df_a, a] are free. These symbols occur in the output expressions but you forgot to declare these as inputs. Set option 'allow_free' to allow free variables.

Would you please offer some solutions or demos about RealTimeL4CasADi object's usage for nlp solving?

Best regards, Hua

Tim-Salzmann commented 1 year ago

Hi Hua,

Thanks for reaching out! Unfortunately, switching from L4CasADi to RealTimeL4CasADi is not trivial. While they are part of the same framework, but fundamentally different approaches. L4CasADi does not make any approximation but will evaluate the function (and sensitivities: Jacobian, Hessian) at the input values.

RealTimeL4CasADi will assume a first or second-order approximation at a user-defined point (e.g. you will have to call get_params from the Python side every time you want to update the approximation). Thus you are responsible for updating the approximation depending on your use case (e.g. for Acados RTI before every solve). So far, there is no C(++) API for updating the approximation from pure C(++). If this is limiting you right now, I would have to think about whether this would be feasible.

You could have a look at the old ML-CasADi examples here [1]. ML-CasADi is fundamentally as RealTimeL4CasADi.

Let me know if this helps.

Best Tim

[1] https://github.com/TUM-AAS/ml-casadi/blob/master/examples/mpc_mlp_cnn_example.py

LilHu7 commented 1 year ago

Hi, Thanks for the quick reply! I will eventually transfer the whole solver coded by Python to a C++ project, through the t_render and generated *.pt files. Will the problem occur in such a framework?

So far, there is no C(++) API for updating the approximation from pure C(++). If this is limiting you right now, I would have to think about whether this would be feasible.

Best, Hua

LilHu7 commented 1 year ago

And I will try to use ml_casadi.torch to form the NN module based on the ML-CasADi.

You could have a look at the old ML-CasADi examples here [1]. ML-CasADi is fundamentally as RealTimeL4CasADi.

if it helps, i will response soon.

Tim-Salzmann commented 1 year ago

Hi Hua,

I will eventually transfer the whole solver coded by Python to a C++ project, through the t_render and generated *.pt files. Will the problem occur in such a framework?

As of now, this will only work with L4CasADi. To make this work with RealTimeL4CasADi (== MLCasADi) there would have to be adaptations and code changes.

Basically, libl4casadi would have to be re-used and extended to calculate the first/second order sensitivities with a C(++) API. Those sensitivity parameters would have to be set in Acados through C++ before running the RTI optimization step.

Best Tim


And I will try to use ml_casadi.torch to form the NN module based on the ML-CasADi.

I would suggest to use RealTimeL4CasADi. As of now, this has the same capabilities as ML-CasADi but will be maintained in the future while ML-CasADi will not.

LilHu7 commented 1 year ago

Hi, Tim

There may some vague understanding about L4CasADi in my mind.

Reference to the paper {Real-time Neural-MPC}, I have benn thought that the L4CasADi and Casadi construct the ODE function of the NN model (the NN without 1-order or 2-order approximation) and the linear squared cost function. Then, the Acados use the GAUSS_NEWTON method to get the Hessian of the cost function, the ERK method to discreatize the dynamics (need the 1-order sensitivity), the SQP_RTI solver type to form the SQP cost function. Then, solve and get the control output.

As of now, this will only work with L4CasADi. To make this work with RealTimeL4CasADi (== MLCasADi) there would have to be adaptations and code changes.

In this way, there is no approximation in 'l4CasADi', but 'RealTimeL4'. So far, there is no C(++) API for updating the approximation function in 'RealTimel4'. So that i can only use 'L4CasADi' to transform my project into c++ form right now, and this framework are with partial realization in that paper? (without Taylor approximation) Will it works with ML-CasADi?

Look forward to your reply! Thanks.

best regards, Hua

Tim-Salzmann commented 1 year ago

Hi Hua,

Reference to the paper {Real-time Neural-MPC}, I have benn thought that the L4CasADi and Casadi construct the ODE function of the NN model (the NN without 1-order or 2-order approximation) and the linear squared cost function.

This is correct with the minor correction that RealTimeL4CasADi (== MLCasADi) was used in this paper. L4CasADi did not exist at that point. So the optimization is and update of the approximation parameters was guided from within Python - not C++.

In this way, there is no approximation in 'l4CasADi', but 'RealTimeL4'.

Correct

So far, there is no C(++) API for updating the approximation function in 'RealTimel4'. So that i can only use 'L4CasADi' to transform my project into c++ form right now

Correct, unless you want to add these capabilities to RealTimeL4CasADi

and this framework are with partial realization in that paper?

In the paper, we use RealTimeL4CasADi from Python. The full code can be found here [1]

Will it works with ML-CasADi?

RealTimeL4CasADi is literally the same as ML-CasADi just merged into the L4CasADi package to be maintained together.

Hope this helps.

Best Tim

[1] https://github.com/TUM-AAS/neural-mpc

LilHu7 commented 1 year ago

Hi Tim

RealTimeL4CasADi is literally the same as ML-CasADi just merged into the L4CasADi package to be maintained together.

By this means, I can reconstruct the partial Python code of model transformation into ML-CasADi form, and updating the approximation function can be realized, right?

Tim-Salzmann commented 1 year ago

Hi Hua,

I am not sure I follow. An example of getting the approximation parameters and using this in the approximation in Python can be found here:

https://github.com/Tim-Salzmann/l4casadi/blob/c5390c302e0aa31103be078d9c6fe9b27a391e5a/l4casadi/realtime/examples/readme.py#L42-L43

As outlined there is currently no functionality implemented doing this in pure C(++).

Best Tim


The interaction with Acados can be seen here in the ML-CasADi example. For RealTimeL4CasADi approx_params has to be replaced with just params call.

https://github.com/TUM-AAS/ml-casadi/blob/6d4aae85526939edf7aa2c7966f6f1d36b8b7349/examples/mpc_mlp_cnn_example.py#L248-L249

Tim-Salzmann commented 1 year ago

Hi Hua,

I spend some time updating the READMEs on RealTimeL4CasADi and adding a example with Acados MPC.

Let me know if thsi clarifies things.

Best Tim

https://github.com/Tim-Salzmann/l4casadi/blob/main/README.md https://github.com/Tim-Salzmann/l4casadi/blob/main/l4casadi/realtime/README.md https://github.com/Tim-Salzmann/l4casadi/blob/main/examples/realtime/mpc_mlp_example.py

LilHu7 commented 1 year ago

Hi Tim,

Thanks for the helpful demos. The size of our model is not large. The naive method's computation efficiency is suitable right now.

Furthermore, we will continue to incorporate real-time approximation method into our project.

Best,

Hua