preprocessor.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #include "ceres/preprocessor.h"
  31. #include <memory>
  32. #include "ceres/callbacks.h"
  33. #include "ceres/gradient_checking_cost_function.h"
  34. #include "ceres/line_search_preprocessor.h"
  35. #include "ceres/parallel_for.h"
  36. #include "ceres/problem_impl.h"
  37. #include "ceres/solver.h"
  38. #include "ceres/trust_region_preprocessor.h"
  39. namespace ceres::internal {
  40. std::unique_ptr<Preprocessor> Preprocessor::Create(
  41. MinimizerType minimizer_type) {
  42. if (minimizer_type == TRUST_REGION) {
  43. return std::make_unique<TrustRegionPreprocessor>();
  44. }
  45. if (minimizer_type == LINE_SEARCH) {
  46. return std::make_unique<LineSearchPreprocessor>();
  47. }
  48. LOG(FATAL) << "Unknown minimizer_type: " << minimizer_type;
  49. return nullptr;
  50. }
  51. Preprocessor::~Preprocessor() = default;
  52. void ChangeNumThreadsIfNeeded(Solver::Options* options) {
  53. if (options->num_threads == 1) {
  54. return;
  55. }
  56. const int num_threads_available = MaxNumThreadsAvailable();
  57. if (options->num_threads > num_threads_available) {
  58. LOG(WARNING) << "Specified options.num_threads: " << options->num_threads
  59. << " exceeds maximum available from the threading model Ceres "
  60. << "was compiled with: " << num_threads_available
  61. << ". Bounding to maximum number available.";
  62. options->num_threads = num_threads_available;
  63. }
  64. }
  65. void SetupCommonMinimizerOptions(PreprocessedProblem* pp) {
  66. const Solver::Options& options = pp->options;
  67. Program* program = pp->reduced_program.get();
  68. // Assuming that the parameter blocks in the program have been
  69. // reordered as needed, extract them into a contiguous vector.
  70. pp->reduced_parameters.resize(program->NumParameters());
  71. double* reduced_parameters = pp->reduced_parameters.data();
  72. program->ParameterBlocksToStateVector(reduced_parameters);
  73. auto context = pp->problem->context();
  74. Minimizer::Options& minimizer_options = pp->minimizer_options;
  75. minimizer_options = Minimizer::Options(options);
  76. minimizer_options.evaluator = pp->evaluator;
  77. minimizer_options.context = context;
  78. if (options.logging_type != SILENT) {
  79. pp->logging_callback = std::make_unique<LoggingCallback>(
  80. options.minimizer_type, options.minimizer_progress_to_stdout);
  81. minimizer_options.callbacks.insert(minimizer_options.callbacks.begin(),
  82. pp->logging_callback.get());
  83. }
  84. if (options.update_state_every_iteration) {
  85. pp->state_updating_callback =
  86. std::make_unique<StateUpdatingCallback>(program, reduced_parameters);
  87. // This must get pushed to the front of the callbacks so that it
  88. // is run before any of the user callbacks.
  89. minimizer_options.callbacks.insert(minimizer_options.callbacks.begin(),
  90. pp->state_updating_callback.get());
  91. }
  92. }
  93. } // namespace ceres::internal