Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
| 2 | // Copyright 2010, 2011, 2012 Google Inc. All rights reserved. |
| 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: keir@google.com (Keir Mierle) |
| 30 | |
| 31 | #ifndef CERES_INTERNAL_SOLVER_IMPL_H_ |
| 32 | #define CERES_INTERNAL_SOLVER_IMPL_H_ |
| 33 | |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 34 | #include <set> |
Sameer Agarwal | 4997cbc | 2012-07-02 12:44:34 -0700 | [diff] [blame] | 35 | #include <string> |
| 36 | #include <vector> |
| 37 | #include "ceres/internal/port.h" |
Sameer Agarwal | 2c94eed | 2012-10-01 16:34:37 -0700 | [diff] [blame] | 38 | #include "ceres/ordered_groups.h" |
Sameer Agarwal | 9123e2f | 2012-09-18 21:49:06 -0700 | [diff] [blame] | 39 | #include "ceres/problem_impl.h" |
Sameer Agarwal | ba8d967 | 2012-10-02 00:48:57 -0700 | [diff] [blame] | 40 | #include "ceres/solver.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 41 | |
| 42 | namespace ceres { |
| 43 | namespace internal { |
| 44 | |
Sameer Agarwal | ba8d967 | 2012-10-02 00:48:57 -0700 | [diff] [blame] | 45 | class CoordinateDescentMinimizer; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 46 | class Evaluator; |
| 47 | class LinearSolver; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 48 | class Program; |
| 49 | |
| 50 | class SolverImpl { |
| 51 | public: |
| 52 | // Mirrors the interface in solver.h, but exposes implementation |
| 53 | // details for testing internally. |
| 54 | static void Solve(const Solver::Options& options, |
Keir Mierle | 6196cba | 2012-06-18 11:03:40 -0700 | [diff] [blame] | 55 | ProblemImpl* problem_impl, |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 56 | Solver::Summary* summary); |
| 57 | |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 58 | static void TrustRegionSolve(const Solver::Options& options, |
| 59 | ProblemImpl* problem_impl, |
| 60 | Solver::Summary* summary); |
| 61 | |
Sameer Agarwal | 8140f0f | 2013-03-12 09:45:08 -0700 | [diff] [blame] | 62 | // Run the TrustRegionMinimizer for the given evaluator and configuration. |
| 63 | static void TrustRegionMinimize( |
| 64 | const Solver::Options &options, |
| 65 | Program* program, |
| 66 | CoordinateDescentMinimizer* inner_iteration_minimizer, |
| 67 | Evaluator* evaluator, |
| 68 | LinearSolver* linear_solver, |
| 69 | double* parameters, |
| 70 | Solver::Summary* summary); |
| 71 | |
| 72 | #ifndef CERES_NO_LINE_SEARCH_MINIMIZER |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 73 | static void LineSearchSolve(const Solver::Options& options, |
| 74 | ProblemImpl* problem_impl, |
| 75 | Solver::Summary* summary); |
| 76 | |
Sameer Agarwal | 8140f0f | 2013-03-12 09:45:08 -0700 | [diff] [blame] | 77 | // Run the LineSearchMinimizer for the given evaluator and configuration. |
| 78 | static void LineSearchMinimize(const Solver::Options &options, |
| 79 | Program* program, |
| 80 | Evaluator* evaluator, |
| 81 | double* parameters, |
| 82 | Solver::Summary* summary); |
Sameer Agarwal | 700d50d | 2013-03-12 16:12:42 -0700 | [diff] [blame] | 83 | #endif // CERES_NO_LINE_SEARCH_MINIMIZER |
Sameer Agarwal | 8140f0f | 2013-03-12 09:45:08 -0700 | [diff] [blame] | 84 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 85 | // Create the transformed Program, which has all the fixed blocks |
| 86 | // and residuals eliminated, and in the case of automatic schur |
| 87 | // ordering, has the E blocks first in the resulting program, with |
| 88 | // options.num_eliminate_blocks set appropriately. |
Sameer Agarwal | 9123e2f | 2012-09-18 21:49:06 -0700 | [diff] [blame] | 89 | // |
Markus Moll | 8de78db | 2012-07-14 15:49:11 +0200 | [diff] [blame] | 90 | // If fixed_cost is not NULL, the residual blocks that are removed |
| 91 | // are evaluated and the sum of their cost is returned in fixed_cost. |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 92 | static Program* CreateReducedProgram(Solver::Options* options, |
| 93 | ProblemImpl* problem_impl, |
Markus Moll | 8de78db | 2012-07-14 15:49:11 +0200 | [diff] [blame] | 94 | double* fixed_cost, |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 95 | string* error); |
| 96 | |
| 97 | // Create the appropriate linear solver, taking into account any |
| 98 | // config changes decided by CreateTransformedProgram(). The |
| 99 | // selected linear solver, which may be different from what the user |
| 100 | // selected; consider the case that the remaining elimininated |
| 101 | // blocks is zero after removing fixed blocks. |
| 102 | static LinearSolver* CreateLinearSolver(Solver::Options* options, |
| 103 | string* error); |
| 104 | |
Sameer Agarwal | 9123e2f | 2012-09-18 21:49:06 -0700 | [diff] [blame] | 105 | // Reorder the parameter blocks in program using the ordering. A |
| 106 | // return value of true indicates success and false indicates an |
| 107 | // error was encountered whose cause is logged to LOG(ERROR). |
| 108 | static bool ApplyUserOrdering(const ProblemImpl::ParameterMap& parameter_map, |
Sameer Agarwal | 2c94eed | 2012-10-01 16:34:37 -0700 | [diff] [blame] | 109 | const ParameterBlockOrdering* ordering, |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 110 | Program* program, |
| 111 | string* error); |
| 112 | |
Sameer Agarwal | 9123e2f | 2012-09-18 21:49:06 -0700 | [diff] [blame] | 113 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 114 | // Reorder the residuals for program, if necessary, so that the |
Sameer Agarwal | 9123e2f | 2012-09-18 21:49:06 -0700 | [diff] [blame] | 115 | // residuals involving e block (i.e., the first num_eliminate_block |
| 116 | // parameter blocks) occur together. This is a necessary condition |
Sameer Agarwal | ba8d967 | 2012-10-02 00:48:57 -0700 | [diff] [blame] | 117 | // for the Schur eliminator. |
Sameer Agarwal | 9123e2f | 2012-09-18 21:49:06 -0700 | [diff] [blame] | 118 | static bool LexicographicallyOrderResidualBlocks( |
| 119 | const int num_eliminate_blocks, |
| 120 | Program* program, |
| 121 | string* error); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 122 | |
| 123 | // Create the appropriate evaluator for the transformed program. |
Sameer Agarwal | 9123e2f | 2012-09-18 21:49:06 -0700 | [diff] [blame] | 124 | static Evaluator* CreateEvaluator( |
| 125 | const Solver::Options& options, |
| 126 | const ProblemImpl::ParameterMap& parameter_map, |
| 127 | Program* program, |
| 128 | string* error); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 129 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 130 | // Remove the fixed or unused parameter blocks and residuals |
| 131 | // depending only on fixed parameters from the problem. Also updates |
| 132 | // num_eliminate_blocks, since removed parameters changes the point |
Sameer Agarwal | 9123e2f | 2012-09-18 21:49:06 -0700 | [diff] [blame] | 133 | // at which the eliminated blocks is valid. If fixed_cost is not |
| 134 | // NULL, the residual blocks that are removed are evaluated and the |
| 135 | // sum of their cost is returned in fixed_cost. |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 136 | static bool RemoveFixedBlocksFromProgram(Program* program, |
Sameer Agarwal | 2c94eed | 2012-10-01 16:34:37 -0700 | [diff] [blame] | 137 | ParameterBlockOrdering* ordering, |
Markus Moll | 8de78db | 2012-07-14 15:49:11 +0200 | [diff] [blame] | 138 | double* fixed_cost, |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 139 | string* error); |
Sameer Agarwal | 65625f7 | 2012-09-17 12:06:57 -0700 | [diff] [blame] | 140 | |
| 141 | static bool IsOrderingValid(const Solver::Options& options, |
| 142 | const ProblemImpl* problem_impl, |
| 143 | string* error); |
Sameer Agarwal | 9123e2f | 2012-09-18 21:49:06 -0700 | [diff] [blame] | 144 | |
| 145 | static bool IsParameterBlockSetIndependent( |
Sameer Agarwal | ba8d967 | 2012-10-02 00:48:57 -0700 | [diff] [blame] | 146 | const set<double*>& parameter_block_ptrs, |
| 147 | const vector<ResidualBlock*>& residual_blocks); |
Sameer Agarwal | 67a107b | 2012-10-08 14:35:41 -0700 | [diff] [blame] | 148 | |
| 149 | static CoordinateDescentMinimizer* CreateInnerIterationMinimizer( |
| 150 | const Solver::Options& options, |
| 151 | const Program& program, |
| 152 | const ProblemImpl::ParameterMap& parameter_map, |
Sameer Agarwal | 977be7c | 2013-01-26 16:01:54 -0800 | [diff] [blame] | 153 | Solver::Summary* summary); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | } // namespace internal |
| 157 | } // namespace ceres |
| 158 | |
| 159 | #endif // CERES_INTERNAL_SOLVER_IMPL_H_ |