Imported Upstream version ceres 1.13.0
[platform/upstream/ceres-solver.git] / internal / ceres / preprocessor.h
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
31 #ifndef CERES_INTERNAL_PREPROCESSOR_H_
32 #define CERES_INTERNAL_PREPROCESSOR_H_
33
34 #include <string>
35 #include <vector>
36
37 #include "ceres/coordinate_descent_minimizer.h"
38 #include "ceres/evaluator.h"
39 #include "ceres/internal/eigen.h"
40 #include "ceres/internal/port.h"
41 #include "ceres/internal/scoped_ptr.h"
42 #include "ceres/iteration_callback.h"
43 #include "ceres/linear_solver.h"
44 #include "ceres/minimizer.h"
45 #include "ceres/problem_impl.h"
46 #include "ceres/program.h"
47 #include "ceres/solver.h"
48
49 namespace ceres {
50 namespace internal {
51
52 struct PreprocessedProblem;
53
54 // Given a Problem object and a Solver::Options object indicating the
55 // configuration of the solver, the job of the Preprocessor is to
56 // analyze the Problem and perform the setup needed to solve it using
57 // the desired Minimization algorithm. The setup involves removing
58 // redundancies in the input problem (inactive parameter and residual
59 // blocks), finding fill reducing orderings as needed, configuring and
60 // creating various objects needed by the Minimizer to solve the
61 // problem such as an evaluator, a linear solver etc.
62 //
63 // Each Minimizer (LineSearchMinimizer and TrustRegionMinimizer) comes
64 // with a corresponding Preprocessor (LineSearchPreprocessor and
65 // TrustRegionPreprocessor) that knows about its needs and performs
66 // the preprocessing needed.
67 //
68 // The output of the Preprocessor is stored in a PreprocessedProblem
69 // object.
70 class Preprocessor {
71  public:
72   // Factory.
73   static Preprocessor* Create(MinimizerType minimizer_type);
74   virtual ~Preprocessor();
75   virtual bool Preprocess(const Solver::Options& options,
76                           ProblemImpl* problem,
77                           PreprocessedProblem* pp) = 0;
78 };
79
80 // A PreprocessedProblem is the result of running the Preprocessor on
81 // a Problem and Solver::Options object.
82 struct PreprocessedProblem {
83   PreprocessedProblem()
84       : fixed_cost(0.0) {
85   }
86
87   std::string error;
88   Solver::Options options;
89   LinearSolver::Options linear_solver_options;
90   Evaluator::Options evaluator_options;
91   Minimizer::Options minimizer_options;
92
93   ProblemImpl* problem;
94   scoped_ptr<ProblemImpl> gradient_checking_problem;
95   scoped_ptr<Program> reduced_program;
96   scoped_ptr<LinearSolver> linear_solver;
97   scoped_ptr<IterationCallback> logging_callback;
98   scoped_ptr<IterationCallback> state_updating_callback;
99
100   shared_ptr<Evaluator> evaluator;
101   shared_ptr<CoordinateDescentMinimizer> inner_iteration_minimizer;
102
103   std::vector<double*> removed_parameter_blocks;
104   Vector reduced_parameters;
105   double fixed_cost;
106 };
107
108 // Common functions used by various preprocessors.
109
110 // If OpenMP support is not available and user has requested more than
111 // one thread, then set the *_num_threads options as needed to 1.
112 void ChangeNumThreadsIfNeeded(Solver::Options* options);
113
114 // Extract the effective parameter vector from the preprocessed
115 // problem and setup bits of the Minimizer::Options object that are
116 // common to all Preprocessors.
117 void SetupCommonMinimizerOptions(PreprocessedProblem* pp);
118
119 }  // namespace internal
120 }  // namespace ceres
121
122 #endif  // CERES_INTERNAL_PREPROCESSOR_H_