trust_region_preprocessor_test.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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/trust_region_preprocessor.h"
  31. #include <array>
  32. #include <map>
  33. #include "ceres/internal/config.h"
  34. #include "ceres/ordered_groups.h"
  35. #include "ceres/problem_impl.h"
  36. #include "ceres/sized_cost_function.h"
  37. #include "ceres/solver.h"
  38. #include "gtest/gtest.h"
  39. namespace ceres {
  40. namespace internal {
  41. TEST(TrustRegionPreprocessor, ZeroProblem) {
  42. ProblemImpl problem;
  43. Solver::Options options;
  44. TrustRegionPreprocessor preprocessor;
  45. PreprocessedProblem pp;
  46. EXPECT_TRUE(preprocessor.Preprocess(options, &problem, &pp));
  47. }
  48. TEST(TrustRegionPreprocessor, ProblemWithInvalidParameterBlock) {
  49. ProblemImpl problem;
  50. double x = std::numeric_limits<double>::quiet_NaN();
  51. problem.AddParameterBlock(&x, 1);
  52. Solver::Options options;
  53. TrustRegionPreprocessor preprocessor;
  54. PreprocessedProblem pp;
  55. EXPECT_FALSE(preprocessor.Preprocess(options, &problem, &pp));
  56. }
  57. TEST(TrustRegionPreprocessor, ParameterBlockBoundsAreInvalid) {
  58. ProblemImpl problem;
  59. double x = 1.0;
  60. problem.AddParameterBlock(&x, 1);
  61. problem.SetParameterUpperBound(&x, 0, 1.0);
  62. problem.SetParameterLowerBound(&x, 0, 2.0);
  63. Solver::Options options;
  64. TrustRegionPreprocessor preprocessor;
  65. PreprocessedProblem pp;
  66. EXPECT_FALSE(preprocessor.Preprocess(options, &problem, &pp));
  67. }
  68. TEST(TrustRegionPreprocessor, ParameterBlockIsInfeasible) {
  69. ProblemImpl problem;
  70. double x = 3.0;
  71. problem.AddParameterBlock(&x, 1);
  72. problem.SetParameterUpperBound(&x, 0, 1.0);
  73. problem.SetParameterLowerBound(&x, 0, 2.0);
  74. problem.SetParameterBlockConstant(&x);
  75. Solver::Options options;
  76. TrustRegionPreprocessor preprocessor;
  77. PreprocessedProblem pp;
  78. EXPECT_FALSE(preprocessor.Preprocess(options, &problem, &pp));
  79. }
  80. class FailingCostFunction : public SizedCostFunction<1, 1> {
  81. public:
  82. bool Evaluate(double const* const* parameters,
  83. double* residuals,
  84. double** jacobians) const override {
  85. return false;
  86. }
  87. };
  88. TEST(TrustRegionPreprocessor, RemoveParameterBlocksFailed) {
  89. ProblemImpl problem;
  90. double x = 3.0;
  91. problem.AddResidualBlock(new FailingCostFunction, nullptr, &x);
  92. problem.SetParameterBlockConstant(&x);
  93. Solver::Options options;
  94. TrustRegionPreprocessor preprocessor;
  95. PreprocessedProblem pp;
  96. EXPECT_FALSE(preprocessor.Preprocess(options, &problem, &pp));
  97. }
  98. TEST(TrustRegionPreprocessor, RemoveParameterBlocksSucceeds) {
  99. ProblemImpl problem;
  100. double x = 3.0;
  101. problem.AddParameterBlock(&x, 1);
  102. Solver::Options options;
  103. TrustRegionPreprocessor preprocessor;
  104. PreprocessedProblem pp;
  105. EXPECT_TRUE(preprocessor.Preprocess(options, &problem, &pp));
  106. }
  107. template <int kNumResiduals, int... Ns>
  108. class DummyCostFunction : public SizedCostFunction<kNumResiduals, Ns...> {
  109. public:
  110. bool Evaluate(double const* const* parameters,
  111. double* residuals,
  112. double** jacobians) const override {
  113. for (int i = 0; i < kNumResiduals; ++i) {
  114. residuals[i] = kNumResiduals * kNumResiduals + i;
  115. }
  116. if (jacobians == nullptr) {
  117. return true;
  118. }
  119. std::array<int, sizeof...(Ns)> N{Ns...};
  120. for (size_t i = 0; i < N.size(); ++i) {
  121. if (jacobians[i] != nullptr) {
  122. MatrixRef j(jacobians[i], kNumResiduals, N[i]);
  123. j.setOnes();
  124. j *= kNumResiduals * N[i];
  125. }
  126. }
  127. return true;
  128. }
  129. };
  130. class LinearSolverAndEvaluatorCreationTest : public ::testing::Test {
  131. public:
  132. void SetUp() final {
  133. x_ = 1.0;
  134. y_ = 1.0;
  135. z_ = 1.0;
  136. problem_.AddResidualBlock(
  137. new DummyCostFunction<1, 1, 1>, nullptr, &x_, &y_);
  138. problem_.AddResidualBlock(
  139. new DummyCostFunction<1, 1, 1>, nullptr, &y_, &z_);
  140. }
  141. void PreprocessForGivenLinearSolverAndVerify(
  142. const LinearSolverType linear_solver_type) {
  143. Solver::Options options;
  144. options.linear_solver_type = linear_solver_type;
  145. TrustRegionPreprocessor preprocessor;
  146. PreprocessedProblem pp;
  147. EXPECT_TRUE(preprocessor.Preprocess(options, &problem_, &pp));
  148. EXPECT_EQ(pp.options.linear_solver_type, linear_solver_type);
  149. EXPECT_EQ(pp.linear_solver_options.type, linear_solver_type);
  150. EXPECT_EQ(pp.evaluator_options.linear_solver_type, linear_solver_type);
  151. EXPECT_TRUE(pp.linear_solver.get() != nullptr);
  152. EXPECT_TRUE(pp.evaluator.get() != nullptr);
  153. }
  154. protected:
  155. ProblemImpl problem_;
  156. double x_;
  157. double y_;
  158. double z_;
  159. };
  160. TEST_F(LinearSolverAndEvaluatorCreationTest, DenseQR) {
  161. PreprocessForGivenLinearSolverAndVerify(DENSE_QR);
  162. }
  163. TEST_F(LinearSolverAndEvaluatorCreationTest, DenseNormalCholesky) {
  164. PreprocessForGivenLinearSolverAndVerify(DENSE_NORMAL_CHOLESKY);
  165. }
  166. TEST_F(LinearSolverAndEvaluatorCreationTest, DenseSchur) {
  167. PreprocessForGivenLinearSolverAndVerify(DENSE_SCHUR);
  168. }
  169. #if !defined(CERES_NO_SPARSE)
  170. TEST_F(LinearSolverAndEvaluatorCreationTest, SparseNormalCholesky) {
  171. PreprocessForGivenLinearSolverAndVerify(SPARSE_NORMAL_CHOLESKY);
  172. }
  173. #endif
  174. #if !defined(CERES_NO_SPARSE)
  175. TEST_F(LinearSolverAndEvaluatorCreationTest, SparseSchur) {
  176. PreprocessForGivenLinearSolverAndVerify(SPARSE_SCHUR);
  177. }
  178. #endif
  179. TEST_F(LinearSolverAndEvaluatorCreationTest, CGNR) {
  180. PreprocessForGivenLinearSolverAndVerify(CGNR);
  181. }
  182. TEST_F(LinearSolverAndEvaluatorCreationTest, IterativeSchur) {
  183. PreprocessForGivenLinearSolverAndVerify(ITERATIVE_SCHUR);
  184. }
  185. TEST_F(LinearSolverAndEvaluatorCreationTest, MinimizerIsAwareOfBounds) {
  186. problem_.SetParameterLowerBound(&x_, 0, 0.0);
  187. Solver::Options options;
  188. TrustRegionPreprocessor preprocessor;
  189. PreprocessedProblem pp;
  190. EXPECT_TRUE(preprocessor.Preprocess(options, &problem_, &pp));
  191. EXPECT_EQ(pp.options.linear_solver_type, options.linear_solver_type);
  192. EXPECT_EQ(pp.linear_solver_options.type, options.linear_solver_type);
  193. EXPECT_EQ(pp.evaluator_options.linear_solver_type,
  194. options.linear_solver_type);
  195. EXPECT_TRUE(pp.linear_solver.get() != nullptr);
  196. EXPECT_TRUE(pp.evaluator.get() != nullptr);
  197. EXPECT_TRUE(pp.minimizer_options.is_constrained);
  198. }
  199. TEST_F(LinearSolverAndEvaluatorCreationTest, SchurTypeSolverWithBadOrdering) {
  200. Solver::Options options;
  201. options.linear_solver_type = DENSE_SCHUR;
  202. options.linear_solver_ordering = std::make_shared<ParameterBlockOrdering>();
  203. options.linear_solver_ordering->AddElementToGroup(&x_, 0);
  204. options.linear_solver_ordering->AddElementToGroup(&y_, 0);
  205. options.linear_solver_ordering->AddElementToGroup(&z_, 1);
  206. TrustRegionPreprocessor preprocessor;
  207. PreprocessedProblem pp;
  208. EXPECT_FALSE(preprocessor.Preprocess(options, &problem_, &pp));
  209. }
  210. TEST_F(LinearSolverAndEvaluatorCreationTest, SchurTypeSolverWithGoodOrdering) {
  211. Solver::Options options;
  212. options.linear_solver_type = DENSE_SCHUR;
  213. options.linear_solver_ordering = std::make_shared<ParameterBlockOrdering>();
  214. options.linear_solver_ordering->AddElementToGroup(&x_, 0);
  215. options.linear_solver_ordering->AddElementToGroup(&z_, 0);
  216. options.linear_solver_ordering->AddElementToGroup(&y_, 1);
  217. TrustRegionPreprocessor preprocessor;
  218. PreprocessedProblem pp;
  219. EXPECT_TRUE(preprocessor.Preprocess(options, &problem_, &pp));
  220. EXPECT_EQ(pp.options.linear_solver_type, DENSE_SCHUR);
  221. EXPECT_EQ(pp.linear_solver_options.type, DENSE_SCHUR);
  222. EXPECT_EQ(pp.evaluator_options.linear_solver_type, DENSE_SCHUR);
  223. EXPECT_TRUE(pp.linear_solver.get() != nullptr);
  224. EXPECT_TRUE(pp.evaluator.get() != nullptr);
  225. }
  226. TEST_F(LinearSolverAndEvaluatorCreationTest,
  227. SchurTypeSolverWithEmptyFirstEliminationGroup) {
  228. problem_.SetParameterBlockConstant(&x_);
  229. problem_.SetParameterBlockConstant(&z_);
  230. Solver::Options options;
  231. options.linear_solver_type = DENSE_SCHUR;
  232. options.linear_solver_ordering = std::make_shared<ParameterBlockOrdering>();
  233. options.linear_solver_ordering->AddElementToGroup(&x_, 0);
  234. options.linear_solver_ordering->AddElementToGroup(&z_, 0);
  235. options.linear_solver_ordering->AddElementToGroup(&y_, 1);
  236. TrustRegionPreprocessor preprocessor;
  237. PreprocessedProblem pp;
  238. EXPECT_TRUE(preprocessor.Preprocess(options, &problem_, &pp));
  239. EXPECT_EQ(pp.options.linear_solver_type, DENSE_QR);
  240. EXPECT_EQ(pp.linear_solver_options.type, DENSE_QR);
  241. EXPECT_EQ(pp.evaluator_options.linear_solver_type, DENSE_QR);
  242. EXPECT_TRUE(pp.linear_solver.get() != nullptr);
  243. EXPECT_TRUE(pp.evaluator.get() != nullptr);
  244. }
  245. TEST_F(LinearSolverAndEvaluatorCreationTest,
  246. SchurTypeSolverWithEmptySecondEliminationGroup) {
  247. problem_.SetParameterBlockConstant(&y_);
  248. Solver::Options options;
  249. options.linear_solver_type = DENSE_SCHUR;
  250. options.linear_solver_ordering = std::make_shared<ParameterBlockOrdering>();
  251. options.linear_solver_ordering->AddElementToGroup(&x_, 0);
  252. options.linear_solver_ordering->AddElementToGroup(&z_, 0);
  253. options.linear_solver_ordering->AddElementToGroup(&y_, 1);
  254. TrustRegionPreprocessor preprocessor;
  255. PreprocessedProblem pp;
  256. EXPECT_TRUE(preprocessor.Preprocess(options, &problem_, &pp));
  257. EXPECT_EQ(pp.options.linear_solver_type, DENSE_SCHUR);
  258. EXPECT_EQ(pp.linear_solver_options.type, DENSE_SCHUR);
  259. EXPECT_EQ(pp.evaluator_options.linear_solver_type, DENSE_SCHUR);
  260. EXPECT_TRUE(pp.linear_solver.get() != nullptr);
  261. EXPECT_TRUE(pp.evaluator.get() != nullptr);
  262. }
  263. TEST(TrustRegionPreprocessorTest, InnerIterationsWithOneParameterBlock) {
  264. ProblemImpl problem;
  265. double x = 1.0;
  266. problem.AddResidualBlock(new DummyCostFunction<1, 1>, nullptr, &x);
  267. Solver::Options options;
  268. options.use_inner_iterations = true;
  269. TrustRegionPreprocessor preprocessor;
  270. PreprocessedProblem pp;
  271. EXPECT_TRUE(preprocessor.Preprocess(options, &problem, &pp));
  272. EXPECT_TRUE(pp.linear_solver.get() != nullptr);
  273. EXPECT_TRUE(pp.evaluator.get() != nullptr);
  274. EXPECT_TRUE(pp.inner_iteration_minimizer.get() == nullptr);
  275. }
  276. TEST_F(LinearSolverAndEvaluatorCreationTest,
  277. InnerIterationsWithTwoParameterBlocks) {
  278. Solver::Options options;
  279. options.use_inner_iterations = true;
  280. TrustRegionPreprocessor preprocessor;
  281. PreprocessedProblem pp;
  282. EXPECT_TRUE(preprocessor.Preprocess(options, &problem_, &pp));
  283. EXPECT_TRUE(pp.linear_solver.get() != nullptr);
  284. EXPECT_TRUE(pp.evaluator.get() != nullptr);
  285. EXPECT_TRUE(pp.inner_iteration_minimizer.get() != nullptr);
  286. }
  287. TEST_F(LinearSolverAndEvaluatorCreationTest, InvalidInnerIterationsOrdering) {
  288. Solver::Options options;
  289. options.use_inner_iterations = true;
  290. options.inner_iteration_ordering = std::make_shared<ParameterBlockOrdering>();
  291. options.inner_iteration_ordering->AddElementToGroup(&x_, 0);
  292. options.inner_iteration_ordering->AddElementToGroup(&z_, 0);
  293. options.inner_iteration_ordering->AddElementToGroup(&y_, 0);
  294. TrustRegionPreprocessor preprocessor;
  295. PreprocessedProblem pp;
  296. EXPECT_FALSE(preprocessor.Preprocess(options, &problem_, &pp));
  297. }
  298. TEST_F(LinearSolverAndEvaluatorCreationTest, ValidInnerIterationsOrdering) {
  299. Solver::Options options;
  300. options.use_inner_iterations = true;
  301. options.inner_iteration_ordering = std::make_shared<ParameterBlockOrdering>();
  302. options.inner_iteration_ordering->AddElementToGroup(&x_, 0);
  303. options.inner_iteration_ordering->AddElementToGroup(&z_, 0);
  304. options.inner_iteration_ordering->AddElementToGroup(&y_, 1);
  305. TrustRegionPreprocessor preprocessor;
  306. PreprocessedProblem pp;
  307. EXPECT_TRUE(preprocessor.Preprocess(options, &problem_, &pp));
  308. EXPECT_TRUE(pp.linear_solver.get() != nullptr);
  309. EXPECT_TRUE(pp.evaluator.get() != nullptr);
  310. EXPECT_TRUE(pp.inner_iteration_minimizer.get() != nullptr);
  311. }
  312. } // namespace internal
  313. } // namespace ceres