control-toolbox
control-toolbox copied to clipboard
Prototype/Request: Dynamic parameters in CppAD cost functions
This PR results from a conversation I had with Michael Neunert back in May via the CT Devs mail group. Sorry for the delay, I finally found the time to open a PR.
This is only a prototype, i.e. no merge is desired! I hope the problem and my workaround can help to develop a proper implementation in the future.
-
Problem: I'm running a MPC with a custom cost function where I want CppAD to generate the derivatives for me. However, in this cost function, environment data must be considered. This data is passive w.r.t. differentiation - CppAD does not need to actually generate derivative code for these variables but only use it in its generated code in such a way that it can be updated between optimization calls. However, this is currently not supported by CppADCodeGen.
-
Ideal solution: I was pointed to this issue over at CppADCodeGen's git repo by Michael. The feature itself appears to be available in CppAD; however, it's not yet available for code generation.
-
Workaround: As suggested by Michael, I've implemented a workaround which you can find in this PR. I've added a new
CostFunctionADParams
class which extends the state vector by the passive parameters. This addition is ignored by 1st and 2nd derivative extraction functions - therefore the parameters are passive as desired. They can be updated using the newsetCurrentParameters
function. -
Drawbacks: This workaround has some rather big drawbacks:
- Although no derivative information is extracted later, CppADCodeGen still generates the corresponding code. This results in larger executables and in very poor JIT-compilation times.
- Since the control toolbox currently only uses Eigen static matrices, the total size of the parameters is limited by the stack size.
- The resulting interface is very ugly, i.e. custom datatypes can not be used and everything must be mapped to a CppAD Scalar.
Although my workaround did the job for me on a smaller project, using it in the long-term is no option due to these drawbacks. I would really like to see this problem addressed at some point as I really enjoy using AD for the cost functions as it makes rapid-prototyping way easier.
Best regards,
Patrick
@neunertm, fyi!