blob: 99bd6c0c5ddf5c068022474c25b0516acfe248ec [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// 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 <memory>
35#include <string>
36#include <vector>
37
38#include "ceres/coordinate_descent_minimizer.h"
39#include "ceres/evaluator.h"
40#include "ceres/internal/eigen.h"
41#include "ceres/internal/port.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
49namespace ceres {
50namespace internal {
51
52struct 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.
70class 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.
82struct 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 std::unique_ptr<ProblemImpl> gradient_checking_problem;
95 std::unique_ptr<Program> reduced_program;
96 std::unique_ptr<LinearSolver> linear_solver;
97 std::unique_ptr<IterationCallback> logging_callback;
98 std::unique_ptr<IterationCallback> state_updating_callback;
99
100 std::shared_ptr<Evaluator> evaluator;
101 std::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 the user has specified a num_threads > the maximum number of threads
111// available from the compiled threading model, bound the number of threads
112// to the maximum.
113void ChangeNumThreadsIfNeeded(Solver::Options* options);
114
115// Extract the effective parameter vector from the preprocessed
116// problem and setup bits of the Minimizer::Options object that are
117// common to all Preprocessors.
118void SetupCommonMinimizerOptions(PreprocessedProblem* pp);
119
120} // namespace internal
121} // namespace ceres
122
123#endif // CERES_INTERNAL_PREPROCESSOR_H_