Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
| 2 | // Copyright 2015 Google Inc. All rights reserved. |
| 3 | // 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 | // keir@google.com (Keir Mierle) |
| 31 | // |
| 32 | // The Problem object is used to build and hold least squares problems. |
| 33 | |
| 34 | #ifndef CERES_PUBLIC_PROBLEM_H_ |
| 35 | #define CERES_PUBLIC_PROBLEM_H_ |
| 36 | |
| 37 | #include <array> |
| 38 | #include <cstddef> |
| 39 | #include <map> |
| 40 | #include <memory> |
| 41 | #include <set> |
| 42 | #include <vector> |
| 43 | |
| 44 | #include "ceres/context.h" |
| 45 | #include "ceres/internal/disable_warnings.h" |
| 46 | #include "ceres/internal/port.h" |
| 47 | #include "ceres/types.h" |
| 48 | #include "glog/logging.h" |
| 49 | |
| 50 | namespace ceres { |
| 51 | |
| 52 | class CostFunction; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 53 | class EvaluationCallback; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 54 | class LossFunction; |
| 55 | class LocalParameterization; |
| 56 | class Solver; |
| 57 | struct CRSMatrix; |
| 58 | |
| 59 | namespace internal { |
| 60 | class Preprocessor; |
| 61 | class ProblemImpl; |
| 62 | class ParameterBlock; |
| 63 | class ResidualBlock; |
| 64 | } // namespace internal |
| 65 | |
| 66 | // A ResidualBlockId is an opaque handle clients can use to remove residual |
| 67 | // blocks from a Problem after adding them. |
| 68 | typedef internal::ResidualBlock* ResidualBlockId; |
| 69 | |
| 70 | // A class to represent non-linear least squares problems. Such |
| 71 | // problems have a cost function that is a sum of error terms (known |
| 72 | // as "residuals"), where each residual is a function of some subset |
| 73 | // of the parameters. The cost function takes the form |
| 74 | // |
| 75 | // N 1 |
| 76 | // SUM --- loss( || r_i1, r_i2,..., r_ik ||^2 ), |
| 77 | // i=1 2 |
| 78 | // |
| 79 | // where |
| 80 | // |
| 81 | // r_ij is residual number i, component j; the residual is a |
| 82 | // function of some subset of the parameters x1...xk. For |
| 83 | // example, in a structure from motion problem a residual |
| 84 | // might be the difference between a measured point in an |
| 85 | // image and the reprojected position for the matching |
| 86 | // camera, point pair. The residual would have two |
| 87 | // components, error in x and error in y. |
| 88 | // |
| 89 | // loss(y) is the loss function; for example, squared error or |
| 90 | // Huber L1 loss. If loss(y) = y, then the cost function is |
| 91 | // non-robustified least squares. |
| 92 | // |
| 93 | // This class is specifically designed to address the important subset |
| 94 | // of "sparse" least squares problems, where each component of the |
| 95 | // residual depends only on a small number number of parameters, even |
| 96 | // though the total number of residuals and parameters may be very |
| 97 | // large. This property affords tremendous gains in scale, allowing |
| 98 | // efficient solving of large problems that are otherwise |
| 99 | // inaccessible. |
| 100 | // |
| 101 | // The canonical example of a sparse least squares problem is |
| 102 | // "structure-from-motion" (SFM), where the parameters are points and |
| 103 | // cameras, and residuals are reprojection errors. Typically a single |
| 104 | // residual will depend only on 9 parameters (3 for the point, 6 for |
| 105 | // the camera). |
| 106 | // |
| 107 | // To create a least squares problem, use the AddResidualBlock() and |
| 108 | // AddParameterBlock() methods, documented below. Here is an example least |
| 109 | // squares problem containing 3 parameter blocks of sizes 3, 4 and 5 |
| 110 | // respectively and two residual terms of size 2 and 6: |
| 111 | // |
| 112 | // double x1[] = { 1.0, 2.0, 3.0 }; |
| 113 | // double x2[] = { 1.0, 2.0, 3.0, 5.0 }; |
| 114 | // double x3[] = { 1.0, 2.0, 3.0, 6.0, 7.0 }; |
| 115 | // |
| 116 | // Problem problem; |
| 117 | // |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 118 | // problem.AddResidualBlock(new MyUnaryCostFunction(...), nullptr, x1); |
| 119 | // problem.AddResidualBlock(new MyBinaryCostFunction(...), nullptr, x2, x3); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 120 | // |
| 121 | // Please see cost_function.h for details of the CostFunction object. |
| 122 | class CERES_EXPORT Problem { |
| 123 | public: |
| 124 | struct CERES_EXPORT Options { |
| 125 | // These flags control whether the Problem object owns the cost |
| 126 | // functions, loss functions, and parameterizations passed into |
| 127 | // the Problem. If set to TAKE_OWNERSHIP, then the problem object |
| 128 | // will delete the corresponding cost or loss functions on |
| 129 | // destruction. The destructor is careful to delete the pointers |
| 130 | // only once, since sharing cost/loss/parameterizations is |
| 131 | // allowed. |
| 132 | Ownership cost_function_ownership = TAKE_OWNERSHIP; |
| 133 | Ownership loss_function_ownership = TAKE_OWNERSHIP; |
| 134 | Ownership local_parameterization_ownership = TAKE_OWNERSHIP; |
| 135 | |
| 136 | // If true, trades memory for faster RemoveResidualBlock() and |
| 137 | // RemoveParameterBlock() operations. |
| 138 | // |
| 139 | // By default, RemoveParameterBlock() and RemoveResidualBlock() take time |
| 140 | // proportional to the size of the entire problem. If you only ever remove |
| 141 | // parameters or residuals from the problem occasionally, this might be |
| 142 | // acceptable. However, if you have memory to spare, enable this option to |
| 143 | // make RemoveParameterBlock() take time proportional to the number of |
| 144 | // residual blocks that depend on it, and RemoveResidualBlock() take (on |
| 145 | // average) constant time. |
| 146 | // |
| 147 | // The increase in memory usage is twofold: an additional hash set per |
| 148 | // parameter block containing all the residuals that depend on the parameter |
| 149 | // block; and a hash set in the problem containing all residuals. |
| 150 | bool enable_fast_removal = false; |
| 151 | |
| 152 | // By default, Ceres performs a variety of safety checks when constructing |
| 153 | // the problem. There is a small but measurable performance penalty to |
| 154 | // these checks, typically around 5% of construction time. If you are sure |
| 155 | // your problem construction is correct, and 5% of the problem construction |
| 156 | // time is truly an overhead you want to avoid, then you can set |
| 157 | // disable_all_safety_checks to true. |
| 158 | // |
| 159 | // WARNING: Do not set this to true, unless you are absolutely sure of what |
| 160 | // you are doing. |
| 161 | bool disable_all_safety_checks = false; |
| 162 | |
| 163 | // A Ceres global context to use for solving this problem. This may help to |
| 164 | // reduce computation time as Ceres can reuse expensive objects to create. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 165 | // The context object can be nullptr, in which case Ceres may create one. |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 166 | // |
| 167 | // Ceres does NOT take ownership of the pointer. |
| 168 | Context* context = nullptr; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 169 | |
| 170 | // Using this callback interface, Ceres can notify you when it is |
| 171 | // about to evaluate the residuals or jacobians. With the |
| 172 | // callback, you can share computation between residual blocks by |
| 173 | // doing the shared computation in |
| 174 | // EvaluationCallback::PrepareForEvaluation() before Ceres calls |
| 175 | // CostFunction::Evaluate(). It also enables caching results |
| 176 | // between a pure residual evaluation and a residual & jacobian |
| 177 | // evaluation. |
| 178 | // |
| 179 | // Problem DOES NOT take ownership of the callback. |
| 180 | // |
| 181 | // NOTE: Evaluation callbacks are incompatible with inner |
| 182 | // iterations. So calling Solve with |
| 183 | // Solver::Options::use_inner_iterations = true on a Problem with |
| 184 | // a non-null evaluation callback is an error. |
| 185 | EvaluationCallback* evaluation_callback = nullptr; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | // The default constructor is equivalent to the |
| 189 | // invocation Problem(Problem::Options()). |
| 190 | Problem(); |
| 191 | explicit Problem(const Options& options); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 192 | Problem(Problem&&); |
| 193 | Problem& operator=(Problem&&); |
| 194 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 195 | Problem(const Problem&) = delete; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 196 | Problem& operator=(const Problem&) = delete; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 197 | |
| 198 | ~Problem(); |
| 199 | |
| 200 | // Add a residual block to the overall cost function. The cost |
| 201 | // function carries with its information about the sizes of the |
| 202 | // parameter blocks it expects. The function checks that these match |
| 203 | // the sizes of the parameter blocks listed in parameter_blocks. The |
| 204 | // program aborts if a mismatch is detected. loss_function can be |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 205 | // nullptr, in which case the cost of the term is just the squared norm |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 206 | // of the residuals. |
| 207 | // |
| 208 | // The user has the option of explicitly adding the parameter blocks |
| 209 | // using AddParameterBlock. This causes additional correctness |
| 210 | // checking; however, AddResidualBlock implicitly adds the parameter |
| 211 | // blocks if they are not present, so calling AddParameterBlock |
| 212 | // explicitly is not required. |
| 213 | // |
| 214 | // The Problem object by default takes ownership of the |
| 215 | // cost_function and loss_function pointers. These objects remain |
| 216 | // live for the life of the Problem object. If the user wishes to |
| 217 | // keep control over the destruction of these objects, then they can |
| 218 | // do this by setting the corresponding enums in the Options struct. |
| 219 | // |
| 220 | // Note: Even though the Problem takes ownership of cost_function |
| 221 | // and loss_function, it does not preclude the user from re-using |
| 222 | // them in another residual block. The destructor takes care to call |
| 223 | // delete on each cost_function or loss_function pointer only once, |
| 224 | // regardless of how many residual blocks refer to them. |
| 225 | // |
| 226 | // Example usage: |
| 227 | // |
| 228 | // double x1[] = {1.0, 2.0, 3.0}; |
| 229 | // double x2[] = {1.0, 2.0, 5.0, 6.0}; |
| 230 | // double x3[] = {3.0, 6.0, 2.0, 5.0, 1.0}; |
| 231 | // |
| 232 | // Problem problem; |
| 233 | // |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 234 | // problem.AddResidualBlock(new MyUnaryCostFunction(...), nullptr, x1); |
| 235 | // problem.AddResidualBlock(new MyBinaryCostFunction(...), nullptr, x2, x1); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 236 | // |
| 237 | // Add a residual block by listing the parameter block pointers |
| 238 | // directly instead of wapping them in a container. |
| 239 | template <typename... Ts> |
| 240 | ResidualBlockId AddResidualBlock(CostFunction* cost_function, |
| 241 | LossFunction* loss_function, |
| 242 | double* x0, |
| 243 | Ts*... xs) { |
| 244 | const std::array<double*, sizeof...(Ts) + 1> parameter_blocks{{x0, xs...}}; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 245 | return AddResidualBlock(cost_function, |
| 246 | loss_function, |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 247 | parameter_blocks.data(), |
| 248 | static_cast<int>(parameter_blocks.size())); |
| 249 | } |
| 250 | |
| 251 | // Add a residual block by providing a vector of parameter blocks. |
| 252 | ResidualBlockId AddResidualBlock( |
| 253 | CostFunction* cost_function, |
| 254 | LossFunction* loss_function, |
| 255 | const std::vector<double*>& parameter_blocks); |
| 256 | |
| 257 | // Add a residual block by providing a pointer to the parameter block array |
| 258 | // and the number of parameter blocks. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 259 | ResidualBlockId AddResidualBlock(CostFunction* cost_function, |
| 260 | LossFunction* loss_function, |
| 261 | double* const* const parameter_blocks, |
| 262 | int num_parameter_blocks); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 263 | |
| 264 | // Add a parameter block with appropriate size to the problem. |
| 265 | // Repeated calls with the same arguments are ignored. Repeated |
| 266 | // calls with the same double pointer but a different size results |
| 267 | // in undefined behaviour. |
| 268 | void AddParameterBlock(double* values, int size); |
| 269 | |
| 270 | // Add a parameter block with appropriate size and parameterization |
| 271 | // to the problem. Repeated calls with the same arguments are |
| 272 | // ignored. Repeated calls with the same double pointer but a |
| 273 | // different size results in undefined behaviour. |
| 274 | void AddParameterBlock(double* values, |
| 275 | int size, |
| 276 | LocalParameterization* local_parameterization); |
| 277 | |
| 278 | // Remove a parameter block from the problem. The parameterization of the |
| 279 | // parameter block, if it exists, will persist until the deletion of the |
| 280 | // problem (similar to cost/loss functions in residual block removal). Any |
| 281 | // residual blocks that depend on the parameter are also removed, as |
| 282 | // described above in RemoveResidualBlock(). |
| 283 | // |
| 284 | // If Problem::Options::enable_fast_removal is true, then the |
| 285 | // removal is fast (almost constant time). Otherwise, removing a parameter |
| 286 | // block will incur a scan of the entire Problem object. |
| 287 | // |
| 288 | // WARNING: Removing a residual or parameter block will destroy the implicit |
| 289 | // ordering, rendering the jacobian or residuals returned from the solver |
| 290 | // uninterpretable. If you depend on the evaluated jacobian, do not use |
| 291 | // remove! This may change in a future release. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 292 | void RemoveParameterBlock(const double* values); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 293 | |
| 294 | // Remove a residual block from the problem. Any parameters that the residual |
| 295 | // block depends on are not removed. The cost and loss functions for the |
| 296 | // residual block will not get deleted immediately; won't happen until the |
| 297 | // problem itself is deleted. |
| 298 | // |
| 299 | // WARNING: Removing a residual or parameter block will destroy the implicit |
| 300 | // ordering, rendering the jacobian or residuals returned from the solver |
| 301 | // uninterpretable. If you depend on the evaluated jacobian, do not use |
| 302 | // remove! This may change in a future release. |
| 303 | void RemoveResidualBlock(ResidualBlockId residual_block); |
| 304 | |
| 305 | // Hold the indicated parameter block constant during optimization. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 306 | void SetParameterBlockConstant(const double* values); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 307 | |
| 308 | // Allow the indicated parameter block to vary during optimization. |
| 309 | void SetParameterBlockVariable(double* values); |
| 310 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 311 | // Returns true if a parameter block is set constant, and false |
| 312 | // otherwise. A parameter block may be set constant in two ways: |
| 313 | // either by calling SetParameterBlockConstant or by associating a |
| 314 | // LocalParameterization with a zero dimensional tangent space with |
| 315 | // it. |
| 316 | bool IsParameterBlockConstant(const double* values) const; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 317 | |
| 318 | // Set the local parameterization for one of the parameter blocks. |
| 319 | // The local_parameterization is owned by the Problem by default. It |
| 320 | // is acceptable to set the same parameterization for multiple |
| 321 | // parameters; the destructor is careful to delete local |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 322 | // parameterizations only once. Calling SetParameterization with |
| 323 | // nullptr will clear any previously set parameterization. |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 324 | void SetParameterization(double* values, |
| 325 | LocalParameterization* local_parameterization); |
| 326 | |
| 327 | // Get the local parameterization object associated with this |
| 328 | // parameter block. If there is no parameterization object |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 329 | // associated then nullptr is returned. |
| 330 | const LocalParameterization* GetParameterization(const double* values) const; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 331 | |
| 332 | // Set the lower/upper bound for the parameter at position "index". |
| 333 | void SetParameterLowerBound(double* values, int index, double lower_bound); |
| 334 | void SetParameterUpperBound(double* values, int index, double upper_bound); |
| 335 | |
| 336 | // Get the lower/upper bound for the parameter at position |
| 337 | // "index". If the parameter is not bounded by the user, then its |
| 338 | // lower bound is -std::numeric_limits<double>::max() and upper |
| 339 | // bound is std::numeric_limits<double>::max(). |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 340 | double GetParameterLowerBound(const double* values, int index) const; |
| 341 | double GetParameterUpperBound(const double* values, int index) const; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 342 | |
| 343 | // Number of parameter blocks in the problem. Always equals |
| 344 | // parameter_blocks().size() and parameter_block_sizes().size(). |
| 345 | int NumParameterBlocks() const; |
| 346 | |
| 347 | // The size of the parameter vector obtained by summing over the |
| 348 | // sizes of all the parameter blocks. |
| 349 | int NumParameters() const; |
| 350 | |
| 351 | // Number of residual blocks in the problem. Always equals |
| 352 | // residual_blocks().size(). |
| 353 | int NumResidualBlocks() const; |
| 354 | |
| 355 | // The size of the residual vector obtained by summing over the |
| 356 | // sizes of all of the residual blocks. |
| 357 | int NumResiduals() const; |
| 358 | |
| 359 | // The size of the parameter block. |
| 360 | int ParameterBlockSize(const double* values) const; |
| 361 | |
| 362 | // The size of local parameterization for the parameter block. If |
| 363 | // there is no local parameterization associated with this parameter |
| 364 | // block, then ParameterBlockLocalSize = ParameterBlockSize. |
| 365 | int ParameterBlockLocalSize(const double* values) const; |
| 366 | |
| 367 | // Is the given parameter block present in this problem or not? |
| 368 | bool HasParameterBlock(const double* values) const; |
| 369 | |
| 370 | // Fills the passed parameter_blocks vector with pointers to the |
| 371 | // parameter blocks currently in the problem. After this call, |
| 372 | // parameter_block.size() == NumParameterBlocks. |
| 373 | void GetParameterBlocks(std::vector<double*>* parameter_blocks) const; |
| 374 | |
| 375 | // Fills the passed residual_blocks vector with pointers to the |
| 376 | // residual blocks currently in the problem. After this call, |
| 377 | // residual_blocks.size() == NumResidualBlocks. |
| 378 | void GetResidualBlocks(std::vector<ResidualBlockId>* residual_blocks) const; |
| 379 | |
| 380 | // Get all the parameter blocks that depend on the given residual block. |
| 381 | void GetParameterBlocksForResidualBlock( |
| 382 | const ResidualBlockId residual_block, |
| 383 | std::vector<double*>* parameter_blocks) const; |
| 384 | |
| 385 | // Get the CostFunction for the given residual block. |
| 386 | const CostFunction* GetCostFunctionForResidualBlock( |
| 387 | const ResidualBlockId residual_block) const; |
| 388 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 389 | // Get the LossFunction for the given residual block. Returns nullptr |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 390 | // if no loss function is associated with this residual block. |
| 391 | const LossFunction* GetLossFunctionForResidualBlock( |
| 392 | const ResidualBlockId residual_block) const; |
| 393 | |
| 394 | // Get all the residual blocks that depend on the given parameter block. |
| 395 | // |
| 396 | // If Problem::Options::enable_fast_removal is true, then |
| 397 | // getting the residual blocks is fast and depends only on the number of |
| 398 | // residual blocks. Otherwise, getting the residual blocks for a parameter |
| 399 | // block will incur a scan of the entire Problem object. |
| 400 | void GetResidualBlocksForParameterBlock( |
| 401 | const double* values, |
| 402 | std::vector<ResidualBlockId>* residual_blocks) const; |
| 403 | |
| 404 | // Options struct to control Problem::Evaluate. |
| 405 | struct EvaluateOptions { |
| 406 | // The set of parameter blocks for which evaluation should be |
| 407 | // performed. This vector determines the order that parameter |
| 408 | // blocks occur in the gradient vector and in the columns of the |
| 409 | // jacobian matrix. If parameter_blocks is empty, then it is |
| 410 | // assumed to be equal to vector containing ALL the parameter |
| 411 | // blocks. Generally speaking the parameter blocks will occur in |
| 412 | // the order in which they were added to the problem. But, this |
| 413 | // may change if the user removes any parameter blocks from the |
| 414 | // problem. |
| 415 | // |
| 416 | // NOTE: This vector should contain the same pointers as the ones |
| 417 | // used to add parameter blocks to the Problem. These parameter |
| 418 | // block should NOT point to new memory locations. Bad things will |
| 419 | // happen otherwise. |
| 420 | std::vector<double*> parameter_blocks; |
| 421 | |
| 422 | // The set of residual blocks to evaluate. This vector determines |
| 423 | // the order in which the residuals occur, and how the rows of the |
| 424 | // jacobian are ordered. If residual_blocks is empty, then it is |
| 425 | // assumed to be equal to the vector containing ALL the residual |
| 426 | // blocks. Generally speaking the residual blocks will occur in |
| 427 | // the order in which they were added to the problem. But, this |
| 428 | // may change if the user removes any residual blocks from the |
| 429 | // problem. |
| 430 | std::vector<ResidualBlockId> residual_blocks; |
| 431 | |
| 432 | // Even though the residual blocks in the problem may contain loss |
| 433 | // functions, setting apply_loss_function to false will turn off |
| 434 | // the application of the loss function to the output of the cost |
| 435 | // function. This is of use for example if the user wishes to |
| 436 | // analyse the solution quality by studying the distribution of |
| 437 | // residuals before and after the solve. |
| 438 | bool apply_loss_function = true; |
| 439 | |
| 440 | int num_threads = 1; |
| 441 | }; |
| 442 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 443 | // Evaluate Problem. Any of the output pointers can be nullptr. Which |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 444 | // residual blocks and parameter blocks are used is controlled by |
| 445 | // the EvaluateOptions struct above. |
| 446 | // |
| 447 | // Note 1: The evaluation will use the values stored in the memory |
| 448 | // locations pointed to by the parameter block pointers used at the |
| 449 | // time of the construction of the problem. i.e., |
| 450 | // |
| 451 | // Problem problem; |
| 452 | // double x = 1; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 453 | // problem.AddResidualBlock(new MyCostFunction, nullptr, &x); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 454 | // |
| 455 | // double cost = 0.0; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 456 | // problem.Evaluate(Problem::EvaluateOptions(), &cost, |
| 457 | // nullptr, nullptr, nullptr); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 458 | // |
| 459 | // The cost is evaluated at x = 1. If you wish to evaluate the |
| 460 | // problem at x = 2, then |
| 461 | // |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 462 | // x = 2; |
| 463 | // problem.Evaluate(Problem::EvaluateOptions(), &cost, |
| 464 | // nullptr, nullptr, nullptr); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 465 | // |
| 466 | // is the way to do so. |
| 467 | // |
| 468 | // Note 2: If no local parameterizations are used, then the size of |
| 469 | // the gradient vector (and the number of columns in the jacobian) |
| 470 | // is the sum of the sizes of all the parameter blocks. If a |
| 471 | // parameter block has a local parameterization, then it contributes |
| 472 | // "LocalSize" entries to the gradient vector (and the number of |
| 473 | // columns in the jacobian). |
| 474 | // |
| 475 | // Note 3: This function cannot be called while the problem is being |
| 476 | // solved, for example it cannot be called from an IterationCallback |
| 477 | // at the end of an iteration during a solve. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 478 | // |
| 479 | // Note 4: If an EvaluationCallback is associated with the problem, |
| 480 | // then its PrepareForEvaluation method will be called every time |
| 481 | // this method is called with new_point = true. |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 482 | bool Evaluate(const EvaluateOptions& options, |
| 483 | double* cost, |
| 484 | std::vector<double>* residuals, |
| 485 | std::vector<double>* gradient, |
| 486 | CRSMatrix* jacobian); |
| 487 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 488 | // Evaluates the residual block, storing the scalar cost in *cost, |
| 489 | // the residual components in *residuals, and the jacobians between |
| 490 | // the parameters and residuals in jacobians[i], in row-major order. |
| 491 | // |
| 492 | // If residuals is nullptr, the residuals are not computed. |
| 493 | // |
| 494 | // If jacobians is nullptr, no Jacobians are computed. If |
| 495 | // jacobians[i] is nullptr, then the Jacobian for that parameter |
| 496 | // block is not computed. |
| 497 | // |
| 498 | // It is not okay to request the Jacobian w.r.t a parameter block |
| 499 | // that is constant. |
| 500 | // |
| 501 | // The return value indicates the success or failure. Even if the |
| 502 | // function returns false, the caller should expect the output |
| 503 | // memory locations to have been modified. |
| 504 | // |
| 505 | // The returned cost and jacobians have had robustification and |
| 506 | // local parameterizations applied already; for example, the |
| 507 | // jacobian for a 4-dimensional quaternion parameter using the |
| 508 | // "QuaternionParameterization" is num_residuals by 3 instead of |
| 509 | // num_residuals by 4. |
| 510 | // |
| 511 | // apply_loss_function as the name implies allows the user to switch |
| 512 | // the application of the loss function on and off. |
| 513 | // |
| 514 | // If an EvaluationCallback is associated with the problem, then its |
| 515 | // PrepareForEvaluation method will be called every time this method |
| 516 | // is called with new_point = true. This conservatively assumes that |
| 517 | // the user may have changed the parameter values since the previous |
| 518 | // call to evaluate / solve. For improved efficiency, and only if |
| 519 | // you know that the parameter values have not changed between |
| 520 | // calls, see EvaluateResidualBlockAssumingParametersUnchanged(). |
| 521 | bool EvaluateResidualBlock(ResidualBlockId residual_block_id, |
| 522 | bool apply_loss_function, |
| 523 | double* cost, |
| 524 | double* residuals, |
| 525 | double** jacobians) const; |
| 526 | |
| 527 | // Same as EvaluateResidualBlock except that if an |
| 528 | // EvaluationCallback is associated with the problem, then its |
| 529 | // PrepareForEvaluation method will be called every time this method |
| 530 | // is called with new_point = false. |
| 531 | // |
| 532 | // This means, if an EvaluationCallback is associated with the |
| 533 | // problem then it is the user's responsibility to call |
| 534 | // PrepareForEvaluation before calling this method if necessary, |
| 535 | // i.e. iff the parameter values have been changed since the last |
| 536 | // call to evaluate / solve.' |
| 537 | // |
| 538 | // This is because, as the name implies, we assume that the |
| 539 | // parameter blocks did not change since the last time |
| 540 | // PrepareForEvaluation was called (via Solve, Evaluate or |
| 541 | // EvaluateResidualBlock). |
| 542 | bool EvaluateResidualBlockAssumingParametersUnchanged( |
| 543 | ResidualBlockId residual_block_id, |
| 544 | bool apply_loss_function, |
| 545 | double* cost, |
| 546 | double* residuals, |
| 547 | double** jacobians) const; |
| 548 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 549 | private: |
| 550 | friend class Solver; |
| 551 | friend class Covariance; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 552 | std::unique_ptr<internal::ProblemImpl> impl_; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 553 | }; |
| 554 | |
| 555 | } // namespace ceres |
| 556 | |
| 557 | #include "ceres/internal/reenable_warnings.h" |
| 558 | |
| 559 | #endif // CERES_PUBLIC_PROBLEM_H_ |