1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2015 Google Inc. All rights reserved.
3 // http://ceres-solver.org/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
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.
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.
29 // Author: keir@google.com (Keir Mierle)
30 // sameeragarwal@google.com (Sameer Agarwal)
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.
37 #include "ceres/autodiff_cost_function.h"
38 #include "ceres/cost_function.h"
39 #include "ceres/dense_qr_solver.h"
40 #include "ceres/dense_sparse_matrix.h"
41 #include "ceres/evaluator.h"
42 #include "ceres/internal/port.h"
43 #include "ceres/linear_solver.h"
44 #include "ceres/minimizer.h"
45 #include "ceres/problem.h"
46 #include "ceres/trust_region_minimizer.h"
47 #include "ceres/trust_region_strategy.h"
48 #include "gtest/gtest.h"
53 // Templated Evaluator for Powell's function. The template parameters
54 // indicate which of the four variables/columns of the jacobian are
55 // active. This is equivalent to constructing a problem and using the
56 // SubsetLocalParameterization. This allows us to test the support for
57 // the Evaluator::Plus operation besides checking for the basic
58 // performance of the trust region algorithm.
59 template <bool col1, bool col2, bool col3, bool col4>
60 class PowellEvaluator2 : public Evaluator {
68 VLOG(1) << "Columns: "
75 virtual ~PowellEvaluator2() {}
77 // Implementation of Evaluator interface.
78 virtual SparseMatrix* CreateJacobian() const {
79 CHECK(col1 || col2 || col3 || col4);
80 DenseSparseMatrix* dense_jacobian =
81 new DenseSparseMatrix(NumResiduals(), NumEffectiveParameters());
82 dense_jacobian->SetZero();
83 return dense_jacobian;
86 virtual bool Evaluate(const Evaluator::EvaluateOptions& evaluate_options,
91 SparseMatrix* jacobian) {
92 const double x1 = state[0];
93 const double x2 = state[1];
94 const double x3 = state[2];
95 const double x4 = state[3];
98 << "x1=" << x1 << ", "
99 << "x2=" << x2 << ", "
100 << "x3=" << x3 << ", "
101 << "x4=" << x4 << ".";
103 const double f1 = x1 + 10.0 * x2;
104 const double f2 = sqrt(5.0) * (x3 - x4);
105 const double f3 = pow(x2 - 2.0 * x3, 2.0);
106 const double f4 = sqrt(10.0) * pow(x1 - x4, 2.0);
108 VLOG(1) << "Function: "
109 << "f1=" << f1 << ", "
110 << "f2=" << f2 << ", "
111 << "f3=" << f3 << ", "
112 << "f4=" << f4 << ".";
114 *cost = (f1*f1 + f2*f2 + f3*f3 + f4*f4) / 2.0;
116 VLOG(1) << "Cost: " << *cost;
118 if (residuals != NULL) {
125 if (jacobian != NULL) {
126 DenseSparseMatrix* dense_jacobian;
127 dense_jacobian = down_cast<DenseSparseMatrix*>(jacobian);
128 dense_jacobian->SetZero();
130 ColMajorMatrixRef jacobian_matrix = dense_jacobian->mutable_matrix();
131 CHECK_EQ(jacobian_matrix.cols(), num_active_cols_);
133 int column_index = 0;
135 jacobian_matrix.col(column_index++) <<
139 sqrt(10.0) * 2.0 * (x1 - x4) * (1.0 - x4);
142 jacobian_matrix.col(column_index++) <<
145 2.0*(x2 - 2.0*x3)*(1.0 - 2.0*x3),
150 jacobian_matrix.col(column_index++) <<
153 2.0*(x2 - 2.0*x3)*(x2 - 2.0),
158 jacobian_matrix.col(column_index++) <<
162 sqrt(10.0) * 2.0 * (x1 - x4) * (x1 - 1.0);
164 VLOG(1) << "\n" << jacobian_matrix;
167 if (gradient != NULL) {
168 int column_index = 0;
170 gradient[column_index++] = f1 + f4 * sqrt(10.0) * 2.0 * (x1 - x4);
174 gradient[column_index++] = f1 * 10.0 + f3 * 2.0 * (x2 - 2.0 * x3);
178 gradient[column_index++] =
179 f2 * sqrt(5.0) + f3 * (2.0 * 2.0 * (2.0 * x3 - x2));
183 gradient[column_index++] =
184 -f2 * sqrt(5.0) + f4 * sqrt(10.0) * 2.0 * (x4 - x1);
191 virtual bool Plus(const double* state,
193 double* state_plus_delta) const {
195 state_plus_delta[0] = (col1 ? state[0] + delta[delta_index++] : state[0]);
196 state_plus_delta[1] = (col2 ? state[1] + delta[delta_index++] : state[1]);
197 state_plus_delta[2] = (col3 ? state[2] + delta[delta_index++] : state[2]);
198 state_plus_delta[3] = (col4 ? state[3] + delta[delta_index++] : state[3]);
202 virtual int NumEffectiveParameters() const { return num_active_cols_; }
203 virtual int NumParameters() const { return 4; }
204 virtual int NumResiduals() const { return 4; }
207 const int num_active_cols_;
210 // Templated function to hold a subset of the columns fixed and check
211 // if the solver converges to the optimal values or not.
212 template<bool col1, bool col2, bool col3, bool col4>
213 void IsTrustRegionSolveSuccessful(TrustRegionStrategyType strategy_type) {
214 Solver::Options solver_options;
215 LinearSolver::Options linear_solver_options;
216 DenseQRSolver linear_solver(linear_solver_options);
218 double parameters[4] = { 3, -1, 0, 1.0 };
220 // If the column is inactive, then set its value to the optimal
222 parameters[0] = (col1 ? parameters[0] : 0.0);
223 parameters[1] = (col2 ? parameters[1] : 0.0);
224 parameters[2] = (col3 ? parameters[2] : 0.0);
225 parameters[3] = (col4 ? parameters[3] : 0.0);
227 Minimizer::Options minimizer_options(solver_options);
228 minimizer_options.gradient_tolerance = 1e-26;
229 minimizer_options.function_tolerance = 1e-26;
230 minimizer_options.parameter_tolerance = 1e-26;
231 minimizer_options.evaluator.reset(
232 new PowellEvaluator2<col1, col2, col3, col4>);
233 minimizer_options.jacobian.reset(
234 minimizer_options.evaluator->CreateJacobian());
236 TrustRegionStrategy::Options trust_region_strategy_options;
237 trust_region_strategy_options.trust_region_strategy_type = strategy_type;
238 trust_region_strategy_options.linear_solver = &linear_solver;
239 trust_region_strategy_options.initial_radius = 1e4;
240 trust_region_strategy_options.max_radius = 1e20;
241 trust_region_strategy_options.min_lm_diagonal = 1e-6;
242 trust_region_strategy_options.max_lm_diagonal = 1e32;
243 minimizer_options.trust_region_strategy.reset(
244 TrustRegionStrategy::Create(trust_region_strategy_options));
246 TrustRegionMinimizer minimizer;
247 Solver::Summary summary;
248 minimizer.Minimize(minimizer_options, parameters, &summary);
250 // The minimum is at x1 = x2 = x3 = x4 = 0.
251 EXPECT_NEAR(0.0, parameters[0], 0.001);
252 EXPECT_NEAR(0.0, parameters[1], 0.001);
253 EXPECT_NEAR(0.0, parameters[2], 0.001);
254 EXPECT_NEAR(0.0, parameters[3], 0.001);
257 TEST(TrustRegionMinimizer, PowellsSingularFunctionUsingLevenbergMarquardt) {
258 // This case is excluded because this has a local minimum and does
259 // not find the optimum. This should not affect the correctness of
260 // this test since we are testing all the other 14 combinations of
261 // column activations.
263 // IsSolveSuccessful<true, true, false, true>();
265 const TrustRegionStrategyType kStrategy = LEVENBERG_MARQUARDT;
266 IsTrustRegionSolveSuccessful<true, true, true, true >(kStrategy);
267 IsTrustRegionSolveSuccessful<true, true, true, false>(kStrategy);
268 IsTrustRegionSolveSuccessful<true, false, true, true >(kStrategy);
269 IsTrustRegionSolveSuccessful<false, true, true, true >(kStrategy);
270 IsTrustRegionSolveSuccessful<true, true, false, false>(kStrategy);
271 IsTrustRegionSolveSuccessful<true, false, true, false>(kStrategy);
272 IsTrustRegionSolveSuccessful<false, true, true, false>(kStrategy);
273 IsTrustRegionSolveSuccessful<true, false, false, true >(kStrategy);
274 IsTrustRegionSolveSuccessful<false, true, false, true >(kStrategy);
275 IsTrustRegionSolveSuccessful<false, false, true, true >(kStrategy);
276 IsTrustRegionSolveSuccessful<true, false, false, false>(kStrategy);
277 IsTrustRegionSolveSuccessful<false, true, false, false>(kStrategy);
278 IsTrustRegionSolveSuccessful<false, false, true, false>(kStrategy);
279 IsTrustRegionSolveSuccessful<false, false, false, true >(kStrategy);
282 TEST(TrustRegionMinimizer, PowellsSingularFunctionUsingDogleg) {
283 // The following two cases are excluded because they encounter a
286 // IsTrustRegionSolveSuccessful<true, true, false, true >(kStrategy);
287 // IsTrustRegionSolveSuccessful<true, true, true, true >(kStrategy);
289 const TrustRegionStrategyType kStrategy = DOGLEG;
290 IsTrustRegionSolveSuccessful<true, true, true, false>(kStrategy);
291 IsTrustRegionSolveSuccessful<true, false, true, true >(kStrategy);
292 IsTrustRegionSolveSuccessful<false, true, true, true >(kStrategy);
293 IsTrustRegionSolveSuccessful<true, true, false, false>(kStrategy);
294 IsTrustRegionSolveSuccessful<true, false, true, false>(kStrategy);
295 IsTrustRegionSolveSuccessful<false, true, true, false>(kStrategy);
296 IsTrustRegionSolveSuccessful<true, false, false, true >(kStrategy);
297 IsTrustRegionSolveSuccessful<false, true, false, true >(kStrategy);
298 IsTrustRegionSolveSuccessful<false, false, true, true >(kStrategy);
299 IsTrustRegionSolveSuccessful<true, false, false, false>(kStrategy);
300 IsTrustRegionSolveSuccessful<false, true, false, false>(kStrategy);
301 IsTrustRegionSolveSuccessful<false, false, true, false>(kStrategy);
302 IsTrustRegionSolveSuccessful<false, false, false, true >(kStrategy);
306 class CurveCostFunction : public CostFunction {
308 CurveCostFunction(int num_vertices, double target_length)
309 : num_vertices_(num_vertices), target_length_(target_length) {
310 set_num_residuals(1);
311 for (int i = 0; i < num_vertices_; ++i) {
312 mutable_parameter_block_sizes()->push_back(2);
316 bool Evaluate(double const* const* parameters,
318 double** jacobians) const {
319 residuals[0] = target_length_;
321 for (int i = 0; i < num_vertices_; ++i) {
322 int prev = (num_vertices_ + i - 1) % num_vertices_;
324 for (int dim = 0; dim < 2; dim++) {
325 const double diff = parameters[prev][dim] - parameters[i][dim];
326 length += diff * diff;
328 residuals[0] -= sqrt(length);
331 if (jacobians == NULL) {
335 for (int i = 0; i < num_vertices_; ++i) {
336 if (jacobians[i] != NULL) {
337 int prev = (num_vertices_ + i - 1) % num_vertices_;
338 int next = (i + 1) % num_vertices_;
341 double norm_u = 0., norm_v = 0.;
342 for (int dim = 0; dim < 2; dim++) {
343 u[dim] = parameters[i][dim] - parameters[prev][dim];
344 norm_u += u[dim] * u[dim];
345 v[dim] = parameters[next][dim] - parameters[i][dim];
346 norm_v += v[dim] * v[dim];
349 norm_u = sqrt(norm_u);
350 norm_v = sqrt(norm_v);
352 for (int dim = 0; dim < 2; dim++) {
353 jacobians[i][dim] = 0.;
355 if (norm_u > std::numeric_limits< double >::min()) {
356 jacobians[i][dim] -= u[dim] / norm_u;
359 if (norm_v > std::numeric_limits< double >::min()) {
360 jacobians[i][dim] += v[dim] / norm_v;
371 double target_length_;
374 TEST(TrustRegionMinimizer, JacobiScalingTest) {
376 std::vector<double*> y(N);
377 const double pi = 3.1415926535897932384626433;
378 for (int i = 0; i < N; i++) {
379 double theta = i * 2. * pi/ static_cast< double >(N);
380 y[i] = new double[2];
381 y[i][0] = cos(theta);
382 y[i][1] = sin(theta);
386 problem.AddResidualBlock(new CurveCostFunction(N, 10.), NULL, y);
387 Solver::Options options;
388 options.linear_solver_type = ceres::DENSE_QR;
389 Solver::Summary summary;
390 Solve(options, &problem, &summary);
391 EXPECT_LE(summary.final_cost, 1e-10);
393 for (int i = 0; i < N; i++) {
398 struct ExpCostFunctor {
399 template <typename T>
400 bool operator()(const T* const x, T* residual) const {
401 residual[0] = T(10.0) - exp(x[0]);
405 static CostFunction* Create() {
406 return new AutoDiffCostFunction<ExpCostFunctor, 1, 1>(
411 TEST(TrustRegionMinimizer, GradientToleranceConvergenceUpdatesStep) {
414 problem.AddResidualBlock(ExpCostFunctor::Create(), NULL, &x);
415 problem.SetParameterLowerBound(&x, 0, 3.0);
416 Solver::Options options;
417 Solver::Summary summary;
418 Solve(options, &problem, &summary);
419 EXPECT_NEAR(3.0, x, 1e-12);
420 const double expected_final_cost = 0.5 * pow(10.0 - exp(3.0), 2);
421 EXPECT_NEAR(expected_final_cost, summary.final_cost, 1e-12);
424 } // namespace internal