ethz-adrl / towr

A light-weight, Eigen-based C++ library for trajectory optimization for legged robots.
http://wiki.ros.org/towr
BSD 3-Clause "New" or "Revised" License
921 stars 230 forks source link

derivative check with IPOPT #83

Open junhyeokahn opened 3 years ago

junhyeokahn commented 3 years ago

Hello, First of all, thanks for maintaining this nice repo!

In the hopper_example.cc, I have added this line to check derivatives.

solver->SetOption("derivative_test", "first-order")
solver->SetOption("derivative_test_tol", 1e-3);

And I got the results as shown in the attached screenshot. Screenshot from 2021-01-14 11-54-59

where the constraints and variables are indexed as following: Screenshot from 2021-01-14 11-53-32

Most of the derivatives are very very close, but there were some jacobian elements that have a relatively bigger error between the analytically computed value and the numerically computed value. To which degree do you expect this comparison should be similar? Should they be exactly the same or ~0.01 error is inevitable and should be accepted?

Best, Junhyeok

justinberi commented 3 years ago

Hi, I am not a maintainer and don't have a specific answer but I might be able to give a little more information that might be helpful.

There is more information about what each term in the checker refers to at https://coin-or.github.io/Ipopt/SPECIALS.html. Changing the "derivative_test_perturbation" option will change the size of the errors and "derivative_test_tol" determines the cut-off for reporting the derivative is an error.

I believe the relative size of the error would depend on the non-linearity in the supplied Jacobian. This is because it is comparing to a first order (linear) estimate of the derivative. Thus the derivatives will not be the same. So treat it more as a test to see if you have implemented the analytic derivatives correctly.

With that in mind, I implemented a cart pole trajectory optimisation problem with IFOPT as the interface to IPOPT which is what is used in TOWR. The derivative checker there revealed errors in on the order of 1e-9 for a "derivative_test_perturbation" of 1e-8 and errors on the order of 1e-4 for a "derivative_test_perturbation" of 1e-4. But again, this is a different problem with different forms for the cost and constraints.

A final note: In my experience, using the analytic derivatives instead of a finite-difference method can provide 1 to 2 orders of magnitude reduction in solution time with IPOPT so regardless it is well worth defining the Jacobian analytically.

Thanks @awinkler for the awesome work in TOWR and IFOPT.