evaluator.h revision 0ae28bd5885b5daa526898fcf7c323dc2c3e1963
10ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Ceres Solver - A fast non-linear least squares minimizer 20ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Copyright 2010, 2011, 2012 Google Inc. All rights reserved. 30ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// http://code.google.com/p/ceres-solver/ 40ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 50ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Redistribution and use in source and binary forms, with or without 60ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// modification, are permitted provided that the following conditions are met: 70ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 80ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions of source code must retain the above copyright notice, 90ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// this list of conditions and the following disclaimer. 100ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions in binary form must reproduce the above copyright notice, 110ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// this list of conditions and the following disclaimer in the documentation 120ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// and/or other materials provided with the distribution. 130ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Neither the name of Google Inc. nor the names of its contributors may be 140ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// used to endorse or promote products derived from this software without 150ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// specific prior written permission. 160ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 170ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 180ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 190ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 200ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 210ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 220ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 230ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 240ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 250ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 260ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 270ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// POSSIBILITY OF SUCH DAMAGE. 280ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 290ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Author: sameeragarwal@google.com (Sameer Agarwal) 300ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// keir@google.com (Keir Mierle) 310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#ifndef CERES_INTERNAL_EVALUATOR_H_ 330ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define CERES_INTERNAL_EVALUATOR_H_ 340ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 350ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include <string> 360ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include <vector> 370ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/internal/port.h" 380ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/types.h" 390ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 400ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace ceres { 410ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 420ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongstruct CRSMatrix; 430ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 440ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace internal { 450ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 460ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongclass Program; 470ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongclass SparseMatrix; 480ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 490ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// The Evaluator interface offers a way to interact with a least squares cost 500ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// function that is useful for an optimizer that wants to minimize the least 510ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// squares objective. This insulates the optimizer from issues like Jacobian 520ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// storage, parameterization, etc. 530ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongclass Evaluator { 540ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong public: 550ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong virtual ~Evaluator(); 560ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 570ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong struct Options { 580ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong Options() 590ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong : num_threads(1), 600ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong num_eliminate_blocks(-1), 610ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong linear_solver_type(DENSE_QR) {} 620ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 630ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong int num_threads; 640ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong int num_eliminate_blocks; 650ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong LinearSolverType linear_solver_type; 660ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong }; 670ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 680ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong static Evaluator* Create(const Options& options, 690ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong Program* program, 700ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong string* error); 710ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 720ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 730ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // This is used for computing the cost, residual and Jacobian for 740ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // returning to the user. For actually solving the optimization 750ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // problem, the optimization algorithm uses the ProgramEvaluator 760ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // objects directly. 770ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // 780ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // The residual, gradients and jacobian pointers can be NULL, in 790ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // which case they will not be evaluated. cost cannot be NULL. 800ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // 810ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // The parallelism of the evaluator is controlled by num_threads; it 820ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // should be at least 1. 830ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // 840ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // Note: That this function does not take a parameter vector as 850ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // input. The parameter blocks are evaluated on the values contained 860ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // in the arrays pointed to by their user_state pointers. 870ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // 880ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // Also worth noting is that this function mutates program by 890ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // calling Program::SetParameterOffsetsAndIndex() on it so that an 900ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // evaluator object can be constructed. 910ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong static bool Evaluate(Program* program, 920ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong int num_threads, 930ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong double* cost, 940ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong vector<double>* residuals, 950ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong vector<double>* gradient, 960ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong CRSMatrix* jacobian); 970ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 980ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // Build and return a sparse matrix for storing and working with the Jacobian 990ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // of the objective function. The jacobian has dimensions 1000ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // NumEffectiveParameters() by NumParameters(), and is typically extremely 1010ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // sparse. Since the sparsity pattern of the Jacobian remains constant over 1020ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // the lifetime of the optimization problem, this method is used to 1030ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // instantiate a SparseMatrix object with the appropriate sparsity structure 1040ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // (which can be an expensive operation) and then reused by the optimization 1050ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // algorithm and the various linear solvers. 1060ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // 1070ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // It is expected that the classes implementing this interface will be aware 1080ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // of their client's requirements for the kind of sparse matrix storage and 1090ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // layout that is needed for an efficient implementation. For example 1100ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // CompressedRowOptimizationProblem creates a compressed row representation of 1110ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // the jacobian for use with CHOLMOD, where as BlockOptimizationProblem 1120ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // creates a BlockSparseMatrix representation of the jacobian for use in the 1130ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // Schur complement based methods. 1140ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong virtual SparseMatrix* CreateJacobian() const = 0; 1150ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1160ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // Evaluate the cost function for the given state. Returns the cost, 1170ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // residuals, and jacobian in the corresponding arguments. Both residuals and 1180ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // jacobian are optional; to avoid computing them, pass NULL. 1190ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // 1200ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // If non-NULL, the Jacobian must have a suitable sparsity pattern; only the 1210ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // values array of the jacobian is modified. 1220ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // 1230ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // state is an array of size NumParameters(), cost is a pointer to a single 1240ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // double, and residuals is an array of doubles of size NumResiduals(). 1250ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong virtual bool Evaluate(const double* state, 1260ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong double* cost, 1270ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong double* residuals, 1280ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong double* gradient, 1290ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong SparseMatrix* jacobian) = 0; 1300ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // Make a change delta (of size NumEffectiveParameters()) to state (of size 1320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // NumParameters()) and store the result in state_plus_delta. 1330ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // 1340ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // In the case that there are no parameterizations used, this is equivalent to 1350ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // 1360ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // state_plus_delta[i] = state[i] + delta[i] ; 1370ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // 1380ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // however, the mapping is more complicated in the case of parameterizations 1390ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // like quaternions. This is the same as the "Plus()" operation in 1400ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // local_parameterization.h, but operating over the entire state vector for a 1410ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // problem. 1420ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong virtual bool Plus(const double* state, 1430ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong const double* delta, 1440ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong double* state_plus_delta) const = 0; 1450ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1460ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // The number of parameters in the optimization problem. 1470ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong virtual int NumParameters() const = 0; 1480ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1490ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // This is the effective number of parameters that the optimizer may adjust. 1500ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // This applies when there are parameterizations on some of the parameters. 1510ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong virtual int NumEffectiveParameters() const = 0; 1520ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1530ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // The number of residuals in the optimization problem. 1540ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong virtual int NumResiduals() const = 0; 1550ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}; 1560ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1570ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong} // namespace internal 1580ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong} // namespace ceres 1590ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1600ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#endif // CERES_INTERNAL_EVALUATOR_H_ 161