tiny_solver_autodiff_function_test.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 Google Inc. All rights reserved.
  3. // http://ceres-solver.org/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Google Inc. nor the names of its contributors may be
  14. // used to endorse or promote products derived from this software without
  15. // specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. // POSSIBILITY OF SUCH DAMAGE.
  28. //
  29. // Author: mierle@gmail.com (Keir Mierle)
  30. #include "ceres/tiny_solver_autodiff_function.h"
  31. #include <algorithm>
  32. #include <cmath>
  33. #include <limits>
  34. #include "ceres/tiny_solver.h"
  35. #include "ceres/tiny_solver_test_util.h"
  36. #include "gtest/gtest.h"
  37. namespace ceres {
  38. struct AutoDiffTestFunctor {
  39. template <typename T>
  40. bool operator()(const T* const parameters, T* residuals) const {
  41. // Shift the parameters so the solution is not at the origin, to prevent
  42. // accidentally showing "PASS".
  43. const T& a = parameters[0] - T(1.0);
  44. const T& b = parameters[1] - T(2.0);
  45. const T& c = parameters[2] - T(3.0);
  46. residuals[0] = 2. * a + 0. * b + 1. * c;
  47. residuals[1] = 0. * a + 4. * b + 6. * c;
  48. return true;
  49. }
  50. };
  51. // Leave a factor of 10 slop since these tests tend to mysteriously break on
  52. // other compilers or architectures if the tolerance is too tight.
  53. static double const kTolerance = std::numeric_limits<double>::epsilon() * 10;
  54. TEST(TinySolverAutoDiffFunction, SimpleFunction) {
  55. using AutoDiffTestFunction =
  56. TinySolverAutoDiffFunction<AutoDiffTestFunctor, 2, 3>;
  57. AutoDiffTestFunctor autodiff_test_functor;
  58. AutoDiffTestFunction f(autodiff_test_functor);
  59. Eigen::Vector3d x(2.0, 1.0, 4.0);
  60. Eigen::Vector2d residuals;
  61. // Check the case with cost-only evaluation.
  62. residuals.setConstant(555); // Arbitrary.
  63. EXPECT_TRUE(f(&x(0), &residuals(0), nullptr));
  64. EXPECT_NEAR(3.0, residuals(0), kTolerance);
  65. EXPECT_NEAR(2.0, residuals(1), kTolerance);
  66. // Check the case with cost and Jacobian evaluation.
  67. Eigen::Matrix<double, 2, 3> jacobian;
  68. residuals.setConstant(555); // Arbitrary.
  69. jacobian.setConstant(555);
  70. EXPECT_TRUE(f(&x(0), &residuals(0), &jacobian(0, 0)));
  71. // Verify cost.
  72. EXPECT_NEAR(3.0, residuals(0), kTolerance);
  73. EXPECT_NEAR(2.0, residuals(1), kTolerance);
  74. // Verify Jacobian Row 1.
  75. EXPECT_NEAR(2.0, jacobian(0, 0), kTolerance);
  76. EXPECT_NEAR(0.0, jacobian(0, 1), kTolerance);
  77. EXPECT_NEAR(1.0, jacobian(0, 2), kTolerance);
  78. // Verify Jacobian row 2.
  79. EXPECT_NEAR(0.0, jacobian(1, 0), kTolerance);
  80. EXPECT_NEAR(4.0, jacobian(1, 1), kTolerance);
  81. EXPECT_NEAR(6.0, jacobian(1, 2), kTolerance);
  82. }
  83. class DynamicResidualsFunctor {
  84. public:
  85. using Scalar = double;
  86. enum {
  87. NUM_RESIDUALS = Eigen::Dynamic,
  88. NUM_PARAMETERS = 3,
  89. };
  90. int NumResiduals() const { return 2; }
  91. template <typename T>
  92. bool operator()(const T* parameters, T* residuals) const {
  93. // Jacobian is not evaluated by cost function, but by autodiff.
  94. T* jacobian = nullptr;
  95. return EvaluateResidualsAndJacobians(parameters, residuals, jacobian);
  96. }
  97. };
  98. template <typename Function, typename Vector>
  99. void TestHelper(const Function& f, const Vector& x0) {
  100. Vector x = x0;
  101. Eigen::Vector2d residuals;
  102. f(x.data(), residuals.data(), nullptr);
  103. EXPECT_GT(residuals.squaredNorm() / 2.0, 1e-10);
  104. TinySolver<Function> solver;
  105. solver.Solve(f, &x);
  106. EXPECT_NEAR(0.0, solver.summary.final_cost, 1e-10);
  107. }
  108. // A test case for when the number of residuals is
  109. // dynamically sized and we use autodiff
  110. TEST(TinySolverAutoDiffFunction, ResidualsDynamicAutoDiff) {
  111. Eigen::Vector3d x0(0.76026643, -30.01799744, 0.55192142);
  112. DynamicResidualsFunctor f;
  113. using AutoDiffCostFunctor = ceres::
  114. TinySolverAutoDiffFunction<DynamicResidualsFunctor, Eigen::Dynamic, 3>;
  115. AutoDiffCostFunctor f_autodiff(f);
  116. Eigen::Vector2d residuals;
  117. f_autodiff(x0.data(), residuals.data(), nullptr);
  118. EXPECT_GT(residuals.squaredNorm() / 2.0, 1e-10);
  119. TinySolver<AutoDiffCostFunctor> solver;
  120. solver.Solve(f_autodiff, &x0);
  121. EXPECT_NEAR(0.0, solver.summary.final_cost, 1e-10);
  122. }
  123. } // namespace ceres