|
Packit |
ea1746 |
// Ceres Solver - A fast non-linear least squares minimizer
|
|
Packit |
ea1746 |
// Copyright 2015 Google Inc. All rights reserved.
|
|
Packit |
ea1746 |
// http://ceres-solver.org/
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Redistribution and use in source and binary forms, with or without
|
|
Packit |
ea1746 |
// modification, are permitted provided that the following conditions are met:
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// * Redistributions of source code must retain the above copyright notice,
|
|
Packit |
ea1746 |
// this list of conditions and the following disclaimer.
|
|
Packit |
ea1746 |
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
Packit |
ea1746 |
// this list of conditions and the following disclaimer in the documentation
|
|
Packit |
ea1746 |
// and/or other materials provided with the distribution.
|
|
Packit |
ea1746 |
// * Neither the name of Google Inc. nor the names of its contributors may be
|
|
Packit |
ea1746 |
// used to endorse or promote products derived from this software without
|
|
Packit |
ea1746 |
// specific prior written permission.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
Packit |
ea1746 |
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit |
ea1746 |
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit |
ea1746 |
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
Packit |
ea1746 |
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
Packit |
ea1746 |
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
Packit |
ea1746 |
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
Packit |
ea1746 |
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
Packit |
ea1746 |
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
Packit |
ea1746 |
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
Packit |
ea1746 |
// POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Author: sameeragarwal@google.com (Sameer Agarwal)
|
|
Packit |
ea1746 |
// keir@google.com (Keir Mierle)
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#ifndef CERES_INTERNAL_EVALUATOR_H_
|
|
Packit |
ea1746 |
#define CERES_INTERNAL_EVALUATOR_H_
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include <map>
|
|
Packit |
ea1746 |
#include <string>
|
|
Packit |
ea1746 |
#include <vector>
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include "ceres/execution_summary.h"
|
|
Packit |
ea1746 |
#include "ceres/internal/port.h"
|
|
Packit |
ea1746 |
#include "ceres/types.h"
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
namespace ceres {
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
struct CRSMatrix;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
namespace internal {
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
class Program;
|
|
Packit |
ea1746 |
class SparseMatrix;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// The Evaluator interface offers a way to interact with a least squares cost
|
|
Packit |
ea1746 |
// function that is useful for an optimizer that wants to minimize the least
|
|
Packit |
ea1746 |
// squares objective. This insulates the optimizer from issues like Jacobian
|
|
Packit |
ea1746 |
// storage, parameterization, etc.
|
|
Packit |
ea1746 |
class Evaluator {
|
|
Packit |
ea1746 |
public:
|
|
Packit |
ea1746 |
virtual ~Evaluator();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
struct Options {
|
|
Packit |
ea1746 |
Options()
|
|
Packit |
ea1746 |
: num_threads(1),
|
|
Packit |
ea1746 |
num_eliminate_blocks(-1),
|
|
Packit |
ea1746 |
linear_solver_type(DENSE_QR),
|
|
Packit |
ea1746 |
dynamic_sparsity(false) {}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
int num_threads;
|
|
Packit |
ea1746 |
int num_eliminate_blocks;
|
|
Packit |
ea1746 |
LinearSolverType linear_solver_type;
|
|
Packit |
ea1746 |
bool dynamic_sparsity;
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
static Evaluator* Create(const Options& options,
|
|
Packit |
ea1746 |
Program* program,
|
|
Packit |
ea1746 |
std::string* error);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// This is used for computing the cost, residual and Jacobian for
|
|
Packit |
ea1746 |
// returning to the user. For actually solving the optimization
|
|
Packit |
ea1746 |
// problem, the optimization algorithm uses the ProgramEvaluator
|
|
Packit |
ea1746 |
// objects directly.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// The residual, gradients and jacobian pointers can be NULL, in
|
|
Packit |
ea1746 |
// which case they will not be evaluated. cost cannot be NULL.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// The parallelism of the evaluator is controlled by num_threads; it
|
|
Packit |
ea1746 |
// should be at least 1.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Note: That this function does not take a parameter vector as
|
|
Packit |
ea1746 |
// input. The parameter blocks are evaluated on the values contained
|
|
Packit |
ea1746 |
// in the arrays pointed to by their user_state pointers.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Also worth noting is that this function mutates program by
|
|
Packit |
ea1746 |
// calling Program::SetParameterOffsetsAndIndex() on it so that an
|
|
Packit |
ea1746 |
// evaluator object can be constructed.
|
|
Packit |
ea1746 |
static bool Evaluate(Program* program,
|
|
Packit |
ea1746 |
int num_threads,
|
|
Packit |
ea1746 |
double* cost,
|
|
Packit |
ea1746 |
std::vector<double>* residuals,
|
|
Packit |
ea1746 |
std::vector<double>* gradient,
|
|
Packit |
ea1746 |
CRSMatrix* jacobian);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Build and return a sparse matrix for storing and working with the Jacobian
|
|
Packit |
ea1746 |
// of the objective function. The jacobian has dimensions
|
|
Packit |
ea1746 |
// NumEffectiveParameters() by NumParameters(), and is typically extremely
|
|
Packit |
ea1746 |
// sparse. Since the sparsity pattern of the Jacobian remains constant over
|
|
Packit |
ea1746 |
// the lifetime of the optimization problem, this method is used to
|
|
Packit |
ea1746 |
// instantiate a SparseMatrix object with the appropriate sparsity structure
|
|
Packit |
ea1746 |
// (which can be an expensive operation) and then reused by the optimization
|
|
Packit |
ea1746 |
// algorithm and the various linear solvers.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// It is expected that the classes implementing this interface will be aware
|
|
Packit |
ea1746 |
// of their client's requirements for the kind of sparse matrix storage and
|
|
Packit |
ea1746 |
// layout that is needed for an efficient implementation. For example
|
|
Packit |
ea1746 |
// CompressedRowOptimizationProblem creates a compressed row representation of
|
|
Packit |
ea1746 |
// the jacobian for use with CHOLMOD, where as BlockOptimizationProblem
|
|
Packit |
ea1746 |
// creates a BlockSparseMatrix representation of the jacobian for use in the
|
|
Packit |
ea1746 |
// Schur complement based methods.
|
|
Packit |
ea1746 |
virtual SparseMatrix* CreateJacobian() const = 0;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Options struct to control Evaluator::Evaluate;
|
|
Packit |
ea1746 |
struct EvaluateOptions {
|
|
Packit |
ea1746 |
EvaluateOptions()
|
|
Packit |
ea1746 |
: apply_loss_function(true) {
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// If false, the loss function correction is not applied to the
|
|
Packit |
ea1746 |
// residual blocks.
|
|
Packit |
ea1746 |
bool apply_loss_function;
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Evaluate the cost function for the given state. Returns the cost,
|
|
Packit |
ea1746 |
// residuals, and jacobian in the corresponding arguments. Both residuals and
|
|
Packit |
ea1746 |
// jacobian are optional; to avoid computing them, pass NULL.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// If non-NULL, the Jacobian must have a suitable sparsity pattern; only the
|
|
Packit |
ea1746 |
// values array of the jacobian is modified.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// state is an array of size NumParameters(), cost is a pointer to a single
|
|
Packit |
ea1746 |
// double, and residuals is an array of doubles of size NumResiduals().
|
|
Packit |
ea1746 |
virtual bool Evaluate(const EvaluateOptions& evaluate_options,
|
|
Packit |
ea1746 |
const double* state,
|
|
Packit |
ea1746 |
double* cost,
|
|
Packit |
ea1746 |
double* residuals,
|
|
Packit |
ea1746 |
double* gradient,
|
|
Packit |
ea1746 |
SparseMatrix* jacobian) = 0;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Variant of Evaluator::Evaluate where the user wishes to use the
|
|
Packit |
ea1746 |
// default EvaluateOptions struct. This is mostly here as a
|
|
Packit |
ea1746 |
// convenience method.
|
|
Packit |
ea1746 |
bool Evaluate(const double* state,
|
|
Packit |
ea1746 |
double* cost,
|
|
Packit |
ea1746 |
double* residuals,
|
|
Packit |
ea1746 |
double* gradient,
|
|
Packit |
ea1746 |
SparseMatrix* jacobian) {
|
|
Packit |
ea1746 |
return Evaluate(EvaluateOptions(),
|
|
Packit |
ea1746 |
state,
|
|
Packit |
ea1746 |
cost,
|
|
Packit |
ea1746 |
residuals,
|
|
Packit |
ea1746 |
gradient,
|
|
Packit |
ea1746 |
jacobian);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Make a change delta (of size NumEffectiveParameters()) to state (of size
|
|
Packit |
ea1746 |
// NumParameters()) and store the result in state_plus_delta.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// In the case that there are no parameterizations used, this is equivalent to
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// state_plus_delta[i] = state[i] + delta[i] ;
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// however, the mapping is more complicated in the case of parameterizations
|
|
Packit |
ea1746 |
// like quaternions. This is the same as the "Plus()" operation in
|
|
Packit |
ea1746 |
// local_parameterization.h, but operating over the entire state vector for a
|
|
Packit |
ea1746 |
// problem.
|
|
Packit |
ea1746 |
virtual bool Plus(const double* state,
|
|
Packit |
ea1746 |
const double* delta,
|
|
Packit |
ea1746 |
double* state_plus_delta) const = 0;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// The number of parameters in the optimization problem.
|
|
Packit |
ea1746 |
virtual int NumParameters() const = 0;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// This is the effective number of parameters that the optimizer may adjust.
|
|
Packit |
ea1746 |
// This applies when there are parameterizations on some of the parameters.
|
|
Packit |
ea1746 |
virtual int NumEffectiveParameters() const = 0;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// The number of residuals in the optimization problem.
|
|
Packit |
ea1746 |
virtual int NumResiduals() const = 0;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// The following two methods return copies instead of references so
|
|
Packit |
ea1746 |
// that the base class implementation does not have to worry about
|
|
Packit |
ea1746 |
// life time issues. Further, these calls are not expected to be
|
|
Packit |
ea1746 |
// frequent or performance sensitive.
|
|
Packit |
ea1746 |
virtual std::map<std::string, int> CallStatistics() const {
|
|
Packit |
ea1746 |
return std::map<std::string, int>();
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
virtual std::map<std::string, double> TimeStatistics() const {
|
|
Packit |
ea1746 |
return std::map<std::string, double>();
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
} // namespace internal
|
|
Packit |
ea1746 |
} // namespace ceres
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#endif // CERES_INTERNAL_EVALUATOR_H_
|