autodiff_cost_function_test.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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: sameeragarwal@google.com (Sameer Agarwal)
  30. #include "ceres/autodiff_cost_function.h"
  31. #include <memory>
  32. #include "ceres/array_utils.h"
  33. #include "ceres/cost_function.h"
  34. #include "gtest/gtest.h"
  35. namespace ceres::internal {
  36. class BinaryScalarCost {
  37. public:
  38. explicit BinaryScalarCost(double a) : a_(a) {}
  39. template <typename T>
  40. bool operator()(const T* const x, const T* const y, T* cost) const {
  41. cost[0] = x[0] * y[0] + x[1] * y[1] - T(a_);
  42. return true;
  43. }
  44. private:
  45. double a_;
  46. };
  47. TEST(AutodiffCostFunction, BilinearDifferentiationTest) {
  48. CostFunction* cost_function =
  49. new AutoDiffCostFunction<BinaryScalarCost, 1, 2, 2>(
  50. new BinaryScalarCost(1.0));
  51. auto** parameters = new double*[2];
  52. parameters[0] = new double[2];
  53. parameters[1] = new double[2];
  54. parameters[0][0] = 1;
  55. parameters[0][1] = 2;
  56. parameters[1][0] = 3;
  57. parameters[1][1] = 4;
  58. auto** jacobians = new double*[2];
  59. jacobians[0] = new double[2];
  60. jacobians[1] = new double[2];
  61. double residuals = 0.0;
  62. cost_function->Evaluate(parameters, &residuals, nullptr);
  63. EXPECT_EQ(10.0, residuals);
  64. cost_function->Evaluate(parameters, &residuals, jacobians);
  65. EXPECT_EQ(10.0, residuals);
  66. EXPECT_EQ(3, jacobians[0][0]);
  67. EXPECT_EQ(4, jacobians[0][1]);
  68. EXPECT_EQ(1, jacobians[1][0]);
  69. EXPECT_EQ(2, jacobians[1][1]);
  70. delete[] jacobians[0];
  71. delete[] jacobians[1];
  72. delete[] parameters[0];
  73. delete[] parameters[1];
  74. delete[] jacobians;
  75. delete[] parameters;
  76. delete cost_function;
  77. }
  78. struct TenParameterCost {
  79. template <typename T>
  80. bool operator()(const T* const x0,
  81. const T* const x1,
  82. const T* const x2,
  83. const T* const x3,
  84. const T* const x4,
  85. const T* const x5,
  86. const T* const x6,
  87. const T* const x7,
  88. const T* const x8,
  89. const T* const x9,
  90. T* cost) const {
  91. cost[0] = *x0 + *x1 + *x2 + *x3 + *x4 + *x5 + *x6 + *x7 + *x8 + *x9;
  92. return true;
  93. }
  94. };
  95. TEST(AutodiffCostFunction, ManyParameterAutodiffInstantiates) {
  96. CostFunction* cost_function =
  97. new AutoDiffCostFunction<TenParameterCost,
  98. 1,
  99. 1,
  100. 1,
  101. 1,
  102. 1,
  103. 1,
  104. 1,
  105. 1,
  106. 1,
  107. 1,
  108. 1>(new TenParameterCost);
  109. auto** parameters = new double*[10];
  110. auto** jacobians = new double*[10];
  111. for (int i = 0; i < 10; ++i) {
  112. parameters[i] = new double[1];
  113. parameters[i][0] = i;
  114. jacobians[i] = new double[1];
  115. }
  116. double residuals = 0.0;
  117. cost_function->Evaluate(parameters, &residuals, nullptr);
  118. EXPECT_EQ(45.0, residuals);
  119. cost_function->Evaluate(parameters, &residuals, jacobians);
  120. EXPECT_EQ(residuals, 45.0);
  121. for (int i = 0; i < 10; ++i) {
  122. EXPECT_EQ(1.0, jacobians[i][0]);
  123. }
  124. for (int i = 0; i < 10; ++i) {
  125. delete[] jacobians[i];
  126. delete[] parameters[i];
  127. }
  128. delete[] jacobians;
  129. delete[] parameters;
  130. delete cost_function;
  131. }
  132. struct OnlyFillsOneOutputFunctor {
  133. template <typename T>
  134. bool operator()(const T* x, T* output) const {
  135. output[0] = x[0];
  136. return true;
  137. }
  138. };
  139. TEST(AutoDiffCostFunction, PartiallyFilledResidualShouldFailEvaluation) {
  140. double parameter = 1.0;
  141. double jacobian[2];
  142. double residuals[2];
  143. double* parameters[] = {&parameter};
  144. double* jacobians[] = {jacobian};
  145. std::unique_ptr<CostFunction> cost_function(
  146. new AutoDiffCostFunction<OnlyFillsOneOutputFunctor, 2, 1>(
  147. new OnlyFillsOneOutputFunctor));
  148. InvalidateArray(2, jacobian);
  149. InvalidateArray(2, residuals);
  150. EXPECT_TRUE(cost_function->Evaluate(parameters, residuals, jacobians));
  151. EXPECT_FALSE(IsArrayValid(2, jacobian));
  152. EXPECT_FALSE(IsArrayValid(2, residuals));
  153. }
  154. } // namespace ceres::internal