blob: d87c963eeb785b89952d4a9f25862fef240f8473 [file] [log] [blame]
Keir Mierle8ebb0732012-04-30 23:09:08 -07001// Ceres Solver - A fast non-linear least squares minimizer
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -07002// Copyright 2012 Google Inc. All rights reserved.
Keir Mierle8ebb0732012-04-30 23:09:08 -07003// 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)
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070030// sameeragarwal@google.com (Sameer Agarwal)
Keir Mierle8ebb0732012-04-30 23:09:08 -070031//
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070032// 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 Mierle8ebb0732012-04-30 23:09:08 -070035
36#include <cmath>
37#include "ceres/dense_qr_solver.h"
38#include "ceres/dense_sparse_matrix.h"
39#include "ceres/evaluator.h"
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070040#include "ceres/internal/port.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070041#include "ceres/linear_solver.h"
42#include "ceres/minimizer.h"
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070043#include "ceres/trust_region_minimizer.h"
Sameer Agarwal4441b5b2012-06-12 18:01:11 -070044#include "ceres/trust_region_strategy.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070045#include "gtest/gtest.h"
46
47namespace ceres {
48namespace internal {
49
50// Templated Evaluator for Powell's function. The template parameters
51// indicate which of the four variables/columns of the jacobian are
52// active. This is equivalent to constructing a problem and using the
53// SubsetLocalParameterization. This allows us to test the support for
54// the Evaluator::Plus operation besides checking for the basic
Sameer Agarwal4441b5b2012-06-12 18:01:11 -070055// performance of the trust region algorithm.
Keir Mierle8ebb0732012-04-30 23:09:08 -070056template <bool col1, bool col2, bool col3, bool col4>
57class PowellEvaluator2 : public Evaluator {
58 public:
59 PowellEvaluator2()
60 : num_active_cols_(
61 (col1 ? 1 : 0) +
62 (col2 ? 1 : 0) +
63 (col3 ? 1 : 0) +
64 (col4 ? 1 : 0)) {
65 VLOG(1) << "Columns: "
66 << col1 << " "
67 << col2 << " "
68 << col3 << " "
69 << col4;
70 }
71
72 virtual ~PowellEvaluator2() {}
73
74 // Implementation of Evaluator interface.
75 virtual SparseMatrix* CreateJacobian() const {
76 CHECK(col1 || col2 || col3 || col4);
77 DenseSparseMatrix* dense_jacobian =
78 new DenseSparseMatrix(NumResiduals(), NumEffectiveParameters());
79 dense_jacobian->SetZero();
80 return dense_jacobian;
81 }
82
83 virtual bool Evaluate(const double* state,
84 double* cost,
85 double* residuals,
86 SparseMatrix* jacobian) {
87 double x1 = state[0];
88 double x2 = state[1];
89 double x3 = state[2];
90 double x4 = state[3];
91
92 VLOG(1) << "State: "
93 << "x1=" << x1 << ", "
94 << "x2=" << x2 << ", "
95 << "x3=" << x3 << ", "
96 << "x4=" << x4 << ".";
97
98 double f1 = x1 + 10.0 * x2;
99 double f2 = sqrt(5.0) * (x3 - x4);
100 double f3 = pow(x2 - 2.0 * x3, 2.0);
101 double f4 = sqrt(10.0) * pow(x1 - x4, 2.0);
102
103 VLOG(1) << "Function: "
104 << "f1=" << f1 << ", "
105 << "f2=" << f2 << ", "
106 << "f3=" << f3 << ", "
107 << "f4=" << f4 << ".";
108
109 *cost = (f1*f1 + f2*f2 + f3*f3 + f4*f4) / 2.0;
110
111 VLOG(1) << "Cost: " << *cost;
112
113 if (residuals != NULL) {
114 residuals[0] = f1;
115 residuals[1] = f2;
116 residuals[2] = f3;
117 residuals[3] = f4;
118 }
119
120 if (jacobian != NULL) {
121 DenseSparseMatrix* dense_jacobian;
122 dense_jacobian = down_cast<DenseSparseMatrix*>(jacobian);
123 dense_jacobian->SetZero();
124
125 AlignedMatrixRef jacobian_matrix = dense_jacobian->mutable_matrix();
126 CHECK_EQ(jacobian_matrix.cols(), num_active_cols_);
127
128 int column_index = 0;
129 if (col1) {
130 jacobian_matrix.col(column_index++) <<
131 1.0,
132 0.0,
133 0.0,
134 sqrt(10) * 2.0 * (x1 - x4) * (1.0 - x4);
135 }
136 if (col2) {
137 jacobian_matrix.col(column_index++) <<
138 10.0,
139 0.0,
140 2.0*(x2 - 2.0*x3)*(1.0 - 2.0*x3),
141 0.0;
142 }
143
144 if (col3) {
145 jacobian_matrix.col(column_index++) <<
146 0.0,
147 sqrt(5.0),
148 2.0*(x2 - 2.0*x3)*(x2 - 2.0),
149 0.0;
150 }
151
152 if (col4) {
153 jacobian_matrix.col(column_index++) <<
154 0.0,
155 -sqrt(5.0),
156 0.0,
157 sqrt(10) * 2.0 * (x1 - x4) * (x1 - 1.0);
158 }
159 VLOG(1) << "\n" << jacobian_matrix;
160 }
161 return true;
162 }
163
164 virtual bool Plus(const double* state,
165 const double* delta,
166 double* state_plus_delta) const {
167 int delta_index = 0;
168 state_plus_delta[0] = (col1 ? state[0] + delta[delta_index++] : state[0]);
169 state_plus_delta[1] = (col2 ? state[1] + delta[delta_index++] : state[1]);
170 state_plus_delta[2] = (col3 ? state[2] + delta[delta_index++] : state[2]);
171 state_plus_delta[3] = (col4 ? state[3] + delta[delta_index++] : state[3]);
172 return true;
173 }
174
175 virtual int NumEffectiveParameters() const { return num_active_cols_; }
176 virtual int NumParameters() const { return 4; }
177 virtual int NumResiduals() const { return 4; }
178
179 private:
180 const int num_active_cols_;
181};
182
183// Templated function to hold a subset of the columns fixed and check
184// if the solver converges to the optimal values or not.
185template<bool col1, bool col2, bool col3, bool col4>
Sameer Agarwal4441b5b2012-06-12 18:01:11 -0700186void IsTrustRegionSolveSuccessful(TrustRegionStrategyType strategy_type) {
Keir Mierle8ebb0732012-04-30 23:09:08 -0700187 Solver::Options solver_options;
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700188 LinearSolver::Options linear_solver_options;
189 DenseQRSolver linear_solver(linear_solver_options);
190
191 double parameters[4] = { 3, -1, 0, 1.0 };
192
193 // If the column is inactive, then set its value to the optimal
194 // value.
195 parameters[0] = (col1 ? parameters[0] : 0.0);
196 parameters[1] = (col2 ? parameters[1] : 0.0);
197 parameters[2] = (col3 ? parameters[2] : 0.0);
198 parameters[3] = (col4 ? parameters[3] : 0.0);
199
200 PowellEvaluator2<col1, col2, col3, col4> powell_evaluator;
201 scoped_ptr<SparseMatrix> jacobian(powell_evaluator.CreateJacobian());
202
Keir Mierle8ebb0732012-04-30 23:09:08 -0700203 Minimizer::Options minimizer_options(solver_options);
204 minimizer_options.gradient_tolerance = 1e-26;
205 minimizer_options.function_tolerance = 1e-26;
206 minimizer_options.parameter_tolerance = 1e-26;
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700207 minimizer_options.evaluator = &powell_evaluator;
208 minimizer_options.jacobian = jacobian.get();
Keir Mierle8ebb0732012-04-30 23:09:08 -0700209
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700210 TrustRegionStrategy::Options trust_region_strategy_options;
Sameer Agarwal4441b5b2012-06-12 18:01:11 -0700211 trust_region_strategy_options.trust_region_strategy_type = strategy_type;
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700212 trust_region_strategy_options.linear_solver = &linear_solver;
213 trust_region_strategy_options.initial_radius = 1e4;
214 trust_region_strategy_options.max_radius = 1e20;
215 trust_region_strategy_options.lm_min_diagonal = 1e-6;
216 trust_region_strategy_options.lm_max_diagonal = 1e32;
217 scoped_ptr<TrustRegionStrategy> strategy(
218 TrustRegionStrategy::Create(trust_region_strategy_options));
219 minimizer_options.trust_region_strategy = strategy.get();
Keir Mierle8ebb0732012-04-30 23:09:08 -0700220
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700221 TrustRegionMinimizer minimizer;
Keir Mierle8ebb0732012-04-30 23:09:08 -0700222 Solver::Summary summary;
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700223 minimizer.Minimize(minimizer_options, parameters, &summary);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700224
225 // The minimum is at x1 = x2 = x3 = x4 = 0.
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700226 EXPECT_NEAR(0.0, parameters[0], 0.001);
227 EXPECT_NEAR(0.0, parameters[1], 0.001);
228 EXPECT_NEAR(0.0, parameters[2], 0.001);
229 EXPECT_NEAR(0.0, parameters[3], 0.001);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700230};
231
Sameer Agarwal4441b5b2012-06-12 18:01:11 -0700232TEST(TrustRegionMinimizer, PowellsSingularFunctionUsingLevenbergMarquardt) {
Keir Mierle8ebb0732012-04-30 23:09:08 -0700233 // This case is excluded because this has a local minimum and does
234 // not find the optimum. This should not affect the correctness of
235 // this test since we are testing all the other 14 combinations of
236 // column activations.
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700237 //
238 // IsSolveSuccessful<true, true, false, true>();
Keir Mierle8ebb0732012-04-30 23:09:08 -0700239
Sameer Agarwal4441b5b2012-06-12 18:01:11 -0700240 const TrustRegionStrategyType kStrategy = LEVENBERG_MARQUARDT;
241 IsTrustRegionSolveSuccessful<true, true, true, true >(kStrategy);
242 IsTrustRegionSolveSuccessful<true, true, true, false>(kStrategy);
243 IsTrustRegionSolveSuccessful<true, false, true, true >(kStrategy);
244 IsTrustRegionSolveSuccessful<false, true, true, true >(kStrategy);
245 IsTrustRegionSolveSuccessful<true, true, false, false>(kStrategy);
246 IsTrustRegionSolveSuccessful<true, false, true, false>(kStrategy);
247 IsTrustRegionSolveSuccessful<false, true, true, false>(kStrategy);
248 IsTrustRegionSolveSuccessful<true, false, false, true >(kStrategy);
249 IsTrustRegionSolveSuccessful<false, true, false, true >(kStrategy);
250 IsTrustRegionSolveSuccessful<false, false, true, true >(kStrategy);
251 IsTrustRegionSolveSuccessful<true, false, false, false>(kStrategy);
252 IsTrustRegionSolveSuccessful<false, true, false, false>(kStrategy);
253 IsTrustRegionSolveSuccessful<false, false, true, false>(kStrategy);
254 IsTrustRegionSolveSuccessful<false, false, false, true >(kStrategy);
255}
256
257TEST(TrustRegionMinimizer, PowellsSingularFunctionUsingDogleg) {
258 // The following two cases are excluded because they encounter a local minimum.
259 //
260 // IsTrustRegionSolveSuccessful<true, true, false, true >(kStrategy);
261 // IsTrustRegionSolveSuccessful<true, true, true, true >(kStrategy);
262
263 const TrustRegionStrategyType kStrategy = DOGLEG;
264 IsTrustRegionSolveSuccessful<true, true, true, false>(kStrategy);
265 IsTrustRegionSolveSuccessful<true, false, true, true >(kStrategy);
266 IsTrustRegionSolveSuccessful<false, true, true, true >(kStrategy);
267 IsTrustRegionSolveSuccessful<true, true, false, false>(kStrategy);
268 IsTrustRegionSolveSuccessful<true, false, true, false>(kStrategy);
269 IsTrustRegionSolveSuccessful<false, true, true, false>(kStrategy);
270 IsTrustRegionSolveSuccessful<true, false, false, true >(kStrategy);
271 IsTrustRegionSolveSuccessful<false, true, false, true >(kStrategy);
272 IsTrustRegionSolveSuccessful<false, false, true, true >(kStrategy);
273 IsTrustRegionSolveSuccessful<true, false, false, false>(kStrategy);
274 IsTrustRegionSolveSuccessful<false, true, false, false>(kStrategy);
275 IsTrustRegionSolveSuccessful<false, false, true, false>(kStrategy);
276 IsTrustRegionSolveSuccessful<false, false, false, true >(kStrategy);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700277}
278
Keir Mierle8ebb0732012-04-30 23:09:08 -0700279} // namespace internal
280} // namespace ceres