Blame include/ceres/problem.h

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
// The Problem object is used to build and hold least squares problems.
Packit ea1746
Packit ea1746
#ifndef CERES_PUBLIC_PROBLEM_H_
Packit ea1746
#define CERES_PUBLIC_PROBLEM_H_
Packit ea1746
Packit ea1746
#include <cstddef>
Packit ea1746
#include <map>
Packit ea1746
#include <set>
Packit ea1746
#include <vector>
Packit ea1746
Packit ea1746
#include "glog/logging.h"
Packit ea1746
#include "ceres/internal/macros.h"
Packit ea1746
#include "ceres/internal/port.h"
Packit ea1746
#include "ceres/internal/scoped_ptr.h"
Packit ea1746
#include "ceres/types.h"
Packit ea1746
#include "ceres/internal/disable_warnings.h"
Packit ea1746
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
Packit ea1746
class CostFunction;
Packit ea1746
class LossFunction;
Packit ea1746
class LocalParameterization;
Packit ea1746
class Solver;
Packit ea1746
struct CRSMatrix;
Packit ea1746
Packit ea1746
namespace internal {
Packit ea1746
class Preprocessor;
Packit ea1746
class ProblemImpl;
Packit ea1746
class ParameterBlock;
Packit ea1746
class ResidualBlock;
Packit ea1746
}  // namespace internal
Packit ea1746
Packit ea1746
// A ResidualBlockId is an opaque handle clients can use to remove residual
Packit ea1746
// blocks from a Problem after adding them.
Packit ea1746
typedef internal::ResidualBlock* ResidualBlockId;
Packit ea1746
Packit ea1746
// A class to represent non-linear least squares problems. Such
Packit ea1746
// problems have a cost function that is a sum of error terms (known
Packit ea1746
// as "residuals"), where each residual is a function of some subset
Packit ea1746
// of the parameters. The cost function takes the form
Packit ea1746
//
Packit ea1746
//    N    1
Packit ea1746
//   SUM  --- loss( || r_i1, r_i2,..., r_ik ||^2  ),
Packit ea1746
//   i=1   2
Packit ea1746
//
Packit ea1746
// where
Packit ea1746
//
Packit ea1746
//   r_ij     is residual number i, component j; the residual is a
Packit ea1746
//            function of some subset of the parameters x1...xk. For
Packit ea1746
//            example, in a structure from motion problem a residual
Packit ea1746
//            might be the difference between a measured point in an
Packit ea1746
//            image and the reprojected position for the matching
Packit ea1746
//            camera, point pair. The residual would have two
Packit ea1746
//            components, error in x and error in y.
Packit ea1746
//
Packit ea1746
//   loss(y)  is the loss function; for example, squared error or
Packit ea1746
//            Huber L1 loss. If loss(y) = y, then the cost function is
Packit ea1746
//            non-robustified least squares.
Packit ea1746
//
Packit ea1746
// This class is specifically designed to address the important subset
Packit ea1746
// of "sparse" least squares problems, where each component of the
Packit ea1746
// residual depends only on a small number number of parameters, even
Packit ea1746
// though the total number of residuals and parameters may be very
Packit ea1746
// large. This property affords tremendous gains in scale, allowing
Packit ea1746
// efficient solving of large problems that are otherwise
Packit ea1746
// inaccessible.
Packit ea1746
//
Packit ea1746
// The canonical example of a sparse least squares problem is
Packit ea1746
// "structure-from-motion" (SFM), where the parameters are points and
Packit ea1746
// cameras, and residuals are reprojection errors. Typically a single
Packit ea1746
// residual will depend only on 9 parameters (3 for the point, 6 for
Packit ea1746
// the camera).
Packit ea1746
//
Packit ea1746
// To create a least squares problem, use the AddResidualBlock() and
Packit ea1746
// AddParameterBlock() methods, documented below. Here is an example least
Packit ea1746
// squares problem containing 3 parameter blocks of sizes 3, 4 and 5
Packit ea1746
// respectively and two residual terms of size 2 and 6:
Packit ea1746
//
Packit ea1746
//   double x1[] = { 1.0, 2.0, 3.0 };
Packit ea1746
//   double x2[] = { 1.0, 2.0, 3.0, 5.0 };
Packit ea1746
//   double x3[] = { 1.0, 2.0, 3.0, 6.0, 7.0 };
Packit ea1746
//
Packit ea1746
//   Problem problem;
Packit ea1746
//
Packit ea1746
//   problem.AddResidualBlock(new MyUnaryCostFunction(...), x1);
Packit ea1746
//   problem.AddResidualBlock(new MyBinaryCostFunction(...), x2, x3);
Packit ea1746
//
Packit ea1746
// Please see cost_function.h for details of the CostFunction object.
Packit ea1746
class CERES_EXPORT Problem {
Packit ea1746
 public:
Packit ea1746
  struct CERES_EXPORT Options {
Packit ea1746
    Options()
Packit ea1746
        : cost_function_ownership(TAKE_OWNERSHIP),
Packit ea1746
          loss_function_ownership(TAKE_OWNERSHIP),
Packit ea1746
          local_parameterization_ownership(TAKE_OWNERSHIP),
Packit ea1746
          enable_fast_removal(false),
Packit ea1746
          disable_all_safety_checks(false) {}
Packit ea1746
Packit ea1746
    // These flags control whether the Problem object owns the cost
Packit ea1746
    // functions, loss functions, and parameterizations passed into
Packit ea1746
    // the Problem. If set to TAKE_OWNERSHIP, then the problem object
Packit ea1746
    // will delete the corresponding cost or loss functions on
Packit ea1746
    // destruction. The destructor is careful to delete the pointers
Packit ea1746
    // only once, since sharing cost/loss/parameterizations is
Packit ea1746
    // allowed.
Packit ea1746
    Ownership cost_function_ownership;
Packit ea1746
    Ownership loss_function_ownership;
Packit ea1746
    Ownership local_parameterization_ownership;
Packit ea1746
Packit ea1746
    // If true, trades memory for faster RemoveResidualBlock() and
Packit ea1746
    // RemoveParameterBlock() operations.
Packit ea1746
    //
Packit ea1746
    // By default, RemoveParameterBlock() and RemoveResidualBlock() take time
Packit ea1746
    // proportional to the size of the entire problem.  If you only ever remove
Packit ea1746
    // parameters or residuals from the problem occassionally, this might be
Packit ea1746
    // acceptable.  However, if you have memory to spare, enable this option to
Packit ea1746
    // make RemoveParameterBlock() take time proportional to the number of
Packit ea1746
    // residual blocks that depend on it, and RemoveResidualBlock() take (on
Packit ea1746
    // average) constant time.
Packit ea1746
    //
Packit ea1746
    // The increase in memory usage is twofold: an additonal hash set per
Packit ea1746
    // parameter block containing all the residuals that depend on the parameter
Packit ea1746
    // block; and a hash set in the problem containing all residuals.
Packit ea1746
    bool enable_fast_removal;
Packit ea1746
Packit ea1746
    // By default, Ceres performs a variety of safety checks when constructing
Packit ea1746
    // the problem. There is a small but measurable performance penalty to
Packit ea1746
    // these checks, typically around 5% of construction time. If you are sure
Packit ea1746
    // your problem construction is correct, and 5% of the problem construction
Packit ea1746
    // time is truly an overhead you want to avoid, then you can set
Packit ea1746
    // disable_all_safety_checks to true.
Packit ea1746
    //
Packit ea1746
    // WARNING: Do not set this to true, unless you are absolutely sure of what
Packit ea1746
    // you are doing.
Packit ea1746
    bool disable_all_safety_checks;
Packit ea1746
  };
Packit ea1746
Packit ea1746
  // The default constructor is equivalent to the
Packit ea1746
  // invocation Problem(Problem::Options()).
Packit ea1746
  Problem();
Packit ea1746
  explicit Problem(const Options& options);
Packit ea1746
Packit ea1746
  ~Problem();
Packit ea1746
Packit ea1746
  // Add a residual block to the overall cost function. The cost
Packit ea1746
  // function carries with it information about the sizes of the
Packit ea1746
  // parameter blocks it expects. The function checks that these match
Packit ea1746
  // the sizes of the parameter blocks listed in parameter_blocks. The
Packit ea1746
  // program aborts if a mismatch is detected. loss_function can be
Packit ea1746
  // NULL, in which case the cost of the term is just the squared norm
Packit ea1746
  // of the residuals.
Packit ea1746
  //
Packit ea1746
  // The user has the option of explicitly adding the parameter blocks
Packit ea1746
  // using AddParameterBlock. This causes additional correctness
Packit ea1746
  // checking; however, AddResidualBlock implicitly adds the parameter
Packit ea1746
  // blocks if they are not present, so calling AddParameterBlock
Packit ea1746
  // explicitly is not required.
Packit ea1746
  //
Packit ea1746
  // The Problem object by default takes ownership of the
Packit ea1746
  // cost_function and loss_function pointers. These objects remain
Packit ea1746
  // live for the life of the Problem object. If the user wishes to
Packit ea1746
  // keep control over the destruction of these objects, then they can
Packit ea1746
  // do this by setting the corresponding enums in the Options struct.
Packit ea1746
  //
Packit ea1746
  // Note: Even though the Problem takes ownership of cost_function
Packit ea1746
  // and loss_function, it does not preclude the user from re-using
Packit ea1746
  // them in another residual block. The destructor takes care to call
Packit ea1746
  // delete on each cost_function or loss_function pointer only once,
Packit ea1746
  // regardless of how many residual blocks refer to them.
Packit ea1746
  //
Packit ea1746
  // Example usage:
Packit ea1746
  //
Packit ea1746
  //   double x1[] = {1.0, 2.0, 3.0};
Packit ea1746
  //   double x2[] = {1.0, 2.0, 5.0, 6.0};
Packit ea1746
  //   double x3[] = {3.0, 6.0, 2.0, 5.0, 1.0};
Packit ea1746
  //
Packit ea1746
  //   Problem problem;
Packit ea1746
  //
Packit ea1746
  //   problem.AddResidualBlock(new MyUnaryCostFunction(...), NULL, x1);
Packit ea1746
  //   problem.AddResidualBlock(new MyBinaryCostFunction(...), NULL, x2, x1);
Packit ea1746
  //
Packit ea1746
  ResidualBlockId AddResidualBlock(
Packit ea1746
      CostFunction* cost_function,
Packit ea1746
      LossFunction* loss_function,
Packit ea1746
      const std::vector<double*>& parameter_blocks);
Packit ea1746
Packit ea1746
  // Convenience methods for adding residuals with a small number of
Packit ea1746
  // parameters. This is the common case. Instead of specifying the
Packit ea1746
  // parameter block arguments as a vector, list them as pointers.
Packit ea1746
  ResidualBlockId AddResidualBlock(CostFunction* cost_function,
Packit ea1746
                                   LossFunction* loss_function,
Packit ea1746
                                   double* x0);
Packit ea1746
  ResidualBlockId AddResidualBlock(CostFunction* cost_function,
Packit ea1746
                                   LossFunction* loss_function,
Packit ea1746
                                   double* x0, double* x1);
Packit ea1746
  ResidualBlockId AddResidualBlock(CostFunction* cost_function,
Packit ea1746
                                   LossFunction* loss_function,
Packit ea1746
                                   double* x0, double* x1, double* x2);
Packit ea1746
  ResidualBlockId AddResidualBlock(CostFunction* cost_function,
Packit ea1746
                                   LossFunction* loss_function,
Packit ea1746
                                   double* x0, double* x1, double* x2,
Packit ea1746
                                   double* x3);
Packit ea1746
  ResidualBlockId AddResidualBlock(CostFunction* cost_function,
Packit ea1746
                                   LossFunction* loss_function,
Packit ea1746
                                   double* x0, double* x1, double* x2,
Packit ea1746
                                   double* x3, double* x4);
Packit ea1746
  ResidualBlockId AddResidualBlock(CostFunction* cost_function,
Packit ea1746
                                   LossFunction* loss_function,
Packit ea1746
                                   double* x0, double* x1, double* x2,
Packit ea1746
                                   double* x3, double* x4, double* x5);
Packit ea1746
  ResidualBlockId AddResidualBlock(CostFunction* cost_function,
Packit ea1746
                                   LossFunction* loss_function,
Packit ea1746
                                   double* x0, double* x1, double* x2,
Packit ea1746
                                   double* x3, double* x4, double* x5,
Packit ea1746
                                   double* x6);
Packit ea1746
  ResidualBlockId AddResidualBlock(CostFunction* cost_function,
Packit ea1746
                                   LossFunction* loss_function,
Packit ea1746
                                   double* x0, double* x1, double* x2,
Packit ea1746
                                   double* x3, double* x4, double* x5,
Packit ea1746
                                   double* x6, double* x7);
Packit ea1746
  ResidualBlockId AddResidualBlock(CostFunction* cost_function,
Packit ea1746
                                   LossFunction* loss_function,
Packit ea1746
                                   double* x0, double* x1, double* x2,
Packit ea1746
                                   double* x3, double* x4, double* x5,
Packit ea1746
                                   double* x6, double* x7, double* x8);
Packit ea1746
  ResidualBlockId AddResidualBlock(CostFunction* cost_function,
Packit ea1746
                                   LossFunction* loss_function,
Packit ea1746
                                   double* x0, double* x1, double* x2,
Packit ea1746
                                   double* x3, double* x4, double* x5,
Packit ea1746
                                   double* x6, double* x7, double* x8,
Packit ea1746
                                   double* x9);
Packit ea1746
Packit ea1746
  // Add a parameter block with appropriate size to the problem.
Packit ea1746
  // Repeated calls with the same arguments are ignored. Repeated
Packit ea1746
  // calls with the same double pointer but a different size results
Packit ea1746
  // in undefined behaviour.
Packit ea1746
  void AddParameterBlock(double* values, int size);
Packit ea1746
Packit ea1746
  // Add a parameter block with appropriate size and parameterization
Packit ea1746
  // to the problem. Repeated calls with the same arguments are
Packit ea1746
  // ignored. Repeated calls with the same double pointer but a
Packit ea1746
  // different size results in undefined behaviour.
Packit ea1746
  void AddParameterBlock(double* values,
Packit ea1746
                         int size,
Packit ea1746
                         LocalParameterization* local_parameterization);
Packit ea1746
Packit ea1746
  // Remove a parameter block from the problem. The parameterization of the
Packit ea1746
  // parameter block, if it exists, will persist until the deletion of the
Packit ea1746
  // problem (similar to cost/loss functions in residual block removal). Any
Packit ea1746
  // residual blocks that depend on the parameter are also removed, as
Packit ea1746
  // described above in RemoveResidualBlock().
Packit ea1746
  //
Packit ea1746
  // If Problem::Options::enable_fast_removal is true, then the
Packit ea1746
  // removal is fast (almost constant time). Otherwise, removing a parameter
Packit ea1746
  // block will incur a scan of the entire Problem object.
Packit ea1746
  //
Packit ea1746
  // WARNING: Removing a residual or parameter block will destroy the implicit
Packit ea1746
  // ordering, rendering the jacobian or residuals returned from the solver
Packit ea1746
  // uninterpretable. If you depend on the evaluated jacobian, do not use
Packit ea1746
  // remove! This may change in a future release.
Packit ea1746
  void RemoveParameterBlock(double* values);
Packit ea1746
Packit ea1746
  // Remove a residual block from the problem. Any parameters that the residual
Packit ea1746
  // block depends on are not removed. The cost and loss functions for the
Packit ea1746
  // residual block will not get deleted immediately; won't happen until the
Packit ea1746
  // problem itself is deleted.
Packit ea1746
  //
Packit ea1746
  // WARNING: Removing a residual or parameter block will destroy the implicit
Packit ea1746
  // ordering, rendering the jacobian or residuals returned from the solver
Packit ea1746
  // uninterpretable. If you depend on the evaluated jacobian, do not use
Packit ea1746
  // remove! This may change in a future release.
Packit ea1746
  void RemoveResidualBlock(ResidualBlockId residual_block);
Packit ea1746
Packit ea1746
  // Hold the indicated parameter block constant during optimization.
Packit ea1746
  void SetParameterBlockConstant(double* values);
Packit ea1746
Packit ea1746
  // Allow the indicated parameter block to vary during optimization.
Packit ea1746
  void SetParameterBlockVariable(double* values);
Packit ea1746
Packit ea1746
  // Returns true if a parameter block is set constant, and false otherwise.
Packit ea1746
  bool IsParameterBlockConstant(double* values) const;
Packit ea1746
Packit ea1746
  // Set the local parameterization for one of the parameter blocks.
Packit ea1746
  // The local_parameterization is owned by the Problem by default. It
Packit ea1746
  // is acceptable to set the same parameterization for multiple
Packit ea1746
  // parameters; the destructor is careful to delete local
Packit ea1746
  // parameterizations only once. The local parameterization can only
Packit ea1746
  // be set once per parameter, and cannot be changed once set.
Packit ea1746
  void SetParameterization(double* values,
Packit ea1746
                           LocalParameterization* local_parameterization);
Packit ea1746
Packit ea1746
  // Get the local parameterization object associated with this
Packit ea1746
  // parameter block. If there is no parameterization object
Packit ea1746
  // associated then NULL is returned.
Packit ea1746
  const LocalParameterization* GetParameterization(double* values) const;
Packit ea1746
Packit ea1746
  // Set the lower/upper bound for the parameter with position "index".
Packit ea1746
  void SetParameterLowerBound(double* values, int index, double lower_bound);
Packit ea1746
  void SetParameterUpperBound(double* values, int index, double upper_bound);
Packit ea1746
Packit ea1746
  // Number of parameter blocks in the problem. Always equals
Packit ea1746
  // parameter_blocks().size() and parameter_block_sizes().size().
Packit ea1746
  int NumParameterBlocks() const;
Packit ea1746
Packit ea1746
  // The size of the parameter vector obtained by summing over the
Packit ea1746
  // sizes of all the parameter blocks.
Packit ea1746
  int NumParameters() const;
Packit ea1746
Packit ea1746
  // Number of residual blocks in the problem. Always equals
Packit ea1746
  // residual_blocks().size().
Packit ea1746
  int NumResidualBlocks() const;
Packit ea1746
Packit ea1746
  // The size of the residual vector obtained by summing over the
Packit ea1746
  // sizes of all of the residual blocks.
Packit ea1746
  int NumResiduals() const;
Packit ea1746
Packit ea1746
  // The size of the parameter block.
Packit ea1746
  int ParameterBlockSize(const double* values) const;
Packit ea1746
Packit ea1746
  // The size of local parameterization for the parameter block. If
Packit ea1746
  // there is no local parameterization associated with this parameter
Packit ea1746
  // block, then ParameterBlockLocalSize = ParameterBlockSize.
Packit ea1746
  int ParameterBlockLocalSize(const double* values) const;
Packit ea1746
Packit ea1746
  // Is the given parameter block present in this problem or not?
Packit ea1746
  bool HasParameterBlock(const double* values) const;
Packit ea1746
Packit ea1746
  // Fills the passed parameter_blocks vector with pointers to the
Packit ea1746
  // parameter blocks currently in the problem. After this call,
Packit ea1746
  // parameter_block.size() == NumParameterBlocks.
Packit ea1746
  void GetParameterBlocks(std::vector<double*>* parameter_blocks) const;
Packit ea1746
Packit ea1746
  // Fills the passed residual_blocks vector with pointers to the
Packit ea1746
  // residual blocks currently in the problem. After this call,
Packit ea1746
  // residual_blocks.size() == NumResidualBlocks.
Packit ea1746
  void GetResidualBlocks(std::vector<ResidualBlockId>* residual_blocks) const;
Packit ea1746
Packit ea1746
  // Get all the parameter blocks that depend on the given residual block.
Packit ea1746
  void GetParameterBlocksForResidualBlock(
Packit ea1746
      const ResidualBlockId residual_block,
Packit ea1746
      std::vector<double*>* parameter_blocks) const;
Packit ea1746
Packit ea1746
  // Get the CostFunction for the given residual block.
Packit ea1746
  const CostFunction* GetCostFunctionForResidualBlock(
Packit ea1746
      const ResidualBlockId residual_block) const;
Packit ea1746
Packit ea1746
  // Get the LossFunction for the given residual block. Returns NULL
Packit ea1746
  // if no loss function is associated with this residual block.
Packit ea1746
  const LossFunction* GetLossFunctionForResidualBlock(
Packit ea1746
      const ResidualBlockId residual_block) const;
Packit ea1746
Packit ea1746
  // Get all the residual blocks that depend on the given parameter block.
Packit ea1746
  //
Packit ea1746
  // If Problem::Options::enable_fast_removal is true, then
Packit ea1746
  // getting the residual blocks is fast and depends only on the number of
Packit ea1746
  // residual blocks. Otherwise, getting the residual blocks for a parameter
Packit ea1746
  // block will incur a scan of the entire Problem object.
Packit ea1746
  void GetResidualBlocksForParameterBlock(
Packit ea1746
      const double* values,
Packit ea1746
      std::vector<ResidualBlockId>* residual_blocks) const;
Packit ea1746
Packit ea1746
  // Options struct to control Problem::Evaluate.
Packit ea1746
  struct EvaluateOptions {
Packit ea1746
    EvaluateOptions()
Packit ea1746
        : apply_loss_function(true),
Packit ea1746
          num_threads(1) {
Packit ea1746
    }
Packit ea1746
Packit ea1746
    // The set of parameter blocks for which evaluation should be
Packit ea1746
    // performed. This vector determines the order that parameter
Packit ea1746
    // blocks occur in the gradient vector and in the columns of the
Packit ea1746
    // jacobian matrix. If parameter_blocks is empty, then it is
Packit ea1746
    // assumed to be equal to vector containing ALL the parameter
Packit ea1746
    // blocks.  Generally speaking the parameter blocks will occur in
Packit ea1746
    // the order in which they were added to the problem. But, this
Packit ea1746
    // may change if the user removes any parameter blocks from the
Packit ea1746
    // problem.
Packit ea1746
    //
Packit ea1746
    // NOTE: This vector should contain the same pointers as the ones
Packit ea1746
    // used to add parameter blocks to the Problem. These parameter
Packit ea1746
    // block should NOT point to new memory locations. Bad things will
Packit ea1746
    // happen otherwise.
Packit ea1746
    std::vector<double*> parameter_blocks;
Packit ea1746
Packit ea1746
    // The set of residual blocks to evaluate. This vector determines
Packit ea1746
    // the order in which the residuals occur, and how the rows of the
Packit ea1746
    // jacobian are ordered. If residual_blocks is empty, then it is
Packit ea1746
    // assumed to be equal to the vector containing ALL the residual
Packit ea1746
    // blocks. Generally speaking the residual blocks will occur in
Packit ea1746
    // the order in which they were added to the problem. But, this
Packit ea1746
    // may change if the user removes any residual blocks from the
Packit ea1746
    // problem.
Packit ea1746
    std::vector<ResidualBlockId> residual_blocks;
Packit ea1746
Packit ea1746
    // Even though the residual blocks in the problem may contain loss
Packit ea1746
    // functions, setting apply_loss_function to false will turn off
Packit ea1746
    // the application of the loss function to the output of the cost
Packit ea1746
    // function. This is of use for example if the user wishes to
Packit ea1746
    // analyse the solution quality by studying the distribution of
Packit ea1746
    // residuals before and after the solve.
Packit ea1746
    bool apply_loss_function;
Packit ea1746
Packit ea1746
    int num_threads;
Packit ea1746
  };
Packit ea1746
Packit ea1746
  // Evaluate Problem. Any of the output pointers can be NULL. Which
Packit ea1746
  // residual blocks and parameter blocks are used is controlled by
Packit ea1746
  // the EvaluateOptions struct above.
Packit ea1746
  //
Packit ea1746
  // Note 1: The evaluation will use the values stored in the memory
Packit ea1746
  // locations pointed to by the parameter block pointers used at the
Packit ea1746
  // time of the construction of the problem. i.e.,
Packit ea1746
  //
Packit ea1746
  //   Problem problem;
Packit ea1746
  //   double x = 1;
Packit ea1746
  //   problem.AddResidualBlock(new MyCostFunction, NULL, &x);
Packit ea1746
  //
Packit ea1746
  //   double cost = 0.0;
Packit ea1746
  //   problem.Evaluate(Problem::EvaluateOptions(), &cost, NULL, NULL, NULL);
Packit ea1746
  //
Packit ea1746
  // The cost is evaluated at x = 1. If you wish to evaluate the
Packit ea1746
  // problem at x = 2, then
Packit ea1746
  //
Packit ea1746
  //    x = 2;
Packit ea1746
  //    problem.Evaluate(Problem::EvaluateOptions(), &cost, NULL, NULL, NULL);
Packit ea1746
  //
Packit ea1746
  // is the way to do so.
Packit ea1746
  //
Packit ea1746
  // Note 2: If no local parameterizations are used, then the size of
Packit ea1746
  // the gradient vector (and the number of columns in the jacobian)
Packit ea1746
  // is the sum of the sizes of all the parameter blocks. If a
Packit ea1746
  // parameter block has a local parameterization, then it contributes
Packit ea1746
  // "LocalSize" entries to the gradient vector (and the number of
Packit ea1746
  // columns in the jacobian).
Packit ea1746
  //
Packit ea1746
  // Note 3: This function cannot be called while the problem is being
Packit ea1746
  // solved, for example it cannot be called from an IterationCallback
Packit ea1746
  // at the end of an iteration during a solve.
Packit ea1746
  bool Evaluate(const EvaluateOptions& options,
Packit ea1746
                double* cost,
Packit ea1746
                std::vector<double>* residuals,
Packit ea1746
                std::vector<double>* gradient,
Packit ea1746
                CRSMatrix* jacobian);
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  friend class Solver;
Packit ea1746
  friend class Covariance;
Packit ea1746
  internal::scoped_ptr<internal::ProblemImpl> problem_impl_;
Packit ea1746
  CERES_DISALLOW_COPY_AND_ASSIGN(Problem);
Packit ea1746
};
Packit ea1746
Packit ea1746
}  // namespace ceres
Packit ea1746
Packit ea1746
#include "ceres/internal/reenable_warnings.h"
Packit ea1746
Packit ea1746
#endif  // CERES_PUBLIC_PROBLEM_H_