1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2015 Google Inc. All rights reserved.
3 // http://ceres-solver.org/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
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.
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.
29 // Author: sameeragarwal@google.com (Sameer Agarwal)
31 #include "ceres/coordinate_descent_minimizer.h"
33 #ifdef CERES_USE_OPENMP
40 #include "ceres/evaluator.h"
41 #include "ceres/linear_solver.h"
42 #include "ceres/minimizer.h"
43 #include "ceres/parameter_block.h"
44 #include "ceres/parameter_block_ordering.h"
45 #include "ceres/problem_impl.h"
46 #include "ceres/program.h"
47 #include "ceres/residual_block.h"
48 #include "ceres/solver.h"
49 #include "ceres/trust_region_minimizer.h"
50 #include "ceres/trust_region_strategy.h"
62 CoordinateDescentMinimizer::~CoordinateDescentMinimizer() {
65 bool CoordinateDescentMinimizer::Init(
66 const Program& program,
67 const ProblemImpl::ParameterMap& parameter_map,
68 const ParameterBlockOrdering& ordering,
70 parameter_blocks_.clear();
71 independent_set_offsets_.clear();
72 independent_set_offsets_.push_back(0);
74 // Serialize the OrderedGroups into a vector of parameter block
75 // offsets for parallel access.
76 map<ParameterBlock*, int> parameter_block_index;
77 map<int, set<double*> > group_to_elements = ordering.group_to_elements();
78 for (map<int, set<double*> >::const_iterator it = group_to_elements.begin();
79 it != group_to_elements.end();
81 for (set<double*>::const_iterator ptr_it = it->second.begin();
82 ptr_it != it->second.end();
84 parameter_blocks_.push_back(parameter_map.find(*ptr_it)->second);
85 parameter_block_index[parameter_blocks_.back()] =
86 parameter_blocks_.size() - 1;
88 independent_set_offsets_.push_back(
89 independent_set_offsets_.back() + it->second.size());
92 // The ordering does not have to contain all parameter blocks, so
93 // assign zero offsets/empty independent sets to these parameter
95 const vector<ParameterBlock*>& parameter_blocks = program.parameter_blocks();
96 for (int i = 0; i < parameter_blocks.size(); ++i) {
97 if (!ordering.IsMember(parameter_blocks[i]->mutable_user_state())) {
98 parameter_blocks_.push_back(parameter_blocks[i]);
99 independent_set_offsets_.push_back(independent_set_offsets_.back());
103 // Compute the set of residual blocks that depend on each parameter
105 residual_blocks_.resize(parameter_block_index.size());
106 const vector<ResidualBlock*>& residual_blocks = program.residual_blocks();
107 for (int i = 0; i < residual_blocks.size(); ++i) {
108 ResidualBlock* residual_block = residual_blocks[i];
109 const int num_parameter_blocks = residual_block->NumParameterBlocks();
110 for (int j = 0; j < num_parameter_blocks; ++j) {
111 ParameterBlock* parameter_block = residual_block->parameter_blocks()[j];
112 const map<ParameterBlock*, int>::const_iterator it =
113 parameter_block_index.find(parameter_block);
114 if (it != parameter_block_index.end()) {
115 residual_blocks_[it->second].push_back(residual_block);
120 evaluator_options_.linear_solver_type = DENSE_QR;
121 evaluator_options_.num_eliminate_blocks = 0;
122 evaluator_options_.num_threads = 1;
127 void CoordinateDescentMinimizer::Minimize(
128 const Minimizer::Options& options,
130 Solver::Summary* summary) {
131 // Set the state and mark all parameter blocks constant.
132 for (int i = 0; i < parameter_blocks_.size(); ++i) {
133 ParameterBlock* parameter_block = parameter_blocks_[i];
134 parameter_block->SetState(parameters + parameter_block->state_offset());
135 parameter_block->SetConstant();
138 scoped_array<LinearSolver*> linear_solvers(
139 new LinearSolver*[options.num_threads]);
141 LinearSolver::Options linear_solver_options;
142 linear_solver_options.type = DENSE_QR;
144 for (int i = 0; i < options.num_threads; ++i) {
145 linear_solvers[i] = LinearSolver::Create(linear_solver_options);
148 for (int i = 0; i < independent_set_offsets_.size() - 1; ++i) {
149 const int num_problems =
150 independent_set_offsets_[i + 1] - independent_set_offsets_[i];
151 // No point paying the price for an OpemMP call if the set is of
153 if (num_problems == 0) {
157 #ifdef CERES_USE_OPENMP
158 const int num_inner_iteration_threads =
159 min(options.num_threads, num_problems);
160 evaluator_options_.num_threads =
161 max(1, options.num_threads / num_inner_iteration_threads);
163 // The parameter blocks in each independent set can be optimized
164 // in parallel, since they do not co-occur in any residual block.
165 #pragma omp parallel for num_threads(num_inner_iteration_threads)
167 for (int j = independent_set_offsets_[i];
168 j < independent_set_offsets_[i + 1];
170 #ifdef CERES_USE_OPENMP
171 int thread_id = omp_get_thread_num();
176 ParameterBlock* parameter_block = parameter_blocks_[j];
177 const int old_index = parameter_block->index();
178 const int old_delta_offset = parameter_block->delta_offset();
179 parameter_block->SetVarying();
180 parameter_block->set_index(0);
181 parameter_block->set_delta_offset(0);
183 Program inner_program;
184 inner_program.mutable_parameter_blocks()->push_back(parameter_block);
185 *inner_program.mutable_residual_blocks() = residual_blocks_[j];
187 // TODO(sameeragarwal): Better error handling. Right now we
188 // assume that this is not going to lead to problems of any
189 // sort. Basically we should be checking for numerical failure
192 // On the other hand, if the optimization is a failure, that in
193 // some ways is fine, since it won't change the parameters and
195 Solver::Summary inner_summary;
196 Solve(&inner_program,
197 linear_solvers[thread_id],
198 parameters + parameter_block->state_offset(),
201 parameter_block->set_index(old_index);
202 parameter_block->set_delta_offset(old_delta_offset);
203 parameter_block->SetState(parameters + parameter_block->state_offset());
204 parameter_block->SetConstant();
208 for (int i = 0; i < parameter_blocks_.size(); ++i) {
209 parameter_blocks_[i]->SetVarying();
212 for (int i = 0; i < options.num_threads; ++i) {
213 delete linear_solvers[i];
217 // Solve the optimization problem for one parameter block.
218 void CoordinateDescentMinimizer::Solve(Program* program,
219 LinearSolver* linear_solver,
221 Solver::Summary* summary) {
222 *summary = Solver::Summary();
223 summary->initial_cost = 0.0;
224 summary->fixed_cost = 0.0;
225 summary->final_cost = 0.0;
228 Minimizer::Options minimizer_options;
229 minimizer_options.evaluator.reset(
230 CHECK_NOTNULL(Evaluator::Create(evaluator_options_, program, &error)));
231 minimizer_options.jacobian.reset(
232 CHECK_NOTNULL(minimizer_options.evaluator->CreateJacobian()));
234 TrustRegionStrategy::Options trs_options;
235 trs_options.linear_solver = linear_solver;
236 minimizer_options.trust_region_strategy.reset(
237 CHECK_NOTNULL(TrustRegionStrategy::Create(trs_options)));
238 minimizer_options.is_silent = true;
240 TrustRegionMinimizer minimizer;
241 minimizer.Minimize(minimizer_options, parameter, summary);
244 bool CoordinateDescentMinimizer::IsOrderingValid(
245 const Program& program,
246 const ParameterBlockOrdering& ordering,
248 const map<int, set<double*> >& group_to_elements =
249 ordering.group_to_elements();
251 // Verify that each group is an independent set
252 map<int, set<double*> >::const_iterator it = group_to_elements.begin();
253 for (; it != group_to_elements.end(); ++it) {
254 if (!program.IsParameterBlockSetIndependent(it->second)) {
256 StringPrintf("The user-provided "
257 "parameter_blocks_for_inner_iterations does not "
258 "form an independent set. Group Id: %d", it->first);
265 // Find a recursive decomposition of the Hessian matrix as a set
266 // of independent sets of decreasing size and invert it. This
267 // seems to work better in practice, i.e., Cameras before
269 ParameterBlockOrdering* CoordinateDescentMinimizer::CreateOrdering(
270 const Program& program) {
271 scoped_ptr<ParameterBlockOrdering> ordering(new ParameterBlockOrdering);
272 ComputeRecursiveIndependentSetOrdering(program, ordering.get());
274 return ordering.release();
277 } // namespace internal