Blame internal/ceres/program.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: keir@google.com (Keir Mierle)
Packit ea1746
Packit ea1746
#ifndef CERES_INTERNAL_PROGRAM_H_
Packit ea1746
#define CERES_INTERNAL_PROGRAM_H_
Packit ea1746
Packit ea1746
#include <set>
Packit ea1746
#include <string>
Packit ea1746
#include <vector>
Packit ea1746
#include "ceres/internal/port.h"
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
namespace internal {
Packit ea1746
Packit ea1746
class ParameterBlock;
Packit ea1746
class ProblemImpl;
Packit ea1746
class ResidualBlock;
Packit ea1746
class TripletSparseMatrix;
Packit ea1746
Packit ea1746
// A nonlinear least squares optimization problem. This is different from the
Packit ea1746
// similarly-named "Problem" object, which offers a mutation interface for
Packit ea1746
// adding and modifying parameters and residuals. The Program contains the core
Packit ea1746
// part of the Problem, which is the parameters and the residuals, stored in a
Packit ea1746
// particular ordering. The ordering is critical, since it defines the mapping
Packit ea1746
// between (residual, parameter) pairs and a position in the jacobian of the
Packit ea1746
// objective function. Various parts of Ceres transform one Program into
Packit ea1746
// another; for example, the first stage of solving involves stripping all
Packit ea1746
// constant parameters and residuals. This is in contrast with Problem, which is
Packit ea1746
// not built for transformation.
Packit ea1746
class Program {
Packit ea1746
 public:
Packit ea1746
  Program();
Packit ea1746
  explicit Program(const Program& program);
Packit ea1746
Packit ea1746
  // The ordered parameter and residual blocks for the program.
Packit ea1746
  const std::vector<ParameterBlock*>& parameter_blocks() const;
Packit ea1746
  const std::vector<ResidualBlock*>& residual_blocks() const;
Packit ea1746
  std::vector<ParameterBlock*>* mutable_parameter_blocks();
Packit ea1746
  std::vector<ResidualBlock*>* mutable_residual_blocks();
Packit ea1746
Packit ea1746
  // Serialize to/from the program and update states.
Packit ea1746
  //
Packit ea1746
  // NOTE: Setting the state of a parameter block can trigger the
Packit ea1746
  // computation of the Jacobian of its local parameterization. If
Packit ea1746
  // this computation fails for some reason, then this method returns
Packit ea1746
  // false and the state of the parameter blocks cannot be trusted.
Packit ea1746
  bool StateVectorToParameterBlocks(const double *state);
Packit ea1746
  void ParameterBlocksToStateVector(double *state) const;
Packit ea1746
Packit ea1746
  // Copy internal state to the user's parameters.
Packit ea1746
  void CopyParameterBlockStateToUserState();
Packit ea1746
Packit ea1746
  // Set the parameter block pointers to the user pointers. Since this
Packit ea1746
  // runs parameter block set state internally, which may call local
Packit ea1746
  // parameterizations, this can fail. False is returned on failure.
Packit ea1746
  bool SetParameterBlockStatePtrsToUserStatePtrs();
Packit ea1746
Packit ea1746
  // Update a state vector for the program given a delta.
Packit ea1746
  bool Plus(const double* state,
Packit ea1746
            const double* delta,
Packit ea1746
            double* state_plus_delta) const;
Packit ea1746
Packit ea1746
  // Set the parameter indices and offsets. This permits mapping backward
Packit ea1746
  // from a ParameterBlock* to an index in the parameter_blocks() vector. For
Packit ea1746
  // any parameter block p, after calling SetParameterOffsetsAndIndex(), it
Packit ea1746
  // is true that
Packit ea1746
  //
Packit ea1746
  //   parameter_blocks()[p->index()] == p
Packit ea1746
  //
Packit ea1746
  // If a parameter appears in a residual but not in the parameter block, then
Packit ea1746
  // it will have an index of -1.
Packit ea1746
  //
Packit ea1746
  // This also updates p->state_offset() and p->delta_offset(), which are the
Packit ea1746
  // position of the parameter in the state and delta vector respectively.
Packit ea1746
  void SetParameterOffsetsAndIndex();
Packit ea1746
Packit ea1746
  // Check if the internal state of the program (the indexing and the
Packit ea1746
  // offsets) are correct.
Packit ea1746
  bool IsValid() const;
Packit ea1746
Packit ea1746
  bool ParameterBlocksAreFinite(std::string* message) const;
Packit ea1746
Packit ea1746
  // Returns true if the program has any non-constant parameter blocks
Packit ea1746
  // which have non-trivial bounds constraints.
Packit ea1746
  bool IsBoundsConstrained() const;
Packit ea1746
Packit ea1746
  // Returns false, if the program has any constant parameter blocks
Packit ea1746
  // which are not feasible, or any variable parameter blocks which
Packit ea1746
  // have a lower bound greater than or equal to the upper bound.
Packit ea1746
  bool IsFeasible(std::string* message) const;
Packit ea1746
Packit ea1746
  // Loop over each residual block and ensure that no two parameter
Packit ea1746
  // blocks in the same residual block are part of
Packit ea1746
  // parameter_blocks as that would violate the assumption that it
Packit ea1746
  // is an independent set in the Hessian matrix.
Packit ea1746
  bool IsParameterBlockSetIndependent(
Packit ea1746
      const std::set<double*>& independent_set) const;
Packit ea1746
Packit ea1746
  // Create a TripletSparseMatrix which contains the zero-one
Packit ea1746
  // structure corresponding to the block sparsity of the transpose of
Packit ea1746
  // the Jacobian matrix.
Packit ea1746
  //
Packit ea1746
  // Caller owns the result.
Packit ea1746
  TripletSparseMatrix* CreateJacobianBlockSparsityTranspose() const;
Packit ea1746
Packit ea1746
  // Create a copy of this program and removes constant parameter
Packit ea1746
  // blocks and residual blocks with no varying parameter blocks while
Packit ea1746
  // preserving their relative order.
Packit ea1746
  //
Packit ea1746
  // removed_parameter_blocks on exit will contain the list of
Packit ea1746
  // parameter blocks that were removed.
Packit ea1746
  //
Packit ea1746
  // fixed_cost will be equal to the sum of the costs of the residual
Packit ea1746
  // blocks that were removed.
Packit ea1746
  //
Packit ea1746
  // If there was a problem, then the function will return a NULL
Packit ea1746
  // pointer and error will contain a human readable description of
Packit ea1746
  // the problem.
Packit ea1746
  Program* CreateReducedProgram(std::vector<double*>* removed_parameter_blocks,
Packit ea1746
                                double* fixed_cost,
Packit ea1746
                                std::string* error) const;
Packit ea1746
Packit ea1746
  // See problem.h for what these do.
Packit ea1746
  int NumParameterBlocks() const;
Packit ea1746
  int NumParameters() const;
Packit ea1746
  int NumEffectiveParameters() const;
Packit ea1746
  int NumResidualBlocks() const;
Packit ea1746
  int NumResiduals() const;
Packit ea1746
Packit ea1746
  int MaxScratchDoublesNeededForEvaluate() const;
Packit ea1746
  int MaxDerivativesPerResidualBlock() const;
Packit ea1746
  int MaxParametersPerResidualBlock() const;
Packit ea1746
  int MaxResidualsPerResidualBlock() const;
Packit ea1746
Packit ea1746
  // A human-readable dump of the parameter blocks for debugging.
Packit ea1746
  // TODO(keir): If necessary, also dump the residual blocks.
Packit ea1746
  std::string ToString() const;
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  // Remove constant parameter blocks and residual blocks with no
Packit ea1746
  // varying parameter blocks while preserving their relative order.
Packit ea1746
  //
Packit ea1746
  // removed_parameter_blocks on exit will contain the list of
Packit ea1746
  // parameter blocks that were removed.
Packit ea1746
  //
Packit ea1746
  // fixed_cost will be equal to the sum of the costs of the residual
Packit ea1746
  // blocks that were removed.
Packit ea1746
  //
Packit ea1746
  // If there was a problem, then the function will return false and
Packit ea1746
  // error will contain a human readable description of the problem.
Packit ea1746
  bool RemoveFixedBlocks(std::vector<double*>* removed_parameter_blocks,
Packit ea1746
                         double* fixed_cost,
Packit ea1746
                         std::string* message);
Packit ea1746
Packit ea1746
  // The Program does not own the ParameterBlock or ResidualBlock objects.
Packit ea1746
  std::vector<ParameterBlock*> parameter_blocks_;
Packit ea1746
  std::vector<ResidualBlock*> residual_blocks_;
Packit ea1746
Packit ea1746
  friend class ProblemImpl;
Packit ea1746
};
Packit ea1746
Packit ea1746
}  // namespace internal
Packit ea1746
}  // namespace ceres
Packit ea1746
Packit ea1746
#endif  // CERES_INTERNAL_PROGRAM_H_