Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
Keir Mierle | 7492b0d | 2015-03-17 22:30:16 -0700 | [diff] [blame] | 2 | // Copyright 2015 Google Inc. All rights reserved. |
| 3 | // http://ceres-solver.org/ |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 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 | // keir@google.com (Keir Mierle) |
| 31 | |
| 32 | #ifndef CERES_INTERNAL_EVALUATOR_H_ |
| 33 | #define CERES_INTERNAL_EVALUATOR_H_ |
| 34 | |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 35 | #include <map> |
Sameer Agarwal | 446487c | 2022-02-12 08:11:33 -0800 | [diff] [blame] | 36 | #include <memory> |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 37 | #include <string> |
Sameer Agarwal | 4997cbc | 2012-07-02 12:44:34 -0700 | [diff] [blame] | 38 | #include <vector> |
Sameer Agarwal | bdd87c0 | 2013-01-29 16:24:31 -0800 | [diff] [blame] | 39 | |
Mike Vitus | f408f89 | 2018-02-22 10:28:39 -0800 | [diff] [blame] | 40 | #include "ceres/context_impl.h" |
Sameer Agarwal | bdd87c0 | 2013-01-29 16:24:31 -0800 | [diff] [blame] | 41 | #include "ceres/execution_summary.h" |
Sergiu Deitsch | f90833f | 2022-02-07 23:43:19 +0100 | [diff] [blame] | 42 | #include "ceres/internal/disable_warnings.h" |
| 43 | #include "ceres/internal/export.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 44 | #include "ceres/types.h" |
| 45 | |
| 46 | namespace ceres { |
Sameer Agarwal | 4997cbc | 2012-07-02 12:44:34 -0700 | [diff] [blame] | 47 | |
Sameer Agarwal | d3ace02 | 2012-09-23 18:19:49 -0700 | [diff] [blame] | 48 | struct CRSMatrix; |
Keir Mierle | 7bdceb4 | 2018-01-10 17:14:57 -0800 | [diff] [blame] | 49 | class EvaluationCallback; |
Sameer Agarwal | 4997cbc | 2012-07-02 12:44:34 -0700 | [diff] [blame] | 50 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 51 | namespace internal { |
| 52 | |
| 53 | class Program; |
| 54 | class SparseMatrix; |
| 55 | |
| 56 | // The Evaluator interface offers a way to interact with a least squares cost |
| 57 | // function that is useful for an optimizer that wants to minimize the least |
| 58 | // squares objective. This insulates the optimizer from issues like Jacobian |
Sameer Agarwal | 125a0e9 | 2021-12-28 07:05:03 -0800 | [diff] [blame] | 59 | // storage, manifolds, etc. |
Sergiu Deitsch | f90833f | 2022-02-07 23:43:19 +0100 | [diff] [blame] | 60 | class CERES_NO_EXPORT Evaluator { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 61 | public: |
| 62 | virtual ~Evaluator(); |
| 63 | |
| 64 | struct Options { |
Sameer Agarwal | 9814a91 | 2018-04-06 08:38:11 -0700 | [diff] [blame] | 65 | int num_threads = 1; |
| 66 | int num_eliminate_blocks = -1; |
| 67 | LinearSolverType linear_solver_type = DENSE_QR; |
Sameer Agarwal | f6f2f0d | 2022-08-16 15:46:22 -0700 | [diff] [blame] | 68 | SparseLinearAlgebraLibraryType sparse_linear_algebra_library_type = |
| 69 | NO_SPARSE; |
Sameer Agarwal | 9814a91 | 2018-04-06 08:38:11 -0700 | [diff] [blame] | 70 | bool dynamic_sparsity = false; |
| 71 | ContextImpl* context = nullptr; |
| 72 | EvaluationCallback* evaluation_callback = nullptr; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
Sameer Agarwal | ae65219 | 2022-02-02 13:17:29 -0800 | [diff] [blame] | 75 | static std::unique_ptr<Evaluator> Create(const Options& options, |
| 76 | Program* program, |
| 77 | std::string* error); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 78 | |
| 79 | // Build and return a sparse matrix for storing and working with the Jacobian |
| 80 | // of the objective function. The jacobian has dimensions |
| 81 | // NumEffectiveParameters() by NumParameters(), and is typically extremely |
| 82 | // sparse. Since the sparsity pattern of the Jacobian remains constant over |
| 83 | // the lifetime of the optimization problem, this method is used to |
| 84 | // instantiate a SparseMatrix object with the appropriate sparsity structure |
| 85 | // (which can be an expensive operation) and then reused by the optimization |
| 86 | // algorithm and the various linear solvers. |
| 87 | // |
| 88 | // It is expected that the classes implementing this interface will be aware |
| 89 | // of their client's requirements for the kind of sparse matrix storage and |
| 90 | // layout that is needed for an efficient implementation. For example |
| 91 | // CompressedRowOptimizationProblem creates a compressed row representation of |
| 92 | // the jacobian for use with CHOLMOD, where as BlockOptimizationProblem |
| 93 | // creates a BlockSparseMatrix representation of the jacobian for use in the |
| 94 | // Schur complement based methods. |
Sameer Agarwal | ae65219 | 2022-02-02 13:17:29 -0800 | [diff] [blame] | 95 | virtual std::unique_ptr<SparseMatrix> CreateJacobian() const = 0; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 96 | |
Sameer Agarwal | 039ff07 | 2013-02-26 09:15:39 -0800 | [diff] [blame] | 97 | // Options struct to control Evaluator::Evaluate; |
| 98 | struct EvaluateOptions { |
Sameer Agarwal | 039ff07 | 2013-02-26 09:15:39 -0800 | [diff] [blame] | 99 | // If false, the loss function correction is not applied to the |
| 100 | // residual blocks. |
Sameer Agarwal | 9814a91 | 2018-04-06 08:38:11 -0700 | [diff] [blame] | 101 | bool apply_loss_function = true; |
Keir Mierle | 7bdceb4 | 2018-01-10 17:14:57 -0800 | [diff] [blame] | 102 | |
| 103 | // If false, this evaluation point is the same as the last one. |
Sameer Agarwal | 9814a91 | 2018-04-06 08:38:11 -0700 | [diff] [blame] | 104 | bool new_evaluation_point = true; |
Sameer Agarwal | 039ff07 | 2013-02-26 09:15:39 -0800 | [diff] [blame] | 105 | }; |
| 106 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 107 | // Evaluate the cost function for the given state. Returns the cost, |
| 108 | // residuals, and jacobian in the corresponding arguments. Both residuals and |
Sameer Agarwal | ae65219 | 2022-02-02 13:17:29 -0800 | [diff] [blame] | 109 | // jacobian are optional; to avoid computing them, pass nullptr. |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 110 | // |
Sameer Agarwal | ae65219 | 2022-02-02 13:17:29 -0800 | [diff] [blame] | 111 | // If non-nullptr, the Jacobian must have a suitable sparsity pattern; only |
| 112 | // the values array of the jacobian is modified. |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 113 | // |
| 114 | // state is an array of size NumParameters(), cost is a pointer to a single |
| 115 | // double, and residuals is an array of doubles of size NumResiduals(). |
Sameer Agarwal | 039ff07 | 2013-02-26 09:15:39 -0800 | [diff] [blame] | 116 | virtual bool Evaluate(const EvaluateOptions& evaluate_options, |
| 117 | const double* state, |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 118 | double* cost, |
| 119 | double* residuals, |
Keir Mierle | f44907f | 2012-07-06 13:52:32 -0700 | [diff] [blame] | 120 | double* gradient, |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 121 | SparseMatrix* jacobian) = 0; |
| 122 | |
Sameer Agarwal | 039ff07 | 2013-02-26 09:15:39 -0800 | [diff] [blame] | 123 | // Variant of Evaluator::Evaluate where the user wishes to use the |
| 124 | // default EvaluateOptions struct. This is mostly here as a |
| 125 | // convenience method. |
Sameer Agarwal | 6fb1024 | 2013-02-26 22:20:18 -0800 | [diff] [blame] | 126 | bool Evaluate(const double* state, |
| 127 | double* cost, |
| 128 | double* residuals, |
| 129 | double* gradient, |
| 130 | SparseMatrix* jacobian) { |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 131 | return Evaluate( |
| 132 | EvaluateOptions(), state, cost, residuals, gradient, jacobian); |
Sameer Agarwal | 039ff07 | 2013-02-26 09:15:39 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 135 | // Make a change delta (of size NumEffectiveParameters()) to state (of size |
| 136 | // NumParameters()) and store the result in state_plus_delta. |
| 137 | // |
Sameer Agarwal | 125a0e9 | 2021-12-28 07:05:03 -0800 | [diff] [blame] | 138 | // In the case that there are no manifolds used, this is equivalent to |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 139 | // |
| 140 | // state_plus_delta[i] = state[i] + delta[i] ; |
| 141 | // |
Sameer Agarwal | 125a0e9 | 2021-12-28 07:05:03 -0800 | [diff] [blame] | 142 | // however, the mapping is more complicated in the case of manifolds |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 143 | // like quaternions. This is the same as the "Plus()" operation in |
Sameer Agarwal | 125a0e9 | 2021-12-28 07:05:03 -0800 | [diff] [blame] | 144 | // manifold.h, but operating over the entire state vector for a |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 145 | // problem. |
| 146 | virtual bool Plus(const double* state, |
| 147 | const double* delta, |
| 148 | double* state_plus_delta) const = 0; |
| 149 | |
| 150 | // The number of parameters in the optimization problem. |
| 151 | virtual int NumParameters() const = 0; |
| 152 | |
| 153 | // This is the effective number of parameters that the optimizer may adjust. |
Sameer Agarwal | 125a0e9 | 2021-12-28 07:05:03 -0800 | [diff] [blame] | 154 | // This applies when there are manifolds on some of the parameters. |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 155 | virtual int NumEffectiveParameters() const = 0; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 156 | |
| 157 | // The number of residuals in the optimization problem. |
| 158 | virtual int NumResiduals() const = 0; |
Sameer Agarwal | bdd87c0 | 2013-01-29 16:24:31 -0800 | [diff] [blame] | 159 | |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 160 | // The following two methods return copies instead of references so |
| 161 | // that the base class implementation does not have to worry about |
| 162 | // life time issues. Further, these calls are not expected to be |
| 163 | // frequent or performance sensitive. |
Sameer Agarwal | 2145c10 | 2018-02-05 16:35:06 -0800 | [diff] [blame] | 164 | virtual std::map<std::string, CallStatistics> Statistics() const { |
Sergiu Deitsch | c8658c8 | 2022-02-20 02:22:17 +0100 | [diff] [blame] | 165 | return {}; |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 166 | } |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | } // namespace internal |
| 170 | } // namespace ceres |
| 171 | |
Sergiu Deitsch | f90833f | 2022-02-07 23:43:19 +0100 | [diff] [blame] | 172 | #include "ceres/internal/reenable_warnings.h" |
| 173 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 174 | #endif // CERES_INTERNAL_EVALUATOR_H_ |