blob: 94c716224cfd8981e1095336dffaca56709397f0 [file] [log] [blame]
Keir Mierle8ebb0732012-04-30 23:09:08 -07001// Ceres Solver - A fast non-linear least squares minimizer
Sameer Agarwal5a30cae2023-09-19 15:29:34 -07002// Copyright 2023 Google Inc. All rights reserved.
Keir Mierle7492b0d2015-03-17 22:30:16 -07003// http://ceres-solver.org/
Keir Mierle8ebb0732012-04-30 23:09:08 -07004//
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
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +020036#include "ceres/trust_region_minimizer.h"
37
Keir Mierle8ebb0732012-04-30 23:09:08 -070038#include <cmath>
Sameer Agarwal446487c2022-02-12 08:11:33 -080039#include <memory>
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +020040
Sameer Agarwal5648c632014-11-04 22:02:09 -080041#include "ceres/autodiff_cost_function.h"
Sameer Agarwala406b172012-08-18 15:28:49 -070042#include "ceres/cost_function.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070043#include "ceres/dense_qr_solver.h"
44#include "ceres/dense_sparse_matrix.h"
45#include "ceres/evaluator.h"
Sergiu Deitschf90833f2022-02-07 23:43:19 +010046#include "ceres/internal/export.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070047#include "ceres/linear_solver.h"
48#include "ceres/minimizer.h"
Sameer Agarwala406b172012-08-18 15:28:49 -070049#include "ceres/problem.h"
Sameer Agarwal4441b5b2012-06-12 18:01:11 -070050#include "ceres/trust_region_strategy.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070051#include "gtest/gtest.h"
52
Sameer Agarwalcaf614a2022-04-21 17:41:10 -070053namespace ceres::internal {
Keir Mierle8ebb0732012-04-30 23:09:08 -070054
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 Agarwal125a0e92021-12-28 07:05:03 -080058// SubsetManifold. This allows us to test the support for
Keir Mierle8ebb0732012-04-30 23:09:08 -070059// the Evaluator::Plus operation besides checking for the basic
Sameer Agarwal4441b5b2012-06-12 18:01:11 -070060// performance of the trust region algorithm.
Keir Mierle8ebb0732012-04-30 23:09:08 -070061template <bool col1, bool col2, bool col3, bool col4>
62class PowellEvaluator2 : public Evaluator {
63 public:
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +020064 // clang-format off
Keir Mierle8ebb0732012-04-30 23:09:08 -070065 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 Demmel7b8f6752020-09-20 21:45:24 +020077 // clang-format on
Keir Mierle8ebb0732012-04-30 23:09:08 -070078
Keir Mierle8ebb0732012-04-30 23:09:08 -070079 // Implementation of Evaluator interface.
Sameer Agarwalae652192022-02-02 13:17:29 -080080 std::unique_ptr<SparseMatrix> CreateJacobian() const final {
Keir Mierle8ebb0732012-04-30 23:09:08 -070081 CHECK(col1 || col2 || col3 || col4);
Sameer Agarwalae652192022-02-02 13:17:29 -080082 auto dense_jacobian = std::make_unique<DenseSparseMatrix>(
83 NumResiduals(), NumEffectiveParameters());
Keir Mierle8ebb0732012-04-30 23:09:08 -070084 dense_jacobian->SetZero();
85 return dense_jacobian;
86 }
87
Sameer Agarwal2ffddac2019-07-14 00:16:13 +020088 bool Evaluate(const Evaluator::EvaluateOptions& evaluate_options,
89 const double* state,
90 double* cost,
91 double* residuals,
92 double* gradient,
93 SparseMatrix* jacobian) final {
Sameer Agarwalc3c3dd82013-04-25 07:49:24 -070094 const double x1 = state[0];
95 const double x2 = state[1];
96 const double x3 = state[2];
97 const double x4 = state[3];
Keir Mierle8ebb0732012-04-30 23:09:08 -070098
99 VLOG(1) << "State: "
100 << "x1=" << x1 << ", "
101 << "x2=" << x2 << ", "
102 << "x3=" << x3 << ", "
103 << "x4=" << x4 << ".";
104
Sameer Agarwalc3c3dd82013-04-25 07:49:24 -0700105 const double f1 = x1 + 10.0 * x2;
106 const double f2 = sqrt(5.0) * (x3 - x4);
107 const double f3 = pow(x2 - 2.0 * x3, 2.0);
108 const double f4 = sqrt(10.0) * pow(x1 - x4, 2.0);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700109
110 VLOG(1) << "Function: "
111 << "f1=" << f1 << ", "
112 << "f2=" << f2 << ", "
113 << "f3=" << f3 << ", "
114 << "f4=" << f4 << ".";
115
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +0200116 *cost = (f1 * f1 + f2 * f2 + f3 * f3 + f4 * f4) / 2.0;
Keir Mierle8ebb0732012-04-30 23:09:08 -0700117
118 VLOG(1) << "Cost: " << *cost;
119
Sameer Agarwalae652192022-02-02 13:17:29 -0800120 if (residuals != nullptr) {
Keir Mierle8ebb0732012-04-30 23:09:08 -0700121 residuals[0] = f1;
122 residuals[1] = f2;
123 residuals[2] = f3;
124 residuals[3] = f4;
125 }
126
Sameer Agarwalae652192022-02-02 13:17:29 -0800127 if (jacobian != nullptr) {
Keir Mierle8ebb0732012-04-30 23:09:08 -0700128 DenseSparseMatrix* dense_jacobian;
129 dense_jacobian = down_cast<DenseSparseMatrix*>(jacobian);
130 dense_jacobian->SetZero();
131
Sameer Agarwalcab853f2022-01-26 11:00:48 -0800132 Matrix& jacobian_matrix = *(dense_jacobian->mutable_matrix());
Keir Mierle8ebb0732012-04-30 23:09:08 -0700133 CHECK_EQ(jacobian_matrix.cols(), num_active_cols_);
134
135 int column_index = 0;
136 if (col1) {
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +0200137 // clang-format off
Keir Mierle8ebb0732012-04-30 23:09:08 -0700138 jacobian_matrix.col(column_index++) <<
139 1.0,
140 0.0,
141 0.0,
Sameer Agarwald4eb83e2021-08-15 16:37:47 -0700142 sqrt(10.0) * 2.0 * (x1 - x4);
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +0200143 // clang-format on
Keir Mierle8ebb0732012-04-30 23:09:08 -0700144 }
145 if (col2) {
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +0200146 // clang-format off
Keir Mierle8ebb0732012-04-30 23:09:08 -0700147 jacobian_matrix.col(column_index++) <<
148 10.0,
149 0.0,
Sameer Agarwald4eb83e2021-08-15 16:37:47 -0700150 2.0*(x2 - 2.0*x3),
Keir Mierle8ebb0732012-04-30 23:09:08 -0700151 0.0;
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +0200152 // clang-format on
Keir Mierle8ebb0732012-04-30 23:09:08 -0700153 }
154
155 if (col3) {
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +0200156 // clang-format off
Keir Mierle8ebb0732012-04-30 23:09:08 -0700157 jacobian_matrix.col(column_index++) <<
158 0.0,
159 sqrt(5.0),
Sameer Agarwald4eb83e2021-08-15 16:37:47 -0700160 4.0*(2.0*x3 - x2),
Keir Mierle8ebb0732012-04-30 23:09:08 -0700161 0.0;
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +0200162 // clang-format on
Keir Mierle8ebb0732012-04-30 23:09:08 -0700163 }
164
165 if (col4) {
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +0200166 // clang-format off
Keir Mierle8ebb0732012-04-30 23:09:08 -0700167 jacobian_matrix.col(column_index++) <<
168 0.0,
169 -sqrt(5.0),
170 0.0,
Sameer Agarwald4eb83e2021-08-15 16:37:47 -0700171 sqrt(10.0) * 2.0 * (x4 - x1);
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +0200172 // clang-format on
Keir Mierle8ebb0732012-04-30 23:09:08 -0700173 }
174 VLOG(1) << "\n" << jacobian_matrix;
175 }
Sameer Agarwalc3c3dd82013-04-25 07:49:24 -0700176
Sameer Agarwalae652192022-02-02 13:17:29 -0800177 if (gradient != nullptr) {
Sameer Agarwalc3c3dd82013-04-25 07:49:24 -0700178 int column_index = 0;
179 if (col1) {
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +0200180 gradient[column_index++] = f1 + f4 * sqrt(10.0) * 2.0 * (x1 - x4);
Sameer Agarwalc3c3dd82013-04-25 07:49:24 -0700181 }
182
183 if (col2) {
184 gradient[column_index++] = f1 * 10.0 + f3 * 2.0 * (x2 - 2.0 * x3);
185 }
186
187 if (col3) {
188 gradient[column_index++] =
Sameer Agarwald4eb83e2021-08-15 16:37:47 -0700189 f2 * sqrt(5.0) + f3 * (4.0 * (2.0 * x3 - x2));
Sameer Agarwalc3c3dd82013-04-25 07:49:24 -0700190 }
191
192 if (col4) {
193 gradient[column_index++] =
194 -f2 * sqrt(5.0) + f4 * sqrt(10.0) * 2.0 * (x4 - x1);
195 }
196 }
197
Keir Mierle8ebb0732012-04-30 23:09:08 -0700198 return true;
199 }
200
Sameer Agarwal2ffddac2019-07-14 00:16:13 +0200201 bool Plus(const double* state,
202 const double* delta,
203 double* state_plus_delta) const final {
Keir Mierle8ebb0732012-04-30 23:09:08 -0700204 int delta_index = 0;
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +0200205 state_plus_delta[0] = (col1 ? state[0] + delta[delta_index++] : state[0]);
206 state_plus_delta[1] = (col2 ? state[1] + delta[delta_index++] : state[1]);
207 state_plus_delta[2] = (col3 ? state[2] + delta[delta_index++] : state[2]);
208 state_plus_delta[3] = (col4 ? state[3] + delta[delta_index++] : state[3]);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700209 return true;
210 }
211
Sameer Agarwal2ffddac2019-07-14 00:16:13 +0200212 int NumEffectiveParameters() const final { return num_active_cols_; }
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +0200213 int NumParameters() const final { return 4; }
214 int NumResiduals() const final { return 4; }
Keir Mierle8ebb0732012-04-30 23:09:08 -0700215
216 private:
217 const int num_active_cols_;
218};
219
220// Templated function to hold a subset of the columns fixed and check
221// if the solver converges to the optimal values or not.
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +0200222template <bool col1, bool col2, bool col3, bool col4>
Sameer Agarwal4441b5b2012-06-12 18:01:11 -0700223void IsTrustRegionSolveSuccessful(TrustRegionStrategyType strategy_type) {
Keir Mierle8ebb0732012-04-30 23:09:08 -0700224 Solver::Options solver_options;
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700225 LinearSolver::Options linear_solver_options;
226 DenseQRSolver linear_solver(linear_solver_options);
227
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +0200228 double parameters[4] = {3, -1, 0, 1.0};
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700229
230 // If the column is inactive, then set its value to the optimal
231 // value.
232 parameters[0] = (col1 ? parameters[0] : 0.0);
233 parameters[1] = (col2 ? parameters[1] : 0.0);
234 parameters[2] = (col3 ? parameters[2] : 0.0);
235 parameters[3] = (col4 ? parameters[3] : 0.0);
236
Keir Mierle8ebb0732012-04-30 23:09:08 -0700237 Minimizer::Options minimizer_options(solver_options);
238 minimizer_options.gradient_tolerance = 1e-26;
239 minimizer_options.function_tolerance = 1e-26;
240 minimizer_options.parameter_tolerance = 1e-26;
Sameer Agarwalae652192022-02-02 13:17:29 -0800241 minimizer_options.evaluator =
242 std::make_unique<PowellEvaluator2<col1, col2, col3, col4>>();
243 minimizer_options.jacobian = minimizer_options.evaluator->CreateJacobian();
Keir Mierle8ebb0732012-04-30 23:09:08 -0700244
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700245 TrustRegionStrategy::Options trust_region_strategy_options;
Sameer Agarwal4441b5b2012-06-12 18:01:11 -0700246 trust_region_strategy_options.trust_region_strategy_type = strategy_type;
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700247 trust_region_strategy_options.linear_solver = &linear_solver;
248 trust_region_strategy_options.initial_radius = 1e4;
249 trust_region_strategy_options.max_radius = 1e20;
Sameer Agarwaleeedd2e2013-07-07 23:04:31 -0700250 trust_region_strategy_options.min_lm_diagonal = 1e-6;
251 trust_region_strategy_options.max_lm_diagonal = 1e32;
Sameer Agarwalae652192022-02-02 13:17:29 -0800252 minimizer_options.trust_region_strategy =
253 TrustRegionStrategy::Create(trust_region_strategy_options);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700254
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700255 TrustRegionMinimizer minimizer;
Keir Mierle8ebb0732012-04-30 23:09:08 -0700256 Solver::Summary summary;
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700257 minimizer.Minimize(minimizer_options, parameters, &summary);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700258
259 // The minimum is at x1 = x2 = x3 = x4 = 0.
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700260 EXPECT_NEAR(0.0, parameters[0], 0.001);
261 EXPECT_NEAR(0.0, parameters[1], 0.001);
262 EXPECT_NEAR(0.0, parameters[2], 0.001);
263 EXPECT_NEAR(0.0, parameters[3], 0.001);
Sameer Agarwalbcc865f2014-12-17 07:35:09 -0800264}
Keir Mierle8ebb0732012-04-30 23:09:08 -0700265
Sameer Agarwal4441b5b2012-06-12 18:01:11 -0700266TEST(TrustRegionMinimizer, PowellsSingularFunctionUsingLevenbergMarquardt) {
Keir Mierle8ebb0732012-04-30 23:09:08 -0700267 // This case is excluded because this has a local minimum and does
268 // not find the optimum. This should not affect the correctness of
269 // this test since we are testing all the other 14 combinations of
270 // column activations.
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700271 //
272 // IsSolveSuccessful<true, true, false, true>();
Keir Mierle8ebb0732012-04-30 23:09:08 -0700273
Sameer Agarwal4441b5b2012-06-12 18:01:11 -0700274 const TrustRegionStrategyType kStrategy = LEVENBERG_MARQUARDT;
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +0200275 // clang-format off
Sameer Agarwal4441b5b2012-06-12 18:01:11 -0700276 IsTrustRegionSolveSuccessful<true, true, true, true >(kStrategy);
277 IsTrustRegionSolveSuccessful<true, true, true, false>(kStrategy);
278 IsTrustRegionSolveSuccessful<true, false, true, true >(kStrategy);
279 IsTrustRegionSolveSuccessful<false, true, true, true >(kStrategy);
280 IsTrustRegionSolveSuccessful<true, true, false, false>(kStrategy);
281 IsTrustRegionSolveSuccessful<true, false, true, false>(kStrategy);
282 IsTrustRegionSolveSuccessful<false, true, true, false>(kStrategy);
283 IsTrustRegionSolveSuccessful<true, false, false, true >(kStrategy);
284 IsTrustRegionSolveSuccessful<false, true, false, true >(kStrategy);
285 IsTrustRegionSolveSuccessful<false, false, true, true >(kStrategy);
286 IsTrustRegionSolveSuccessful<true, false, false, false>(kStrategy);
287 IsTrustRegionSolveSuccessful<false, true, false, false>(kStrategy);
288 IsTrustRegionSolveSuccessful<false, false, true, false>(kStrategy);
289 IsTrustRegionSolveSuccessful<false, false, false, true >(kStrategy);
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +0200290 // clang-format on
Sameer Agarwal4441b5b2012-06-12 18:01:11 -0700291}
292
293TEST(TrustRegionMinimizer, PowellsSingularFunctionUsingDogleg) {
Sameer Agarwal509f68c2013-02-20 01:39:03 -0800294 // The following two cases are excluded because they encounter a
295 // local minimum.
Sameer Agarwal4441b5b2012-06-12 18:01:11 -0700296 //
297 // IsTrustRegionSolveSuccessful<true, true, false, true >(kStrategy);
298 // IsTrustRegionSolveSuccessful<true, true, true, true >(kStrategy);
299
300 const TrustRegionStrategyType kStrategy = DOGLEG;
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +0200301 // clang-format off
Sameer Agarwal4441b5b2012-06-12 18:01:11 -0700302 IsTrustRegionSolveSuccessful<true, true, true, false>(kStrategy);
303 IsTrustRegionSolveSuccessful<true, false, true, true >(kStrategy);
304 IsTrustRegionSolveSuccessful<false, true, true, true >(kStrategy);
305 IsTrustRegionSolveSuccessful<true, true, false, false>(kStrategy);
306 IsTrustRegionSolveSuccessful<true, false, true, false>(kStrategy);
307 IsTrustRegionSolveSuccessful<false, true, true, false>(kStrategy);
308 IsTrustRegionSolveSuccessful<true, false, false, true >(kStrategy);
309 IsTrustRegionSolveSuccessful<false, true, false, true >(kStrategy);
310 IsTrustRegionSolveSuccessful<false, false, true, true >(kStrategy);
311 IsTrustRegionSolveSuccessful<true, false, false, false>(kStrategy);
312 IsTrustRegionSolveSuccessful<false, true, false, false>(kStrategy);
313 IsTrustRegionSolveSuccessful<false, false, true, false>(kStrategy);
314 IsTrustRegionSolveSuccessful<false, false, false, true >(kStrategy);
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +0200315 // clang-format on
Keir Mierle8ebb0732012-04-30 23:09:08 -0700316}
317
Sameer Agarwala406b172012-08-18 15:28:49 -0700318class CurveCostFunction : public CostFunction {
319 public:
320 CurveCostFunction(int num_vertices, double target_length)
321 : num_vertices_(num_vertices), target_length_(target_length) {
322 set_num_residuals(1);
323 for (int i = 0; i < num_vertices_; ++i) {
324 mutable_parameter_block_sizes()->push_back(2);
325 }
326 }
327
328 bool Evaluate(double const* const* parameters,
329 double* residuals,
Sergiu Deitsch484d3412022-02-09 00:34:05 +0100330 double** jacobians) const override {
Sameer Agarwala406b172012-08-18 15:28:49 -0700331 residuals[0] = target_length_;
332
333 for (int i = 0; i < num_vertices_; ++i) {
334 int prev = (num_vertices_ + i - 1) % num_vertices_;
335 double length = 0.0;
336 for (int dim = 0; dim < 2; dim++) {
337 const double diff = parameters[prev][dim] - parameters[i][dim];
338 length += diff * diff;
339 }
340 residuals[0] -= sqrt(length);
341 }
342
Sameer Agarwalae652192022-02-02 13:17:29 -0800343 if (jacobians == nullptr) {
Sameer Agarwala406b172012-08-18 15:28:49 -0700344 return true;
345 }
346
347 for (int i = 0; i < num_vertices_; ++i) {
Sameer Agarwalae652192022-02-02 13:17:29 -0800348 if (jacobians[i] != nullptr) {
Sameer Agarwala406b172012-08-18 15:28:49 -0700349 int prev = (num_vertices_ + i - 1) % num_vertices_;
350 int next = (i + 1) % num_vertices_;
351
352 double u[2], v[2];
353 double norm_u = 0., norm_v = 0.;
354 for (int dim = 0; dim < 2; dim++) {
355 u[dim] = parameters[i][dim] - parameters[prev][dim];
356 norm_u += u[dim] * u[dim];
357 v[dim] = parameters[next][dim] - parameters[i][dim];
358 norm_v += v[dim] * v[dim];
359 }
360
361 norm_u = sqrt(norm_u);
362 norm_v = sqrt(norm_v);
363
364 for (int dim = 0; dim < 2; dim++) {
365 jacobians[i][dim] = 0.;
366
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +0200367 if (norm_u > std::numeric_limits<double>::min()) {
Sameer Agarwala406b172012-08-18 15:28:49 -0700368 jacobians[i][dim] -= u[dim] / norm_u;
369 }
370
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +0200371 if (norm_v > std::numeric_limits<double>::min()) {
Sameer Agarwala406b172012-08-18 15:28:49 -0700372 jacobians[i][dim] += v[dim] / norm_v;
373 }
374 }
375 }
376 }
377
378 return true;
379 }
380
381 private:
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +0200382 int num_vertices_;
383 double target_length_;
Sameer Agarwala406b172012-08-18 15:28:49 -0700384};
385
386TEST(TrustRegionMinimizer, JacobiScalingTest) {
387 int N = 6;
Sameer Agarwalbcc865f2014-12-17 07:35:09 -0800388 std::vector<double*> y(N);
Sameer Agarwala406b172012-08-18 15:28:49 -0700389 const double pi = 3.1415926535897932384626433;
390 for (int i = 0; i < N; i++) {
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +0200391 double theta = i * 2. * pi / static_cast<double>(N);
Sameer Agarwala406b172012-08-18 15:28:49 -0700392 y[i] = new double[2];
393 y[i][0] = cos(theta);
394 y[i][1] = sin(theta);
395 }
396
397 Problem problem;
Sameer Agarwalae652192022-02-02 13:17:29 -0800398 problem.AddResidualBlock(new CurveCostFunction(N, 10.), nullptr, y);
Sameer Agarwala406b172012-08-18 15:28:49 -0700399 Solver::Options options;
400 options.linear_solver_type = ceres::DENSE_QR;
401 Solver::Summary summary;
402 Solve(options, &problem, &summary);
403 EXPECT_LE(summary.final_cost, 1e-10);
Sameer Agarwal66fcc7d2012-10-08 09:54:16 -0700404
405 for (int i = 0; i < N; i++) {
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +0200406 delete[] y[i];
Sameer Agarwal66fcc7d2012-10-08 09:54:16 -0700407 }
Sameer Agarwala406b172012-08-18 15:28:49 -0700408}
409
Sameer Agarwal5648c632014-11-04 22:02:09 -0800410struct ExpCostFunctor {
411 template <typename T>
412 bool operator()(const T* const x, T* residual) const {
413 residual[0] = T(10.0) - exp(x[0]);
414 return true;
415 }
416
417 static CostFunction* Create() {
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +0200418 return new AutoDiffCostFunction<ExpCostFunctor, 1, 1>(new ExpCostFunctor);
Sameer Agarwal5648c632014-11-04 22:02:09 -0800419 }
420};
421
422TEST(TrustRegionMinimizer, GradientToleranceConvergenceUpdatesStep) {
423 double x = 5;
424 Problem problem;
Sameer Agarwalae652192022-02-02 13:17:29 -0800425 problem.AddResidualBlock(ExpCostFunctor::Create(), nullptr, &x);
Sameer Agarwal5648c632014-11-04 22:02:09 -0800426 problem.SetParameterLowerBound(&x, 0, 3.0);
427 Solver::Options options;
428 Solver::Summary summary;
429 Solve(options, &problem, &summary);
430 EXPECT_NEAR(3.0, x, 1e-12);
431 const double expected_final_cost = 0.5 * pow(10.0 - exp(3.0), 2);
432 EXPECT_NEAR(expected_final_cost, summary.final_cost, 1e-12);
433}
434
Sameer Agarwalcaf614a2022-04-21 17:41:10 -0700435} // namespace ceres::internal