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: sameeragarwal@google.com (Sameer Agarwal)
33 #include "ceres/problem_impl.h"
34 #include "ceres/sized_cost_function.h"
35 #include "ceres/solver.h"
36 #include "ceres/line_search_preprocessor.h"
37 #include "gtest/gtest.h"
42 TEST(LineSearchPreprocessor, ZeroProblem) {
44 Solver::Options options;
45 options.minimizer_type = LINE_SEARCH;
46 LineSearchPreprocessor preprocessor;
47 PreprocessedProblem pp;
48 EXPECT_TRUE(preprocessor.Preprocess(options, &problem, &pp));
51 TEST(LineSearchPreprocessor, ProblemWithInvalidParameterBlock) {
53 double x = std::numeric_limits<double>::quiet_NaN();
54 problem.AddParameterBlock(&x, 1);
55 Solver::Options options;
56 options.minimizer_type = LINE_SEARCH;
57 LineSearchPreprocessor preprocessor;
58 PreprocessedProblem pp;
59 EXPECT_FALSE(preprocessor.Preprocess(options, &problem, &pp));
62 TEST(LineSearchPreprocessor, ParameterBlockHasBounds) {
65 problem.AddParameterBlock(&x, 1);
66 problem.SetParameterUpperBound(&x, 0, 1.0);
67 problem.SetParameterLowerBound(&x, 0, 2.0);
68 Solver::Options options;
69 options.minimizer_type = LINE_SEARCH;
70 LineSearchPreprocessor preprocessor;
71 PreprocessedProblem pp;
72 EXPECT_FALSE(preprocessor.Preprocess(options, &problem, &pp));
75 class FailingCostFunction : public SizedCostFunction<1, 1> {
77 bool Evaluate(double const* const* parameters,
79 double** jacobians) const {
84 TEST(LineSearchPreprocessor, RemoveParameterBlocksFailed) {
87 problem.AddResidualBlock(new FailingCostFunction, NULL, &x);
88 problem.SetParameterBlockConstant(&x);
89 Solver::Options options;
90 options.minimizer_type = LINE_SEARCH;
91 LineSearchPreprocessor preprocessor;
92 PreprocessedProblem pp;
93 EXPECT_FALSE(preprocessor.Preprocess(options, &problem, &pp));
96 TEST(LineSearchPreprocessor, RemoveParameterBlocksSucceeds) {
99 problem.AddParameterBlock(&x, 1);
100 Solver::Options options;
101 options.minimizer_type = LINE_SEARCH;
102 LineSearchPreprocessor preprocessor;
103 PreprocessedProblem pp;
104 EXPECT_TRUE(preprocessor.Preprocess(options, &problem, &pp));
107 template<int kNumResiduals, int N1 = 0, int N2 = 0, int N3 = 0>
108 class DummyCostFunction : public SizedCostFunction<kNumResiduals, N1, N2, N3> {
110 bool Evaluate(double const* const* parameters,
112 double** jacobians) const {
117 TEST(LineSearchPreprocessor, NormalOperation) {
122 problem.AddResidualBlock(new DummyCostFunction<1, 1, 1>, NULL, &x, &y);
123 problem.AddResidualBlock(new DummyCostFunction<1, 1, 1>, NULL, &y, &z);
125 Solver::Options options;
126 options.minimizer_type = LINE_SEARCH;
128 LineSearchPreprocessor preprocessor;
129 PreprocessedProblem pp;
130 EXPECT_TRUE(preprocessor.Preprocess(options, &problem, &pp));
131 EXPECT_EQ(pp.evaluator_options.linear_solver_type, CGNR);
132 EXPECT_TRUE(pp.evaluator.get() != NULL);
135 } // namespace internal