preprocessor.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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: sameragarwal@google.com (Sameer Agarwal)
  30. #ifndef CERES_INTERNAL_PREPROCESSOR_H_
  31. #define CERES_INTERNAL_PREPROCESSOR_H_
  32. #include <memory>
  33. #include <string>
  34. #include <vector>
  35. #include "ceres/coordinate_descent_minimizer.h"
  36. #include "ceres/evaluator.h"
  37. #include "ceres/internal/disable_warnings.h"
  38. #include "ceres/internal/eigen.h"
  39. #include "ceres/internal/export.h"
  40. #include "ceres/iteration_callback.h"
  41. #include "ceres/linear_solver.h"
  42. #include "ceres/minimizer.h"
  43. #include "ceres/problem_impl.h"
  44. #include "ceres/program.h"
  45. #include "ceres/solver.h"
  46. namespace ceres::internal {
  47. struct PreprocessedProblem;
  48. // Given a Problem object and a Solver::Options object indicating the
  49. // configuration of the solver, the job of the Preprocessor is to
  50. // analyze the Problem and perform the setup needed to solve it using
  51. // the desired Minimization algorithm. The setup involves removing
  52. // redundancies in the input problem (inactive parameter and residual
  53. // blocks), finding fill reducing orderings as needed, configuring and
  54. // creating various objects needed by the Minimizer to solve the
  55. // problem such as an evaluator, a linear solver etc.
  56. //
  57. // Each Minimizer (LineSearchMinimizer and TrustRegionMinimizer) comes
  58. // with a corresponding Preprocessor (LineSearchPreprocessor and
  59. // TrustRegionPreprocessor) that knows about its needs and performs
  60. // the preprocessing needed.
  61. //
  62. // The output of the Preprocessor is stored in a PreprocessedProblem
  63. // object.
  64. class CERES_NO_EXPORT Preprocessor {
  65. public:
  66. // Factory.
  67. static std::unique_ptr<Preprocessor> Create(MinimizerType minimizer_type);
  68. virtual ~Preprocessor();
  69. virtual bool Preprocess(const Solver::Options& options,
  70. ProblemImpl* problem,
  71. PreprocessedProblem* pp) = 0;
  72. };
  73. // A PreprocessedProblem is the result of running the Preprocessor on
  74. // a Problem and Solver::Options object.
  75. struct CERES_NO_EXPORT PreprocessedProblem {
  76. PreprocessedProblem() = default;
  77. std::string error;
  78. Solver::Options options;
  79. LinearSolver::Options linear_solver_options;
  80. Evaluator::Options evaluator_options;
  81. Minimizer::Options minimizer_options;
  82. ProblemImpl* problem;
  83. std::unique_ptr<ProblemImpl> gradient_checking_problem;
  84. std::unique_ptr<Program> reduced_program;
  85. std::unique_ptr<LinearSolver> linear_solver;
  86. std::unique_ptr<IterationCallback> logging_callback;
  87. std::unique_ptr<IterationCallback> state_updating_callback;
  88. std::shared_ptr<Evaluator> evaluator;
  89. std::shared_ptr<CoordinateDescentMinimizer> inner_iteration_minimizer;
  90. std::vector<double*> removed_parameter_blocks;
  91. Vector reduced_parameters;
  92. double fixed_cost{0.0};
  93. };
  94. // Common functions used by various preprocessors.
  95. // If the user has specified a num_threads > the maximum number of threads
  96. // available from the compiled threading model, bound the number of threads
  97. // to the maximum.
  98. CERES_NO_EXPORT
  99. void ChangeNumThreadsIfNeeded(Solver::Options* options);
  100. // Extract the effective parameter vector from the preprocessed
  101. // problem and setup bits of the Minimizer::Options object that are
  102. // common to all Preprocessors.
  103. CERES_NO_EXPORT
  104. void SetupCommonMinimizerOptions(PreprocessedProblem* pp);
  105. } // namespace ceres::internal
  106. #include "ceres/internal/reenable_warnings.h"
  107. #endif // CERES_INTERNAL_PREPROCESSOR_H_