dynamic_numeric_diff_cost_function_test.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2022 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. // mierle@gmail.com (Keir Mierle)
  31. #include "ceres/dynamic_numeric_diff_cost_function.h"
  32. #include <cstddef>
  33. #include <memory>
  34. #include <vector>
  35. #include "gtest/gtest.h"
  36. namespace ceres::internal {
  37. const double kTolerance = 1e-6;
  38. // Takes 2 parameter blocks:
  39. // parameters[0] is size 10.
  40. // parameters[1] is size 5.
  41. // Emits 21 residuals:
  42. // A: i - parameters[0][i], for i in [0,10) -- this is 10 residuals
  43. // B: parameters[0][i] - i, for i in [0,10) -- this is another 10.
  44. // C: sum(parameters[0][i]^2 - 8*parameters[0][i]) + sum(parameters[1][i])
  45. class MyCostFunctor {
  46. public:
  47. bool operator()(double const* const* parameters, double* residuals) const {
  48. const double* params0 = parameters[0];
  49. int r = 0;
  50. for (int i = 0; i < 10; ++i) {
  51. residuals[r++] = i - params0[i];
  52. residuals[r++] = params0[i] - i;
  53. }
  54. double c_residual = 0.0;
  55. for (int i = 0; i < 10; ++i) {
  56. c_residual += pow(params0[i], 2) - 8.0 * params0[i];
  57. }
  58. const double* params1 = parameters[1];
  59. for (int i = 0; i < 5; ++i) {
  60. c_residual += params1[i];
  61. }
  62. residuals[r++] = c_residual;
  63. return true;
  64. }
  65. };
  66. TEST(DynamicNumericdiffCostFunctionTest, TestResiduals) {
  67. std::vector<double> param_block_0(10, 0.0);
  68. std::vector<double> param_block_1(5, 0.0);
  69. DynamicNumericDiffCostFunction<MyCostFunctor> cost_function(
  70. new MyCostFunctor());
  71. cost_function.AddParameterBlock(param_block_0.size());
  72. cost_function.AddParameterBlock(param_block_1.size());
  73. cost_function.SetNumResiduals(21);
  74. // Test residual computation.
  75. std::vector<double> residuals(21, -100000);
  76. std::vector<double*> parameter_blocks(2);
  77. parameter_blocks[0] = &param_block_0[0];
  78. parameter_blocks[1] = &param_block_1[0];
  79. EXPECT_TRUE(
  80. cost_function.Evaluate(&parameter_blocks[0], residuals.data(), nullptr));
  81. for (int r = 0; r < 10; ++r) {
  82. EXPECT_EQ(1.0 * r, residuals.at(r * 2));
  83. EXPECT_EQ(-1.0 * r, residuals.at(r * 2 + 1));
  84. }
  85. EXPECT_EQ(0, residuals.at(20));
  86. }
  87. TEST(DynamicNumericdiffCostFunctionTest, TestJacobian) {
  88. // Test the residual counting.
  89. std::vector<double> param_block_0(10, 0.0);
  90. for (int i = 0; i < 10; ++i) {
  91. param_block_0[i] = 2 * i;
  92. }
  93. std::vector<double> param_block_1(5, 0.0);
  94. DynamicNumericDiffCostFunction<MyCostFunctor> cost_function(
  95. new MyCostFunctor());
  96. cost_function.AddParameterBlock(param_block_0.size());
  97. cost_function.AddParameterBlock(param_block_1.size());
  98. cost_function.SetNumResiduals(21);
  99. // Prepare the residuals.
  100. std::vector<double> residuals(21, -100000);
  101. // Prepare the parameters.
  102. std::vector<double*> parameter_blocks(2);
  103. parameter_blocks[0] = &param_block_0[0];
  104. parameter_blocks[1] = &param_block_1[0];
  105. // Prepare the jacobian.
  106. std::vector<std::vector<double>> jacobian_vect(2);
  107. jacobian_vect[0].resize(21 * 10, -100000);
  108. jacobian_vect[1].resize(21 * 5, -100000);
  109. std::vector<double*> jacobian;
  110. jacobian.push_back(jacobian_vect[0].data());
  111. jacobian.push_back(jacobian_vect[1].data());
  112. // Test jacobian computation.
  113. EXPECT_TRUE(cost_function.Evaluate(
  114. parameter_blocks.data(), residuals.data(), jacobian.data()));
  115. for (int r = 0; r < 10; ++r) {
  116. EXPECT_EQ(-1.0 * r, residuals.at(r * 2));
  117. EXPECT_EQ(+1.0 * r, residuals.at(r * 2 + 1));
  118. }
  119. EXPECT_EQ(420, residuals.at(20));
  120. for (int p = 0; p < 10; ++p) {
  121. // Check "A" Jacobian.
  122. EXPECT_NEAR(-1.0, jacobian_vect[0][2 * p * 10 + p], kTolerance);
  123. // Check "B" Jacobian.
  124. EXPECT_NEAR(+1.0, jacobian_vect[0][(2 * p + 1) * 10 + p], kTolerance);
  125. jacobian_vect[0][2 * p * 10 + p] = 0.0;
  126. jacobian_vect[0][(2 * p + 1) * 10 + p] = 0.0;
  127. }
  128. // Check "C" Jacobian for first parameter block.
  129. for (int p = 0; p < 10; ++p) {
  130. EXPECT_NEAR(4 * p - 8, jacobian_vect[0][20 * 10 + p], kTolerance);
  131. jacobian_vect[0][20 * 10 + p] = 0.0;
  132. }
  133. for (double entry : jacobian_vect[0]) {
  134. EXPECT_NEAR(0.0, entry, kTolerance);
  135. }
  136. // Check "C" Jacobian for second parameter block.
  137. for (int p = 0; p < 5; ++p) {
  138. EXPECT_NEAR(1.0, jacobian_vect[1][20 * 5 + p], kTolerance);
  139. jacobian_vect[1][20 * 5 + p] = 0.0;
  140. }
  141. for (double entry : jacobian_vect[1]) {
  142. EXPECT_NEAR(0.0, entry, kTolerance);
  143. }
  144. }
  145. TEST(DynamicNumericdiffCostFunctionTest,
  146. JacobianWithFirstParameterBlockConstant) { // NOLINT
  147. // Test the residual counting.
  148. std::vector<double> param_block_0(10, 0.0);
  149. for (int i = 0; i < 10; ++i) {
  150. param_block_0[i] = 2 * i;
  151. }
  152. std::vector<double> param_block_1(5, 0.0);
  153. DynamicNumericDiffCostFunction<MyCostFunctor> cost_function(
  154. new MyCostFunctor());
  155. cost_function.AddParameterBlock(param_block_0.size());
  156. cost_function.AddParameterBlock(param_block_1.size());
  157. cost_function.SetNumResiduals(21);
  158. // Prepare the residuals.
  159. std::vector<double> residuals(21, -100000);
  160. // Prepare the parameters.
  161. std::vector<double*> parameter_blocks(2);
  162. parameter_blocks[0] = &param_block_0[0];
  163. parameter_blocks[1] = &param_block_1[0];
  164. // Prepare the jacobian.
  165. std::vector<std::vector<double>> jacobian_vect(2);
  166. jacobian_vect[0].resize(21 * 10, -100000);
  167. jacobian_vect[1].resize(21 * 5, -100000);
  168. std::vector<double*> jacobian;
  169. jacobian.push_back(nullptr);
  170. jacobian.push_back(jacobian_vect[1].data());
  171. // Test jacobian computation.
  172. EXPECT_TRUE(cost_function.Evaluate(
  173. parameter_blocks.data(), residuals.data(), jacobian.data()));
  174. for (int r = 0; r < 10; ++r) {
  175. EXPECT_EQ(-1.0 * r, residuals.at(r * 2));
  176. EXPECT_EQ(+1.0 * r, residuals.at(r * 2 + 1));
  177. }
  178. EXPECT_EQ(420, residuals.at(20));
  179. // Check "C" Jacobian for second parameter block.
  180. for (int p = 0; p < 5; ++p) {
  181. EXPECT_NEAR(1.0, jacobian_vect[1][20 * 5 + p], kTolerance);
  182. jacobian_vect[1][20 * 5 + p] = 0.0;
  183. }
  184. for (double& i : jacobian_vect[1]) {
  185. EXPECT_EQ(0.0, i);
  186. }
  187. }
  188. TEST(DynamicNumericdiffCostFunctionTest,
  189. JacobianWithSecondParameterBlockConstant) { // NOLINT
  190. // Test the residual counting.
  191. std::vector<double> param_block_0(10, 0.0);
  192. for (int i = 0; i < 10; ++i) {
  193. param_block_0[i] = 2 * i;
  194. }
  195. std::vector<double> param_block_1(5, 0.0);
  196. DynamicNumericDiffCostFunction<MyCostFunctor> cost_function(
  197. new MyCostFunctor());
  198. cost_function.AddParameterBlock(param_block_0.size());
  199. cost_function.AddParameterBlock(param_block_1.size());
  200. cost_function.SetNumResiduals(21);
  201. // Prepare the residuals.
  202. std::vector<double> residuals(21, -100000);
  203. // Prepare the parameters.
  204. std::vector<double*> parameter_blocks(2);
  205. parameter_blocks[0] = &param_block_0[0];
  206. parameter_blocks[1] = &param_block_1[0];
  207. // Prepare the jacobian.
  208. std::vector<std::vector<double>> jacobian_vect(2);
  209. jacobian_vect[0].resize(21 * 10, -100000);
  210. jacobian_vect[1].resize(21 * 5, -100000);
  211. std::vector<double*> jacobian;
  212. jacobian.push_back(jacobian_vect[0].data());
  213. jacobian.push_back(nullptr);
  214. // Test jacobian computation.
  215. EXPECT_TRUE(cost_function.Evaluate(
  216. parameter_blocks.data(), residuals.data(), jacobian.data()));
  217. for (int r = 0; r < 10; ++r) {
  218. EXPECT_EQ(-1.0 * r, residuals.at(r * 2));
  219. EXPECT_EQ(+1.0 * r, residuals.at(r * 2 + 1));
  220. }
  221. EXPECT_EQ(420, residuals.at(20));
  222. for (int p = 0; p < 10; ++p) {
  223. // Check "A" Jacobian.
  224. EXPECT_NEAR(-1.0, jacobian_vect[0][2 * p * 10 + p], kTolerance);
  225. // Check "B" Jacobian.
  226. EXPECT_NEAR(+1.0, jacobian_vect[0][(2 * p + 1) * 10 + p], kTolerance);
  227. jacobian_vect[0][2 * p * 10 + p] = 0.0;
  228. jacobian_vect[0][(2 * p + 1) * 10 + p] = 0.0;
  229. }
  230. // Check "C" Jacobian for first parameter block.
  231. for (int p = 0; p < 10; ++p) {
  232. EXPECT_NEAR(4 * p - 8, jacobian_vect[0][20 * 10 + p], kTolerance);
  233. jacobian_vect[0][20 * 10 + p] = 0.0;
  234. }
  235. for (double& i : jacobian_vect[0]) {
  236. EXPECT_EQ(0.0, i);
  237. }
  238. }
  239. // Takes 3 parameter blocks:
  240. // parameters[0] (x) is size 1.
  241. // parameters[1] (y) is size 2.
  242. // parameters[2] (z) is size 3.
  243. // Emits 7 residuals:
  244. // A: x[0] (= sum_x)
  245. // B: y[0] + 2.0 * y[1] (= sum_y)
  246. // C: z[0] + 3.0 * z[1] + 6.0 * z[2] (= sum_z)
  247. // D: sum_x * sum_y
  248. // E: sum_y * sum_z
  249. // F: sum_x * sum_z
  250. // G: sum_x * sum_y * sum_z
  251. class MyThreeParameterCostFunctor {
  252. public:
  253. template <typename T>
  254. bool operator()(T const* const* parameters, T* residuals) const {
  255. const T* x = parameters[0];
  256. const T* y = parameters[1];
  257. const T* z = parameters[2];
  258. T sum_x = x[0];
  259. T sum_y = y[0] + 2.0 * y[1];
  260. T sum_z = z[0] + 3.0 * z[1] + 6.0 * z[2];
  261. residuals[0] = sum_x;
  262. residuals[1] = sum_y;
  263. residuals[2] = sum_z;
  264. residuals[3] = sum_x * sum_y;
  265. residuals[4] = sum_y * sum_z;
  266. residuals[5] = sum_x * sum_z;
  267. residuals[6] = sum_x * sum_y * sum_z;
  268. return true;
  269. }
  270. };
  271. class ThreeParameterCostFunctorTest : public ::testing::Test {
  272. protected:
  273. void SetUp() final {
  274. // Prepare the parameters.
  275. x_.resize(1);
  276. x_[0] = 0.0;
  277. y_.resize(2);
  278. y_[0] = 1.0;
  279. y_[1] = 3.0;
  280. z_.resize(3);
  281. z_[0] = 2.0;
  282. z_[1] = 4.0;
  283. z_[2] = 6.0;
  284. parameter_blocks_.resize(3);
  285. parameter_blocks_[0] = &x_[0];
  286. parameter_blocks_[1] = &y_[0];
  287. parameter_blocks_[2] = &z_[0];
  288. // Prepare the cost function.
  289. using DynamicMyThreeParameterCostFunction =
  290. DynamicNumericDiffCostFunction<MyThreeParameterCostFunctor>;
  291. auto cost_function = std::make_unique<DynamicMyThreeParameterCostFunction>(
  292. new MyThreeParameterCostFunctor());
  293. cost_function->AddParameterBlock(1);
  294. cost_function->AddParameterBlock(2);
  295. cost_function->AddParameterBlock(3);
  296. cost_function->SetNumResiduals(7);
  297. cost_function_ = std::move(cost_function);
  298. // Setup jacobian data.
  299. jacobian_vect_.resize(3);
  300. jacobian_vect_[0].resize(7 * x_.size(), -100000);
  301. jacobian_vect_[1].resize(7 * y_.size(), -100000);
  302. jacobian_vect_[2].resize(7 * z_.size(), -100000);
  303. // Prepare the expected residuals.
  304. const double sum_x = x_[0];
  305. const double sum_y = y_[0] + 2.0 * y_[1];
  306. const double sum_z = z_[0] + 3.0 * z_[1] + 6.0 * z_[2];
  307. expected_residuals_.resize(7);
  308. expected_residuals_[0] = sum_x;
  309. expected_residuals_[1] = sum_y;
  310. expected_residuals_[2] = sum_z;
  311. expected_residuals_[3] = sum_x * sum_y;
  312. expected_residuals_[4] = sum_y * sum_z;
  313. expected_residuals_[5] = sum_x * sum_z;
  314. expected_residuals_[6] = sum_x * sum_y * sum_z;
  315. // Prepare the expected jacobian entries.
  316. expected_jacobian_x_.resize(7);
  317. expected_jacobian_x_[0] = 1.0;
  318. expected_jacobian_x_[1] = 0.0;
  319. expected_jacobian_x_[2] = 0.0;
  320. expected_jacobian_x_[3] = sum_y;
  321. expected_jacobian_x_[4] = 0.0;
  322. expected_jacobian_x_[5] = sum_z;
  323. expected_jacobian_x_[6] = sum_y * sum_z;
  324. expected_jacobian_y_.resize(14);
  325. expected_jacobian_y_[0] = 0.0;
  326. expected_jacobian_y_[1] = 0.0;
  327. expected_jacobian_y_[2] = 1.0;
  328. expected_jacobian_y_[3] = 2.0;
  329. expected_jacobian_y_[4] = 0.0;
  330. expected_jacobian_y_[5] = 0.0;
  331. expected_jacobian_y_[6] = sum_x;
  332. expected_jacobian_y_[7] = 2.0 * sum_x;
  333. expected_jacobian_y_[8] = sum_z;
  334. expected_jacobian_y_[9] = 2.0 * sum_z;
  335. expected_jacobian_y_[10] = 0.0;
  336. expected_jacobian_y_[11] = 0.0;
  337. expected_jacobian_y_[12] = sum_x * sum_z;
  338. expected_jacobian_y_[13] = 2.0 * sum_x * sum_z;
  339. expected_jacobian_z_.resize(21);
  340. expected_jacobian_z_[0] = 0.0;
  341. expected_jacobian_z_[1] = 0.0;
  342. expected_jacobian_z_[2] = 0.0;
  343. expected_jacobian_z_[3] = 0.0;
  344. expected_jacobian_z_[4] = 0.0;
  345. expected_jacobian_z_[5] = 0.0;
  346. expected_jacobian_z_[6] = 1.0;
  347. expected_jacobian_z_[7] = 3.0;
  348. expected_jacobian_z_[8] = 6.0;
  349. expected_jacobian_z_[9] = 0.0;
  350. expected_jacobian_z_[10] = 0.0;
  351. expected_jacobian_z_[11] = 0.0;
  352. expected_jacobian_z_[12] = sum_y;
  353. expected_jacobian_z_[13] = 3.0 * sum_y;
  354. expected_jacobian_z_[14] = 6.0 * sum_y;
  355. expected_jacobian_z_[15] = sum_x;
  356. expected_jacobian_z_[16] = 3.0 * sum_x;
  357. expected_jacobian_z_[17] = 6.0 * sum_x;
  358. expected_jacobian_z_[18] = sum_x * sum_y;
  359. expected_jacobian_z_[19] = 3.0 * sum_x * sum_y;
  360. expected_jacobian_z_[20] = 6.0 * sum_x * sum_y;
  361. }
  362. protected:
  363. std::vector<double> x_;
  364. std::vector<double> y_;
  365. std::vector<double> z_;
  366. std::vector<double*> parameter_blocks_;
  367. std::unique_ptr<CostFunction> cost_function_;
  368. std::vector<std::vector<double>> jacobian_vect_;
  369. std::vector<double> expected_residuals_;
  370. std::vector<double> expected_jacobian_x_;
  371. std::vector<double> expected_jacobian_y_;
  372. std::vector<double> expected_jacobian_z_;
  373. };
  374. TEST_F(ThreeParameterCostFunctorTest, TestThreeParameterResiduals) {
  375. std::vector<double> residuals(7, -100000);
  376. EXPECT_TRUE(cost_function_->Evaluate(
  377. parameter_blocks_.data(), residuals.data(), nullptr));
  378. for (int i = 0; i < 7; ++i) {
  379. EXPECT_EQ(expected_residuals_[i], residuals[i]);
  380. }
  381. }
  382. TEST_F(ThreeParameterCostFunctorTest, TestThreeParameterJacobian) {
  383. std::vector<double> residuals(7, -100000);
  384. std::vector<double*> jacobian;
  385. jacobian.push_back(jacobian_vect_[0].data());
  386. jacobian.push_back(jacobian_vect_[1].data());
  387. jacobian.push_back(jacobian_vect_[2].data());
  388. EXPECT_TRUE(cost_function_->Evaluate(
  389. parameter_blocks_.data(), residuals.data(), jacobian.data()));
  390. for (int i = 0; i < 7; ++i) {
  391. EXPECT_EQ(expected_residuals_[i], residuals[i]);
  392. }
  393. for (int i = 0; i < 7; ++i) {
  394. EXPECT_NEAR(expected_jacobian_x_[i], jacobian[0][i], kTolerance);
  395. }
  396. for (int i = 0; i < 14; ++i) {
  397. EXPECT_NEAR(expected_jacobian_y_[i], jacobian[1][i], kTolerance);
  398. }
  399. for (int i = 0; i < 21; ++i) {
  400. EXPECT_NEAR(expected_jacobian_z_[i], jacobian[2][i], kTolerance);
  401. }
  402. }
  403. TEST_F(ThreeParameterCostFunctorTest,
  404. ThreeParameterJacobianWithFirstAndLastParameterBlockConstant) {
  405. std::vector<double> residuals(7, -100000);
  406. std::vector<double*> jacobian;
  407. jacobian.push_back(nullptr);
  408. jacobian.push_back(jacobian_vect_[1].data());
  409. jacobian.push_back(nullptr);
  410. EXPECT_TRUE(cost_function_->Evaluate(
  411. parameter_blocks_.data(), residuals.data(), jacobian.data()));
  412. for (int i = 0; i < 7; ++i) {
  413. EXPECT_EQ(expected_residuals_[i], residuals[i]);
  414. }
  415. for (int i = 0; i < 14; ++i) {
  416. EXPECT_NEAR(expected_jacobian_y_[i], jacobian[1][i], kTolerance);
  417. }
  418. }
  419. TEST_F(ThreeParameterCostFunctorTest,
  420. ThreeParameterJacobianWithSecondParameterBlockConstant) {
  421. std::vector<double> residuals(7, -100000);
  422. std::vector<double*> jacobian;
  423. jacobian.push_back(jacobian_vect_[0].data());
  424. jacobian.push_back(nullptr);
  425. jacobian.push_back(jacobian_vect_[2].data());
  426. EXPECT_TRUE(cost_function_->Evaluate(
  427. parameter_blocks_.data(), residuals.data(), jacobian.data()));
  428. for (int i = 0; i < 7; ++i) {
  429. EXPECT_EQ(expected_residuals_[i], residuals[i]);
  430. }
  431. for (int i = 0; i < 7; ++i) {
  432. EXPECT_NEAR(expected_jacobian_x_[i], jacobian[0][i], kTolerance);
  433. }
  434. for (int i = 0; i < 21; ++i) {
  435. EXPECT_NEAR(expected_jacobian_z_[i], jacobian[2][i], kTolerance);
  436. }
  437. }
  438. } // namespace ceres::internal