gradient_problem_solver_test.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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: strandmark@google.com (Petter Strandmark)
  30. #include "ceres/gradient_problem_solver.h"
  31. #include "ceres/gradient_problem.h"
  32. #include "gtest/gtest.h"
  33. namespace ceres::internal {
  34. // Rosenbrock function; see http://en.wikipedia.org/wiki/Rosenbrock_function .
  35. class Rosenbrock : public ceres::FirstOrderFunction {
  36. public:
  37. bool Evaluate(const double* parameters,
  38. double* cost,
  39. double* gradient) const final {
  40. const double x = parameters[0];
  41. const double y = parameters[1];
  42. cost[0] = (1.0 - x) * (1.0 - x) + 100.0 * (y - x * x) * (y - x * x);
  43. if (gradient != nullptr) {
  44. gradient[0] = -2.0 * (1.0 - x) - 200.0 * (y - x * x) * 2.0 * x;
  45. gradient[1] = 200.0 * (y - x * x);
  46. }
  47. return true;
  48. }
  49. int NumParameters() const final { return 2; }
  50. };
  51. TEST(GradientProblemSolver, SolvesRosenbrockWithDefaultOptions) {
  52. const double expected_tolerance = 1e-9;
  53. double parameters[2] = {-1.2, 0.0};
  54. ceres::GradientProblemSolver::Options options;
  55. ceres::GradientProblemSolver::Summary summary;
  56. ceres::GradientProblem problem(new Rosenbrock());
  57. ceres::Solve(options, problem, parameters, &summary);
  58. EXPECT_EQ(CONVERGENCE, summary.termination_type);
  59. EXPECT_NEAR(1.0, parameters[0], expected_tolerance);
  60. EXPECT_NEAR(1.0, parameters[1], expected_tolerance);
  61. }
  62. class QuadraticFunction : public ceres::FirstOrderFunction {
  63. bool Evaluate(const double* parameters,
  64. double* cost,
  65. double* gradient) const final {
  66. const double x = parameters[0];
  67. *cost = 0.5 * (5.0 - x) * (5.0 - x);
  68. if (gradient != nullptr) {
  69. gradient[0] = x - 5.0;
  70. }
  71. return true;
  72. }
  73. int NumParameters() const final { return 1; }
  74. };
  75. struct RememberingCallback : public IterationCallback {
  76. explicit RememberingCallback(double* x) : calls(0), x(x) {}
  77. CallbackReturnType operator()(const IterationSummary& summary) final {
  78. x_values.push_back(*x);
  79. return SOLVER_CONTINUE;
  80. }
  81. int calls;
  82. double* x;
  83. std::vector<double> x_values;
  84. };
  85. TEST(Solver, UpdateStateEveryIterationOption) {
  86. double x = 50.0;
  87. const double original_x = x;
  88. ceres::GradientProblem problem(new QuadraticFunction);
  89. ceres::GradientProblemSolver::Options options;
  90. RememberingCallback callback(&x);
  91. options.callbacks.push_back(&callback);
  92. ceres::GradientProblemSolver::Summary summary;
  93. int num_iterations;
  94. // First try: no updating.
  95. ceres::Solve(options, problem, &x, &summary);
  96. num_iterations = summary.iterations.size() - 1;
  97. EXPECT_GT(num_iterations, 1);
  98. for (double value : callback.x_values) {
  99. EXPECT_EQ(50.0, value);
  100. }
  101. // Second try: with updating
  102. x = 50.0;
  103. options.update_state_every_iteration = true;
  104. callback.x_values.clear();
  105. ceres::Solve(options, problem, &x, &summary);
  106. num_iterations = summary.iterations.size() - 1;
  107. EXPECT_GT(num_iterations, 1);
  108. EXPECT_EQ(original_x, callback.x_values[0]);
  109. EXPECT_NE(original_x, callback.x_values[1]);
  110. }
  111. } // namespace ceres::internal