system_test.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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: keir@google.com (Keir Mierle)
  30. // sameeragarwal@google.com (Sameer Agarwal)
  31. //
  32. // End-to-end tests for Ceres using Powell's function.
  33. #include <cmath>
  34. #include <cstdlib>
  35. #include "ceres/autodiff_cost_function.h"
  36. #include "ceres/internal/config.h"
  37. #include "ceres/problem.h"
  38. #include "ceres/solver.h"
  39. #include "ceres/test_util.h"
  40. #include "ceres/types.h"
  41. #include "glog/logging.h"
  42. #include "gtest/gtest.h"
  43. namespace ceres::internal {
  44. // This class implements the SystemTestProblem interface and provides
  45. // access to an implementation of Powell's singular function.
  46. //
  47. // F = 1/2 (f1^2 + f2^2 + f3^2 + f4^2)
  48. //
  49. // f1 = x1 + 10*x2;
  50. // f2 = sqrt(5) * (x3 - x4)
  51. // f3 = (x2 - 2*x3)^2
  52. // f4 = sqrt(10) * (x1 - x4)^2
  53. //
  54. // The starting values are x1 = 3, x2 = -1, x3 = 0, x4 = 1.
  55. // The minimum is 0 at (x1, x2, x3, x4) = 0.
  56. //
  57. // From: Testing Unconstrained Optimization Software by Jorge J. More, Burton S.
  58. // Garbow and Kenneth E. Hillstrom in ACM Transactions on Mathematical Software,
  59. // Vol 7(1), March 1981.
  60. class PowellsFunction {
  61. public:
  62. PowellsFunction() {
  63. x_[0] = 3.0;
  64. x_[1] = -1.0;
  65. x_[2] = 0.0;
  66. x_[3] = 1.0;
  67. problem_.AddResidualBlock(
  68. new AutoDiffCostFunction<F1, 1, 1, 1>(new F1), nullptr, &x_[0], &x_[1]);
  69. problem_.AddResidualBlock(
  70. new AutoDiffCostFunction<F2, 1, 1, 1>(new F2), nullptr, &x_[2], &x_[3]);
  71. problem_.AddResidualBlock(
  72. new AutoDiffCostFunction<F3, 1, 1, 1>(new F3), nullptr, &x_[1], &x_[2]);
  73. problem_.AddResidualBlock(
  74. new AutoDiffCostFunction<F4, 1, 1, 1>(new F4), nullptr, &x_[0], &x_[3]);
  75. // Settings for the reference solution.
  76. options_.linear_solver_type = ceres::DENSE_QR;
  77. options_.max_num_iterations = 10;
  78. options_.num_threads = 1;
  79. }
  80. Problem* mutable_problem() { return &problem_; }
  81. Solver::Options* mutable_solver_options() { return &options_; }
  82. static double kResidualTolerance;
  83. private:
  84. // Templated functions used for automatically differentiated cost
  85. // functions.
  86. class F1 {
  87. public:
  88. template <typename T>
  89. bool operator()(const T* const x1, const T* const x2, T* residual) const {
  90. // f1 = x1 + 10 * x2;
  91. *residual = x1[0] + 10.0 * x2[0];
  92. return true;
  93. }
  94. };
  95. class F2 {
  96. public:
  97. template <typename T>
  98. bool operator()(const T* const x3, const T* const x4, T* residual) const {
  99. // f2 = sqrt(5) (x3 - x4)
  100. *residual = sqrt(5.0) * (x3[0] - x4[0]);
  101. return true;
  102. }
  103. };
  104. class F3 {
  105. public:
  106. template <typename T>
  107. bool operator()(const T* const x2, const T* const x3, T* residual) const {
  108. // f3 = (x2 - 2 x3)^2
  109. residual[0] = (x2[0] - 2.0 * x3[0]) * (x2[0] - 2.0 * x3[0]);
  110. return true;
  111. }
  112. };
  113. class F4 {
  114. public:
  115. template <typename T>
  116. bool operator()(const T* const x1, const T* const x4, T* residual) const {
  117. // f4 = sqrt(10) (x1 - x4)^2
  118. residual[0] = sqrt(10.0) * (x1[0] - x4[0]) * (x1[0] - x4[0]);
  119. return true;
  120. }
  121. };
  122. double x_[4];
  123. Problem problem_;
  124. Solver::Options options_;
  125. };
  126. double PowellsFunction::kResidualTolerance = 1e-8;
  127. using PowellTest = SystemTest<PowellsFunction>;
  128. TEST_F(PowellTest, DenseQR) {
  129. PowellsFunction powells_function;
  130. Solver::Options* options = powells_function.mutable_solver_options();
  131. options->linear_solver_type = DENSE_QR;
  132. RunSolverForConfigAndExpectResidualsMatch(*options,
  133. powells_function.mutable_problem());
  134. }
  135. TEST_F(PowellTest, DenseNormalCholesky) {
  136. PowellsFunction powells_function;
  137. Solver::Options* options = powells_function.mutable_solver_options();
  138. options->linear_solver_type = DENSE_NORMAL_CHOLESKY;
  139. RunSolverForConfigAndExpectResidualsMatch(*options,
  140. powells_function.mutable_problem());
  141. }
  142. TEST_F(PowellTest, DenseSchur) {
  143. PowellsFunction powells_function;
  144. Solver::Options* options = powells_function.mutable_solver_options();
  145. options->linear_solver_type = DENSE_SCHUR;
  146. RunSolverForConfigAndExpectResidualsMatch(*options,
  147. powells_function.mutable_problem());
  148. }
  149. TEST_F(PowellTest, IterativeSchurWithJacobi) {
  150. PowellsFunction powells_function;
  151. Solver::Options* options = powells_function.mutable_solver_options();
  152. options->linear_solver_type = ITERATIVE_SCHUR;
  153. options->sparse_linear_algebra_library_type = NO_SPARSE;
  154. options->preconditioner_type = JACOBI;
  155. RunSolverForConfigAndExpectResidualsMatch(*options,
  156. powells_function.mutable_problem());
  157. }
  158. #ifndef CERES_NO_SUITESPARSE
  159. TEST_F(PowellTest, SparseNormalCholeskyUsingSuiteSparse) {
  160. PowellsFunction powells_function;
  161. Solver::Options* options = powells_function.mutable_solver_options();
  162. options->linear_solver_type = SPARSE_NORMAL_CHOLESKY;
  163. options->sparse_linear_algebra_library_type = SUITE_SPARSE;
  164. RunSolverForConfigAndExpectResidualsMatch(*options,
  165. powells_function.mutable_problem());
  166. }
  167. #endif // CERES_NO_SUITESPARSE
  168. #ifndef CERES_NO_ACCELERATE_SPARSE
  169. TEST_F(PowellTest, SparseNormalCholeskyUsingAccelerateSparse) {
  170. PowellsFunction powells_function;
  171. Solver::Options* options = powells_function.mutable_solver_options();
  172. options->linear_solver_type = SPARSE_NORMAL_CHOLESKY;
  173. options->sparse_linear_algebra_library_type = ACCELERATE_SPARSE;
  174. RunSolverForConfigAndExpectResidualsMatch(*options,
  175. powells_function.mutable_problem());
  176. }
  177. #endif // CERES_NO_ACCELERATE_SPARSE
  178. #ifdef CERES_USE_EIGEN_SPARSE
  179. TEST_F(PowellTest, SparseNormalCholeskyUsingEigenSparse) {
  180. PowellsFunction powells_function;
  181. Solver::Options* options = powells_function.mutable_solver_options();
  182. options->linear_solver_type = SPARSE_NORMAL_CHOLESKY;
  183. options->sparse_linear_algebra_library_type = EIGEN_SPARSE;
  184. RunSolverForConfigAndExpectResidualsMatch(*options,
  185. powells_function.mutable_problem());
  186. }
  187. #endif // CERES_USE_EIGEN_SPARSE
  188. } // namespace ceres::internal