Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
| 2 | // Copyright 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: sameeragarwal@google.com (Sameer Agarwal) |
| 30 | |
Sameer Agarwal | 05292bf | 2012-08-20 07:40:45 -0700 | [diff] [blame] | 31 | #include "ceres/internal/eigen.h" |
| 32 | #include "ceres/internal/scoped_ptr.h" |
| 33 | #include "ceres/levenberg_marquardt_strategy.h" |
| 34 | #include "ceres/linear_solver.h" |
| 35 | #include "ceres/trust_region_strategy.h" |
Sameer Agarwal | 0beab86 | 2012-08-13 15:12:01 -0700 | [diff] [blame] | 36 | #include "glog/logging.h" |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 37 | #include "gmock/gmock.h" |
Sameer Agarwal | 62f50d1 | 2012-08-14 14:26:13 -0700 | [diff] [blame] | 38 | #include "gmock/mock-log.h" |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 39 | #include "gtest/gtest.h" |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 40 | |
| 41 | using testing::AllOf; |
| 42 | using testing::AnyNumber; |
| 43 | using testing::HasSubstr; |
| 44 | using testing::ScopedMockLog; |
| 45 | using testing::_; |
| 46 | |
| 47 | namespace ceres { |
| 48 | namespace internal { |
| 49 | |
| 50 | const double kTolerance = 1e-16; |
| 51 | |
| 52 | // Linear solver that takes as input a vector and checks that the |
| 53 | // caller passes the same vector as LinearSolver::PerSolveOptions.D. |
| 54 | class RegularizationCheckingLinearSolver : public DenseSparseMatrixSolver { |
| 55 | public: |
| 56 | RegularizationCheckingLinearSolver(const int num_cols, const double* diagonal) |
| 57 | : num_cols_(num_cols), |
| 58 | diagonal_(diagonal) { |
| 59 | } |
| 60 | |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 61 | virtual ~RegularizationCheckingLinearSolver() {} |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 62 | |
| 63 | private: |
| 64 | virtual LinearSolver::Summary SolveImpl( |
| 65 | DenseSparseMatrix* A, |
| 66 | const double* b, |
| 67 | const LinearSolver::PerSolveOptions& per_solve_options, |
| 68 | double* x) { |
| 69 | CHECK_NOTNULL(per_solve_options.D); |
| 70 | for (int i = 0; i < num_cols_; ++i) { |
| 71 | EXPECT_NEAR(per_solve_options.D[i], diagonal_[i], kTolerance) |
| 72 | << i << " " << per_solve_options.D[i] << " " << diagonal_[i]; |
| 73 | } |
Sameer Agarwal | 4a6cc1c | 2012-06-18 10:20:08 -0700 | [diff] [blame] | 74 | return LinearSolver::Summary(); |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 75 | } |
Sameer Agarwal | 4a6cc1c | 2012-06-18 10:20:08 -0700 | [diff] [blame] | 76 | |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 77 | const int num_cols_; |
| 78 | const double* diagonal_; |
| 79 | }; |
| 80 | |
| 81 | TEST(LevenbergMarquardtStrategy, AcceptRejectStepRadiusScaling) { |
| 82 | TrustRegionStrategy::Options options; |
| 83 | options.initial_radius = 2.0; |
| 84 | options.max_radius = 20.0; |
Sameer Agarwal | eeedd2e | 2013-07-07 23:04:31 -0700 | [diff] [blame] | 85 | options.min_lm_diagonal = 1e-8; |
| 86 | options.max_lm_diagonal = 1e8; |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 87 | |
| 88 | // We need a non-null pointer here, so anything should do. |
Keir Mierle | 5c44678 | 2012-08-16 11:39:41 -0700 | [diff] [blame] | 89 | scoped_ptr<LinearSolver> linear_solver( |
| 90 | new RegularizationCheckingLinearSolver(0, NULL)); |
| 91 | options.linear_solver = linear_solver.get(); |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 92 | |
| 93 | LevenbergMarquardtStrategy lms(options); |
| 94 | EXPECT_EQ(lms.Radius(), options.initial_radius); |
| 95 | lms.StepRejected(0.0); |
| 96 | EXPECT_EQ(lms.Radius(), 1.0); |
| 97 | lms.StepRejected(-1.0); |
| 98 | EXPECT_EQ(lms.Radius(), 0.25); |
| 99 | lms.StepAccepted(1.0); |
| 100 | EXPECT_EQ(lms.Radius(), 0.25 * 3.0); |
| 101 | lms.StepAccepted(1.0); |
| 102 | EXPECT_EQ(lms.Radius(), 0.25 * 3.0 * 3.0); |
| 103 | lms.StepAccepted(0.25); |
| 104 | EXPECT_EQ(lms.Radius(), 0.25 * 3.0 * 3.0 / 1.125); |
| 105 | lms.StepAccepted(1.0); |
| 106 | EXPECT_EQ(lms.Radius(), 0.25 * 3.0 * 3.0 / 1.125 * 3.0); |
| 107 | lms.StepAccepted(1.0); |
| 108 | EXPECT_EQ(lms.Radius(), 0.25 * 3.0 * 3.0 / 1.125 * 3.0 * 3.0); |
| 109 | lms.StepAccepted(1.0); |
| 110 | EXPECT_EQ(lms.Radius(), options.max_radius); |
| 111 | } |
| 112 | |
| 113 | TEST(LevenbergMarquardtStrategy, CorrectDiagonalToLinearSolver) { |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 114 | Matrix jacobian(2, 3); |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 115 | jacobian.setZero(); |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 116 | jacobian(0, 0) = 0.0; |
| 117 | jacobian(0, 1) = 1.0; |
| 118 | jacobian(1, 1) = 1.0; |
| 119 | jacobian(0, 2) = 100.0; |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 120 | |
| 121 | double residual = 1.0; |
Sameer Agarwal | fba3844 | 2012-09-07 17:04:53 -0700 | [diff] [blame] | 122 | double x[3]; |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 123 | DenseSparseMatrix dsm(jacobian); |
| 124 | |
| 125 | TrustRegionStrategy::Options options; |
| 126 | options.initial_radius = 2.0; |
| 127 | options.max_radius = 20.0; |
Sameer Agarwal | eeedd2e | 2013-07-07 23:04:31 -0700 | [diff] [blame] | 128 | options.min_lm_diagonal = 1e-2; |
| 129 | options.max_lm_diagonal = 1e2; |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 130 | |
| 131 | double diagonal[3]; |
Sameer Agarwal | eeedd2e | 2013-07-07 23:04:31 -0700 | [diff] [blame] | 132 | diagonal[0] = options.min_lm_diagonal; |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 133 | diagonal[1] = 2.0; |
Sameer Agarwal | eeedd2e | 2013-07-07 23:04:31 -0700 | [diff] [blame] | 134 | diagonal[2] = options.max_lm_diagonal; |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 135 | for (int i = 0; i < 3; ++i) { |
| 136 | diagonal[i] = sqrt(diagonal[i] / options.initial_radius); |
| 137 | } |
| 138 | |
| 139 | RegularizationCheckingLinearSolver linear_solver(3, diagonal); |
| 140 | options.linear_solver = &linear_solver; |
| 141 | |
| 142 | LevenbergMarquardtStrategy lms(options); |
| 143 | TrustRegionStrategy::PerSolveOptions pso; |
| 144 | |
| 145 | { |
| 146 | ScopedMockLog log; |
| 147 | EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber()); |
| 148 | EXPECT_CALL(log, Log(WARNING, _, |
| 149 | HasSubstr("Failed to compute a finite step."))); |
| 150 | |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 151 | TrustRegionStrategy::Summary summary = |
| 152 | lms.ComputeStep(pso, &dsm, &residual, x); |
Sameer Agarwal | 33e01b9 | 2013-11-27 10:24:03 -0800 | [diff] [blame] | 153 | EXPECT_EQ(summary.termination_type, LINEAR_SOLVER_FAILURE); |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | } // namespace internal |
| 158 | } // namespace ceres |