blob: cbb7aa23c34574d09840308578a98f98c3d1199a [file] [log] [blame]
Keir Mierle8ebb0732012-04-30 23:09:08 -07001// Ceres Solver - A fast non-linear least squares minimizer
Keir Mierle7492b0d2015-03-17 22:30:16 -07002// Copyright 2015 Google Inc. All rights reserved.
3// http://ceres-solver.org/
Keir Mierle8ebb0732012-04-30 23:09:08 -07004//
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 Agarwal509f68c2013-02-20 01:39:03 -080035#include <map>
Keir Mierle8ebb0732012-04-30 23:09:08 -070036#include <string>
Sameer Agarwal4997cbc2012-07-02 12:44:34 -070037#include <vector>
Sameer Agarwalbdd87c02013-01-29 16:24:31 -080038
Mike Vitusf408f892018-02-22 10:28:39 -080039#include "ceres/context_impl.h"
Sameer Agarwalbdd87c02013-01-29 16:24:31 -080040#include "ceres/execution_summary.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070041#include "ceres/internal/port.h"
42#include "ceres/types.h"
43
44namespace ceres {
Sameer Agarwal4997cbc2012-07-02 12:44:34 -070045
Sameer Agarwald3ace022012-09-23 18:19:49 -070046struct CRSMatrix;
Sameer Agarwal4997cbc2012-07-02 12:44:34 -070047
Keir Mierle8ebb0732012-04-30 23:09:08 -070048namespace internal {
49
50class Program;
51class SparseMatrix;
52
53// The Evaluator interface offers a way to interact with a least squares cost
54// function that is useful for an optimizer that wants to minimize the least
55// squares objective. This insulates the optimizer from issues like Jacobian
56// storage, parameterization, etc.
57class Evaluator {
58 public:
59 virtual ~Evaluator();
60
61 struct Options {
62 Options()
63 : num_threads(1),
64 num_eliminate_blocks(-1),
Richard Stebbing32530782014-04-26 07:42:23 +010065 linear_solver_type(DENSE_QR),
Mike Vitusf408f892018-02-22 10:28:39 -080066 dynamic_sparsity(false),
67 context(NULL) {}
Keir Mierle8ebb0732012-04-30 23:09:08 -070068
69 int num_threads;
70 int num_eliminate_blocks;
71 LinearSolverType linear_solver_type;
Richard Stebbing32530782014-04-26 07:42:23 +010072 bool dynamic_sparsity;
Mike Vitusf408f892018-02-22 10:28:39 -080073 ContextImpl* context;
Keir Mierle8ebb0732012-04-30 23:09:08 -070074 };
75
76 static Evaluator* Create(const Options& options,
77 Program* program,
Sameer Agarwal05a07ec2015-01-07 15:10:46 -080078 std::string* error);
Keir Mierle8ebb0732012-04-30 23:09:08 -070079
80 // Build and return a sparse matrix for storing and working with the Jacobian
81 // of the objective function. The jacobian has dimensions
82 // NumEffectiveParameters() by NumParameters(), and is typically extremely
83 // sparse. Since the sparsity pattern of the Jacobian remains constant over
84 // the lifetime of the optimization problem, this method is used to
85 // instantiate a SparseMatrix object with the appropriate sparsity structure
86 // (which can be an expensive operation) and then reused by the optimization
87 // algorithm and the various linear solvers.
88 //
89 // It is expected that the classes implementing this interface will be aware
90 // of their client's requirements for the kind of sparse matrix storage and
91 // layout that is needed for an efficient implementation. For example
92 // CompressedRowOptimizationProblem creates a compressed row representation of
93 // the jacobian for use with CHOLMOD, where as BlockOptimizationProblem
94 // creates a BlockSparseMatrix representation of the jacobian for use in the
95 // Schur complement based methods.
96 virtual SparseMatrix* CreateJacobian() const = 0;
97
Sameer Agarwal039ff072013-02-26 09:15:39 -080098 // Options struct to control Evaluator::Evaluate;
99 struct EvaluateOptions {
100 EvaluateOptions()
101 : apply_loss_function(true) {
102 }
103
104 // If false, the loss function correction is not applied to the
105 // residual blocks.
106 bool apply_loss_function;
107 };
108
Keir Mierle8ebb0732012-04-30 23:09:08 -0700109 // Evaluate the cost function for the given state. Returns the cost,
110 // residuals, and jacobian in the corresponding arguments. Both residuals and
111 // jacobian are optional; to avoid computing them, pass NULL.
112 //
113 // If non-NULL, the Jacobian must have a suitable sparsity pattern; only the
114 // values array of the jacobian is modified.
115 //
116 // state is an array of size NumParameters(), cost is a pointer to a single
117 // double, and residuals is an array of doubles of size NumResiduals().
Sameer Agarwal039ff072013-02-26 09:15:39 -0800118 virtual bool Evaluate(const EvaluateOptions& evaluate_options,
119 const double* state,
Keir Mierle8ebb0732012-04-30 23:09:08 -0700120 double* cost,
121 double* residuals,
Keir Mierlef44907f2012-07-06 13:52:32 -0700122 double* gradient,
Keir Mierle8ebb0732012-04-30 23:09:08 -0700123 SparseMatrix* jacobian) = 0;
124
Sameer Agarwal039ff072013-02-26 09:15:39 -0800125 // Variant of Evaluator::Evaluate where the user wishes to use the
126 // default EvaluateOptions struct. This is mostly here as a
127 // convenience method.
Sameer Agarwal6fb10242013-02-26 22:20:18 -0800128 bool Evaluate(const double* state,
129 double* cost,
130 double* residuals,
131 double* gradient,
132 SparseMatrix* jacobian) {
Sameer Agarwal039ff072013-02-26 09:15:39 -0800133 return Evaluate(EvaluateOptions(),
134 state,
135 cost,
136 residuals,
137 gradient,
138 jacobian);
139 }
140
Keir Mierle8ebb0732012-04-30 23:09:08 -0700141 // Make a change delta (of size NumEffectiveParameters()) to state (of size
142 // NumParameters()) and store the result in state_plus_delta.
143 //
144 // In the case that there are no parameterizations used, this is equivalent to
145 //
146 // state_plus_delta[i] = state[i] + delta[i] ;
147 //
148 // however, the mapping is more complicated in the case of parameterizations
149 // like quaternions. This is the same as the "Plus()" operation in
150 // local_parameterization.h, but operating over the entire state vector for a
151 // problem.
152 virtual bool Plus(const double* state,
153 const double* delta,
154 double* state_plus_delta) const = 0;
155
156 // The number of parameters in the optimization problem.
157 virtual int NumParameters() const = 0;
158
159 // This is the effective number of parameters that the optimizer may adjust.
160 // This applies when there are parameterizations on some of the parameters.
161 virtual int NumEffectiveParameters() const = 0;
162
163 // The number of residuals in the optimization problem.
164 virtual int NumResiduals() const = 0;
Sameer Agarwalbdd87c02013-01-29 16:24:31 -0800165
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800166 // The following two methods return copies instead of references so
167 // that the base class implementation does not have to worry about
168 // life time issues. Further, these calls are not expected to be
169 // frequent or performance sensitive.
Sameer Agarwal2145c102018-02-05 16:35:06 -0800170 virtual std::map<std::string, CallStatistics> Statistics() const {
171 return std::map<std::string, CallStatistics>();
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800172 }
Keir Mierle8ebb0732012-04-30 23:09:08 -0700173};
174
175} // namespace internal
176} // namespace ceres
177
178#endif // CERES_INTERNAL_EVALUATOR_H_