blob: 3d62272ec5c0bbadbd35c668fa36640b1c14fa9f [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#include "ceres/program.h"
32
33#include <map>
34#include <vector>
35#include "ceres/parameter_block.h"
36#include "ceres/residual_block.h"
37#include "ceres/stl_util.h"
38#include "ceres/map_util.h"
39#include "ceres/problem.h"
40#include "ceres/cost_function.h"
41#include "ceres/loss_function.h"
42#include "ceres/local_parameterization.h"
43
44namespace ceres {
45namespace internal {
46
47Program::Program() {}
48
49Program::Program(const Program& program)
50 : parameter_blocks_(program.parameter_blocks_),
51 residual_blocks_(program.residual_blocks_) {
52}
53
54const vector<ParameterBlock*>& Program::parameter_blocks() const {
55 return parameter_blocks_;
56}
57
58const vector<ResidualBlock*>& Program::residual_blocks() const {
59 return residual_blocks_;
60}
61
62vector<ParameterBlock*>* Program::mutable_parameter_blocks() {
63 return &parameter_blocks_;
64}
65
66vector<ResidualBlock*>* Program::mutable_residual_blocks() {
67 return &residual_blocks_;
68}
69
70bool Program::StateVectorToParameterBlocks(const double *state) {
71 for (int i = 0; i < parameter_blocks_.size(); ++i) {
72 if (!parameter_blocks_[i]->SetState(state)) {
73 return false;
74 }
75 state += parameter_blocks_[i]->Size();
76 }
77 return true;
78}
79
80void Program::ParameterBlocksToStateVector(double *state) const {
81 for (int i = 0; i < parameter_blocks_.size(); ++i) {
82 parameter_blocks_[i]->GetState(state);
83 state += parameter_blocks_[i]->Size();
84 }
85}
86
87void Program::CopyParameterBlockStateToUserState() {
88 for (int i = 0; i < parameter_blocks_.size(); ++i) {
Keir Mierle57d91f52012-06-17 23:45:23 -070089 parameter_blocks_[i]->GetState(parameter_blocks_[i]->mutable_user_state());
Keir Mierle8ebb0732012-04-30 23:09:08 -070090 }
91}
92
Keir Mierle6196cba2012-06-18 11:03:40 -070093bool Program::SetParameterBlockStatePtrsToUserStatePtrs() {
Keir Mierle57d91f52012-06-17 23:45:23 -070094 for (int i = 0; i < parameter_blocks_.size(); ++i) {
95 if (!parameter_blocks_[i]->SetState(parameter_blocks_[i]->user_state())) {
96 return false;
97 }
98 }
99 return true;
100}
101
Keir Mierle8ebb0732012-04-30 23:09:08 -0700102bool Program::Plus(const double* state,
103 const double* delta,
104 double* state_plus_delta) const {
105 for (int i = 0; i < parameter_blocks_.size(); ++i) {
106 if (!parameter_blocks_[i]->Plus(state, delta, state_plus_delta)) {
107 return false;
108 }
109 state += parameter_blocks_[i]->Size();
110 delta += parameter_blocks_[i]->LocalSize();
111 state_plus_delta += parameter_blocks_[i]->Size();
112 }
113 return true;
114}
115
116void Program::SetParameterOffsetsAndIndex() {
117 // Set positions for all parameters appearing as arguments to residuals to one
118 // past the end of the parameter block array.
119 for (int i = 0; i < residual_blocks_.size(); ++i) {
120 ResidualBlock* residual_block = residual_blocks_[i];
121 for (int j = 0; j < residual_block->NumParameterBlocks(); ++j) {
122 residual_block->parameter_blocks()[j]->set_index(-1);
123 }
124 }
125 // For parameters that appear in the program, set their position and offset.
126 int state_offset = 0;
127 int delta_offset = 0;
128 for (int i = 0; i < parameter_blocks_.size(); ++i) {
129 parameter_blocks_[i]->set_index(i);
130 parameter_blocks_[i]->set_state_offset(state_offset);
131 parameter_blocks_[i]->set_delta_offset(delta_offset);
132 state_offset += parameter_blocks_[i]->Size();
133 delta_offset += parameter_blocks_[i]->LocalSize();
134 }
135}
136
137int Program::NumResidualBlocks() const {
138 return residual_blocks_.size();
139}
140
141int Program::NumParameterBlocks() const {
142 return parameter_blocks_.size();
143}
144
145int Program::NumResiduals() const {
146 int num_residuals = 0;
147 for (int i = 0; i < residual_blocks_.size(); ++i) {
148 num_residuals += residual_blocks_[i]->NumResiduals();
149 }
150 return num_residuals;
151}
152
153int Program::NumParameters() const {
154 int num_parameters = 0;
155 for (int i = 0; i < parameter_blocks_.size(); ++i) {
156 num_parameters += parameter_blocks_[i]->Size();
157 }
158 return num_parameters;
159}
160
161int Program::NumEffectiveParameters() const {
162 int num_parameters = 0;
163 for (int i = 0; i < parameter_blocks_.size(); ++i) {
164 num_parameters += parameter_blocks_[i]->LocalSize();
165 }
166 return num_parameters;
167}
168
169int Program::MaxScratchDoublesNeededForEvaluate() const {
170 // Compute the scratch space needed for evaluate.
171 int max_scratch_bytes_for_evaluate = 0;
172 for (int i = 0; i < residual_blocks_.size(); ++i) {
173 max_scratch_bytes_for_evaluate =
174 max(max_scratch_bytes_for_evaluate,
175 residual_blocks_[i]->NumScratchDoublesForEvaluate());
176 }
177 return max_scratch_bytes_for_evaluate;
178}
179
180int Program::MaxDerivativesPerResidualBlock() const {
181 int max_derivatives = 0;
182 for (int i = 0; i < residual_blocks_.size(); ++i) {
183 int derivatives = 0;
184 ResidualBlock* residual_block = residual_blocks_[i];
185 int num_parameters = residual_block->NumParameterBlocks();
186 for (int j = 0; j < num_parameters; ++j) {
187 derivatives += residual_block->NumResiduals() *
188 residual_block->parameter_blocks()[j]->LocalSize();
189 }
190 max_derivatives = max(max_derivatives, derivatives);
191 }
192 return max_derivatives;
193}
194
195int Program::MaxParametersPerResidualBlock() const {
196 int max_parameters = 0;
197 for (int i = 0; i < residual_blocks_.size(); ++i) {
198 max_parameters = max(max_parameters,
199 residual_blocks_[i]->NumParameterBlocks());
200 }
201 return max_parameters;
202}
203
204bool Program::Evaluate(double* cost, double* residuals) {
205 *cost = 0.0;
206
207 // Scratch space is only needed if residuals is NULL.
208 scoped_array<double> scratch;
209 if (residuals == NULL) {
210 scratch.reset(new double[MaxScratchDoublesNeededForEvaluate()]);
211 } else {
212 // TODO(keir): Is this needed? Check by removing the equivalent statement in
213 // dense_evaluator.cc and running the tests.
214 VectorRef(residuals, NumResiduals()).setZero();
215 }
216
217 for (int i = 0; i < residual_blocks_.size(); ++i) {
218 ResidualBlock* residual_block = residual_blocks_[i];
219
220 // Evaluate the cost function for this residual.
221 double residual_cost;
222 if (!residual_block->Evaluate(&residual_cost,
223 residuals,
224 NULL, // No jacobian.
225 scratch.get())) {
226 return false;
227 }
228
229 // Accumulate residual cost into the total cost.
230 *cost += residual_cost;
231
232 // Update the residuals cursor.
233 if (residuals != NULL) {
234 residuals += residual_block->NumResiduals();
235 }
236 }
237 return true;
238}
239
240} // namespace internal
241} // namespace ceres