Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
Keir Mierle | 7492b0d | 2015-03-17 22:30:16 -0700 | [diff] [blame] | 2 | // Copyright 2015 Google Inc. All rights reserved. |
| 3 | // http://ceres-solver.org/ |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 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) |
Keir Mierle | fda69b5 | 2013-10-10 00:25:24 -0700 | [diff] [blame] | 30 | // mierle@gmail.com (Keir Mierle) |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 31 | |
| 32 | #include "ceres/problem_impl.h" |
| 33 | |
| 34 | #include <algorithm> |
| 35 | #include <cstddef> |
Sergey Sharybin | 16636ef | 2013-03-21 15:12:01 +0600 | [diff] [blame] | 36 | #include <iterator> |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 37 | #include <set> |
| 38 | #include <string> |
| 39 | #include <utility> |
| 40 | #include <vector> |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 41 | #include "ceres/casts.h" |
| 42 | #include "ceres/compressed_row_sparse_matrix.h" |
Sameer Agarwal | 0beab86 | 2012-08-13 15:12:01 -0700 | [diff] [blame] | 43 | #include "ceres/cost_function.h" |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 44 | #include "ceres/crs_matrix.h" |
| 45 | #include "ceres/evaluator.h" |
Sameer Agarwal | 0beab86 | 2012-08-13 15:12:01 -0700 | [diff] [blame] | 46 | #include "ceres/loss_function.h" |
| 47 | #include "ceres/map_util.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 48 | #include "ceres/parameter_block.h" |
| 49 | #include "ceres/program.h" |
| 50 | #include "ceres/residual_block.h" |
| 51 | #include "ceres/stl_util.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 52 | #include "ceres/stringprintf.h" |
Sameer Agarwal | 0beab86 | 2012-08-13 15:12:01 -0700 | [diff] [blame] | 53 | #include "glog/logging.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 54 | |
| 55 | namespace ceres { |
| 56 | namespace internal { |
| 57 | |
Sameer Agarwal | bcc865f | 2014-12-17 07:35:09 -0800 | [diff] [blame] | 58 | using std::map; |
Sameer Agarwal | 05a07ec | 2015-01-07 15:10:46 -0800 | [diff] [blame] | 59 | using std::string; |
| 60 | using std::vector; |
Sameer Agarwal | bcc865f | 2014-12-17 07:35:09 -0800 | [diff] [blame] | 61 | typedef std::map<double*, internal::ParameterBlock*> ParameterMap; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 62 | |
Sergey Sharybin | f46de9e | 2013-03-21 15:31:35 +0600 | [diff] [blame] | 63 | namespace { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 64 | // Returns true if two regions of memory, a and b, with sizes size_a and size_b |
| 65 | // respectively, overlap. |
Sergey Sharybin | f46de9e | 2013-03-21 15:31:35 +0600 | [diff] [blame] | 66 | bool RegionsAlias(const double* a, int size_a, |
| 67 | const double* b, int size_b) { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 68 | return (a < b) ? b < (a + size_a) |
| 69 | : a < (b + size_b); |
| 70 | } |
| 71 | |
Sergey Sharybin | f46de9e | 2013-03-21 15:31:35 +0600 | [diff] [blame] | 72 | void CheckForNoAliasing(double* existing_block, |
| 73 | int existing_block_size, |
| 74 | double* new_block, |
| 75 | int new_block_size) { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 76 | CHECK(!RegionsAlias(existing_block, existing_block_size, |
| 77 | new_block, new_block_size)) |
| 78 | << "Aliasing detected between existing parameter block at memory " |
| 79 | << "location " << existing_block |
| 80 | << " and has size " << existing_block_size << " with new parameter " |
Sameer Agarwal | c290df8 | 2013-04-07 09:17:47 -0700 | [diff] [blame] | 81 | << "block that has memory address " << new_block << " and would have " |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 82 | << "size " << new_block_size << "."; |
| 83 | } |
| 84 | |
Sergey Sharybin | f46de9e | 2013-03-21 15:31:35 +0600 | [diff] [blame] | 85 | } // namespace |
| 86 | |
Sameer Agarwal | 8e1f83c | 2013-02-15 08:35:40 -0800 | [diff] [blame] | 87 | ParameterBlock* ProblemImpl::InternalAddParameterBlock(double* values, |
| 88 | int size) { |
| 89 | CHECK(values != NULL) << "Null pointer passed to AddParameterBlock " |
| 90 | << "for a parameter with size " << size; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 91 | |
| 92 | // Ignore the request if there is a block for the given pointer already. |
Sameer Agarwal | 8e1f83c | 2013-02-15 08:35:40 -0800 | [diff] [blame] | 93 | ParameterMap::iterator it = parameter_block_map_.find(values); |
| 94 | if (it != parameter_block_map_.end()) { |
| 95 | if (!options_.disable_all_safety_checks) { |
| 96 | int existing_size = it->second->Size(); |
| 97 | CHECK(size == existing_size) |
| 98 | << "Tried adding a parameter block with the same double pointer, " |
| 99 | << values << ", twice, but with different block sizes. Original " |
| 100 | << "size was " << existing_size << " but new size is " |
| 101 | << size; |
| 102 | } |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 103 | return it->second; |
| 104 | } |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 105 | |
Sameer Agarwal | 8e1f83c | 2013-02-15 08:35:40 -0800 | [diff] [blame] | 106 | if (!options_.disable_all_safety_checks) { |
| 107 | // Before adding the parameter block, also check that it doesn't alias any |
| 108 | // other parameter blocks. |
| 109 | if (!parameter_block_map_.empty()) { |
| 110 | ParameterMap::iterator lb = parameter_block_map_.lower_bound(values); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 111 | |
Sameer Agarwal | 8e1f83c | 2013-02-15 08:35:40 -0800 | [diff] [blame] | 112 | // If lb is not the first block, check the previous block for aliasing. |
| 113 | if (lb != parameter_block_map_.begin()) { |
| 114 | ParameterMap::iterator previous = lb; |
| 115 | --previous; |
| 116 | CheckForNoAliasing(previous->first, |
| 117 | previous->second->Size(), |
| 118 | values, |
| 119 | size); |
| 120 | } |
| 121 | |
| 122 | // If lb is not off the end, check lb for aliasing. |
| 123 | if (lb != parameter_block_map_.end()) { |
| 124 | CheckForNoAliasing(lb->first, |
| 125 | lb->second->Size(), |
| 126 | values, |
| 127 | size); |
| 128 | } |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
Sameer Agarwal | 8e1f83c | 2013-02-15 08:35:40 -0800 | [diff] [blame] | 131 | |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 132 | // Pass the index of the new parameter block as well to keep the index in |
| 133 | // sync with the position of the parameter in the program's parameter vector. |
| 134 | ParameterBlock* new_parameter_block = |
| 135 | new ParameterBlock(values, size, program_->parameter_blocks_.size()); |
| 136 | |
| 137 | // For dynamic problems, add the list of dependent residual blocks, which is |
| 138 | // empty to start. |
Alex Stewart | 195e493 | 2014-03-26 11:36:11 +0000 | [diff] [blame] | 139 | if (options_.enable_fast_removal) { |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 140 | new_parameter_block->EnableResidualBlockDependencies(); |
| 141 | } |
Sameer Agarwal | 8e1f83c | 2013-02-15 08:35:40 -0800 | [diff] [blame] | 142 | parameter_block_map_[values] = new_parameter_block; |
| 143 | program_->parameter_blocks_.push_back(new_parameter_block); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 144 | return new_parameter_block; |
| 145 | } |
| 146 | |
Alex Stewart | 195e493 | 2014-03-26 11:36:11 +0000 | [diff] [blame] | 147 | void ProblemImpl::InternalRemoveResidualBlock(ResidualBlock* residual_block) { |
| 148 | CHECK_NOTNULL(residual_block); |
| 149 | // Perform no check on the validity of residual_block, that is handled in |
| 150 | // the public method: RemoveResidualBlock(). |
| 151 | |
| 152 | // If needed, remove the parameter dependencies on this residual block. |
| 153 | if (options_.enable_fast_removal) { |
| 154 | const int num_parameter_blocks_for_residual = |
| 155 | residual_block->NumParameterBlocks(); |
| 156 | for (int i = 0; i < num_parameter_blocks_for_residual; ++i) { |
| 157 | residual_block->parameter_blocks()[i] |
| 158 | ->RemoveResidualBlock(residual_block); |
| 159 | } |
| 160 | |
| 161 | ResidualBlockSet::iterator it = residual_block_set_.find(residual_block); |
| 162 | residual_block_set_.erase(it); |
| 163 | } |
| 164 | DeleteBlockInVector(program_->mutable_residual_blocks(), residual_block); |
| 165 | } |
| 166 | |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 167 | // Deletes the residual block in question, assuming there are no other |
| 168 | // references to it inside the problem (e.g. by another parameter). Referenced |
| 169 | // cost and loss functions are tucked away for future deletion, since it is not |
| 170 | // possible to know whether other parts of the problem depend on them without |
| 171 | // doing a full scan. |
| 172 | void ProblemImpl::DeleteBlock(ResidualBlock* residual_block) { |
| 173 | // The const casts here are legit, since ResidualBlock holds these |
| 174 | // pointers as const pointers but we have ownership of them and |
| 175 | // have the right to destroy them when the destructor is called. |
| 176 | if (options_.cost_function_ownership == TAKE_OWNERSHIP && |
| 177 | residual_block->cost_function() != NULL) { |
| 178 | cost_functions_to_delete_.push_back( |
| 179 | const_cast<CostFunction*>(residual_block->cost_function())); |
| 180 | } |
| 181 | if (options_.loss_function_ownership == TAKE_OWNERSHIP && |
| 182 | residual_block->loss_function() != NULL) { |
| 183 | loss_functions_to_delete_.push_back( |
| 184 | const_cast<LossFunction*>(residual_block->loss_function())); |
| 185 | } |
| 186 | delete residual_block; |
| 187 | } |
| 188 | |
| 189 | // Deletes the parameter block in question, assuming there are no other |
| 190 | // references to it inside the problem (e.g. by any residual blocks). |
| 191 | // Referenced parameterizations are tucked away for future deletion, since it |
| 192 | // is not possible to know whether other parts of the problem depend on them |
| 193 | // without doing a full scan. |
| 194 | void ProblemImpl::DeleteBlock(ParameterBlock* parameter_block) { |
| 195 | if (options_.local_parameterization_ownership == TAKE_OWNERSHIP && |
| 196 | parameter_block->local_parameterization() != NULL) { |
| 197 | local_parameterizations_to_delete_.push_back( |
| 198 | parameter_block->mutable_local_parameterization()); |
| 199 | } |
| 200 | parameter_block_map_.erase(parameter_block->mutable_user_state()); |
| 201 | delete parameter_block; |
| 202 | } |
| 203 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 204 | ProblemImpl::ProblemImpl() : program_(new internal::Program) {} |
| 205 | ProblemImpl::ProblemImpl(const Problem::Options& options) |
| 206 | : options_(options), |
| 207 | program_(new internal::Program) {} |
| 208 | |
| 209 | ProblemImpl::~ProblemImpl() { |
| 210 | // Collect the unique cost/loss functions and delete the residuals. |
Sameer Agarwal | beb4505 | 2013-02-22 13:37:01 -0800 | [diff] [blame] | 211 | const int num_residual_blocks = program_->residual_blocks_.size(); |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 212 | cost_functions_to_delete_.reserve(num_residual_blocks); |
| 213 | loss_functions_to_delete_.reserve(num_residual_blocks); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 214 | for (int i = 0; i < program_->residual_blocks_.size(); ++i) { |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 215 | DeleteBlock(program_->residual_blocks_[i]); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | // Collect the unique parameterizations and delete the parameters. |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 219 | for (int i = 0; i < program_->parameter_blocks_.size(); ++i) { |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 220 | DeleteBlock(program_->parameter_blocks_[i]); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | // Delete the owned cost/loss functions and parameterizations. |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 224 | STLDeleteUniqueContainerPointers(local_parameterizations_to_delete_.begin(), |
| 225 | local_parameterizations_to_delete_.end()); |
| 226 | STLDeleteUniqueContainerPointers(cost_functions_to_delete_.begin(), |
| 227 | cost_functions_to_delete_.end()); |
| 228 | STLDeleteUniqueContainerPointers(loss_functions_to_delete_.begin(), |
| 229 | loss_functions_to_delete_.end()); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 232 | ResidualBlock* ProblemImpl::AddResidualBlock( |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 233 | CostFunction* cost_function, |
| 234 | LossFunction* loss_function, |
| 235 | const vector<double*>& parameter_blocks) { |
| 236 | CHECK_NOTNULL(cost_function); |
| 237 | CHECK_EQ(parameter_blocks.size(), |
| 238 | cost_function->parameter_block_sizes().size()); |
| 239 | |
| 240 | // Check the sizes match. |
Sameer Agarwal | 85561ee | 2014-01-07 22:22:14 -0800 | [diff] [blame] | 241 | const vector<int32>& parameter_block_sizes = |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 242 | cost_function->parameter_block_sizes(); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 243 | |
Sameer Agarwal | 8e1f83c | 2013-02-15 08:35:40 -0800 | [diff] [blame] | 244 | if (!options_.disable_all_safety_checks) { |
| 245 | CHECK_EQ(parameter_block_sizes.size(), parameter_blocks.size()) |
| 246 | << "Number of blocks input is different than the number of blocks " |
| 247 | << "that the cost function expects."; |
| 248 | |
| 249 | // Check for duplicate parameter blocks. |
| 250 | vector<double*> sorted_parameter_blocks(parameter_blocks); |
| 251 | sort(sorted_parameter_blocks.begin(), sorted_parameter_blocks.end()); |
Sameer Agarwal | a1019f6 | 2015-12-12 15:02:15 -0800 | [diff] [blame] | 252 | const bool has_duplicate_items = |
| 253 | (std::adjacent_find(sorted_parameter_blocks.begin(), |
| 254 | sorted_parameter_blocks.end()) |
| 255 | != sorted_parameter_blocks.end()); |
Sameer Agarwal | 8ecfb2d | 2015-12-17 13:55:02 -0800 | [diff] [blame] | 256 | if (has_duplicate_items) { |
Sameer Agarwal | 8e1f83c | 2013-02-15 08:35:40 -0800 | [diff] [blame] | 257 | string blocks; |
| 258 | for (int i = 0; i < parameter_blocks.size(); ++i) { |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 259 | blocks += StringPrintf(" %p ", parameter_blocks[i]); |
Sameer Agarwal | 8e1f83c | 2013-02-15 08:35:40 -0800 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | LOG(FATAL) << "Duplicate parameter blocks in a residual parameter " |
| 263 | << "are not allowed. Parameter block pointers: [" |
| 264 | << blocks << "]"; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 265 | } |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | // Add parameter blocks and convert the double*'s to parameter blocks. |
| 269 | vector<ParameterBlock*> parameter_block_ptrs(parameter_blocks.size()); |
| 270 | for (int i = 0; i < parameter_blocks.size(); ++i) { |
| 271 | parameter_block_ptrs[i] = |
| 272 | InternalAddParameterBlock(parameter_blocks[i], |
Sameer Agarwal | 8e1f83c | 2013-02-15 08:35:40 -0800 | [diff] [blame] | 273 | parameter_block_sizes[i]); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Sameer Agarwal | 8e1f83c | 2013-02-15 08:35:40 -0800 | [diff] [blame] | 276 | if (!options_.disable_all_safety_checks) { |
| 277 | // Check that the block sizes match the block sizes expected by the |
| 278 | // cost_function. |
| 279 | for (int i = 0; i < parameter_block_ptrs.size(); ++i) { |
| 280 | CHECK_EQ(cost_function->parameter_block_sizes()[i], |
| 281 | parameter_block_ptrs[i]->Size()) |
| 282 | << "The cost function expects parameter block " << i |
| 283 | << " of size " << cost_function->parameter_block_sizes()[i] |
| 284 | << " but was given a block of size " |
| 285 | << parameter_block_ptrs[i]->Size(); |
| 286 | } |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | ResidualBlock* new_residual_block = |
| 290 | new ResidualBlock(cost_function, |
| 291 | loss_function, |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 292 | parameter_block_ptrs, |
| 293 | program_->residual_blocks_.size()); |
| 294 | |
| 295 | // Add dependencies on the residual to the parameter blocks. |
Alex Stewart | 195e493 | 2014-03-26 11:36:11 +0000 | [diff] [blame] | 296 | if (options_.enable_fast_removal) { |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 297 | for (int i = 0; i < parameter_blocks.size(); ++i) { |
| 298 | parameter_block_ptrs[i]->AddResidualBlock(new_residual_block); |
| 299 | } |
| 300 | } |
| 301 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 302 | program_->residual_blocks_.push_back(new_residual_block); |
Alex Stewart | 195e493 | 2014-03-26 11:36:11 +0000 | [diff] [blame] | 303 | |
| 304 | if (options_.enable_fast_removal) { |
| 305 | residual_block_set_.insert(new_residual_block); |
| 306 | } |
| 307 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 308 | return new_residual_block; |
| 309 | } |
| 310 | |
| 311 | // Unfortunately, macros don't help much to reduce this code, and var args don't |
| 312 | // work because of the ambiguous case that there is no loss function. |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 313 | ResidualBlock* ProblemImpl::AddResidualBlock( |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 314 | CostFunction* cost_function, |
| 315 | LossFunction* loss_function, |
| 316 | double* x0) { |
| 317 | vector<double*> residual_parameters; |
| 318 | residual_parameters.push_back(x0); |
| 319 | return AddResidualBlock(cost_function, loss_function, residual_parameters); |
| 320 | } |
| 321 | |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 322 | ResidualBlock* ProblemImpl::AddResidualBlock( |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 323 | CostFunction* cost_function, |
| 324 | LossFunction* loss_function, |
| 325 | double* x0, double* x1) { |
| 326 | vector<double*> residual_parameters; |
| 327 | residual_parameters.push_back(x0); |
| 328 | residual_parameters.push_back(x1); |
| 329 | return AddResidualBlock(cost_function, loss_function, residual_parameters); |
| 330 | } |
| 331 | |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 332 | ResidualBlock* ProblemImpl::AddResidualBlock( |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 333 | CostFunction* cost_function, |
| 334 | LossFunction* loss_function, |
| 335 | double* x0, double* x1, double* x2) { |
| 336 | vector<double*> residual_parameters; |
| 337 | residual_parameters.push_back(x0); |
| 338 | residual_parameters.push_back(x1); |
| 339 | residual_parameters.push_back(x2); |
| 340 | return AddResidualBlock(cost_function, loss_function, residual_parameters); |
| 341 | } |
| 342 | |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 343 | ResidualBlock* ProblemImpl::AddResidualBlock( |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 344 | CostFunction* cost_function, |
| 345 | LossFunction* loss_function, |
| 346 | double* x0, double* x1, double* x2, double* x3) { |
| 347 | vector<double*> residual_parameters; |
| 348 | residual_parameters.push_back(x0); |
| 349 | residual_parameters.push_back(x1); |
| 350 | residual_parameters.push_back(x2); |
| 351 | residual_parameters.push_back(x3); |
| 352 | return AddResidualBlock(cost_function, loss_function, residual_parameters); |
| 353 | } |
| 354 | |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 355 | ResidualBlock* ProblemImpl::AddResidualBlock( |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 356 | CostFunction* cost_function, |
| 357 | LossFunction* loss_function, |
| 358 | double* x0, double* x1, double* x2, double* x3, double* x4) { |
| 359 | vector<double*> residual_parameters; |
| 360 | residual_parameters.push_back(x0); |
| 361 | residual_parameters.push_back(x1); |
| 362 | residual_parameters.push_back(x2); |
| 363 | residual_parameters.push_back(x3); |
| 364 | residual_parameters.push_back(x4); |
| 365 | return AddResidualBlock(cost_function, loss_function, residual_parameters); |
| 366 | } |
| 367 | |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 368 | ResidualBlock* ProblemImpl::AddResidualBlock( |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 369 | CostFunction* cost_function, |
| 370 | LossFunction* loss_function, |
| 371 | double* x0, double* x1, double* x2, double* x3, double* x4, double* x5) { |
| 372 | vector<double*> residual_parameters; |
| 373 | residual_parameters.push_back(x0); |
| 374 | residual_parameters.push_back(x1); |
| 375 | residual_parameters.push_back(x2); |
| 376 | residual_parameters.push_back(x3); |
| 377 | residual_parameters.push_back(x4); |
| 378 | residual_parameters.push_back(x5); |
| 379 | return AddResidualBlock(cost_function, loss_function, residual_parameters); |
| 380 | } |
| 381 | |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 382 | ResidualBlock* ProblemImpl::AddResidualBlock( |
Fisher | 12626e8 | 2012-10-21 14:12:04 -0400 | [diff] [blame] | 383 | CostFunction* cost_function, |
| 384 | LossFunction* loss_function, |
| 385 | double* x0, double* x1, double* x2, double* x3, double* x4, double* x5, |
| 386 | double* x6) { |
| 387 | vector<double*> residual_parameters; |
| 388 | residual_parameters.push_back(x0); |
| 389 | residual_parameters.push_back(x1); |
| 390 | residual_parameters.push_back(x2); |
| 391 | residual_parameters.push_back(x3); |
| 392 | residual_parameters.push_back(x4); |
| 393 | residual_parameters.push_back(x5); |
| 394 | residual_parameters.push_back(x6); |
| 395 | return AddResidualBlock(cost_function, loss_function, residual_parameters); |
| 396 | } |
| 397 | |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 398 | ResidualBlock* ProblemImpl::AddResidualBlock( |
Fisher | 12626e8 | 2012-10-21 14:12:04 -0400 | [diff] [blame] | 399 | CostFunction* cost_function, |
| 400 | LossFunction* loss_function, |
| 401 | double* x0, double* x1, double* x2, double* x3, double* x4, double* x5, |
| 402 | double* x6, double* x7) { |
| 403 | vector<double*> residual_parameters; |
| 404 | residual_parameters.push_back(x0); |
| 405 | residual_parameters.push_back(x1); |
| 406 | residual_parameters.push_back(x2); |
| 407 | residual_parameters.push_back(x3); |
| 408 | residual_parameters.push_back(x4); |
| 409 | residual_parameters.push_back(x5); |
| 410 | residual_parameters.push_back(x6); |
| 411 | residual_parameters.push_back(x7); |
| 412 | return AddResidualBlock(cost_function, loss_function, residual_parameters); |
| 413 | } |
| 414 | |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 415 | ResidualBlock* ProblemImpl::AddResidualBlock( |
Fisher | 12626e8 | 2012-10-21 14:12:04 -0400 | [diff] [blame] | 416 | CostFunction* cost_function, |
| 417 | LossFunction* loss_function, |
| 418 | double* x0, double* x1, double* x2, double* x3, double* x4, double* x5, |
| 419 | double* x6, double* x7, double* x8) { |
| 420 | vector<double*> residual_parameters; |
| 421 | residual_parameters.push_back(x0); |
| 422 | residual_parameters.push_back(x1); |
| 423 | residual_parameters.push_back(x2); |
| 424 | residual_parameters.push_back(x3); |
| 425 | residual_parameters.push_back(x4); |
| 426 | residual_parameters.push_back(x5); |
| 427 | residual_parameters.push_back(x6); |
| 428 | residual_parameters.push_back(x7); |
| 429 | residual_parameters.push_back(x8); |
| 430 | return AddResidualBlock(cost_function, loss_function, residual_parameters); |
| 431 | } |
| 432 | |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 433 | ResidualBlock* ProblemImpl::AddResidualBlock( |
Fisher | 12626e8 | 2012-10-21 14:12:04 -0400 | [diff] [blame] | 434 | CostFunction* cost_function, |
| 435 | LossFunction* loss_function, |
| 436 | double* x0, double* x1, double* x2, double* x3, double* x4, double* x5, |
| 437 | double* x6, double* x7, double* x8, double* x9) { |
| 438 | vector<double*> residual_parameters; |
| 439 | residual_parameters.push_back(x0); |
| 440 | residual_parameters.push_back(x1); |
| 441 | residual_parameters.push_back(x2); |
| 442 | residual_parameters.push_back(x3); |
| 443 | residual_parameters.push_back(x4); |
| 444 | residual_parameters.push_back(x5); |
| 445 | residual_parameters.push_back(x6); |
| 446 | residual_parameters.push_back(x7); |
| 447 | residual_parameters.push_back(x8); |
| 448 | residual_parameters.push_back(x9); |
| 449 | return AddResidualBlock(cost_function, loss_function, residual_parameters); |
| 450 | } |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 451 | |
| 452 | void ProblemImpl::AddParameterBlock(double* values, int size) { |
Sameer Agarwal | 8e1f83c | 2013-02-15 08:35:40 -0800 | [diff] [blame] | 453 | InternalAddParameterBlock(values, size); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | void ProblemImpl::AddParameterBlock( |
| 457 | double* values, |
| 458 | int size, |
| 459 | LocalParameterization* local_parameterization) { |
| 460 | ParameterBlock* parameter_block = |
Sameer Agarwal | 8e1f83c | 2013-02-15 08:35:40 -0800 | [diff] [blame] | 461 | InternalAddParameterBlock(values, size); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 462 | if (local_parameterization != NULL) { |
| 463 | parameter_block->SetParameterization(local_parameterization); |
| 464 | } |
| 465 | } |
| 466 | |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 467 | // Delete a block from a vector of blocks, maintaining the indexing invariant. |
| 468 | // This is done in constant time by moving an element from the end of the |
| 469 | // vector over the element to remove, then popping the last element. It |
| 470 | // destroys the ordering in the interest of speed. |
| 471 | template<typename Block> |
| 472 | void ProblemImpl::DeleteBlockInVector(vector<Block*>* mutable_blocks, |
| 473 | Block* block_to_remove) { |
| 474 | CHECK_EQ((*mutable_blocks)[block_to_remove->index()], block_to_remove) |
Sameer Agarwal | 8af1322 | 2013-09-23 22:10:24 -0700 | [diff] [blame] | 475 | << "You found a Ceres bug! \n" |
| 476 | << "Block requested: " |
| 477 | << block_to_remove->ToString() << "\n" |
| 478 | << "Block present: " |
| 479 | << (*mutable_blocks)[block_to_remove->index()]->ToString(); |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 480 | |
| 481 | // Prepare the to-be-moved block for the new, lower-in-index position by |
| 482 | // setting the index to the blocks final location. |
| 483 | Block* tmp = mutable_blocks->back(); |
| 484 | tmp->set_index(block_to_remove->index()); |
| 485 | |
| 486 | // Overwrite the to-be-deleted residual block with the one at the end. |
| 487 | (*mutable_blocks)[block_to_remove->index()] = tmp; |
| 488 | |
| 489 | DeleteBlock(block_to_remove); |
| 490 | |
| 491 | // The block is gone so shrink the vector of blocks accordingly. |
| 492 | mutable_blocks->pop_back(); |
| 493 | } |
| 494 | |
| 495 | void ProblemImpl::RemoveResidualBlock(ResidualBlock* residual_block) { |
| 496 | CHECK_NOTNULL(residual_block); |
| 497 | |
Alex Stewart | 195e493 | 2014-03-26 11:36:11 +0000 | [diff] [blame] | 498 | // Verify that residual_block identifies a residual in the current problem. |
| 499 | const string residual_not_found_message = |
| 500 | StringPrintf("Residual block to remove: %p not found. This usually means " |
| 501 | "one of three things have happened:\n" |
| 502 | " 1) residual_block is uninitialised and points to a random " |
| 503 | "area in memory.\n" |
| 504 | " 2) residual_block represented a residual that was added to" |
| 505 | " the problem, but referred to a parameter block which has " |
| 506 | "since been removed, which removes all residuals which " |
| 507 | "depend on that parameter block, and was thus removed.\n" |
| 508 | " 3) residual_block referred to a residual that has already " |
| 509 | "been removed from the problem (by the user).", |
| 510 | residual_block); |
| 511 | if (options_.enable_fast_removal) { |
| 512 | CHECK(residual_block_set_.find(residual_block) != |
| 513 | residual_block_set_.end()) |
| 514 | << residual_not_found_message; |
| 515 | } else { |
| 516 | // Perform a full search over all current residuals. |
| 517 | CHECK(std::find(program_->residual_blocks().begin(), |
| 518 | program_->residual_blocks().end(), |
| 519 | residual_block) != program_->residual_blocks().end()) |
| 520 | << residual_not_found_message; |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 521 | } |
Alex Stewart | 195e493 | 2014-03-26 11:36:11 +0000 | [diff] [blame] | 522 | |
| 523 | InternalRemoveResidualBlock(residual_block); |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | void ProblemImpl::RemoveParameterBlock(double* values) { |
Sameer Agarwal | 020d8e1 | 2013-03-06 16:19:26 -0800 | [diff] [blame] | 527 | ParameterBlock* parameter_block = |
Sameer Agarwal | db1a76d | 2015-01-16 11:47:07 -0800 | [diff] [blame] | 528 | FindWithDefault(parameter_block_map_, values, NULL); |
| 529 | if (parameter_block == NULL) { |
| 530 | LOG(FATAL) << "Parameter block not found: " << values |
| 531 | << ". You must add the parameter block to the problem before " |
| 532 | << "it can be removed."; |
| 533 | } |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 534 | |
Alex Stewart | 195e493 | 2014-03-26 11:36:11 +0000 | [diff] [blame] | 535 | if (options_.enable_fast_removal) { |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 536 | // Copy the dependent residuals from the parameter block because the set of |
| 537 | // dependents will change after each call to RemoveResidualBlock(). |
| 538 | vector<ResidualBlock*> residual_blocks_to_remove( |
| 539 | parameter_block->mutable_residual_blocks()->begin(), |
| 540 | parameter_block->mutable_residual_blocks()->end()); |
| 541 | for (int i = 0; i < residual_blocks_to_remove.size(); ++i) { |
Alex Stewart | 195e493 | 2014-03-26 11:36:11 +0000 | [diff] [blame] | 542 | InternalRemoveResidualBlock(residual_blocks_to_remove[i]); |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 543 | } |
| 544 | } else { |
| 545 | // Scan all the residual blocks to remove ones that depend on the parameter |
| 546 | // block. Do the scan backwards since the vector changes while iterating. |
| 547 | const int num_residual_blocks = NumResidualBlocks(); |
| 548 | for (int i = num_residual_blocks - 1; i >= 0; --i) { |
| 549 | ResidualBlock* residual_block = |
| 550 | (*(program_->mutable_residual_blocks()))[i]; |
| 551 | const int num_parameter_blocks = residual_block->NumParameterBlocks(); |
Sameer Agarwal | beb4505 | 2013-02-22 13:37:01 -0800 | [diff] [blame] | 552 | for (int j = 0; j < num_parameter_blocks; ++j) { |
| 553 | if (residual_block->parameter_blocks()[j] == parameter_block) { |
Alex Stewart | 195e493 | 2014-03-26 11:36:11 +0000 | [diff] [blame] | 554 | InternalRemoveResidualBlock(residual_block); |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 555 | // The parameter blocks are guaranteed unique. |
| 556 | break; |
| 557 | } |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | DeleteBlockInVector(program_->mutable_parameter_blocks(), parameter_block); |
| 562 | } |
| 563 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 564 | void ProblemImpl::SetParameterBlockConstant(double* values) { |
Sameer Agarwal | db1a76d | 2015-01-16 11:47:07 -0800 | [diff] [blame] | 565 | ParameterBlock* parameter_block = |
| 566 | FindWithDefault(parameter_block_map_, values, NULL); |
| 567 | if (parameter_block == NULL) { |
| 568 | LOG(FATAL) << "Parameter block not found: " << values |
| 569 | << ". You must add the parameter block to the problem before " |
| 570 | << "it can be set constant."; |
| 571 | } |
| 572 | |
| 573 | parameter_block->SetConstant(); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 574 | } |
| 575 | |
Thomas Schneider | d61e94d | 2016-04-06 10:40:29 +0200 | [diff] [blame] | 576 | bool ProblemImpl::IsParameterBlockConstant(double* values) const { |
| 577 | const ParameterBlock* parameter_block = |
| 578 | FindWithDefault(parameter_block_map_, values, NULL); |
| 579 | CHECK(parameter_block != NULL) |
| 580 | << "Parameter block not found: " << values << ". You must add the " |
| 581 | << "parameter block to the problem before it can be queried."; |
| 582 | |
| 583 | return parameter_block->IsConstant(); |
| 584 | } |
| 585 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 586 | void ProblemImpl::SetParameterBlockVariable(double* values) { |
Sameer Agarwal | db1a76d | 2015-01-16 11:47:07 -0800 | [diff] [blame] | 587 | ParameterBlock* parameter_block = |
| 588 | FindWithDefault(parameter_block_map_, values, NULL); |
| 589 | if (parameter_block == NULL) { |
| 590 | LOG(FATAL) << "Parameter block not found: " << values |
| 591 | << ". You must add the parameter block to the problem before " |
| 592 | << "it can be set varying."; |
| 593 | } |
| 594 | |
| 595 | parameter_block->SetVarying(); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | void ProblemImpl::SetParameterization( |
| 599 | double* values, |
| 600 | LocalParameterization* local_parameterization) { |
Sameer Agarwal | db1a76d | 2015-01-16 11:47:07 -0800 | [diff] [blame] | 601 | ParameterBlock* parameter_block = |
| 602 | FindWithDefault(parameter_block_map_, values, NULL); |
| 603 | if (parameter_block == NULL) { |
| 604 | LOG(FATAL) << "Parameter block not found: " << values |
| 605 | << ". You must add the parameter block to the problem before " |
| 606 | << "you can set its local parameterization."; |
| 607 | } |
| 608 | |
| 609 | parameter_block->SetParameterization(local_parameterization); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 610 | } |
| 611 | |
Sameer Agarwal | f949bab | 2014-02-18 10:11:02 -0800 | [diff] [blame] | 612 | const LocalParameterization* ProblemImpl::GetParameterization( |
| 613 | double* values) const { |
Sameer Agarwal | db1a76d | 2015-01-16 11:47:07 -0800 | [diff] [blame] | 614 | ParameterBlock* parameter_block = |
| 615 | FindWithDefault(parameter_block_map_, values, NULL); |
| 616 | if (parameter_block == NULL) { |
| 617 | LOG(FATAL) << "Parameter block not found: " << values |
| 618 | << ". You must add the parameter block to the problem before " |
| 619 | << "you can get its local parameterization."; |
| 620 | } |
| 621 | |
| 622 | return parameter_block->local_parameterization(); |
Sameer Agarwal | f949bab | 2014-02-18 10:11:02 -0800 | [diff] [blame] | 623 | } |
| 624 | |
Sameer Agarwal | a482ab8 | 2014-02-18 22:24:03 -0800 | [diff] [blame] | 625 | void ProblemImpl::SetParameterLowerBound(double* values, |
| 626 | int index, |
| 627 | double lower_bound) { |
Sameer Agarwal | db1a76d | 2015-01-16 11:47:07 -0800 | [diff] [blame] | 628 | ParameterBlock* parameter_block = |
| 629 | FindWithDefault(parameter_block_map_, values, NULL); |
| 630 | if (parameter_block == NULL) { |
| 631 | LOG(FATAL) << "Parameter block not found: " << values |
| 632 | << ". You must add the parameter block to the problem before " |
| 633 | << "you can set a lower bound on one of its components."; |
| 634 | } |
| 635 | |
| 636 | parameter_block->SetLowerBound(index, lower_bound); |
Sameer Agarwal | a482ab8 | 2014-02-18 22:24:03 -0800 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | void ProblemImpl::SetParameterUpperBound(double* values, |
| 640 | int index, |
| 641 | double upper_bound) { |
Sameer Agarwal | db1a76d | 2015-01-16 11:47:07 -0800 | [diff] [blame] | 642 | ParameterBlock* parameter_block = |
| 643 | FindWithDefault(parameter_block_map_, values, NULL); |
| 644 | if (parameter_block == NULL) { |
| 645 | LOG(FATAL) << "Parameter block not found: " << values |
| 646 | << ". You must add the parameter block to the problem before " |
| 647 | << "you can set an upper bound on one of its components."; |
| 648 | } |
| 649 | parameter_block->SetUpperBound(index, upper_bound); |
Sameer Agarwal | a482ab8 | 2014-02-18 22:24:03 -0800 | [diff] [blame] | 650 | } |
| 651 | |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 652 | bool ProblemImpl::Evaluate(const Problem::EvaluateOptions& evaluate_options, |
| 653 | double* cost, |
| 654 | vector<double>* residuals, |
| 655 | vector<double>* gradient, |
| 656 | CRSMatrix* jacobian) { |
| 657 | if (cost == NULL && |
| 658 | residuals == NULL && |
| 659 | gradient == NULL && |
| 660 | jacobian == NULL) { |
| 661 | LOG(INFO) << "Nothing to do."; |
| 662 | return true; |
| 663 | } |
| 664 | |
| 665 | // If the user supplied residual blocks, then use them, otherwise |
| 666 | // take the residual blocks from the underlying program. |
| 667 | Program program; |
| 668 | *program.mutable_residual_blocks() = |
| 669 | ((evaluate_options.residual_blocks.size() > 0) |
| 670 | ? evaluate_options.residual_blocks : program_->residual_blocks()); |
| 671 | |
| 672 | const vector<double*>& parameter_block_ptrs = |
| 673 | evaluate_options.parameter_blocks; |
| 674 | |
| 675 | vector<ParameterBlock*> variable_parameter_blocks; |
| 676 | vector<ParameterBlock*>& parameter_blocks = |
| 677 | *program.mutable_parameter_blocks(); |
| 678 | |
| 679 | if (parameter_block_ptrs.size() == 0) { |
| 680 | // The user did not provide any parameter blocks, so default to |
| 681 | // using all the parameter blocks in the order that they are in |
| 682 | // the underlying program object. |
| 683 | parameter_blocks = program_->parameter_blocks(); |
| 684 | } else { |
| 685 | // The user supplied a vector of parameter blocks. Using this list |
| 686 | // requires a number of steps. |
| 687 | |
| 688 | // 1. Convert double* into ParameterBlock* |
| 689 | parameter_blocks.resize(parameter_block_ptrs.size()); |
| 690 | for (int i = 0; i < parameter_block_ptrs.size(); ++i) { |
Sameer Agarwal | db1a76d | 2015-01-16 11:47:07 -0800 | [diff] [blame] | 691 | parameter_blocks[i] = FindWithDefault(parameter_block_map_, |
| 692 | parameter_block_ptrs[i], |
| 693 | NULL); |
| 694 | if (parameter_blocks[i] == NULL) { |
| 695 | LOG(FATAL) << "No known parameter block for " |
| 696 | << "Problem::Evaluate::Options.parameter_blocks[" << i << "]" |
| 697 | << " = " << parameter_block_ptrs[i]; |
| 698 | } |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | // 2. The user may have only supplied a subset of parameter |
| 702 | // blocks, so identify the ones that are not supplied by the user |
| 703 | // and are NOT constant. These parameter blocks are stored in |
| 704 | // variable_parameter_blocks. |
| 705 | // |
| 706 | // To ensure that the parameter blocks are not included in the |
| 707 | // columns of the jacobian, we need to make sure that they are |
| 708 | // constant during evaluation and then make them variable again |
| 709 | // after we are done. |
| 710 | vector<ParameterBlock*> all_parameter_blocks(program_->parameter_blocks()); |
| 711 | vector<ParameterBlock*> included_parameter_blocks( |
| 712 | program.parameter_blocks()); |
| 713 | |
| 714 | vector<ParameterBlock*> excluded_parameter_blocks; |
| 715 | sort(all_parameter_blocks.begin(), all_parameter_blocks.end()); |
| 716 | sort(included_parameter_blocks.begin(), included_parameter_blocks.end()); |
| 717 | set_difference(all_parameter_blocks.begin(), |
| 718 | all_parameter_blocks.end(), |
| 719 | included_parameter_blocks.begin(), |
| 720 | included_parameter_blocks.end(), |
| 721 | back_inserter(excluded_parameter_blocks)); |
| 722 | |
| 723 | variable_parameter_blocks.reserve(excluded_parameter_blocks.size()); |
| 724 | for (int i = 0; i < excluded_parameter_blocks.size(); ++i) { |
| 725 | ParameterBlock* parameter_block = excluded_parameter_blocks[i]; |
| 726 | if (!parameter_block->IsConstant()) { |
| 727 | variable_parameter_blocks.push_back(parameter_block); |
| 728 | parameter_block->SetConstant(); |
| 729 | } |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | // Setup the Parameter indices and offsets before an evaluator can |
| 734 | // be constructed and used. |
| 735 | program.SetParameterOffsetsAndIndex(); |
| 736 | |
| 737 | Evaluator::Options evaluator_options; |
| 738 | |
| 739 | // Even though using SPARSE_NORMAL_CHOLESKY requires SuiteSparse or |
| 740 | // CXSparse, here it just being used for telling the evaluator to |
| 741 | // use a SparseRowCompressedMatrix for the jacobian. This is because |
| 742 | // the Evaluator decides the storage for the Jacobian based on the |
| 743 | // type of linear solver being used. |
| 744 | evaluator_options.linear_solver_type = SPARSE_NORMAL_CHOLESKY; |
Alex Stewart | ffa3247 | 2015-03-15 18:11:17 +0000 | [diff] [blame] | 745 | #ifndef CERES_USE_OPENMP |
| 746 | LOG_IF(WARNING, evaluate_options.num_threads > 1) |
| 747 | << "OpenMP support is not compiled into this binary; " |
| 748 | << "only evaluate_options.num_threads = 1 is supported. Switching " |
| 749 | << "to single threaded mode."; |
| 750 | evaluator_options.num_threads = 1; |
| 751 | #else |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 752 | evaluator_options.num_threads = evaluate_options.num_threads; |
Alex Stewart | ffa3247 | 2015-03-15 18:11:17 +0000 | [diff] [blame] | 753 | #endif // CERES_USE_OPENMP |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 754 | |
| 755 | string error; |
| 756 | scoped_ptr<Evaluator> evaluator( |
| 757 | Evaluator::Create(evaluator_options, &program, &error)); |
| 758 | if (evaluator.get() == NULL) { |
| 759 | LOG(ERROR) << "Unable to create an Evaluator object. " |
| 760 | << "Error: " << error |
| 761 | << "This is a Ceres bug; please contact the developers!"; |
| 762 | |
| 763 | // Make the parameter blocks that were temporarily marked |
| 764 | // constant, variable again. |
| 765 | for (int i = 0; i < variable_parameter_blocks.size(); ++i) { |
| 766 | variable_parameter_blocks[i]->SetVarying(); |
| 767 | } |
Sameer Agarwal | 324eccb | 2013-12-03 09:28:14 -0800 | [diff] [blame] | 768 | |
| 769 | program_->SetParameterBlockStatePtrsToUserStatePtrs(); |
| 770 | program_->SetParameterOffsetsAndIndex(); |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 771 | return false; |
| 772 | } |
| 773 | |
| 774 | if (residuals !=NULL) { |
| 775 | residuals->resize(evaluator->NumResiduals()); |
| 776 | } |
| 777 | |
| 778 | if (gradient != NULL) { |
| 779 | gradient->resize(evaluator->NumEffectiveParameters()); |
| 780 | } |
| 781 | |
| 782 | scoped_ptr<CompressedRowSparseMatrix> tmp_jacobian; |
| 783 | if (jacobian != NULL) { |
| 784 | tmp_jacobian.reset( |
| 785 | down_cast<CompressedRowSparseMatrix*>(evaluator->CreateJacobian())); |
| 786 | } |
| 787 | |
| 788 | // Point the state pointers to the user state pointers. This is |
| 789 | // needed so that we can extract a parameter vector which is then |
| 790 | // passed to Evaluator::Evaluate. |
| 791 | program.SetParameterBlockStatePtrsToUserStatePtrs(); |
| 792 | |
| 793 | // Copy the value of the parameter blocks into a vector, since the |
| 794 | // Evaluate::Evaluate method needs its input as such. The previous |
| 795 | // call to SetParameterBlockStatePtrsToUserStatePtrs ensures that |
| 796 | // these values are the ones corresponding to the actual state of |
| 797 | // the parameter blocks, rather than the temporary state pointer |
| 798 | // used for evaluation. |
| 799 | Vector parameters(program.NumParameters()); |
| 800 | program.ParameterBlocksToStateVector(parameters.data()); |
| 801 | |
| 802 | double tmp_cost = 0; |
Sameer Agarwal | 039ff07 | 2013-02-26 09:15:39 -0800 | [diff] [blame] | 803 | |
| 804 | Evaluator::EvaluateOptions evaluator_evaluate_options; |
| 805 | evaluator_evaluate_options.apply_loss_function = |
| 806 | evaluate_options.apply_loss_function; |
| 807 | bool status = evaluator->Evaluate(evaluator_evaluate_options, |
| 808 | parameters.data(), |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 809 | &tmp_cost, |
| 810 | residuals != NULL ? &(*residuals)[0] : NULL, |
| 811 | gradient != NULL ? &(*gradient)[0] : NULL, |
| 812 | tmp_jacobian.get()); |
| 813 | |
Sameer Agarwal | 931c309 | 2013-02-25 09:46:21 -0800 | [diff] [blame] | 814 | // Make the parameter blocks that were temporarily marked constant, |
| 815 | // variable again. |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 816 | for (int i = 0; i < variable_parameter_blocks.size(); ++i) { |
| 817 | variable_parameter_blocks[i]->SetVarying(); |
| 818 | } |
| 819 | |
| 820 | if (status) { |
| 821 | if (cost != NULL) { |
| 822 | *cost = tmp_cost; |
| 823 | } |
| 824 | if (jacobian != NULL) { |
| 825 | tmp_jacobian->ToCRSMatrix(jacobian); |
| 826 | } |
| 827 | } |
| 828 | |
Sameer Agarwal | 324eccb | 2013-12-03 09:28:14 -0800 | [diff] [blame] | 829 | program_->SetParameterBlockStatePtrsToUserStatePtrs(); |
| 830 | program_->SetParameterOffsetsAndIndex(); |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 831 | return status; |
| 832 | } |
| 833 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 834 | int ProblemImpl::NumParameterBlocks() const { |
| 835 | return program_->NumParameterBlocks(); |
| 836 | } |
| 837 | |
| 838 | int ProblemImpl::NumParameters() const { |
| 839 | return program_->NumParameters(); |
| 840 | } |
| 841 | |
| 842 | int ProblemImpl::NumResidualBlocks() const { |
| 843 | return program_->NumResidualBlocks(); |
| 844 | } |
| 845 | |
| 846 | int ProblemImpl::NumResiduals() const { |
| 847 | return program_->NumResiduals(); |
| 848 | } |
| 849 | |
Sameer Agarwal | db1a76d | 2015-01-16 11:47:07 -0800 | [diff] [blame] | 850 | int ProblemImpl::ParameterBlockSize(const double* values) const { |
| 851 | ParameterBlock* parameter_block = |
| 852 | FindWithDefault(parameter_block_map_, const_cast<double*>(values), NULL); |
| 853 | if (parameter_block == NULL) { |
| 854 | LOG(FATAL) << "Parameter block not found: " << values |
| 855 | << ". You must add the parameter block to the problem before " |
| 856 | << "you can get its size."; |
| 857 | } |
| 858 | |
| 859 | return parameter_block->Size(); |
Sameer Agarwal | bcc865f | 2014-12-17 07:35:09 -0800 | [diff] [blame] | 860 | } |
Sameer Agarwal | 3d95469 | 2013-04-18 14:54:55 -0700 | [diff] [blame] | 861 | |
Sameer Agarwal | db1a76d | 2015-01-16 11:47:07 -0800 | [diff] [blame] | 862 | int ProblemImpl::ParameterBlockLocalSize(const double* values) const { |
| 863 | ParameterBlock* parameter_block = |
| 864 | FindWithDefault(parameter_block_map_, const_cast<double*>(values), NULL); |
| 865 | if (parameter_block == NULL) { |
| 866 | LOG(FATAL) << "Parameter block not found: " << values |
| 867 | << ". You must add the parameter block to the problem before " |
| 868 | << "you can get its local size."; |
| 869 | } |
| 870 | |
| 871 | return parameter_block->LocalSize(); |
Sameer Agarwal | bcc865f | 2014-12-17 07:35:09 -0800 | [diff] [blame] | 872 | } |
Sameer Agarwal | 3d95469 | 2013-04-18 14:54:55 -0700 | [diff] [blame] | 873 | |
Sameer Agarwal | 5ecb1c3 | 2014-04-01 09:20:35 -0700 | [diff] [blame] | 874 | bool ProblemImpl::HasParameterBlock(const double* parameter_block) const { |
| 875 | return (parameter_block_map_.find(const_cast<double*>(parameter_block)) != |
| 876 | parameter_block_map_.end()); |
| 877 | } |
| 878 | |
Sameer Agarwal | 3d95469 | 2013-04-18 14:54:55 -0700 | [diff] [blame] | 879 | void ProblemImpl::GetParameterBlocks(vector<double*>* parameter_blocks) const { |
| 880 | CHECK_NOTNULL(parameter_blocks); |
| 881 | parameter_blocks->resize(0); |
| 882 | for (ParameterMap::const_iterator it = parameter_block_map_.begin(); |
| 883 | it != parameter_block_map_.end(); |
| 884 | ++it) { |
| 885 | parameter_blocks->push_back(it->first); |
| 886 | } |
| 887 | } |
| 888 | |
Keir Mierle | fda69b5 | 2013-10-10 00:25:24 -0700 | [diff] [blame] | 889 | void ProblemImpl::GetResidualBlocks( |
| 890 | vector<ResidualBlockId>* residual_blocks) const { |
| 891 | CHECK_NOTNULL(residual_blocks); |
| 892 | *residual_blocks = program().residual_blocks(); |
| 893 | } |
| 894 | |
| 895 | void ProblemImpl::GetParameterBlocksForResidualBlock( |
| 896 | const ResidualBlockId residual_block, |
| 897 | vector<double*>* parameter_blocks) const { |
| 898 | int num_parameter_blocks = residual_block->NumParameterBlocks(); |
| 899 | CHECK_NOTNULL(parameter_blocks)->resize(num_parameter_blocks); |
| 900 | for (int i = 0; i < num_parameter_blocks; ++i) { |
| 901 | (*parameter_blocks)[i] = |
| 902 | residual_block->parameter_blocks()[i]->mutable_user_state(); |
| 903 | } |
| 904 | } |
| 905 | |
Sameer Agarwal | 6c45d6b | 2014-09-11 07:48:30 -0700 | [diff] [blame] | 906 | const CostFunction* ProblemImpl::GetCostFunctionForResidualBlock( |
| 907 | const ResidualBlockId residual_block) const { |
| 908 | return residual_block->cost_function(); |
| 909 | } |
| 910 | |
| 911 | const LossFunction* ProblemImpl::GetLossFunctionForResidualBlock( |
| 912 | const ResidualBlockId residual_block) const { |
| 913 | return residual_block->loss_function(); |
| 914 | } |
| 915 | |
Keir Mierle | fda69b5 | 2013-10-10 00:25:24 -0700 | [diff] [blame] | 916 | void ProblemImpl::GetResidualBlocksForParameterBlock( |
| 917 | const double* values, |
| 918 | vector<ResidualBlockId>* residual_blocks) const { |
| 919 | ParameterBlock* parameter_block = |
Sameer Agarwal | db1a76d | 2015-01-16 11:47:07 -0800 | [diff] [blame] | 920 | FindWithDefault(parameter_block_map_, const_cast<double*>(values), NULL); |
| 921 | if (parameter_block == NULL) { |
| 922 | LOG(FATAL) << "Parameter block not found: " << values |
| 923 | << ". You must add the parameter block to the problem before " |
| 924 | << "you can get the residual blocks that depend on it."; |
| 925 | } |
Keir Mierle | fda69b5 | 2013-10-10 00:25:24 -0700 | [diff] [blame] | 926 | |
Alex Stewart | 195e493 | 2014-03-26 11:36:11 +0000 | [diff] [blame] | 927 | if (options_.enable_fast_removal) { |
Keir Mierle | fda69b5 | 2013-10-10 00:25:24 -0700 | [diff] [blame] | 928 | // In this case the residual blocks that depend on the parameter block are |
| 929 | // stored in the parameter block already, so just copy them out. |
Sameer Agarwal | 89a592f | 2013-11-26 11:35:49 -0800 | [diff] [blame] | 930 | CHECK_NOTNULL(residual_blocks)->resize( |
| 931 | parameter_block->mutable_residual_blocks()->size()); |
Keir Mierle | fda69b5 | 2013-10-10 00:25:24 -0700 | [diff] [blame] | 932 | std::copy(parameter_block->mutable_residual_blocks()->begin(), |
| 933 | parameter_block->mutable_residual_blocks()->end(), |
| 934 | residual_blocks->begin()); |
| 935 | return; |
| 936 | } |
| 937 | |
| 938 | // Find residual blocks that depend on the parameter block. |
| 939 | CHECK_NOTNULL(residual_blocks)->clear(); |
| 940 | const int num_residual_blocks = NumResidualBlocks(); |
| 941 | for (int i = 0; i < num_residual_blocks; ++i) { |
| 942 | ResidualBlock* residual_block = |
| 943 | (*(program_->mutable_residual_blocks()))[i]; |
| 944 | const int num_parameter_blocks = residual_block->NumParameterBlocks(); |
| 945 | for (int j = 0; j < num_parameter_blocks; ++j) { |
| 946 | if (residual_block->parameter_blocks()[j] == parameter_block) { |
| 947 | residual_blocks->push_back(residual_block); |
| 948 | // The parameter blocks are guaranteed unique. |
| 949 | break; |
| 950 | } |
| 951 | } |
| 952 | } |
| 953 | } |
Sameer Agarwal | 3d95469 | 2013-04-18 14:54:55 -0700 | [diff] [blame] | 954 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 955 | } // namespace internal |
| 956 | } // namespace ceres |