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: keir@google.com (Keir Mierle) |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 30 | // sameeragarwal@google.com (Sameer Agarwal) |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 31 | // |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 32 | // This tests the TrustRegionMinimizer loop using a direct Evaluator |
| 33 | // implementation, rather than having a test that goes through all the |
| 34 | // Program and Problem machinery. |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 35 | |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 36 | #include "ceres/trust_region_minimizer.h" |
| 37 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 38 | #include <cmath> |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 39 | |
Sameer Agarwal | 5648c63 | 2014-11-04 22:02:09 -0800 | [diff] [blame] | 40 | #include "ceres/autodiff_cost_function.h" |
Sameer Agarwal | a406b17 | 2012-08-18 15:28:49 -0700 | [diff] [blame] | 41 | #include "ceres/cost_function.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 42 | #include "ceres/dense_qr_solver.h" |
| 43 | #include "ceres/dense_sparse_matrix.h" |
| 44 | #include "ceres/evaluator.h" |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 45 | #include "ceres/internal/port.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 46 | #include "ceres/linear_solver.h" |
| 47 | #include "ceres/minimizer.h" |
Sameer Agarwal | a406b17 | 2012-08-18 15:28:49 -0700 | [diff] [blame] | 48 | #include "ceres/problem.h" |
Sameer Agarwal | 4441b5b | 2012-06-12 18:01:11 -0700 | [diff] [blame] | 49 | #include "ceres/trust_region_strategy.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 50 | #include "gtest/gtest.h" |
| 51 | |
| 52 | namespace ceres { |
| 53 | namespace internal { |
| 54 | |
| 55 | // Templated Evaluator for Powell's function. The template parameters |
| 56 | // indicate which of the four variables/columns of the jacobian are |
| 57 | // active. This is equivalent to constructing a problem and using the |
Sameer Agarwal | 125a0e9 | 2021-12-28 07:05:03 -0800 | [diff] [blame] | 58 | // SubsetManifold. This allows us to test the support for |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 59 | // the Evaluator::Plus operation besides checking for the basic |
Sameer Agarwal | 4441b5b | 2012-06-12 18:01:11 -0700 | [diff] [blame] | 60 | // performance of the trust region algorithm. |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 61 | template <bool col1, bool col2, bool col3, bool col4> |
| 62 | class PowellEvaluator2 : public Evaluator { |
| 63 | public: |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 64 | // clang-format off |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 65 | PowellEvaluator2() |
| 66 | : num_active_cols_( |
| 67 | (col1 ? 1 : 0) + |
| 68 | (col2 ? 1 : 0) + |
| 69 | (col3 ? 1 : 0) + |
| 70 | (col4 ? 1 : 0)) { |
| 71 | VLOG(1) << "Columns: " |
| 72 | << col1 << " " |
| 73 | << col2 << " " |
| 74 | << col3 << " " |
| 75 | << col4; |
| 76 | } |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 77 | // clang-format on |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 78 | |
Sergiu Deitsch | 484d341 | 2022-02-09 00:34:05 +0100 | [diff] [blame^] | 79 | ~PowellEvaluator2() override {} |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 80 | |
| 81 | // Implementation of Evaluator interface. |
Sameer Agarwal | 2ffddac | 2019-07-14 00:16:13 +0200 | [diff] [blame] | 82 | SparseMatrix* CreateJacobian() const final { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 83 | CHECK(col1 || col2 || col3 || col4); |
| 84 | DenseSparseMatrix* dense_jacobian = |
| 85 | new DenseSparseMatrix(NumResiduals(), NumEffectiveParameters()); |
| 86 | dense_jacobian->SetZero(); |
| 87 | return dense_jacobian; |
| 88 | } |
| 89 | |
Sameer Agarwal | 2ffddac | 2019-07-14 00:16:13 +0200 | [diff] [blame] | 90 | bool Evaluate(const Evaluator::EvaluateOptions& evaluate_options, |
| 91 | const double* state, |
| 92 | double* cost, |
| 93 | double* residuals, |
| 94 | double* gradient, |
| 95 | SparseMatrix* jacobian) final { |
Sameer Agarwal | c3c3dd8 | 2013-04-25 07:49:24 -0700 | [diff] [blame] | 96 | const double x1 = state[0]; |
| 97 | const double x2 = state[1]; |
| 98 | const double x3 = state[2]; |
| 99 | const double x4 = state[3]; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 100 | |
| 101 | VLOG(1) << "State: " |
| 102 | << "x1=" << x1 << ", " |
| 103 | << "x2=" << x2 << ", " |
| 104 | << "x3=" << x3 << ", " |
| 105 | << "x4=" << x4 << "."; |
| 106 | |
Sameer Agarwal | c3c3dd8 | 2013-04-25 07:49:24 -0700 | [diff] [blame] | 107 | const double f1 = x1 + 10.0 * x2; |
| 108 | const double f2 = sqrt(5.0) * (x3 - x4); |
| 109 | const double f3 = pow(x2 - 2.0 * x3, 2.0); |
| 110 | const double f4 = sqrt(10.0) * pow(x1 - x4, 2.0); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 111 | |
| 112 | VLOG(1) << "Function: " |
| 113 | << "f1=" << f1 << ", " |
| 114 | << "f2=" << f2 << ", " |
| 115 | << "f3=" << f3 << ", " |
| 116 | << "f4=" << f4 << "."; |
| 117 | |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 118 | *cost = (f1 * f1 + f2 * f2 + f3 * f3 + f4 * f4) / 2.0; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 119 | |
| 120 | VLOG(1) << "Cost: " << *cost; |
| 121 | |
| 122 | if (residuals != NULL) { |
| 123 | residuals[0] = f1; |
| 124 | residuals[1] = f2; |
| 125 | residuals[2] = f3; |
| 126 | residuals[3] = f4; |
| 127 | } |
| 128 | |
| 129 | if (jacobian != NULL) { |
| 130 | DenseSparseMatrix* dense_jacobian; |
| 131 | dense_jacobian = down_cast<DenseSparseMatrix*>(jacobian); |
| 132 | dense_jacobian->SetZero(); |
| 133 | |
Sameer Agarwal | cab853f | 2022-01-26 11:00:48 -0800 | [diff] [blame] | 134 | Matrix& jacobian_matrix = *(dense_jacobian->mutable_matrix()); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 135 | CHECK_EQ(jacobian_matrix.cols(), num_active_cols_); |
| 136 | |
| 137 | int column_index = 0; |
| 138 | if (col1) { |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 139 | // clang-format off |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 140 | jacobian_matrix.col(column_index++) << |
| 141 | 1.0, |
| 142 | 0.0, |
| 143 | 0.0, |
Sameer Agarwal | d4eb83e | 2021-08-15 16:37:47 -0700 | [diff] [blame] | 144 | sqrt(10.0) * 2.0 * (x1 - x4); |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 145 | // clang-format on |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 146 | } |
| 147 | if (col2) { |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 148 | // clang-format off |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 149 | jacobian_matrix.col(column_index++) << |
| 150 | 10.0, |
| 151 | 0.0, |
Sameer Agarwal | d4eb83e | 2021-08-15 16:37:47 -0700 | [diff] [blame] | 152 | 2.0*(x2 - 2.0*x3), |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 153 | 0.0; |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 154 | // clang-format on |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | if (col3) { |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 158 | // clang-format off |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 159 | jacobian_matrix.col(column_index++) << |
| 160 | 0.0, |
| 161 | sqrt(5.0), |
Sameer Agarwal | d4eb83e | 2021-08-15 16:37:47 -0700 | [diff] [blame] | 162 | 4.0*(2.0*x3 - x2), |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 163 | 0.0; |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 164 | // clang-format on |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | if (col4) { |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 168 | // clang-format off |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 169 | jacobian_matrix.col(column_index++) << |
| 170 | 0.0, |
| 171 | -sqrt(5.0), |
| 172 | 0.0, |
Sameer Agarwal | d4eb83e | 2021-08-15 16:37:47 -0700 | [diff] [blame] | 173 | sqrt(10.0) * 2.0 * (x4 - x1); |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 174 | // clang-format on |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 175 | } |
| 176 | VLOG(1) << "\n" << jacobian_matrix; |
| 177 | } |
Sameer Agarwal | c3c3dd8 | 2013-04-25 07:49:24 -0700 | [diff] [blame] | 178 | |
| 179 | if (gradient != NULL) { |
| 180 | int column_index = 0; |
| 181 | if (col1) { |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 182 | gradient[column_index++] = f1 + f4 * sqrt(10.0) * 2.0 * (x1 - x4); |
Sameer Agarwal | c3c3dd8 | 2013-04-25 07:49:24 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | if (col2) { |
| 186 | gradient[column_index++] = f1 * 10.0 + f3 * 2.0 * (x2 - 2.0 * x3); |
| 187 | } |
| 188 | |
| 189 | if (col3) { |
| 190 | gradient[column_index++] = |
Sameer Agarwal | d4eb83e | 2021-08-15 16:37:47 -0700 | [diff] [blame] | 191 | f2 * sqrt(5.0) + f3 * (4.0 * (2.0 * x3 - x2)); |
Sameer Agarwal | c3c3dd8 | 2013-04-25 07:49:24 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | if (col4) { |
| 195 | gradient[column_index++] = |
| 196 | -f2 * sqrt(5.0) + f4 * sqrt(10.0) * 2.0 * (x4 - x1); |
| 197 | } |
| 198 | } |
| 199 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 200 | return true; |
| 201 | } |
| 202 | |
Sameer Agarwal | 2ffddac | 2019-07-14 00:16:13 +0200 | [diff] [blame] | 203 | bool Plus(const double* state, |
| 204 | const double* delta, |
| 205 | double* state_plus_delta) const final { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 206 | int delta_index = 0; |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 207 | state_plus_delta[0] = (col1 ? state[0] + delta[delta_index++] : state[0]); |
| 208 | state_plus_delta[1] = (col2 ? state[1] + delta[delta_index++] : state[1]); |
| 209 | state_plus_delta[2] = (col3 ? state[2] + delta[delta_index++] : state[2]); |
| 210 | state_plus_delta[3] = (col4 ? state[3] + delta[delta_index++] : state[3]); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 211 | return true; |
| 212 | } |
| 213 | |
Sameer Agarwal | 2ffddac | 2019-07-14 00:16:13 +0200 | [diff] [blame] | 214 | int NumEffectiveParameters() const final { return num_active_cols_; } |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 215 | int NumParameters() const final { return 4; } |
| 216 | int NumResiduals() const final { return 4; } |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 217 | |
| 218 | private: |
| 219 | const int num_active_cols_; |
| 220 | }; |
| 221 | |
| 222 | // Templated function to hold a subset of the columns fixed and check |
| 223 | // if the solver converges to the optimal values or not. |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 224 | template <bool col1, bool col2, bool col3, bool col4> |
Sameer Agarwal | 4441b5b | 2012-06-12 18:01:11 -0700 | [diff] [blame] | 225 | void IsTrustRegionSolveSuccessful(TrustRegionStrategyType strategy_type) { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 226 | Solver::Options solver_options; |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 227 | LinearSolver::Options linear_solver_options; |
| 228 | DenseQRSolver linear_solver(linear_solver_options); |
| 229 | |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 230 | double parameters[4] = {3, -1, 0, 1.0}; |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 231 | |
| 232 | // If the column is inactive, then set its value to the optimal |
| 233 | // value. |
| 234 | parameters[0] = (col1 ? parameters[0] : 0.0); |
| 235 | parameters[1] = (col2 ? parameters[1] : 0.0); |
| 236 | parameters[2] = (col3 ? parameters[2] : 0.0); |
| 237 | parameters[3] = (col4 ? parameters[3] : 0.0); |
| 238 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 239 | Minimizer::Options minimizer_options(solver_options); |
| 240 | minimizer_options.gradient_tolerance = 1e-26; |
| 241 | minimizer_options.function_tolerance = 1e-26; |
| 242 | minimizer_options.parameter_tolerance = 1e-26; |
Sameer Agarwal | bd90384 | 2014-08-18 11:27:06 -0700 | [diff] [blame] | 243 | minimizer_options.evaluator.reset( |
| 244 | new PowellEvaluator2<col1, col2, col3, col4>); |
| 245 | minimizer_options.jacobian.reset( |
| 246 | minimizer_options.evaluator->CreateJacobian()); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 247 | |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 248 | TrustRegionStrategy::Options trust_region_strategy_options; |
Sameer Agarwal | 4441b5b | 2012-06-12 18:01:11 -0700 | [diff] [blame] | 249 | trust_region_strategy_options.trust_region_strategy_type = strategy_type; |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 250 | trust_region_strategy_options.linear_solver = &linear_solver; |
| 251 | trust_region_strategy_options.initial_radius = 1e4; |
| 252 | trust_region_strategy_options.max_radius = 1e20; |
Sameer Agarwal | eeedd2e | 2013-07-07 23:04:31 -0700 | [diff] [blame] | 253 | trust_region_strategy_options.min_lm_diagonal = 1e-6; |
| 254 | trust_region_strategy_options.max_lm_diagonal = 1e32; |
Sameer Agarwal | 4a2a888 | 2014-08-07 11:48:03 -0700 | [diff] [blame] | 255 | minimizer_options.trust_region_strategy.reset( |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 256 | TrustRegionStrategy::Create(trust_region_strategy_options)); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 257 | |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 258 | TrustRegionMinimizer minimizer; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 259 | Solver::Summary summary; |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 260 | minimizer.Minimize(minimizer_options, parameters, &summary); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 261 | |
| 262 | // The minimum is at x1 = x2 = x3 = x4 = 0. |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 263 | EXPECT_NEAR(0.0, parameters[0], 0.001); |
| 264 | EXPECT_NEAR(0.0, parameters[1], 0.001); |
| 265 | EXPECT_NEAR(0.0, parameters[2], 0.001); |
| 266 | EXPECT_NEAR(0.0, parameters[3], 0.001); |
Sameer Agarwal | bcc865f | 2014-12-17 07:35:09 -0800 | [diff] [blame] | 267 | } |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 268 | |
Sameer Agarwal | 4441b5b | 2012-06-12 18:01:11 -0700 | [diff] [blame] | 269 | TEST(TrustRegionMinimizer, PowellsSingularFunctionUsingLevenbergMarquardt) { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 270 | // This case is excluded because this has a local minimum and does |
| 271 | // not find the optimum. This should not affect the correctness of |
| 272 | // this test since we are testing all the other 14 combinations of |
| 273 | // column activations. |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 274 | // |
| 275 | // IsSolveSuccessful<true, true, false, true>(); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 276 | |
Sameer Agarwal | 4441b5b | 2012-06-12 18:01:11 -0700 | [diff] [blame] | 277 | const TrustRegionStrategyType kStrategy = LEVENBERG_MARQUARDT; |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 278 | // clang-format off |
Sameer Agarwal | 4441b5b | 2012-06-12 18:01:11 -0700 | [diff] [blame] | 279 | IsTrustRegionSolveSuccessful<true, true, true, true >(kStrategy); |
| 280 | IsTrustRegionSolveSuccessful<true, true, true, false>(kStrategy); |
| 281 | IsTrustRegionSolveSuccessful<true, false, true, true >(kStrategy); |
| 282 | IsTrustRegionSolveSuccessful<false, true, true, true >(kStrategy); |
| 283 | IsTrustRegionSolveSuccessful<true, true, false, false>(kStrategy); |
| 284 | IsTrustRegionSolveSuccessful<true, false, true, false>(kStrategy); |
| 285 | IsTrustRegionSolveSuccessful<false, true, true, false>(kStrategy); |
| 286 | IsTrustRegionSolveSuccessful<true, false, false, true >(kStrategy); |
| 287 | IsTrustRegionSolveSuccessful<false, true, false, true >(kStrategy); |
| 288 | IsTrustRegionSolveSuccessful<false, false, true, true >(kStrategy); |
| 289 | IsTrustRegionSolveSuccessful<true, false, false, false>(kStrategy); |
| 290 | IsTrustRegionSolveSuccessful<false, true, false, false>(kStrategy); |
| 291 | IsTrustRegionSolveSuccessful<false, false, true, false>(kStrategy); |
| 292 | IsTrustRegionSolveSuccessful<false, false, false, true >(kStrategy); |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 293 | // clang-format on |
Sameer Agarwal | 4441b5b | 2012-06-12 18:01:11 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | TEST(TrustRegionMinimizer, PowellsSingularFunctionUsingDogleg) { |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 297 | // The following two cases are excluded because they encounter a |
| 298 | // local minimum. |
Sameer Agarwal | 4441b5b | 2012-06-12 18:01:11 -0700 | [diff] [blame] | 299 | // |
| 300 | // IsTrustRegionSolveSuccessful<true, true, false, true >(kStrategy); |
| 301 | // IsTrustRegionSolveSuccessful<true, true, true, true >(kStrategy); |
| 302 | |
| 303 | const TrustRegionStrategyType kStrategy = DOGLEG; |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 304 | // clang-format off |
Sameer Agarwal | 4441b5b | 2012-06-12 18:01:11 -0700 | [diff] [blame] | 305 | IsTrustRegionSolveSuccessful<true, true, true, false>(kStrategy); |
| 306 | IsTrustRegionSolveSuccessful<true, false, true, true >(kStrategy); |
| 307 | IsTrustRegionSolveSuccessful<false, true, true, true >(kStrategy); |
| 308 | IsTrustRegionSolveSuccessful<true, true, false, false>(kStrategy); |
| 309 | IsTrustRegionSolveSuccessful<true, false, true, false>(kStrategy); |
| 310 | IsTrustRegionSolveSuccessful<false, true, true, false>(kStrategy); |
| 311 | IsTrustRegionSolveSuccessful<true, false, false, true >(kStrategy); |
| 312 | IsTrustRegionSolveSuccessful<false, true, false, true >(kStrategy); |
| 313 | IsTrustRegionSolveSuccessful<false, false, true, true >(kStrategy); |
| 314 | IsTrustRegionSolveSuccessful<true, false, false, false>(kStrategy); |
| 315 | IsTrustRegionSolveSuccessful<false, true, false, false>(kStrategy); |
| 316 | IsTrustRegionSolveSuccessful<false, false, true, false>(kStrategy); |
| 317 | IsTrustRegionSolveSuccessful<false, false, false, true >(kStrategy); |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 318 | // clang-format on |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Sameer Agarwal | a406b17 | 2012-08-18 15:28:49 -0700 | [diff] [blame] | 321 | class CurveCostFunction : public CostFunction { |
| 322 | public: |
| 323 | CurveCostFunction(int num_vertices, double target_length) |
| 324 | : num_vertices_(num_vertices), target_length_(target_length) { |
| 325 | set_num_residuals(1); |
| 326 | for (int i = 0; i < num_vertices_; ++i) { |
| 327 | mutable_parameter_block_sizes()->push_back(2); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | bool Evaluate(double const* const* parameters, |
| 332 | double* residuals, |
Sergiu Deitsch | 484d341 | 2022-02-09 00:34:05 +0100 | [diff] [blame^] | 333 | double** jacobians) const override { |
Sameer Agarwal | a406b17 | 2012-08-18 15:28:49 -0700 | [diff] [blame] | 334 | residuals[0] = target_length_; |
| 335 | |
| 336 | for (int i = 0; i < num_vertices_; ++i) { |
| 337 | int prev = (num_vertices_ + i - 1) % num_vertices_; |
| 338 | double length = 0.0; |
| 339 | for (int dim = 0; dim < 2; dim++) { |
| 340 | const double diff = parameters[prev][dim] - parameters[i][dim]; |
| 341 | length += diff * diff; |
| 342 | } |
| 343 | residuals[0] -= sqrt(length); |
| 344 | } |
| 345 | |
| 346 | if (jacobians == NULL) { |
| 347 | return true; |
| 348 | } |
| 349 | |
| 350 | for (int i = 0; i < num_vertices_; ++i) { |
| 351 | if (jacobians[i] != NULL) { |
| 352 | int prev = (num_vertices_ + i - 1) % num_vertices_; |
| 353 | int next = (i + 1) % num_vertices_; |
| 354 | |
| 355 | double u[2], v[2]; |
| 356 | double norm_u = 0., norm_v = 0.; |
| 357 | for (int dim = 0; dim < 2; dim++) { |
| 358 | u[dim] = parameters[i][dim] - parameters[prev][dim]; |
| 359 | norm_u += u[dim] * u[dim]; |
| 360 | v[dim] = parameters[next][dim] - parameters[i][dim]; |
| 361 | norm_v += v[dim] * v[dim]; |
| 362 | } |
| 363 | |
| 364 | norm_u = sqrt(norm_u); |
| 365 | norm_v = sqrt(norm_v); |
| 366 | |
| 367 | for (int dim = 0; dim < 2; dim++) { |
| 368 | jacobians[i][dim] = 0.; |
| 369 | |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 370 | if (norm_u > std::numeric_limits<double>::min()) { |
Sameer Agarwal | a406b17 | 2012-08-18 15:28:49 -0700 | [diff] [blame] | 371 | jacobians[i][dim] -= u[dim] / norm_u; |
| 372 | } |
| 373 | |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 374 | if (norm_v > std::numeric_limits<double>::min()) { |
Sameer Agarwal | a406b17 | 2012-08-18 15:28:49 -0700 | [diff] [blame] | 375 | jacobians[i][dim] += v[dim] / norm_v; |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | return true; |
| 382 | } |
| 383 | |
| 384 | private: |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 385 | int num_vertices_; |
| 386 | double target_length_; |
Sameer Agarwal | a406b17 | 2012-08-18 15:28:49 -0700 | [diff] [blame] | 387 | }; |
| 388 | |
| 389 | TEST(TrustRegionMinimizer, JacobiScalingTest) { |
| 390 | int N = 6; |
Sameer Agarwal | bcc865f | 2014-12-17 07:35:09 -0800 | [diff] [blame] | 391 | std::vector<double*> y(N); |
Sameer Agarwal | a406b17 | 2012-08-18 15:28:49 -0700 | [diff] [blame] | 392 | const double pi = 3.1415926535897932384626433; |
| 393 | for (int i = 0; i < N; i++) { |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 394 | double theta = i * 2. * pi / static_cast<double>(N); |
Sameer Agarwal | a406b17 | 2012-08-18 15:28:49 -0700 | [diff] [blame] | 395 | y[i] = new double[2]; |
| 396 | y[i][0] = cos(theta); |
| 397 | y[i][1] = sin(theta); |
| 398 | } |
| 399 | |
| 400 | Problem problem; |
| 401 | problem.AddResidualBlock(new CurveCostFunction(N, 10.), NULL, y); |
| 402 | Solver::Options options; |
| 403 | options.linear_solver_type = ceres::DENSE_QR; |
| 404 | Solver::Summary summary; |
| 405 | Solve(options, &problem, &summary); |
| 406 | EXPECT_LE(summary.final_cost, 1e-10); |
Sameer Agarwal | 66fcc7d | 2012-10-08 09:54:16 -0700 | [diff] [blame] | 407 | |
| 408 | for (int i = 0; i < N; i++) { |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 409 | delete[] y[i]; |
Sameer Agarwal | 66fcc7d | 2012-10-08 09:54:16 -0700 | [diff] [blame] | 410 | } |
Sameer Agarwal | a406b17 | 2012-08-18 15:28:49 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Sameer Agarwal | 5648c63 | 2014-11-04 22:02:09 -0800 | [diff] [blame] | 413 | struct ExpCostFunctor { |
| 414 | template <typename T> |
| 415 | bool operator()(const T* const x, T* residual) const { |
| 416 | residual[0] = T(10.0) - exp(x[0]); |
| 417 | return true; |
| 418 | } |
| 419 | |
| 420 | static CostFunction* Create() { |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 421 | return new AutoDiffCostFunction<ExpCostFunctor, 1, 1>(new ExpCostFunctor); |
Sameer Agarwal | 5648c63 | 2014-11-04 22:02:09 -0800 | [diff] [blame] | 422 | } |
| 423 | }; |
| 424 | |
| 425 | TEST(TrustRegionMinimizer, GradientToleranceConvergenceUpdatesStep) { |
| 426 | double x = 5; |
| 427 | Problem problem; |
| 428 | problem.AddResidualBlock(ExpCostFunctor::Create(), NULL, &x); |
| 429 | problem.SetParameterLowerBound(&x, 0, 3.0); |
| 430 | Solver::Options options; |
| 431 | Solver::Summary summary; |
| 432 | Solve(options, &problem, &summary); |
| 433 | EXPECT_NEAR(3.0, x, 1e-12); |
| 434 | const double expected_final_cost = 0.5 * pow(10.0 - exp(3.0), 2); |
| 435 | EXPECT_NEAR(expected_final_cost, summary.final_cost, 1e-12); |
| 436 | } |
| 437 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 438 | } // namespace internal |
| 439 | } // namespace ceres |