blob: 7ae7db90101c5227375a3147e576ae762a293571 [file] [log] [blame]
Keir Mierle8ebb0732012-04-30 23:09:08 -07001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
3// http://code.google.com/p/ceres-solver/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7//
8// * Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors may be
14// used to endorse or promote products derived from this software without
15// specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27// POSSIBILITY OF SUCH DAMAGE.
28//
29// Author: keir@google.com (Keir Mierle)
30
31#ifndef CERES_INTERNAL_PROGRAM_H_
32#define CERES_INTERNAL_PROGRAM_H_
33
34#include <vector>
35#include "ceres/internal/port.h"
36
37namespace ceres {
38namespace internal {
39
40class ParameterBlock;
41class ProblemImpl;
42class ResidualBlock;
43
44// A nonlinear least squares optimization problem. This is different from the
45// similarly-named "Problem" object, which offers a mutation interface for
46// adding and modifying parameters and residuals. The Program contains the core
47// part of the Problem, which is the parameters and the residuals, stored in a
48// particular ordering. The ordering is critical, since it defines the mapping
49// between (residual, parameter) pairs and a position in the jacobian of the
50// objective function. Various parts of Ceres transform one Program into
51// another; for example, the first stage of solving involves stripping all
52// constant parameters and residuals. This is in contrast with Problem, which is
53// not built for transformation.
54class Program {
55 public:
56 Program();
57 explicit Program(const Program& program);
58
59 // The ordered parameter and residual blocks for the program.
60 const vector<ParameterBlock*>& parameter_blocks() const;
61 const vector<ResidualBlock*>& residual_blocks() const;
62 vector<ParameterBlock*>* mutable_parameter_blocks();
63 vector<ResidualBlock*>* mutable_residual_blocks();
64
65 // Serialize to/from the program and update states.
66 //
67 // NOTE: Setting the state of a parameter block can trigger the
68 // computation of the Jacobian of its local parameterization. If
69 // this computation fails for some reason, then this method returns
70 // false and the state of the parameter blocks cannot be trusted.
71 bool StateVectorToParameterBlocks(const double *state);
72 void ParameterBlocksToStateVector(double *state) const;
73
Keir Mierle6196cba2012-06-18 11:03:40 -070074 // Copy internal state to the user's parameters.
Keir Mierle8ebb0732012-04-30 23:09:08 -070075 void CopyParameterBlockStateToUserState();
Keir Mierle6196cba2012-06-18 11:03:40 -070076
77 // Set the parameter block pointers to the user pointers. Since this
78 // runs parameter block set state internally, which may call local
79 // parameterizations, this can fail. False is returned on failure.
80 bool SetParameterBlockStatePtrsToUserStatePtrs();
Keir Mierle8ebb0732012-04-30 23:09:08 -070081
82 // Update a state vector for the program given a delta.
83 bool Plus(const double* state,
84 const double* delta,
85 double* state_plus_delta) const;
86
87 // Set the parameter indices and offsets. This permits mapping backward
88 // from a ParameterBlock* to an index in the parameter_blocks() vector. For
89 // any parameter block p, after calling SetParameterOffsetsAndIndex(), it
90 // is true that
91 //
92 // parameter_blocks()[p->index()] == p
93 //
94 // If a parameter appears in a residual but not in the parameter block, then
95 // it will have an index of -1.
96 //
97 // This also updates p->state_offset() and p->delta_offset(), which are the
98 // position of the parameter in the state and delta vector respectively.
99 void SetParameterOffsetsAndIndex();
100
101 // See problem.h for what these do.
102 int NumParameterBlocks() const;
103 int NumParameters() const;
104 int NumEffectiveParameters() const;
105 int NumResidualBlocks() const;
106 int NumResiduals() const;
107
108 int MaxScratchDoublesNeededForEvaluate() const;
109 int MaxDerivativesPerResidualBlock() const;
110 int MaxParametersPerResidualBlock() const;
Keir Mierlef44907f2012-07-06 13:52:32 -0700111 int MaxResidualsPerResidualBlock() const;
Keir Mierle8ebb0732012-04-30 23:09:08 -0700112
Keir Mierle8ebb0732012-04-30 23:09:08 -0700113 private:
114 // The Program does not own the ParameterBlock or ResidualBlock objects.
115 vector<ParameterBlock*> parameter_blocks_;
116 vector<ResidualBlock*> residual_blocks_;
117
118 friend class ProblemImpl;
119};
120
121} // namespace internal
122} // namespace ceres
123
124#endif // CERES_INTERNAL_PROGRAM_H_