line_search_preprocessor.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #include "ceres/line_search_preprocessor.h"
  31. #include <numeric>
  32. #include <string>
  33. #include "ceres/casts.h"
  34. #include "ceres/context_impl.h"
  35. #include "ceres/evaluator.h"
  36. #include "ceres/minimizer.h"
  37. #include "ceres/problem_impl.h"
  38. #include "ceres/program.h"
  39. #include "ceres/wall_time.h"
  40. namespace ceres::internal {
  41. namespace {
  42. bool IsProgramValid(const Program& program, std::string* error) {
  43. if (program.IsBoundsConstrained()) {
  44. *error = "LINE_SEARCH Minimizer does not support bounds.";
  45. return false;
  46. }
  47. return program.ParameterBlocksAreFinite(error);
  48. }
  49. bool SetupEvaluator(PreprocessedProblem* pp) {
  50. pp->evaluator_options = Evaluator::Options();
  51. // This ensures that we get a Block Jacobian Evaluator without any
  52. // requirement on orderings.
  53. pp->evaluator_options.linear_solver_type = CGNR;
  54. pp->evaluator_options.num_eliminate_blocks = 0;
  55. pp->evaluator_options.num_threads = pp->options.num_threads;
  56. pp->evaluator_options.context = pp->problem->context();
  57. pp->evaluator_options.evaluation_callback =
  58. pp->reduced_program->mutable_evaluation_callback();
  59. pp->evaluator = Evaluator::Create(
  60. pp->evaluator_options, pp->reduced_program.get(), &pp->error);
  61. return (pp->evaluator.get() != nullptr);
  62. }
  63. } // namespace
  64. bool LineSearchPreprocessor::Preprocess(const Solver::Options& options,
  65. ProblemImpl* problem,
  66. PreprocessedProblem* pp) {
  67. CHECK(pp != nullptr);
  68. pp->options = options;
  69. ChangeNumThreadsIfNeeded(&pp->options);
  70. pp->problem = problem;
  71. Program* program = problem->mutable_program();
  72. if (!IsProgramValid(*program, &pp->error)) {
  73. return false;
  74. }
  75. pp->reduced_program = program->CreateReducedProgram(
  76. &pp->removed_parameter_blocks, &pp->fixed_cost, &pp->error);
  77. if (pp->reduced_program.get() == nullptr) {
  78. return false;
  79. }
  80. if (pp->reduced_program->NumParameterBlocks() == 0) {
  81. return true;
  82. }
  83. if (!SetupEvaluator(pp)) {
  84. return false;
  85. }
  86. SetupCommonMinimizerOptions(pp);
  87. return true;
  88. }
  89. } // namespace ceres::internal