Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
Keir Mierle | fda69b5 | 2013-10-10 00:25:24 -0700 | [diff] [blame] | 2 | // Copyright 2013 Google Inc. All rights reserved. |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 3 | // http://code.google.com/p/ceres-solver/ |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright notice, |
| 9 | // this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | // this list of conditions and the following disclaimer in the documentation |
| 12 | // and/or other materials provided with the distribution. |
| 13 | // * Neither the name of Google Inc. nor the names of its contributors may be |
| 14 | // used to endorse or promote products derived from this software without |
| 15 | // specific prior written permission. |
| 16 | // |
| 17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 21 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | // POSSIBILITY OF SUCH DAMAGE. |
| 28 | // |
| 29 | // Author: 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 <cstddef> |
| 38 | #include <map> |
| 39 | #include <set> |
| 40 | #include <vector> |
| 41 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 42 | #include "ceres/internal/macros.h" |
| 43 | #include "ceres/internal/port.h" |
| 44 | #include "ceres/internal/scoped_ptr.h" |
| 45 | #include "ceres/types.h" |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 46 | #include "glog/logging.h" |
| 47 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 48 | |
| 49 | namespace ceres { |
| 50 | |
| 51 | class CostFunction; |
| 52 | class LossFunction; |
| 53 | class LocalParameterization; |
Keir Mierle | 6196cba | 2012-06-18 11:03:40 -0700 | [diff] [blame] | 54 | class Solver; |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 55 | struct CRSMatrix; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 56 | |
| 57 | namespace internal { |
| 58 | class Preprocessor; |
| 59 | class ProblemImpl; |
| 60 | class ParameterBlock; |
| 61 | class ResidualBlock; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 62 | } // namespace internal |
| 63 | |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 64 | // A ResidualBlockId is an opaque handle clients can use to remove residual |
| 65 | // blocks from a Problem after adding them. |
| 66 | typedef internal::ResidualBlock* ResidualBlockId; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 67 | |
| 68 | // A class to represent non-linear least squares problems. Such |
| 69 | // problems have a cost function that is a sum of error terms (known |
| 70 | // as "residuals"), where each residual is a function of some subset |
| 71 | // of the parameters. The cost function takes the form |
| 72 | // |
| 73 | // N 1 |
| 74 | // SUM --- loss( || r_i1, r_i2,..., r_ik ||^2 ), |
| 75 | // i=1 2 |
| 76 | // |
| 77 | // where |
| 78 | // |
| 79 | // r_ij is residual number i, component j; the residual is a |
| 80 | // function of some subset of the parameters x1...xk. For |
| 81 | // example, in a structure from motion problem a residual |
| 82 | // might be the difference between a measured point in an |
| 83 | // image and the reprojected position for the matching |
| 84 | // camera, point pair. The residual would have two |
| 85 | // components, error in x and error in y. |
| 86 | // |
| 87 | // loss(y) is the loss function; for example, squared error or |
| 88 | // Huber L1 loss. If loss(y) = y, then the cost function is |
| 89 | // non-robustified least squares. |
| 90 | // |
| 91 | // This class is specifically designed to address the important subset |
| 92 | // of "sparse" least squares problems, where each component of the |
| 93 | // residual depends only on a small number number of parameters, even |
| 94 | // though the total number of residuals and parameters may be very |
| 95 | // large. This property affords tremendous gains in scale, allowing |
| 96 | // efficient solving of large problems that are otherwise |
| 97 | // inaccessible. |
| 98 | // |
| 99 | // The canonical example of a sparse least squares problem is |
| 100 | // "structure-from-motion" (SFM), where the parameters are points and |
| 101 | // cameras, and residuals are reprojection errors. Typically a single |
| 102 | // residual will depend only on 9 parameters (3 for the point, 6 for |
| 103 | // the camera). |
| 104 | // |
| 105 | // To create a least squares problem, use the AddResidualBlock() and |
| 106 | // AddParameterBlock() methods, documented below. Here is an example least |
| 107 | // squares problem containing 3 parameter blocks of sizes 3, 4 and 5 |
| 108 | // respectively and two residual terms of size 2 and 6: |
| 109 | // |
| 110 | // double x1[] = { 1.0, 2.0, 3.0 }; |
| 111 | // double x2[] = { 1.0, 2.0, 3.0, 5.0 }; |
| 112 | // double x3[] = { 1.0, 2.0, 3.0, 6.0, 7.0 }; |
| 113 | // |
| 114 | // Problem problem; |
| 115 | // |
| 116 | // problem.AddResidualBlock(new MyUnaryCostFunction(...), x1); |
| 117 | // problem.AddResidualBlock(new MyBinaryCostFunction(...), x2, x3); |
| 118 | // |
| 119 | // Please see cost_function.h for details of the CostFunction object. |
| 120 | class Problem { |
| 121 | public: |
| 122 | struct Options { |
| 123 | Options() |
| 124 | : cost_function_ownership(TAKE_OWNERSHIP), |
| 125 | loss_function_ownership(TAKE_OWNERSHIP), |
Sameer Agarwal | 8e1f83c | 2013-02-15 08:35:40 -0800 | [diff] [blame] | 126 | local_parameterization_ownership(TAKE_OWNERSHIP), |
Alex Stewart | 195e493 | 2014-03-26 11:36:11 +0000 | [diff] [blame] | 127 | enable_fast_removal(false), |
Sameer Agarwal | 8e1f83c | 2013-02-15 08:35:40 -0800 | [diff] [blame] | 128 | disable_all_safety_checks(false) {} |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 129 | |
| 130 | // These flags control whether the Problem object owns the cost |
| 131 | // functions, loss functions, and parameterizations passed into |
| 132 | // the Problem. If set to TAKE_OWNERSHIP, then the problem object |
| 133 | // will delete the corresponding cost or loss functions on |
| 134 | // destruction. The destructor is careful to delete the pointers |
| 135 | // only once, since sharing cost/loss/parameterizations is |
| 136 | // allowed. |
| 137 | Ownership cost_function_ownership; |
| 138 | Ownership loss_function_ownership; |
| 139 | Ownership local_parameterization_ownership; |
Sameer Agarwal | 8e1f83c | 2013-02-15 08:35:40 -0800 | [diff] [blame] | 140 | |
Alex Stewart | 195e493 | 2014-03-26 11:36:11 +0000 | [diff] [blame] | 141 | // If true, trades memory for faster RemoveResidualBlock() and |
| 142 | // RemoveParameterBlock() operations. |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 143 | // |
Alex Stewart | 195e493 | 2014-03-26 11:36:11 +0000 | [diff] [blame] | 144 | // By default, RemoveParameterBlock() and RemoveResidualBlock() take time |
| 145 | // proportional to the size of the entire problem. If you only ever remove |
| 146 | // parameters or residuals from the problem occassionally, this might be |
| 147 | // acceptable. However, if you have memory to spare, enable this option to |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 148 | // make RemoveParameterBlock() take time proportional to the number of |
Alex Stewart | 195e493 | 2014-03-26 11:36:11 +0000 | [diff] [blame] | 149 | // residual blocks that depend on it, and RemoveResidualBlock() take (on |
| 150 | // average) constant time. |
| 151 | // |
| 152 | // The increase in memory usage is twofold: an additonal hash set per |
| 153 | // parameter block containing all the residuals that depend on the parameter |
| 154 | // block; and a hash set in the problem containing all residuals. |
| 155 | bool enable_fast_removal; |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 156 | |
| 157 | // By default, Ceres performs a variety of safety checks when constructing |
| 158 | // the problem. There is a small but measurable performance penalty to |
| 159 | // these checks, typically around 5% of construction time. If you are sure |
| 160 | // your problem construction is correct, and 5% of the problem construction |
Sameer Agarwal | 8e1f83c | 2013-02-15 08:35:40 -0800 | [diff] [blame] | 161 | // time is truly an overhead you want to avoid, then you can set |
| 162 | // disable_all_safety_checks to true. |
| 163 | // |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 164 | // WARNING: Do not set this to true, unless you are absolutely sure of what |
Sameer Agarwal | 8e1f83c | 2013-02-15 08:35:40 -0800 | [diff] [blame] | 165 | // you are doing. |
| 166 | bool disable_all_safety_checks; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | // The default constructor is equivalent to the |
| 170 | // invocation Problem(Problem::Options()). |
| 171 | Problem(); |
| 172 | explicit Problem(const Options& options); |
| 173 | |
| 174 | ~Problem(); |
| 175 | |
| 176 | // Add a residual block to the overall cost function. The cost |
| 177 | // function carries with it information about the sizes of the |
| 178 | // parameter blocks it expects. The function checks that these match |
| 179 | // the sizes of the parameter blocks listed in parameter_blocks. The |
| 180 | // program aborts if a mismatch is detected. loss_function can be |
| 181 | // NULL, in which case the cost of the term is just the squared norm |
| 182 | // of the residuals. |
| 183 | // |
| 184 | // The user has the option of explicitly adding the parameter blocks |
| 185 | // using AddParameterBlock. This causes additional correctness |
| 186 | // checking; however, AddResidualBlock implicitly adds the parameter |
| 187 | // blocks if they are not present, so calling AddParameterBlock |
| 188 | // explicitly is not required. |
| 189 | // |
| 190 | // The Problem object by default takes ownership of the |
| 191 | // cost_function and loss_function pointers. These objects remain |
| 192 | // live for the life of the Problem object. If the user wishes to |
| 193 | // keep control over the destruction of these objects, then they can |
| 194 | // do this by setting the corresponding enums in the Options struct. |
| 195 | // |
| 196 | // Note: Even though the Problem takes ownership of cost_function |
| 197 | // and loss_function, it does not preclude the user from re-using |
| 198 | // them in another residual block. The destructor takes care to call |
| 199 | // delete on each cost_function or loss_function pointer only once, |
| 200 | // regardless of how many residual blocks refer to them. |
| 201 | // |
| 202 | // Example usage: |
| 203 | // |
| 204 | // double x1[] = {1.0, 2.0, 3.0}; |
| 205 | // double x2[] = {1.0, 2.0, 5.0, 6.0}; |
| 206 | // double x3[] = {3.0, 6.0, 2.0, 5.0, 1.0}; |
| 207 | // |
| 208 | // Problem problem; |
| 209 | // |
| 210 | // problem.AddResidualBlock(new MyUnaryCostFunction(...), NULL, x1); |
| 211 | // problem.AddResidualBlock(new MyBinaryCostFunction(...), NULL, x2, x1); |
| 212 | // |
| 213 | ResidualBlockId AddResidualBlock(CostFunction* cost_function, |
| 214 | LossFunction* loss_function, |
| 215 | const vector<double*>& parameter_blocks); |
| 216 | |
| 217 | // Convenience methods for adding residuals with a small number of |
| 218 | // parameters. This is the common case. Instead of specifying the |
| 219 | // parameter block arguments as a vector, list them as pointers. |
| 220 | ResidualBlockId AddResidualBlock(CostFunction* cost_function, |
| 221 | LossFunction* loss_function, |
| 222 | double* x0); |
| 223 | ResidualBlockId AddResidualBlock(CostFunction* cost_function, |
| 224 | LossFunction* loss_function, |
| 225 | double* x0, double* x1); |
| 226 | ResidualBlockId AddResidualBlock(CostFunction* cost_function, |
| 227 | LossFunction* loss_function, |
| 228 | double* x0, double* x1, double* x2); |
| 229 | ResidualBlockId AddResidualBlock(CostFunction* cost_function, |
| 230 | LossFunction* loss_function, |
| 231 | double* x0, double* x1, double* x2, |
| 232 | double* x3); |
| 233 | ResidualBlockId AddResidualBlock(CostFunction* cost_function, |
| 234 | LossFunction* loss_function, |
| 235 | double* x0, double* x1, double* x2, |
| 236 | double* x3, double* x4); |
| 237 | ResidualBlockId AddResidualBlock(CostFunction* cost_function, |
| 238 | LossFunction* loss_function, |
| 239 | double* x0, double* x1, double* x2, |
| 240 | double* x3, double* x4, double* x5); |
Fisher | 12626e8 | 2012-10-21 14:12:04 -0400 | [diff] [blame] | 241 | ResidualBlockId AddResidualBlock(CostFunction* cost_function, |
| 242 | LossFunction* loss_function, |
| 243 | double* x0, double* x1, double* x2, |
| 244 | double* x3, double* x4, double* x5, |
| 245 | double* x6); |
| 246 | ResidualBlockId AddResidualBlock(CostFunction* cost_function, |
| 247 | LossFunction* loss_function, |
| 248 | double* x0, double* x1, double* x2, |
| 249 | double* x3, double* x4, double* x5, |
| 250 | double* x6, double* x7); |
| 251 | ResidualBlockId AddResidualBlock(CostFunction* cost_function, |
| 252 | LossFunction* loss_function, |
| 253 | double* x0, double* x1, double* x2, |
| 254 | double* x3, double* x4, double* x5, |
| 255 | double* x6, double* x7, double* x8); |
| 256 | ResidualBlockId AddResidualBlock(CostFunction* cost_function, |
| 257 | LossFunction* loss_function, |
| 258 | double* x0, double* x1, double* x2, |
| 259 | double* x3, double* x4, double* x5, |
| 260 | double* x6, double* x7, double* x8, |
| 261 | double* x9); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 262 | |
| 263 | // Add a parameter block with appropriate size to the problem. |
| 264 | // Repeated calls with the same arguments are ignored. Repeated |
| 265 | // calls with the same double pointer but a different size results |
| 266 | // in undefined behaviour. |
| 267 | void AddParameterBlock(double* values, int size); |
| 268 | |
| 269 | // Add a parameter block with appropriate size and parameterization |
| 270 | // to the problem. Repeated calls with the same arguments are |
| 271 | // ignored. Repeated calls with the same double pointer but a |
| 272 | // different size results in undefined behaviour. |
| 273 | void AddParameterBlock(double* values, |
| 274 | int size, |
| 275 | LocalParameterization* local_parameterization); |
| 276 | |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 277 | // Remove a parameter block from the problem. The parameterization of the |
| 278 | // parameter block, if it exists, will persist until the deletion of the |
| 279 | // problem (similar to cost/loss functions in residual block removal). Any |
| 280 | // residual blocks that depend on the parameter are also removed, as |
| 281 | // described above in RemoveResidualBlock(). |
| 282 | // |
Alex Stewart | 195e493 | 2014-03-26 11:36:11 +0000 | [diff] [blame] | 283 | // If Problem::Options::enable_fast_removal is true, then the |
Keir Mierle | 04938ef | 2013-02-17 12:37:55 -0800 | [diff] [blame] | 284 | // removal is fast (almost constant time). Otherwise, removing a parameter |
| 285 | // block will incur a scan of the entire Problem object. |
| 286 | // |
| 287 | // WARNING: Removing a residual or parameter block will destroy the implicit |
| 288 | // ordering, rendering the jacobian or residuals returned from the solver |
| 289 | // uninterpretable. If you depend on the evaluated jacobian, do not use |
| 290 | // remove! This may change in a future release. |
| 291 | void RemoveParameterBlock(double* values); |
| 292 | |
| 293 | // Remove a residual block from the problem. Any parameters that the residual |
| 294 | // block depends on are not removed. The cost and loss functions for the |
| 295 | // residual block will not get deleted immediately; won't happen until the |
| 296 | // problem itself is deleted. |
| 297 | // |
| 298 | // WARNING: Removing a residual or parameter block will destroy the implicit |
| 299 | // ordering, rendering the jacobian or residuals returned from the solver |
| 300 | // uninterpretable. If you depend on the evaluated jacobian, do not use |
| 301 | // remove! This may change in a future release. |
| 302 | void RemoveResidualBlock(ResidualBlockId residual_block); |
| 303 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 304 | // Hold the indicated parameter block constant during optimization. |
| 305 | void SetParameterBlockConstant(double* values); |
| 306 | |
Sameer Agarwal | a482ab8 | 2014-02-18 22:24:03 -0800 | [diff] [blame] | 307 | // Allow the indicated parameter block to vary during optimization. |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 308 | void SetParameterBlockVariable(double* values); |
| 309 | |
| 310 | // Set the local parameterization for one of the parameter blocks. |
| 311 | // The local_parameterization is owned by the Problem by default. It |
| 312 | // is acceptable to set the same parameterization for multiple |
| 313 | // parameters; the destructor is careful to delete local |
| 314 | // parameterizations only once. The local parameterization can only |
| 315 | // be set once per parameter, and cannot be changed once set. |
| 316 | void SetParameterization(double* values, |
| 317 | LocalParameterization* local_parameterization); |
| 318 | |
Sameer Agarwal | f949bab | 2014-02-18 10:11:02 -0800 | [diff] [blame] | 319 | // Get the local parameterization object associated with this |
| 320 | // parameter block. If there is no parameterization object |
| 321 | // associated then NULL is returned. |
| 322 | const LocalParameterization* GetParameterization(double* values) const; |
| 323 | |
Sameer Agarwal | a482ab8 | 2014-02-18 22:24:03 -0800 | [diff] [blame] | 324 | // Set the lower/upper bound for the parameter with position "index". |
| 325 | void SetParameterLowerBound(double* values, int index, double lower_bound); |
| 326 | void SetParameterUpperBound(double* values, int index, double upper_bound); |
| 327 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 328 | // Number of parameter blocks in the problem. Always equals |
| 329 | // parameter_blocks().size() and parameter_block_sizes().size(). |
| 330 | int NumParameterBlocks() const; |
| 331 | |
| 332 | // The size of the parameter vector obtained by summing over the |
| 333 | // sizes of all the parameter blocks. |
| 334 | int NumParameters() const; |
| 335 | |
| 336 | // Number of residual blocks in the problem. Always equals |
| 337 | // residual_blocks().size(). |
| 338 | int NumResidualBlocks() const; |
| 339 | |
| 340 | // The size of the residual vector obtained by summing over the |
| 341 | // sizes of all of the residual blocks. |
| 342 | int NumResiduals() const; |
| 343 | |
Sameer Agarwal | 3d95469 | 2013-04-18 14:54:55 -0700 | [diff] [blame] | 344 | // The size of the parameter block. |
Sameer Agarwal | 02706c1 | 2013-05-12 22:07:55 -0700 | [diff] [blame] | 345 | int ParameterBlockSize(const double* values) const; |
Sameer Agarwal | 3d95469 | 2013-04-18 14:54:55 -0700 | [diff] [blame] | 346 | |
| 347 | // The size of local parameterization for the parameter block. If |
| 348 | // there is no local parameterization associated with this parameter |
Sameer Agarwal | 7823cf2 | 2013-04-18 16:13:56 -0700 | [diff] [blame] | 349 | // block, then ParameterBlockLocalSize = ParameterBlockSize. |
Sameer Agarwal | 02706c1 | 2013-05-12 22:07:55 -0700 | [diff] [blame] | 350 | int ParameterBlockLocalSize(const double* values) const; |
Sameer Agarwal | 3d95469 | 2013-04-18 14:54:55 -0700 | [diff] [blame] | 351 | |
Sameer Agarwal | 5ecb1c3 | 2014-04-01 09:20:35 -0700 | [diff] [blame] | 352 | // Is the given parameter block present in this problem or not? |
| 353 | bool HasParameterBlock(const double* values) const; |
| 354 | |
Sameer Agarwal | 3d95469 | 2013-04-18 14:54:55 -0700 | [diff] [blame] | 355 | // Fills the passed parameter_blocks vector with pointers to the |
| 356 | // parameter blocks currently in the problem. After this call, |
| 357 | // parameter_block.size() == NumParameterBlocks. |
| 358 | void GetParameterBlocks(vector<double*>* parameter_blocks) const; |
| 359 | |
Keir Mierle | fda69b5 | 2013-10-10 00:25:24 -0700 | [diff] [blame] | 360 | // Fills the passed residual_blocks vector with pointers to the |
| 361 | // residual blocks currently in the problem. After this call, |
| 362 | // residual_blocks.size() == NumResidualBlocks. |
| 363 | void GetResidualBlocks(vector<ResidualBlockId>* residual_blocks) const; |
| 364 | |
| 365 | // Get all the parameter blocks that depend on the given residual block. |
| 366 | void GetParameterBlocksForResidualBlock( |
| 367 | const ResidualBlockId residual_block, |
| 368 | vector<double*>* parameter_blocks) const; |
| 369 | |
| 370 | // Get all the residual blocks that depend on the given parameter block. |
| 371 | // |
Alex Stewart | 195e493 | 2014-03-26 11:36:11 +0000 | [diff] [blame] | 372 | // If Problem::Options::enable_fast_removal is true, then |
Keir Mierle | fda69b5 | 2013-10-10 00:25:24 -0700 | [diff] [blame] | 373 | // getting the residual blocks is fast and depends only on the number of |
| 374 | // residual blocks. Otherwise, getting the residual blocks for a parameter |
| 375 | // block will incur a scan of the entire Problem object. |
| 376 | void GetResidualBlocksForParameterBlock( |
| 377 | const double* values, |
| 378 | vector<ResidualBlockId>* residual_blocks) const; |
| 379 | |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 380 | // Options struct to control Problem::Evaluate. |
| 381 | struct EvaluateOptions { |
| 382 | EvaluateOptions() |
Sameer Agarwal | 039ff07 | 2013-02-26 09:15:39 -0800 | [diff] [blame] | 383 | : apply_loss_function(true), |
| 384 | num_threads(1) { |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | // The set of parameter blocks for which evaluation should be |
| 388 | // performed. This vector determines the order that parameter |
| 389 | // blocks occur in the gradient vector and in the columns of the |
| 390 | // jacobian matrix. If parameter_blocks is empty, then it is |
| 391 | // assumed to be equal to vector containing ALL the parameter |
| 392 | // blocks. Generally speaking the parameter blocks will occur in |
| 393 | // the order in which they were added to the problem. But, this |
| 394 | // may change if the user removes any parameter blocks from the |
| 395 | // problem. |
| 396 | // |
| 397 | // NOTE: This vector should contain the same pointers as the ones |
Sameer Agarwal | 931c309 | 2013-02-25 09:46:21 -0800 | [diff] [blame] | 398 | // used to add parameter blocks to the Problem. These parameter |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 399 | // block should NOT point to new memory locations. Bad things will |
| 400 | // happen otherwise. |
| 401 | vector<double*> parameter_blocks; |
| 402 | |
| 403 | // The set of residual blocks to evaluate. This vector determines |
| 404 | // the order in which the residuals occur, and how the rows of the |
| 405 | // jacobian are ordered. If residual_blocks is empty, then it is |
| 406 | // assumed to be equal to the vector containing all the residual |
| 407 | // blocks. If this vector is empty, then it is assumed to be equal |
| 408 | // to a vector containing ALL the residual blocks. Generally |
| 409 | // speaking the residual blocks will occur in the order in which |
| 410 | // they were added to the problem. But, this may change if the |
| 411 | // user removes any residual blocks from the problem. |
| 412 | vector<ResidualBlockId> residual_blocks; |
Sameer Agarwal | 039ff07 | 2013-02-26 09:15:39 -0800 | [diff] [blame] | 413 | |
| 414 | // Even though the residual blocks in the problem may contain loss |
| 415 | // functions, setting apply_loss_function to false will turn off |
| 416 | // the application of the loss function to the output of the cost |
| 417 | // function. This is of use for example if the user wishes to |
| 418 | // analyse the solution quality by studying the distribution of |
| 419 | // residuals before and after the solve. |
| 420 | bool apply_loss_function; |
| 421 | |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 422 | int num_threads; |
| 423 | }; |
| 424 | |
| 425 | // Evaluate Problem. Any of the output pointers can be NULL. Which |
| 426 | // residual blocks and parameter blocks are used is controlled by |
| 427 | // the EvaluateOptions struct above. |
| 428 | // |
| 429 | // Note 1: The evaluation will use the values stored in the memory |
| 430 | // locations pointed to by the parameter block pointers used at the |
| 431 | // time of the construction of the problem. i.e., |
| 432 | // |
| 433 | // Problem problem; |
| 434 | // double x = 1; |
Sameer Agarwal | 5e7ce8a | 2013-03-06 11:38:41 -0800 | [diff] [blame] | 435 | // problem.AddResidualBlock(new MyCostFunction, NULL, &x); |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 436 | // |
| 437 | // double cost = 0.0; |
| 438 | // problem.Evaluate(Problem::EvaluateOptions(), &cost, NULL, NULL, NULL); |
| 439 | // |
| 440 | // The cost is evaluated at x = 1. If you wish to evaluate the |
| 441 | // problem at x = 2, then |
| 442 | // |
| 443 | // x = 2; |
| 444 | // problem.Evaluate(Problem::EvaluateOptions(), &cost, NULL, NULL, NULL); |
| 445 | // |
| 446 | // is the way to do so. |
| 447 | // |
| 448 | // Note 2: If no local parameterizations are used, then the size of |
| 449 | // the gradient vector (and the number of columns in the jacobian) |
| 450 | // is the sum of the sizes of all the parameter blocks. If a |
| 451 | // parameter block has a local parameterization, then it contributes |
Sameer Agarwal | 931c309 | 2013-02-25 09:46:21 -0800 | [diff] [blame] | 452 | // "LocalSize" entries to the gradient vector (and the number of |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 453 | // columns in the jacobian). |
| 454 | bool Evaluate(const EvaluateOptions& options, |
| 455 | double* cost, |
| 456 | vector<double>* residuals, |
| 457 | vector<double>* gradient, |
| 458 | CRSMatrix* jacobian); |
| 459 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 460 | private: |
Keir Mierle | 6196cba | 2012-06-18 11:03:40 -0700 | [diff] [blame] | 461 | friend class Solver; |
Sameer Agarwal | 02706c1 | 2013-05-12 22:07:55 -0700 | [diff] [blame] | 462 | friend class Covariance; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 463 | internal::scoped_ptr<internal::ProblemImpl> problem_impl_; |
Sameer Agarwal | 237d659 | 2012-05-30 20:34:49 -0700 | [diff] [blame] | 464 | CERES_DISALLOW_COPY_AND_ASSIGN(Problem); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 465 | }; |
| 466 | |
| 467 | } // namespace ceres |
| 468 | |
| 469 | #endif // CERES_PUBLIC_PROBLEM_H_ |