Imported Upstream version ceres 1.13.0
[platform/upstream/ceres-solver.git] / internal / ceres / line_search_preprocessor_test.cc
1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2015 Google Inc. All rights reserved.
3 // http://ceres-solver.org/
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
31 #include <map>
32
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"
38
39 namespace ceres {
40 namespace internal {
41
42 TEST(LineSearchPreprocessor, ZeroProblem) {
43   ProblemImpl problem;
44   Solver::Options options;
45   options.minimizer_type = LINE_SEARCH;
46   LineSearchPreprocessor preprocessor;
47   PreprocessedProblem pp;
48   EXPECT_TRUE(preprocessor.Preprocess(options, &problem, &pp));
49 }
50
51 TEST(LineSearchPreprocessor, ProblemWithInvalidParameterBlock) {
52   ProblemImpl problem;
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));
60 }
61
62 TEST(LineSearchPreprocessor, ParameterBlockHasBounds) {
63   ProblemImpl problem;
64   double x = 1.0;
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));
73 }
74
75 class FailingCostFunction : public SizedCostFunction<1, 1> {
76  public:
77   bool Evaluate(double const* const* parameters,
78                 double* residuals,
79                 double** jacobians) const {
80     return false;
81   }
82 };
83
84 TEST(LineSearchPreprocessor, RemoveParameterBlocksFailed) {
85   ProblemImpl problem;
86   double x = 3.0;
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));
94 }
95
96 TEST(LineSearchPreprocessor, RemoveParameterBlocksSucceeds) {
97   ProblemImpl problem;
98   double x = 3.0;
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));
105 }
106
107 template<int kNumResiduals, int N1 = 0, int N2 = 0, int N3 = 0>
108 class DummyCostFunction : public SizedCostFunction<kNumResiduals, N1, N2, N3> {
109  public:
110   bool Evaluate(double const* const* parameters,
111                 double* residuals,
112                 double** jacobians) const {
113     return true;
114   }
115 };
116
117 TEST(LineSearchPreprocessor, NormalOperation) {
118   ProblemImpl problem;
119   double x = 1.0;
120   double y = 1.0;
121   double z = 1.0;
122   problem.AddResidualBlock(new DummyCostFunction<1, 1, 1>, NULL, &x, &y);
123   problem.AddResidualBlock(new DummyCostFunction<1, 1, 1>, NULL, &y, &z);
124
125   Solver::Options options;
126   options.minimizer_type = LINE_SEARCH;
127
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);
133 }
134
135 }  // namespace internal
136 }  // namespace ceres