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)
31 #include "ceres/parameter_block_ordering.h"
35 #include "gtest/gtest.h"
36 #include "ceres/collections_port.h"
37 #include "ceres/graph.h"
38 #include "ceres/problem_impl.h"
39 #include "ceres/program.h"
40 #include "ceres/stl_util.h"
41 #include "ceres/cost_function.h"
42 #include "ceres/internal/scoped_ptr.h"
43 #include "ceres/sized_cost_function.h"
50 typedef Graph<ParameterBlock*> HessianGraph;
51 typedef HashSet<ParameterBlock*> VertexSet;
53 template <int M, int N1 = 0, int N2 = 0, int N3 = 0>
54 class DummyCostFunction: public SizedCostFunction<M, N1, N2, N3> {
55 virtual bool Evaluate(double const* const* parameters,
57 double** jacobians) const {
62 class SchurOrderingTest : public ::testing::Test {
64 virtual void SetUp() {
65 // The explicit calls to AddParameterBlock are necessary because
66 // the below tests depend on the specific numbering of the
68 problem_.AddParameterBlock(x_, 3);
69 problem_.AddParameterBlock(y_, 4);
70 problem_.AddParameterBlock(z_, 5);
71 problem_.AddParameterBlock(w_, 6);
73 problem_.AddResidualBlock(new DummyCostFunction<2, 3>, NULL, x_);
74 problem_.AddResidualBlock(new DummyCostFunction<6, 5, 4>, NULL, z_, y_);
75 problem_.AddResidualBlock(new DummyCostFunction<3, 3, 5>, NULL, x_, z_);
76 problem_.AddResidualBlock(new DummyCostFunction<7, 5, 3>, NULL, z_, x_);
77 problem_.AddResidualBlock(new DummyCostFunction<1, 5, 3, 6>, NULL,
82 double x_[3], y_[4], z_[5], w_[6];
85 TEST_F(SchurOrderingTest, NoFixed) {
86 const Program& program = problem_.program();
87 const vector<ParameterBlock*>& parameter_blocks = program.parameter_blocks();
88 scoped_ptr<HessianGraph> graph(CreateHessianGraph(program));
90 const VertexSet& vertices = graph->vertices();
91 EXPECT_EQ(vertices.size(), 4);
93 for (int i = 0; i < 4; ++i) {
94 EXPECT_TRUE(vertices.find(parameter_blocks[i]) != vertices.end());
98 const VertexSet& neighbors = graph->Neighbors(parameter_blocks[0]);
99 EXPECT_EQ(neighbors.size(), 2);
100 EXPECT_TRUE(neighbors.find(parameter_blocks[2]) != neighbors.end());
101 EXPECT_TRUE(neighbors.find(parameter_blocks[3]) != neighbors.end());
105 const VertexSet& neighbors = graph->Neighbors(parameter_blocks[1]);
106 EXPECT_EQ(neighbors.size(), 1);
107 EXPECT_TRUE(neighbors.find(parameter_blocks[2]) != neighbors.end());
111 const VertexSet& neighbors = graph->Neighbors(parameter_blocks[2]);
112 EXPECT_EQ(neighbors.size(), 3);
113 EXPECT_TRUE(neighbors.find(parameter_blocks[0]) != neighbors.end());
114 EXPECT_TRUE(neighbors.find(parameter_blocks[1]) != neighbors.end());
115 EXPECT_TRUE(neighbors.find(parameter_blocks[3]) != neighbors.end());
119 const VertexSet& neighbors = graph->Neighbors(parameter_blocks[3]);
120 EXPECT_EQ(neighbors.size(), 2);
121 EXPECT_TRUE(neighbors.find(parameter_blocks[0]) != neighbors.end());
122 EXPECT_TRUE(neighbors.find(parameter_blocks[2]) != neighbors.end());
126 TEST_F(SchurOrderingTest, AllFixed) {
127 problem_.SetParameterBlockConstant(x_);
128 problem_.SetParameterBlockConstant(y_);
129 problem_.SetParameterBlockConstant(z_);
130 problem_.SetParameterBlockConstant(w_);
132 const Program& program = problem_.program();
133 scoped_ptr<HessianGraph> graph(CreateHessianGraph(program));
134 EXPECT_EQ(graph->vertices().size(), 0);
137 TEST_F(SchurOrderingTest, OneFixed) {
138 problem_.SetParameterBlockConstant(x_);
140 const Program& program = problem_.program();
141 const vector<ParameterBlock*>& parameter_blocks = program.parameter_blocks();
142 scoped_ptr<HessianGraph> graph(CreateHessianGraph(program));
144 const VertexSet& vertices = graph->vertices();
146 EXPECT_EQ(vertices.size(), 3);
147 EXPECT_TRUE(vertices.find(parameter_blocks[0]) == vertices.end());
149 for (int i = 1; i < 3; ++i) {
150 EXPECT_TRUE(vertices.find(parameter_blocks[i]) != vertices.end());
154 const VertexSet& neighbors = graph->Neighbors(parameter_blocks[1]);
155 EXPECT_EQ(neighbors.size(), 1);
156 EXPECT_TRUE(neighbors.find(parameter_blocks[2]) != neighbors.end());
160 const VertexSet& neighbors = graph->Neighbors(parameter_blocks[2]);
161 EXPECT_EQ(neighbors.size(), 2);
162 EXPECT_TRUE(neighbors.find(parameter_blocks[1]) != neighbors.end());
163 EXPECT_TRUE(neighbors.find(parameter_blocks[3]) != neighbors.end());
167 const VertexSet& neighbors = graph->Neighbors(parameter_blocks[3]);
168 EXPECT_EQ(neighbors.size(), 1);
169 EXPECT_TRUE(neighbors.find(parameter_blocks[2]) != neighbors.end());
172 // The constant parameter block is at the end.
173 vector<ParameterBlock*> ordering;
174 ComputeSchurOrdering(program, &ordering);
175 EXPECT_EQ(ordering.back(), parameter_blocks[0]);
178 } // namespace internal