line_search_preprocessor_test.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2015 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/line_search_preprocessor.h"
  31. #include <map>
  32. #include "ceres/problem_impl.h"
  33. #include "ceres/sized_cost_function.h"
  34. #include "ceres/solver.h"
  35. #include "gtest/gtest.h"
  36. namespace ceres::internal {
  37. TEST(LineSearchPreprocessor, ZeroProblem) {
  38. ProblemImpl problem;
  39. Solver::Options options;
  40. options.minimizer_type = LINE_SEARCH;
  41. LineSearchPreprocessor preprocessor;
  42. PreprocessedProblem pp;
  43. EXPECT_TRUE(preprocessor.Preprocess(options, &problem, &pp));
  44. }
  45. TEST(LineSearchPreprocessor, ProblemWithInvalidParameterBlock) {
  46. ProblemImpl problem;
  47. double x = std::numeric_limits<double>::quiet_NaN();
  48. problem.AddParameterBlock(&x, 1);
  49. Solver::Options options;
  50. options.minimizer_type = LINE_SEARCH;
  51. LineSearchPreprocessor preprocessor;
  52. PreprocessedProblem pp;
  53. EXPECT_FALSE(preprocessor.Preprocess(options, &problem, &pp));
  54. }
  55. TEST(LineSearchPreprocessor, ParameterBlockHasBounds) {
  56. ProblemImpl problem;
  57. double x = 1.0;
  58. problem.AddParameterBlock(&x, 1);
  59. problem.SetParameterUpperBound(&x, 0, 1.0);
  60. problem.SetParameterLowerBound(&x, 0, 2.0);
  61. Solver::Options options;
  62. options.minimizer_type = LINE_SEARCH;
  63. LineSearchPreprocessor preprocessor;
  64. PreprocessedProblem pp;
  65. EXPECT_FALSE(preprocessor.Preprocess(options, &problem, &pp));
  66. }
  67. class FailingCostFunction : public SizedCostFunction<1, 1> {
  68. public:
  69. bool Evaluate(double const* const* parameters,
  70. double* residuals,
  71. double** jacobians) const override {
  72. return false;
  73. }
  74. };
  75. TEST(LineSearchPreprocessor, RemoveParameterBlocksFailed) {
  76. ProblemImpl problem;
  77. double x = 3.0;
  78. problem.AddResidualBlock(new FailingCostFunction, nullptr, &x);
  79. problem.SetParameterBlockConstant(&x);
  80. Solver::Options options;
  81. options.minimizer_type = LINE_SEARCH;
  82. LineSearchPreprocessor preprocessor;
  83. PreprocessedProblem pp;
  84. EXPECT_FALSE(preprocessor.Preprocess(options, &problem, &pp));
  85. }
  86. TEST(LineSearchPreprocessor, RemoveParameterBlocksSucceeds) {
  87. ProblemImpl problem;
  88. double x = 3.0;
  89. problem.AddParameterBlock(&x, 1);
  90. Solver::Options options;
  91. options.minimizer_type = LINE_SEARCH;
  92. LineSearchPreprocessor preprocessor;
  93. PreprocessedProblem pp;
  94. EXPECT_TRUE(preprocessor.Preprocess(options, &problem, &pp));
  95. }
  96. template <int kNumResiduals, int... Ns>
  97. class DummyCostFunction : public SizedCostFunction<kNumResiduals, Ns...> {
  98. public:
  99. bool Evaluate(double const* const* parameters,
  100. double* residuals,
  101. double** jacobians) const override {
  102. return true;
  103. }
  104. };
  105. TEST(LineSearchPreprocessor, NormalOperation) {
  106. ProblemImpl problem;
  107. double x = 1.0;
  108. double y = 1.0;
  109. double z = 1.0;
  110. problem.AddResidualBlock(new DummyCostFunction<1, 1, 1>, nullptr, &x, &y);
  111. problem.AddResidualBlock(new DummyCostFunction<1, 1, 1>, nullptr, &y, &z);
  112. Solver::Options options;
  113. options.minimizer_type = LINE_SEARCH;
  114. LineSearchPreprocessor preprocessor;
  115. PreprocessedProblem pp;
  116. EXPECT_TRUE(preprocessor.Preprocess(options, &problem, &pp));
  117. EXPECT_EQ(pp.evaluator_options.linear_solver_type, CGNR);
  118. EXPECT_TRUE(pp.evaluator.get() != nullptr);
  119. }
  120. } // namespace ceres::internal