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: keir@google.com (Keir Mierle)
300ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#ifndef CERES_INTERNAL_PROGRAM_H_
320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define CERES_INTERNAL_PROGRAM_H_
330ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
3479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#include <set>
350ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include <string>
360ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include <vector>
370ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/internal/port.h"
380ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
390ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace ceres {
400ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace internal {
410ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
420ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongclass ParameterBlock;
430ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongclass ProblemImpl;
440ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongclass ResidualBlock;
4579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandezclass TripletSparseMatrix;
460ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
470ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// A nonlinear least squares optimization problem. This is different from the
480ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// similarly-named "Problem" object, which offers a mutation interface for
490ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// adding and modifying parameters and residuals. The Program contains the core
500ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// part of the Problem, which is the parameters and the residuals, stored in a
510ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// particular ordering. The ordering is critical, since it defines the mapping
520ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// between (residual, parameter) pairs and a position in the jacobian of the
530ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// objective function. Various parts of Ceres transform one Program into
540ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// another; for example, the first stage of solving involves stripping all
550ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// constant parameters and residuals. This is in contrast with Problem, which is
560ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// not built for transformation.
570ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongclass Program {
580ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong public:
590ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  Program();
600ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  explicit Program(const Program& program);
610ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
620ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // The ordered parameter and residual blocks for the program.
630ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  const vector<ParameterBlock*>& parameter_blocks() const;
640ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  const vector<ResidualBlock*>& residual_blocks() const;
650ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  vector<ParameterBlock*>* mutable_parameter_blocks();
660ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  vector<ResidualBlock*>* mutable_residual_blocks();
670ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
680ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // Serialize to/from the program and update states.
690ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  //
700ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // NOTE: Setting the state of a parameter block can trigger the
710ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // computation of the Jacobian of its local parameterization. If
720ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // this computation fails for some reason, then this method returns
730ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // false and the state of the parameter blocks cannot be trusted.
740ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  bool StateVectorToParameterBlocks(const double *state);
750ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  void ParameterBlocksToStateVector(double *state) const;
760ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
770ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // Copy internal state to the user's parameters.
780ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  void CopyParameterBlockStateToUserState();
790ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
800ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // Set the parameter block pointers to the user pointers. Since this
810ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // runs parameter block set state internally, which may call local
820ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // parameterizations, this can fail. False is returned on failure.
830ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  bool SetParameterBlockStatePtrsToUserStatePtrs();
840ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
850ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // Update a state vector for the program given a delta.
860ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  bool Plus(const double* state,
870ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong            const double* delta,
880ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong            double* state_plus_delta) const;
890ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
900ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // Set the parameter indices and offsets. This permits mapping backward
910ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // from a ParameterBlock* to an index in the parameter_blocks() vector. For
920ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // any parameter block p, after calling SetParameterOffsetsAndIndex(), it
930ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // is true that
940ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  //
950ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  //   parameter_blocks()[p->index()] == p
960ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  //
970ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // If a parameter appears in a residual but not in the parameter block, then
980ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // it will have an index of -1.
990ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  //
1000ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // This also updates p->state_offset() and p->delta_offset(), which are the
1010ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // position of the parameter in the state and delta vector respectively.
1020ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  void SetParameterOffsetsAndIndex();
1030ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
10479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // Check if the internal state of the program (the indexing and the
10579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // offsets) are correct.
10679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  bool IsValid() const;
10779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez
10879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  bool ParameterBlocksAreFinite(string* message) const;
10979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez
11079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // Returns true if the program has any non-constant parameter blocks
11179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // which have non-trivial bounds constraints.
11279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  bool IsBoundsConstrained() const;
11379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez
11479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // Returns false, if the program has any constant parameter blocks
11579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // which are not feasible, or any variable parameter blocks which
11679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // have a lower bound greater than or equal to the upper bound.
11779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  bool IsFeasible(string* message) const;
11879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez
11979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // Loop over each residual block and ensure that no two parameter
12079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // blocks in the same residual block are part of
12179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // parameter_blocks as that would violate the assumption that it
12279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // is an independent set in the Hessian matrix.
12379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  bool IsParameterBlockSetIndependent(const set<double*>& independent_set) const;
12479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez
12579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // Create a TripletSparseMatrix which contains the zero-one
12679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // structure corresponding to the block sparsity of the transpose of
12779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // the Jacobian matrix.
12879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  //
12979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // Caller owns the result.
13079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  TripletSparseMatrix* CreateJacobianBlockSparsityTranspose() const;
13179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez
13279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // Create a copy of this program and removes constant parameter
13379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // blocks and residual blocks with no varying parameter blocks while
13479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // preserving their relative order.
13579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  //
13679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // removed_parameter_blocks on exit will contain the list of
13779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // parameter blocks that were removed.
13879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  //
13979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // fixed_cost will be equal to the sum of the costs of the residual
14079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // blocks that were removed.
14179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  //
14279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // If there was a problem, then the function will return a NULL
14379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // pointer and error will contain a human readable description of
14479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // the problem.
14579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  Program* CreateReducedProgram(vector<double*>* removed_parameter_blocks,
14679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez                                double* fixed_cost,
14779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez                                string* error) const;
14879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez
1490ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // See problem.h for what these do.
1500ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  int NumParameterBlocks() const;
1510ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  int NumParameters() const;
1520ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  int NumEffectiveParameters() const;
1530ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  int NumResidualBlocks() const;
1540ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  int NumResiduals() const;
1550ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1560ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  int MaxScratchDoublesNeededForEvaluate() const;
1570ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  int MaxDerivativesPerResidualBlock() const;
1580ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  int MaxParametersPerResidualBlock() const;
1590ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  int MaxResidualsPerResidualBlock() const;
1600ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1610ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // A human-readable dump of the parameter blocks for debugging.
1620ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // TODO(keir): If necessary, also dump the residual blocks.
1630ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  string ToString() const;
1640ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1650ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong private:
16679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // Remove constant parameter blocks and residual blocks with no
16779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // varying parameter blocks while preserving their relative order.
16879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  //
16979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // removed_parameter_blocks on exit will contain the list of
17079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // parameter blocks that were removed.
17179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  //
17279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // fixed_cost will be equal to the sum of the costs of the residual
17379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // blocks that were removed.
17479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  //
17579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // If there was a problem, then the function will return false and
17679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // error will contain a human readable description of the problem.
17779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  bool RemoveFixedBlocks(vector<double*>* removed_parameter_blocks,
17879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez                         double* fixed_cost,
17979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez                         string* message);
18079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez
1810ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // The Program does not own the ParameterBlock or ResidualBlock objects.
1820ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  vector<ParameterBlock*> parameter_blocks_;
1830ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  vector<ResidualBlock*> residual_blocks_;
1840ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1850ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  friend class ProblemImpl;
1860ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong};
1870ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1880ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}  // namespace internal
1890ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}  // namespace ceres
1900ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1910ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#endif  // CERES_INTERNAL_PROGRAM_H_
192