gradient_problem.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #ifndef CERES_PUBLIC_GRADIENT_PROBLEM_H_
  31. #define CERES_PUBLIC_GRADIENT_PROBLEM_H_
  32. #include <memory>
  33. #include "ceres/first_order_function.h"
  34. #include "ceres/internal/disable_warnings.h"
  35. #include "ceres/internal/export.h"
  36. #include "ceres/manifold.h"
  37. namespace ceres {
  38. class FirstOrderFunction;
  39. // Instances of GradientProblem represent general non-linear
  40. // optimization problems that must be solved using just the value of
  41. // the objective function and its gradient.
  42. // Unlike the Problem class, which can only be used to model non-linear least
  43. // squares problems, instances of GradientProblem are not restricted in the form
  44. // of the objective function.
  45. //
  46. // Structurally GradientProblem is a composition of a FirstOrderFunction and
  47. // optionally a Manifold.
  48. //
  49. // The FirstOrderFunction is responsible for evaluating the cost and gradient of
  50. // the objective function.
  51. //
  52. // The Manifold is responsible for going back and forth between the ambient
  53. // space and the local tangent space. (See manifold.h for more details). When a
  54. // Manifold is not provided, then the tangent space is assumed to coincide with
  55. // the ambient Euclidean space that the gradient vector lives in.
  56. //
  57. // Example usage:
  58. //
  59. // The following demonstrate the problem construction for Rosenbrock's function
  60. //
  61. // f(x,y) = (1-x)^2 + 100(y - x^2)^2;
  62. //
  63. // class Rosenbrock : public ceres::FirstOrderFunction {
  64. // public:
  65. // virtual ~Rosenbrock() {}
  66. //
  67. // virtual bool Evaluate(const double* parameters,
  68. // double* cost,
  69. // double* gradient) const {
  70. // const double x = parameters[0];
  71. // const double y = parameters[1];
  72. //
  73. // cost[0] = (1.0 - x) * (1.0 - x) + 100.0 * (y - x * x) * (y - x * x);
  74. // if (gradient != nullptr) {
  75. // gradient[0] = -2.0 * (1.0 - x) - 200.0 * (y - x * x) * 2.0 * x;
  76. // gradient[1] = 200.0 * (y - x * x);
  77. // }
  78. // return true;
  79. // };
  80. //
  81. // virtual int NumParameters() const { return 2; };
  82. // };
  83. //
  84. // ceres::GradientProblem problem(new Rosenbrock());
  85. class CERES_EXPORT GradientProblem {
  86. public:
  87. // Takes ownership of the function.
  88. explicit GradientProblem(FirstOrderFunction* function);
  89. // Takes ownership of the function and the manifold.
  90. GradientProblem(FirstOrderFunction* function, Manifold* manifold);
  91. int NumParameters() const;
  92. // Dimension of the manifold (and its tangent space).
  93. int NumTangentParameters() const;
  94. // This call is not thread safe.
  95. bool Evaluate(const double* parameters, double* cost, double* gradient) const;
  96. bool Plus(const double* x, const double* delta, double* x_plus_delta) const;
  97. const FirstOrderFunction* function() const { return function_.get(); }
  98. FirstOrderFunction* mutable_function() { return function_.get(); }
  99. const Manifold* manifold() const { return manifold_.get(); }
  100. Manifold* mutable_manifold() { return manifold_.get(); }
  101. private:
  102. std::unique_ptr<FirstOrderFunction> function_;
  103. std::unique_ptr<Manifold> manifold_;
  104. std::unique_ptr<double[]> scratch_;
  105. };
  106. } // namespace ceres
  107. #include "ceres/internal/reenable_warnings.h"
  108. #endif // CERES_PUBLIC_GRADIENT_PROBLEM_H_