blob: 3155bc3569e1c4e6828651f26659787eed5301b9 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh1d1e6ea2020-12-23 21:56:30 -08002// Copyright 2019 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -08003// http://ceres-solver.org/
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: sameeragarwal@google.com (Sameer Agarwal)
30// mierle@gmail.com (Keir Mierle)
31
32#include "ceres/problem_impl.h"
33
34#include <algorithm>
35#include <cstddef>
36#include <cstdint>
37#include <iterator>
38#include <memory>
39#include <set>
40#include <string>
41#include <utility>
42#include <vector>
43
44#include "ceres/casts.h"
45#include "ceres/compressed_row_jacobian_writer.h"
46#include "ceres/compressed_row_sparse_matrix.h"
47#include "ceres/context_impl.h"
48#include "ceres/cost_function.h"
49#include "ceres/crs_matrix.h"
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080050#include "ceres/evaluation_callback.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080051#include "ceres/evaluator.h"
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080052#include "ceres/internal/fixed_array.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080053#include "ceres/internal/port.h"
54#include "ceres/loss_function.h"
55#include "ceres/map_util.h"
56#include "ceres/parameter_block.h"
57#include "ceres/program.h"
58#include "ceres/program_evaluator.h"
59#include "ceres/residual_block.h"
60#include "ceres/scratch_evaluate_preparer.h"
61#include "ceres/stl_util.h"
62#include "ceres/stringprintf.h"
63#include "glog/logging.h"
64
65namespace ceres {
66namespace internal {
67
68using std::map;
69using std::string;
70using std::vector;
71
72namespace {
73// Returns true if two regions of memory, a and b, with sizes size_a and size_b
74// respectively, overlap.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080075bool RegionsAlias(const double* a, int size_a, const double* b, int size_b) {
76 return (a < b) ? b < (a + size_a) : a < (b + size_b);
Austin Schuh70cc9552019-01-21 19:46:48 -080077}
78
79void CheckForNoAliasing(double* existing_block,
80 int existing_block_size,
81 double* new_block,
82 int new_block_size) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080083 CHECK(!RegionsAlias(
84 existing_block, existing_block_size, new_block, new_block_size))
Austin Schuh70cc9552019-01-21 19:46:48 -080085 << "Aliasing detected between existing parameter block at memory "
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080086 << "location " << existing_block << " and has size "
87 << existing_block_size << " with new parameter "
Austin Schuh70cc9552019-01-21 19:46:48 -080088 << "block that has memory address " << new_block << " and would have "
89 << "size " << new_block_size << ".";
90}
91
92template <typename KeyType>
93void DecrementValueOrDeleteKey(const KeyType key,
94 std::map<KeyType, int>* container) {
95 auto it = container->find(key);
96 if (it->second == 1) {
97 delete key;
98 container->erase(it);
99 } else {
100 --it->second;
101 }
102}
103
104template <typename ForwardIterator>
105void STLDeleteContainerPairFirstPointers(ForwardIterator begin,
106 ForwardIterator end) {
107 while (begin != end) {
108 delete begin->first;
109 ++begin;
110 }
111}
112
113void InitializeContext(Context* context,
114 ContextImpl** context_impl,
115 bool* context_impl_owned) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800116 if (context == nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800117 *context_impl_owned = true;
118 *context_impl = new ContextImpl;
119 } else {
120 *context_impl_owned = false;
121 *context_impl = down_cast<ContextImpl*>(context);
122 }
123}
124
125} // namespace
126
127ParameterBlock* ProblemImpl::InternalAddParameterBlock(double* values,
128 int size) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800129 CHECK(values != nullptr) << "Null pointer passed to AddParameterBlock "
130 << "for a parameter with size " << size;
Austin Schuh70cc9552019-01-21 19:46:48 -0800131
132 // Ignore the request if there is a block for the given pointer already.
133 ParameterMap::iterator it = parameter_block_map_.find(values);
134 if (it != parameter_block_map_.end()) {
135 if (!options_.disable_all_safety_checks) {
136 int existing_size = it->second->Size();
137 CHECK(size == existing_size)
138 << "Tried adding a parameter block with the same double pointer, "
139 << values << ", twice, but with different block sizes. Original "
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800140 << "size was " << existing_size << " but new size is " << size;
Austin Schuh70cc9552019-01-21 19:46:48 -0800141 }
142 return it->second;
143 }
144
145 if (!options_.disable_all_safety_checks) {
146 // Before adding the parameter block, also check that it doesn't alias any
147 // other parameter blocks.
148 if (!parameter_block_map_.empty()) {
149 ParameterMap::iterator lb = parameter_block_map_.lower_bound(values);
150
151 // If lb is not the first block, check the previous block for aliasing.
152 if (lb != parameter_block_map_.begin()) {
153 ParameterMap::iterator previous = lb;
154 --previous;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800155 CheckForNoAliasing(
156 previous->first, previous->second->Size(), values, size);
Austin Schuh70cc9552019-01-21 19:46:48 -0800157 }
158
159 // If lb is not off the end, check lb for aliasing.
160 if (lb != parameter_block_map_.end()) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800161 CheckForNoAliasing(lb->first, lb->second->Size(), values, size);
Austin Schuh70cc9552019-01-21 19:46:48 -0800162 }
163 }
164 }
165
166 // Pass the index of the new parameter block as well to keep the index in
167 // sync with the position of the parameter in the program's parameter vector.
168 ParameterBlock* new_parameter_block =
169 new ParameterBlock(values, size, program_->parameter_blocks_.size());
170
171 // For dynamic problems, add the list of dependent residual blocks, which is
172 // empty to start.
173 if (options_.enable_fast_removal) {
174 new_parameter_block->EnableResidualBlockDependencies();
175 }
176 parameter_block_map_[values] = new_parameter_block;
177 program_->parameter_blocks_.push_back(new_parameter_block);
178 return new_parameter_block;
179}
180
181void ProblemImpl::InternalRemoveResidualBlock(ResidualBlock* residual_block) {
182 CHECK(residual_block != nullptr);
183 // Perform no check on the validity of residual_block, that is handled in
184 // the public method: RemoveResidualBlock().
185
186 // If needed, remove the parameter dependencies on this residual block.
187 if (options_.enable_fast_removal) {
188 const int num_parameter_blocks_for_residual =
189 residual_block->NumParameterBlocks();
190 for (int i = 0; i < num_parameter_blocks_for_residual; ++i) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800191 residual_block->parameter_blocks()[i]->RemoveResidualBlock(
192 residual_block);
Austin Schuh70cc9552019-01-21 19:46:48 -0800193 }
194
195 ResidualBlockSet::iterator it = residual_block_set_.find(residual_block);
196 residual_block_set_.erase(it);
197 }
198 DeleteBlockInVector(program_->mutable_residual_blocks(), residual_block);
199}
200
201// Deletes the residual block in question, assuming there are no other
202// references to it inside the problem (e.g. by another parameter). Referenced
203// cost and loss functions are tucked away for future deletion, since it is not
204// possible to know whether other parts of the problem depend on them without
205// doing a full scan.
206void ProblemImpl::DeleteBlock(ResidualBlock* residual_block) {
207 // The const casts here are legit, since ResidualBlock holds these
208 // pointers as const pointers but we have ownership of them and
209 // have the right to destroy them when the destructor is called.
210 CostFunction* cost_function =
211 const_cast<CostFunction*>(residual_block->cost_function());
212 if (options_.cost_function_ownership == TAKE_OWNERSHIP) {
213 DecrementValueOrDeleteKey(cost_function, &cost_function_ref_count_);
214 }
215
216 LossFunction* loss_function =
217 const_cast<LossFunction*>(residual_block->loss_function());
218 if (options_.loss_function_ownership == TAKE_OWNERSHIP &&
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800219 loss_function != nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800220 DecrementValueOrDeleteKey(loss_function, &loss_function_ref_count_);
221 }
222
223 delete residual_block;
224}
225
226// Deletes the parameter block in question, assuming there are no other
227// references to it inside the problem (e.g. by any residual blocks).
228// Referenced parameterizations are tucked away for future deletion, since it
229// is not possible to know whether other parts of the problem depend on them
230// without doing a full scan.
231void ProblemImpl::DeleteBlock(ParameterBlock* parameter_block) {
232 if (options_.local_parameterization_ownership == TAKE_OWNERSHIP &&
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800233 parameter_block->local_parameterization() != nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800234 local_parameterizations_to_delete_.push_back(
235 parameter_block->mutable_local_parameterization());
236 }
237 parameter_block_map_.erase(parameter_block->mutable_user_state());
238 delete parameter_block;
239}
240
241ProblemImpl::ProblemImpl()
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800242 : options_(Problem::Options()), program_(new internal::Program) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800243 InitializeContext(options_.context, &context_impl_, &context_impl_owned_);
244}
245
246ProblemImpl::ProblemImpl(const Problem::Options& options)
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800247 : options_(options), program_(new internal::Program) {
248 program_->evaluation_callback_ = options.evaluation_callback;
Austin Schuh70cc9552019-01-21 19:46:48 -0800249 InitializeContext(options_.context, &context_impl_, &context_impl_owned_);
250}
251
252ProblemImpl::~ProblemImpl() {
253 STLDeleteContainerPointers(program_->residual_blocks_.begin(),
254 program_->residual_blocks_.end());
255
256 if (options_.cost_function_ownership == TAKE_OWNERSHIP) {
257 STLDeleteContainerPairFirstPointers(cost_function_ref_count_.begin(),
258 cost_function_ref_count_.end());
259 }
260
261 if (options_.loss_function_ownership == TAKE_OWNERSHIP) {
262 STLDeleteContainerPairFirstPointers(loss_function_ref_count_.begin(),
263 loss_function_ref_count_.end());
264 }
265
266 // Collect the unique parameterizations and delete the parameters.
267 for (int i = 0; i < program_->parameter_blocks_.size(); ++i) {
268 DeleteBlock(program_->parameter_blocks_[i]);
269 }
270
271 // Delete the owned parameterizations.
272 STLDeleteUniqueContainerPointers(local_parameterizations_to_delete_.begin(),
273 local_parameterizations_to_delete_.end());
274
275 if (context_impl_owned_) {
276 delete context_impl_;
277 }
278}
279
280ResidualBlockId ProblemImpl::AddResidualBlock(
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800281 CostFunction* cost_function,
282 LossFunction* loss_function,
283 double* const* const parameter_blocks,
284 int num_parameter_blocks) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800285 CHECK(cost_function != nullptr);
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800286 CHECK_EQ(num_parameter_blocks, cost_function->parameter_block_sizes().size());
Austin Schuh70cc9552019-01-21 19:46:48 -0800287
288 // Check the sizes match.
289 const vector<int32_t>& parameter_block_sizes =
290 cost_function->parameter_block_sizes();
291
292 if (!options_.disable_all_safety_checks) {
293 CHECK_EQ(parameter_block_sizes.size(), num_parameter_blocks)
294 << "Number of blocks input is different than the number of blocks "
295 << "that the cost function expects.";
296
297 // Check for duplicate parameter blocks.
298 vector<double*> sorted_parameter_blocks(
299 parameter_blocks, parameter_blocks + num_parameter_blocks);
300 sort(sorted_parameter_blocks.begin(), sorted_parameter_blocks.end());
301 const bool has_duplicate_items =
302 (std::adjacent_find(sorted_parameter_blocks.begin(),
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800303 sorted_parameter_blocks.end()) !=
304 sorted_parameter_blocks.end());
Austin Schuh70cc9552019-01-21 19:46:48 -0800305 if (has_duplicate_items) {
306 string blocks;
307 for (int i = 0; i < num_parameter_blocks; ++i) {
308 blocks += StringPrintf(" %p ", parameter_blocks[i]);
309 }
310
311 LOG(FATAL) << "Duplicate parameter blocks in a residual parameter "
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800312 << "are not allowed. Parameter block pointers: [" << blocks
313 << "]";
Austin Schuh70cc9552019-01-21 19:46:48 -0800314 }
315 }
316
317 // Add parameter blocks and convert the double*'s to parameter blocks.
318 vector<ParameterBlock*> parameter_block_ptrs(num_parameter_blocks);
319 for (int i = 0; i < num_parameter_blocks; ++i) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800320 parameter_block_ptrs[i] = InternalAddParameterBlock(
321 parameter_blocks[i], parameter_block_sizes[i]);
Austin Schuh70cc9552019-01-21 19:46:48 -0800322 }
323
324 if (!options_.disable_all_safety_checks) {
325 // Check that the block sizes match the block sizes expected by the
326 // cost_function.
327 for (int i = 0; i < parameter_block_ptrs.size(); ++i) {
328 CHECK_EQ(cost_function->parameter_block_sizes()[i],
329 parameter_block_ptrs[i]->Size())
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800330 << "The cost function expects parameter block " << i << " of size "
331 << cost_function->parameter_block_sizes()[i]
Austin Schuh70cc9552019-01-21 19:46:48 -0800332 << " but was given a block of size "
333 << parameter_block_ptrs[i]->Size();
334 }
335 }
336
337 ResidualBlock* new_residual_block =
338 new ResidualBlock(cost_function,
339 loss_function,
340 parameter_block_ptrs,
341 program_->residual_blocks_.size());
342
343 // Add dependencies on the residual to the parameter blocks.
344 if (options_.enable_fast_removal) {
345 for (int i = 0; i < num_parameter_blocks; ++i) {
346 parameter_block_ptrs[i]->AddResidualBlock(new_residual_block);
347 }
348 }
349
350 program_->residual_blocks_.push_back(new_residual_block);
351
352 if (options_.enable_fast_removal) {
353 residual_block_set_.insert(new_residual_block);
354 }
355
356 if (options_.cost_function_ownership == TAKE_OWNERSHIP) {
357 // Increment the reference count, creating an entry in the table if
358 // needed. Note: C++ maps guarantee that new entries have default
359 // constructed values; this implies integers are zero initialized.
360 ++cost_function_ref_count_[cost_function];
361 }
362
363 if (options_.loss_function_ownership == TAKE_OWNERSHIP &&
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800364 loss_function != nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800365 ++loss_function_ref_count_[loss_function];
366 }
367
368 return new_residual_block;
369}
370
371void ProblemImpl::AddParameterBlock(double* values, int size) {
372 InternalAddParameterBlock(values, size);
373}
374
375void ProblemImpl::AddParameterBlock(
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800376 double* values, int size, LocalParameterization* local_parameterization) {
377 ParameterBlock* parameter_block = InternalAddParameterBlock(values, size);
378 if (local_parameterization != nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800379 parameter_block->SetParameterization(local_parameterization);
380 }
381}
382
383// Delete a block from a vector of blocks, maintaining the indexing invariant.
384// This is done in constant time by moving an element from the end of the
385// vector over the element to remove, then popping the last element. It
386// destroys the ordering in the interest of speed.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800387template <typename Block>
Austin Schuh70cc9552019-01-21 19:46:48 -0800388void ProblemImpl::DeleteBlockInVector(vector<Block*>* mutable_blocks,
389 Block* block_to_remove) {
390 CHECK_EQ((*mutable_blocks)[block_to_remove->index()], block_to_remove)
391 << "You found a Ceres bug! \n"
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800392 << "Block requested: " << block_to_remove->ToString() << "\n"
Austin Schuh70cc9552019-01-21 19:46:48 -0800393 << "Block present: "
394 << (*mutable_blocks)[block_to_remove->index()]->ToString();
395
396 // Prepare the to-be-moved block for the new, lower-in-index position by
397 // setting the index to the blocks final location.
398 Block* tmp = mutable_blocks->back();
399 tmp->set_index(block_to_remove->index());
400
401 // Overwrite the to-be-deleted residual block with the one at the end.
402 (*mutable_blocks)[block_to_remove->index()] = tmp;
403
404 DeleteBlock(block_to_remove);
405
406 // The block is gone so shrink the vector of blocks accordingly.
407 mutable_blocks->pop_back();
408}
409
410void ProblemImpl::RemoveResidualBlock(ResidualBlock* residual_block) {
411 CHECK(residual_block != nullptr);
412
413 // Verify that residual_block identifies a residual in the current problem.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800414 const string residual_not_found_message = StringPrintf(
415 "Residual block to remove: %p not found. This usually means "
416 "one of three things have happened:\n"
417 " 1) residual_block is uninitialised and points to a random "
418 "area in memory.\n"
419 " 2) residual_block represented a residual that was added to"
420 " the problem, but referred to a parameter block which has "
421 "since been removed, which removes all residuals which "
422 "depend on that parameter block, and was thus removed.\n"
423 " 3) residual_block referred to a residual that has already "
424 "been removed from the problem (by the user).",
425 residual_block);
Austin Schuh70cc9552019-01-21 19:46:48 -0800426 if (options_.enable_fast_removal) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800427 CHECK(residual_block_set_.find(residual_block) != residual_block_set_.end())
Austin Schuh70cc9552019-01-21 19:46:48 -0800428 << residual_not_found_message;
429 } else {
430 // Perform a full search over all current residuals.
431 CHECK(std::find(program_->residual_blocks().begin(),
432 program_->residual_blocks().end(),
433 residual_block) != program_->residual_blocks().end())
434 << residual_not_found_message;
435 }
436
437 InternalRemoveResidualBlock(residual_block);
438}
439
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800440void ProblemImpl::RemoveParameterBlock(const double* values) {
441 ParameterBlock* parameter_block = FindWithDefault(
442 parameter_block_map_, const_cast<double*>(values), nullptr);
443 if (parameter_block == nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800444 LOG(FATAL) << "Parameter block not found: " << values
445 << ". You must add the parameter block to the problem before "
446 << "it can be removed.";
447 }
448
449 if (options_.enable_fast_removal) {
450 // Copy the dependent residuals from the parameter block because the set of
451 // dependents will change after each call to RemoveResidualBlock().
452 vector<ResidualBlock*> residual_blocks_to_remove(
453 parameter_block->mutable_residual_blocks()->begin(),
454 parameter_block->mutable_residual_blocks()->end());
455 for (int i = 0; i < residual_blocks_to_remove.size(); ++i) {
456 InternalRemoveResidualBlock(residual_blocks_to_remove[i]);
457 }
458 } else {
459 // Scan all the residual blocks to remove ones that depend on the parameter
460 // block. Do the scan backwards since the vector changes while iterating.
461 const int num_residual_blocks = NumResidualBlocks();
462 for (int i = num_residual_blocks - 1; i >= 0; --i) {
463 ResidualBlock* residual_block =
464 (*(program_->mutable_residual_blocks()))[i];
465 const int num_parameter_blocks = residual_block->NumParameterBlocks();
466 for (int j = 0; j < num_parameter_blocks; ++j) {
467 if (residual_block->parameter_blocks()[j] == parameter_block) {
468 InternalRemoveResidualBlock(residual_block);
469 // The parameter blocks are guaranteed unique.
470 break;
471 }
472 }
473 }
474 }
475 DeleteBlockInVector(program_->mutable_parameter_blocks(), parameter_block);
476}
477
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800478void ProblemImpl::SetParameterBlockConstant(const double* values) {
479 ParameterBlock* parameter_block = FindWithDefault(
480 parameter_block_map_, const_cast<double*>(values), nullptr);
481 if (parameter_block == nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800482 LOG(FATAL) << "Parameter block not found: " << values
483 << ". You must add the parameter block to the problem before "
484 << "it can be set constant.";
485 }
486
487 parameter_block->SetConstant();
488}
489
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800490bool ProblemImpl::IsParameterBlockConstant(const double* values) const {
491 const ParameterBlock* parameter_block = FindWithDefault(
492 parameter_block_map_, const_cast<double*>(values), nullptr);
493 CHECK(parameter_block != nullptr)
494 << "Parameter block not found: " << values << ". You must add the "
495 << "parameter block to the problem before it can be queried.";
496 return parameter_block->IsConstant();
Austin Schuh70cc9552019-01-21 19:46:48 -0800497}
498
499void ProblemImpl::SetParameterBlockVariable(double* values) {
500 ParameterBlock* parameter_block =
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800501 FindWithDefault(parameter_block_map_, values, nullptr);
502 if (parameter_block == nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800503 LOG(FATAL) << "Parameter block not found: " << values
504 << ". You must add the parameter block to the problem before "
505 << "it can be set varying.";
506 }
507
508 parameter_block->SetVarying();
509}
510
511void ProblemImpl::SetParameterization(
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800512 double* values, LocalParameterization* local_parameterization) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800513 ParameterBlock* parameter_block =
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800514 FindWithDefault(parameter_block_map_, values, nullptr);
515 if (parameter_block == nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800516 LOG(FATAL) << "Parameter block not found: " << values
517 << ". You must add the parameter block to the problem before "
518 << "you can set its local parameterization.";
519 }
520
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800521 // If the parameter block already has a local parameterization and
522 // we are to take ownership of local parameterizations, then add it
523 // to local_parameterizations_to_delete_ for eventual deletion.
524 if (parameter_block->local_parameterization_ &&
525 options_.local_parameterization_ownership == TAKE_OWNERSHIP) {
526 local_parameterizations_to_delete_.push_back(
527 parameter_block->local_parameterization_);
528 }
529
Austin Schuh70cc9552019-01-21 19:46:48 -0800530 parameter_block->SetParameterization(local_parameterization);
531}
532
533const LocalParameterization* ProblemImpl::GetParameterization(
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800534 const double* values) const {
535 ParameterBlock* parameter_block = FindWithDefault(
536 parameter_block_map_, const_cast<double*>(values), nullptr);
537 if (parameter_block == nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800538 LOG(FATAL) << "Parameter block not found: " << values
539 << ". You must add the parameter block to the problem before "
540 << "you can get its local parameterization.";
541 }
542
543 return parameter_block->local_parameterization();
544}
545
546void ProblemImpl::SetParameterLowerBound(double* values,
547 int index,
548 double lower_bound) {
549 ParameterBlock* parameter_block =
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800550 FindWithDefault(parameter_block_map_, values, nullptr);
551 if (parameter_block == nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800552 LOG(FATAL) << "Parameter block not found: " << values
553 << ". You must add the parameter block to the problem before "
554 << "you can set a lower bound on one of its components.";
555 }
556
557 parameter_block->SetLowerBound(index, lower_bound);
558}
559
560void ProblemImpl::SetParameterUpperBound(double* values,
561 int index,
562 double upper_bound) {
563 ParameterBlock* parameter_block =
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800564 FindWithDefault(parameter_block_map_, values, nullptr);
565 if (parameter_block == nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800566 LOG(FATAL) << "Parameter block not found: " << values
567 << ". You must add the parameter block to the problem before "
568 << "you can set an upper bound on one of its components.";
569 }
570 parameter_block->SetUpperBound(index, upper_bound);
571}
572
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800573double ProblemImpl::GetParameterLowerBound(const double* values,
574 int index) const {
575 ParameterBlock* parameter_block = FindWithDefault(
576 parameter_block_map_, const_cast<double*>(values), nullptr);
577 if (parameter_block == nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800578 LOG(FATAL) << "Parameter block not found: " << values
579 << ". You must add the parameter block to the problem before "
580 << "you can get the lower bound on one of its components.";
581 }
582 return parameter_block->LowerBound(index);
583}
584
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800585double ProblemImpl::GetParameterUpperBound(const double* values,
586 int index) const {
587 ParameterBlock* parameter_block = FindWithDefault(
588 parameter_block_map_, const_cast<double*>(values), nullptr);
589 if (parameter_block == nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800590 LOG(FATAL) << "Parameter block not found: " << values
591 << ". You must add the parameter block to the problem before "
592 << "you can set an upper bound on one of its components.";
593 }
594 return parameter_block->UpperBound(index);
595}
596
597bool ProblemImpl::Evaluate(const Problem::EvaluateOptions& evaluate_options,
598 double* cost,
599 vector<double>* residuals,
600 vector<double>* gradient,
601 CRSMatrix* jacobian) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800602 if (cost == nullptr && residuals == nullptr && gradient == nullptr &&
603 jacobian == nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800604 return true;
605 }
606
607 // If the user supplied residual blocks, then use them, otherwise
608 // take the residual blocks from the underlying program.
609 Program program;
610 *program.mutable_residual_blocks() =
611 ((evaluate_options.residual_blocks.size() > 0)
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800612 ? evaluate_options.residual_blocks
613 : program_->residual_blocks());
Austin Schuh70cc9552019-01-21 19:46:48 -0800614
615 const vector<double*>& parameter_block_ptrs =
616 evaluate_options.parameter_blocks;
617
618 vector<ParameterBlock*> variable_parameter_blocks;
619 vector<ParameterBlock*>& parameter_blocks =
620 *program.mutable_parameter_blocks();
621
622 if (parameter_block_ptrs.size() == 0) {
623 // The user did not provide any parameter blocks, so default to
624 // using all the parameter blocks in the order that they are in
625 // the underlying program object.
626 parameter_blocks = program_->parameter_blocks();
627 } else {
628 // The user supplied a vector of parameter blocks. Using this list
629 // requires a number of steps.
630
631 // 1. Convert double* into ParameterBlock*
632 parameter_blocks.resize(parameter_block_ptrs.size());
633 for (int i = 0; i < parameter_block_ptrs.size(); ++i) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800634 parameter_blocks[i] = FindWithDefault(
635 parameter_block_map_, parameter_block_ptrs[i], nullptr);
636 if (parameter_blocks[i] == nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800637 LOG(FATAL) << "No known parameter block for "
638 << "Problem::Evaluate::Options.parameter_blocks[" << i << "]"
639 << " = " << parameter_block_ptrs[i];
640 }
641 }
642
643 // 2. The user may have only supplied a subset of parameter
644 // blocks, so identify the ones that are not supplied by the user
645 // and are NOT constant. These parameter blocks are stored in
646 // variable_parameter_blocks.
647 //
648 // To ensure that the parameter blocks are not included in the
649 // columns of the jacobian, we need to make sure that they are
650 // constant during evaluation and then make them variable again
651 // after we are done.
652 vector<ParameterBlock*> all_parameter_blocks(program_->parameter_blocks());
653 vector<ParameterBlock*> included_parameter_blocks(
654 program.parameter_blocks());
655
656 vector<ParameterBlock*> excluded_parameter_blocks;
657 sort(all_parameter_blocks.begin(), all_parameter_blocks.end());
658 sort(included_parameter_blocks.begin(), included_parameter_blocks.end());
659 set_difference(all_parameter_blocks.begin(),
660 all_parameter_blocks.end(),
661 included_parameter_blocks.begin(),
662 included_parameter_blocks.end(),
663 back_inserter(excluded_parameter_blocks));
664
665 variable_parameter_blocks.reserve(excluded_parameter_blocks.size());
666 for (int i = 0; i < excluded_parameter_blocks.size(); ++i) {
667 ParameterBlock* parameter_block = excluded_parameter_blocks[i];
668 if (!parameter_block->IsConstant()) {
669 variable_parameter_blocks.push_back(parameter_block);
670 parameter_block->SetConstant();
671 }
672 }
673 }
674
675 // Setup the Parameter indices and offsets before an evaluator can
676 // be constructed and used.
677 program.SetParameterOffsetsAndIndex();
678
679 Evaluator::Options evaluator_options;
680
681 // Even though using SPARSE_NORMAL_CHOLESKY requires SuiteSparse or
682 // CXSparse, here it just being used for telling the evaluator to
683 // use a SparseRowCompressedMatrix for the jacobian. This is because
684 // the Evaluator decides the storage for the Jacobian based on the
685 // type of linear solver being used.
686 evaluator_options.linear_solver_type = SPARSE_NORMAL_CHOLESKY;
687#ifdef CERES_NO_THREADS
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800688 if (evaluate_options.num_threads > 1) {
689 LOG(WARNING)
690 << "No threading support is compiled into this binary; "
691 << "only evaluate_options.num_threads = 1 is supported. Switching "
692 << "to single threaded mode.";
693 }
Austin Schuh70cc9552019-01-21 19:46:48 -0800694 evaluator_options.num_threads = 1;
695#else
696 evaluator_options.num_threads = evaluate_options.num_threads;
697#endif // CERES_NO_THREADS
698
699 // The main thread also does work so we only need to launch num_threads - 1.
700 context_impl_->EnsureMinimumThreads(evaluator_options.num_threads - 1);
701 evaluator_options.context = context_impl_;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800702 evaluator_options.evaluation_callback =
703 program_->mutable_evaluation_callback();
Austin Schuh70cc9552019-01-21 19:46:48 -0800704 std::unique_ptr<Evaluator> evaluator(
705 new ProgramEvaluator<ScratchEvaluatePreparer,
706 CompressedRowJacobianWriter>(evaluator_options,
707 &program));
708
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800709 if (residuals != nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800710 residuals->resize(evaluator->NumResiduals());
711 }
712
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800713 if (gradient != nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800714 gradient->resize(evaluator->NumEffectiveParameters());
715 }
716
717 std::unique_ptr<CompressedRowSparseMatrix> tmp_jacobian;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800718 if (jacobian != nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800719 tmp_jacobian.reset(
720 down_cast<CompressedRowSparseMatrix*>(evaluator->CreateJacobian()));
721 }
722
723 // Point the state pointers to the user state pointers. This is
724 // needed so that we can extract a parameter vector which is then
725 // passed to Evaluator::Evaluate.
726 program.SetParameterBlockStatePtrsToUserStatePtrs();
727
728 // Copy the value of the parameter blocks into a vector, since the
729 // Evaluate::Evaluate method needs its input as such. The previous
730 // call to SetParameterBlockStatePtrsToUserStatePtrs ensures that
731 // these values are the ones corresponding to the actual state of
732 // the parameter blocks, rather than the temporary state pointer
733 // used for evaluation.
734 Vector parameters(program.NumParameters());
735 program.ParameterBlocksToStateVector(parameters.data());
736
737 double tmp_cost = 0;
738
739 Evaluator::EvaluateOptions evaluator_evaluate_options;
740 evaluator_evaluate_options.apply_loss_function =
741 evaluate_options.apply_loss_function;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800742 bool status =
743 evaluator->Evaluate(evaluator_evaluate_options,
744 parameters.data(),
745 &tmp_cost,
746 residuals != nullptr ? &(*residuals)[0] : nullptr,
747 gradient != nullptr ? &(*gradient)[0] : nullptr,
748 tmp_jacobian.get());
Austin Schuh70cc9552019-01-21 19:46:48 -0800749
750 // Make the parameter blocks that were temporarily marked constant,
751 // variable again.
752 for (int i = 0; i < variable_parameter_blocks.size(); ++i) {
753 variable_parameter_blocks[i]->SetVarying();
754 }
755
756 if (status) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800757 if (cost != nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800758 *cost = tmp_cost;
759 }
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800760 if (jacobian != nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800761 tmp_jacobian->ToCRSMatrix(jacobian);
762 }
763 }
764
765 program_->SetParameterBlockStatePtrsToUserStatePtrs();
766 program_->SetParameterOffsetsAndIndex();
767 return status;
768}
769
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800770bool ProblemImpl::EvaluateResidualBlock(ResidualBlock* residual_block,
771 bool apply_loss_function,
772 bool new_point,
773 double* cost,
774 double* residuals,
775 double** jacobians) const {
776 auto evaluation_callback = program_->mutable_evaluation_callback();
777 if (evaluation_callback) {
778 evaluation_callback->PrepareForEvaluation(jacobians != nullptr, new_point);
779 }
780
781 ParameterBlock* const* parameter_blocks = residual_block->parameter_blocks();
782 const int num_parameter_blocks = residual_block->NumParameterBlocks();
783 for (int i = 0; i < num_parameter_blocks; ++i) {
784 ParameterBlock* parameter_block = parameter_blocks[i];
785 if (parameter_block->IsConstant()) {
786 if (jacobians != nullptr && jacobians[i] != nullptr) {
787 LOG(ERROR) << "Jacobian requested for parameter block : " << i
788 << ". But the parameter block is marked constant.";
789 return false;
790 }
791 } else {
792 CHECK(parameter_block->SetState(parameter_block->user_state()))
793 << "Congratulations, you found a Ceres bug! Please report this error "
794 << "to the developers.";
795 }
796 }
797
798 double dummy_cost = 0.0;
799 FixedArray<double, 32> scratch(
800 residual_block->NumScratchDoublesForEvaluate());
801 return residual_block->Evaluate(apply_loss_function,
802 cost ? cost : &dummy_cost,
803 residuals,
804 jacobians,
805 scratch.data());
806}
807
Austin Schuh70cc9552019-01-21 19:46:48 -0800808int ProblemImpl::NumParameterBlocks() const {
809 return program_->NumParameterBlocks();
810}
811
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800812int ProblemImpl::NumParameters() const { return program_->NumParameters(); }
Austin Schuh70cc9552019-01-21 19:46:48 -0800813
814int ProblemImpl::NumResidualBlocks() const {
815 return program_->NumResidualBlocks();
816}
817
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800818int ProblemImpl::NumResiduals() const { return program_->NumResiduals(); }
Austin Schuh70cc9552019-01-21 19:46:48 -0800819
820int ProblemImpl::ParameterBlockSize(const double* values) const {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800821 ParameterBlock* parameter_block = FindWithDefault(
822 parameter_block_map_, const_cast<double*>(values), nullptr);
823 if (parameter_block == nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800824 LOG(FATAL) << "Parameter block not found: " << values
825 << ". You must add the parameter block to the problem before "
826 << "you can get its size.";
827 }
828
829 return parameter_block->Size();
830}
831
832int ProblemImpl::ParameterBlockLocalSize(const double* values) const {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800833 ParameterBlock* parameter_block = FindWithDefault(
834 parameter_block_map_, const_cast<double*>(values), nullptr);
835 if (parameter_block == nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800836 LOG(FATAL) << "Parameter block not found: " << values
837 << ". You must add the parameter block to the problem before "
838 << "you can get its local size.";
839 }
840
841 return parameter_block->LocalSize();
842}
843
844bool ProblemImpl::HasParameterBlock(const double* parameter_block) const {
845 return (parameter_block_map_.find(const_cast<double*>(parameter_block)) !=
846 parameter_block_map_.end());
847}
848
849void ProblemImpl::GetParameterBlocks(vector<double*>* parameter_blocks) const {
850 CHECK(parameter_blocks != nullptr);
851 parameter_blocks->resize(0);
852 parameter_blocks->reserve(parameter_block_map_.size());
853 for (const auto& entry : parameter_block_map_) {
854 parameter_blocks->push_back(entry.first);
855 }
856}
857
858void ProblemImpl::GetResidualBlocks(
859 vector<ResidualBlockId>* residual_blocks) const {
860 CHECK(residual_blocks != nullptr);
861 *residual_blocks = program().residual_blocks();
862}
863
864void ProblemImpl::GetParameterBlocksForResidualBlock(
865 const ResidualBlockId residual_block,
866 vector<double*>* parameter_blocks) const {
867 int num_parameter_blocks = residual_block->NumParameterBlocks();
868 CHECK(parameter_blocks != nullptr);
869 parameter_blocks->resize(num_parameter_blocks);
870 for (int i = 0; i < num_parameter_blocks; ++i) {
871 (*parameter_blocks)[i] =
872 residual_block->parameter_blocks()[i]->mutable_user_state();
873 }
874}
875
876const CostFunction* ProblemImpl::GetCostFunctionForResidualBlock(
877 const ResidualBlockId residual_block) const {
878 return residual_block->cost_function();
879}
880
881const LossFunction* ProblemImpl::GetLossFunctionForResidualBlock(
882 const ResidualBlockId residual_block) const {
883 return residual_block->loss_function();
884}
885
886void ProblemImpl::GetResidualBlocksForParameterBlock(
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800887 const double* values, vector<ResidualBlockId>* residual_blocks) const {
888 ParameterBlock* parameter_block = FindWithDefault(
889 parameter_block_map_, const_cast<double*>(values), nullptr);
890 if (parameter_block == nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800891 LOG(FATAL) << "Parameter block not found: " << values
892 << ". You must add the parameter block to the problem before "
893 << "you can get the residual blocks that depend on it.";
894 }
895
896 if (options_.enable_fast_removal) {
897 // In this case the residual blocks that depend on the parameter block are
898 // stored in the parameter block already, so just copy them out.
899 CHECK(residual_blocks != nullptr);
900 residual_blocks->resize(parameter_block->mutable_residual_blocks()->size());
901 std::copy(parameter_block->mutable_residual_blocks()->begin(),
902 parameter_block->mutable_residual_blocks()->end(),
903 residual_blocks->begin());
904 return;
905 }
906
907 // Find residual blocks that depend on the parameter block.
908 CHECK(residual_blocks != nullptr);
909 residual_blocks->clear();
910 const int num_residual_blocks = NumResidualBlocks();
911 for (int i = 0; i < num_residual_blocks; ++i) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800912 ResidualBlock* residual_block = (*(program_->mutable_residual_blocks()))[i];
Austin Schuh70cc9552019-01-21 19:46:48 -0800913 const int num_parameter_blocks = residual_block->NumParameterBlocks();
914 for (int j = 0; j < num_parameter_blocks; ++j) {
915 if (residual_block->parameter_blocks()[j] == parameter_block) {
916 residual_blocks->push_back(residual_block);
917 // The parameter blocks are guaranteed unique.
918 break;
919 }
920 }
921 }
922}
923
924} // namespace internal
925} // namespace ceres