Can I use automatic differentiation in fmincon?
Show older comments
I have a likelihood function that I'm solving in Matlab but it takes several hours to maximize using fmincon. I wondered if the package autodiff, or some Matlab package for automatic differentiation, could speed it up. If so, how can I use it with fmincon or some other constrained optimization command? There doesn't seem to be an automatic differentiation option in the fmincon documentation.
Accepted Answer
More Answers (2)
Shaun Forth
on 11 Sep 2017
1 vote
Hi Michael,
To the best of my knowledge the Matlab optimization toolbox still does not have automatic differentiation.
Some key questions are:
- How many inputs N do you have and how many constraints Mc? As you have a constrained problem (objective + constraints) you will have M=Mc+1 functions required to be differentiated. If M<<N then reverse (adjoint) mode AD would be preferred.
- Is your code vectorized? The effect of vectorization in your code may be huge. If you have a large loop then for overloaded AD each operation in the loop has to be overloaded and functions called each cycle of the loop. The overhead of calling the functions may be substantial. If that same loop were instead vectorized then just one function is called for each operation. For reverse mode, even source transformation AD tools are likely to store data to a so-called tape (a.k.a. stack) in order to perform the reverse pass through the code to compute derivatives. Loops will result in many calls of a function to store data, vectorized functions result in many fewer calls (though with more data stored each call).
For Matlab the operator-overloaded AD tools are relatively robust, my own tool MAD has been used for many years but only supports forward mode. It is also integrated within the TOMLAB optimization library. The source transformation tools may require more interaction with the developer to get the coverage of all the Matlab functions used in your code.
Regards Shaun
1 Comment
Michael Sikivie
on 11 Sep 2017
Steve Grikschat
on 8 Oct 2020
0 votes
Updated for R2020b
Optimization Toolbox now includes automatic differentiation for problems built with the problem-based workflow. Nonlinear constrained and unconstrained problems now solve with fmincon and fminunc using automatic differentiation.
This blog post by Alan Weiss explains it excellently
For more information, see the following
Categories
Find more on Solver Outputs and Iterative Display in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!