Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 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 "ceres/reorder_program.h" |
| 32 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 33 | #include <random> |
| 34 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 35 | #include "ceres/parameter_block.h" |
| 36 | #include "ceres/problem_impl.h" |
| 37 | #include "ceres/program.h" |
| 38 | #include "ceres/sized_cost_function.h" |
| 39 | #include "ceres/solver.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 40 | #include "gmock/gmock.h" |
| 41 | #include "gtest/gtest.h" |
| 42 | |
| 43 | namespace ceres { |
| 44 | namespace internal { |
| 45 | |
| 46 | using std::vector; |
| 47 | |
| 48 | // Templated base class for the CostFunction signatures. |
| 49 | template <int kNumResiduals, int... Ns> |
| 50 | class MockCostFunctionBase : public SizedCostFunction<kNumResiduals, Ns...> { |
| 51 | public: |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 52 | bool Evaluate(double const* const* parameters, |
| 53 | double* residuals, |
| 54 | double** jacobians) const final { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 55 | // Do nothing. This is never called. |
| 56 | return true; |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | class UnaryCostFunction : public MockCostFunctionBase<2, 1> {}; |
| 61 | class BinaryCostFunction : public MockCostFunctionBase<2, 1, 1> {}; |
| 62 | class TernaryCostFunction : public MockCostFunctionBase<2, 1, 1, 1> {}; |
| 63 | |
| 64 | TEST(_, ReorderResidualBlockNormalFunction) { |
| 65 | ProblemImpl problem; |
| 66 | double x; |
| 67 | double y; |
| 68 | double z; |
| 69 | |
| 70 | problem.AddParameterBlock(&x, 1); |
| 71 | problem.AddParameterBlock(&y, 1); |
| 72 | problem.AddParameterBlock(&z, 1); |
| 73 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 74 | problem.AddResidualBlock(new UnaryCostFunction(), nullptr, &x); |
| 75 | problem.AddResidualBlock(new BinaryCostFunction(), nullptr, &z, &x); |
| 76 | problem.AddResidualBlock(new BinaryCostFunction(), nullptr, &z, &y); |
| 77 | problem.AddResidualBlock(new UnaryCostFunction(), nullptr, &z); |
| 78 | problem.AddResidualBlock(new BinaryCostFunction(), nullptr, &x, &y); |
| 79 | problem.AddResidualBlock(new UnaryCostFunction(), nullptr, &y); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 80 | |
| 81 | ParameterBlockOrdering* linear_solver_ordering = new ParameterBlockOrdering; |
| 82 | linear_solver_ordering->AddElementToGroup(&x, 0); |
| 83 | linear_solver_ordering->AddElementToGroup(&y, 0); |
| 84 | linear_solver_ordering->AddElementToGroup(&z, 1); |
| 85 | |
| 86 | Solver::Options options; |
| 87 | options.linear_solver_type = DENSE_SCHUR; |
| 88 | options.linear_solver_ordering.reset(linear_solver_ordering); |
| 89 | |
| 90 | const vector<ResidualBlock*>& residual_blocks = |
| 91 | problem.program().residual_blocks(); |
| 92 | |
| 93 | vector<ResidualBlock*> expected_residual_blocks; |
| 94 | |
| 95 | // This is a bit fragile, but it serves the purpose. We know the |
| 96 | // bucketing algorithm that the reordering function uses, so we |
| 97 | // expect the order for residual blocks for each e_block to be |
| 98 | // filled in reverse. |
| 99 | expected_residual_blocks.push_back(residual_blocks[4]); |
| 100 | expected_residual_blocks.push_back(residual_blocks[1]); |
| 101 | expected_residual_blocks.push_back(residual_blocks[0]); |
| 102 | expected_residual_blocks.push_back(residual_blocks[5]); |
| 103 | expected_residual_blocks.push_back(residual_blocks[2]); |
| 104 | expected_residual_blocks.push_back(residual_blocks[3]); |
| 105 | |
| 106 | Program* program = problem.mutable_program(); |
| 107 | program->SetParameterOffsetsAndIndex(); |
| 108 | |
| 109 | std::string message; |
| 110 | EXPECT_TRUE(LexicographicallyOrderResidualBlocks( |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 111 | 2, problem.mutable_program(), &message)); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 112 | EXPECT_EQ(residual_blocks.size(), expected_residual_blocks.size()); |
| 113 | for (int i = 0; i < expected_residual_blocks.size(); ++i) { |
| 114 | EXPECT_EQ(residual_blocks[i], expected_residual_blocks[i]); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | TEST(_, ApplyOrderingOrderingTooSmall) { |
| 119 | ProblemImpl problem; |
| 120 | double x; |
| 121 | double y; |
| 122 | double z; |
| 123 | |
| 124 | problem.AddParameterBlock(&x, 1); |
| 125 | problem.AddParameterBlock(&y, 1); |
| 126 | problem.AddParameterBlock(&z, 1); |
| 127 | |
| 128 | ParameterBlockOrdering linear_solver_ordering; |
| 129 | linear_solver_ordering.AddElementToGroup(&x, 0); |
| 130 | linear_solver_ordering.AddElementToGroup(&y, 1); |
| 131 | |
| 132 | Program program(problem.program()); |
| 133 | std::string message; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 134 | EXPECT_FALSE(ApplyOrdering( |
| 135 | problem.parameter_map(), linear_solver_ordering, &program, &message)); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | TEST(_, ApplyOrderingNormal) { |
| 139 | ProblemImpl problem; |
| 140 | double x; |
| 141 | double y; |
| 142 | double z; |
| 143 | |
| 144 | problem.AddParameterBlock(&x, 1); |
| 145 | problem.AddParameterBlock(&y, 1); |
| 146 | problem.AddParameterBlock(&z, 1); |
| 147 | |
| 148 | ParameterBlockOrdering linear_solver_ordering; |
| 149 | linear_solver_ordering.AddElementToGroup(&x, 0); |
| 150 | linear_solver_ordering.AddElementToGroup(&y, 2); |
| 151 | linear_solver_ordering.AddElementToGroup(&z, 1); |
| 152 | |
| 153 | Program* program = problem.mutable_program(); |
| 154 | std::string message; |
| 155 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 156 | EXPECT_TRUE(ApplyOrdering( |
| 157 | problem.parameter_map(), linear_solver_ordering, program, &message)); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 158 | const vector<ParameterBlock*>& parameter_blocks = program->parameter_blocks(); |
| 159 | |
| 160 | EXPECT_EQ(parameter_blocks.size(), 3); |
| 161 | EXPECT_EQ(parameter_blocks[0]->user_state(), &x); |
| 162 | EXPECT_EQ(parameter_blocks[1]->user_state(), &z); |
| 163 | EXPECT_EQ(parameter_blocks[2]->user_state(), &y); |
| 164 | } |
| 165 | |
| 166 | #ifndef CERES_NO_SUITESPARSE |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 167 | class ReorderProgramFoSparseCholeskyUsingSuiteSparseTest |
| 168 | : public ::testing::Test { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 169 | protected: |
| 170 | void SetUp() { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 171 | problem_.AddResidualBlock(new UnaryCostFunction(), nullptr, &x_); |
| 172 | problem_.AddResidualBlock(new BinaryCostFunction(), nullptr, &z_, &x_); |
| 173 | problem_.AddResidualBlock(new BinaryCostFunction(), nullptr, &z_, &y_); |
| 174 | problem_.AddResidualBlock(new UnaryCostFunction(), nullptr, &z_); |
| 175 | problem_.AddResidualBlock(new BinaryCostFunction(), nullptr, &x_, &y_); |
| 176 | problem_.AddResidualBlock(new UnaryCostFunction(), nullptr, &y_); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void ComputeAndValidateOrdering( |
| 180 | const ParameterBlockOrdering& linear_solver_ordering) { |
| 181 | Program* program = problem_.mutable_program(); |
| 182 | vector<ParameterBlock*> unordered_parameter_blocks = |
| 183 | program->parameter_blocks(); |
| 184 | |
| 185 | std::string error; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 186 | EXPECT_TRUE(ReorderProgramForSparseCholesky(ceres::SUITE_SPARSE, |
| 187 | linear_solver_ordering, |
| 188 | 0, /* use all rows */ |
| 189 | program, |
| 190 | &error)); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 191 | const vector<ParameterBlock*>& ordered_parameter_blocks = |
| 192 | program->parameter_blocks(); |
| 193 | EXPECT_EQ(ordered_parameter_blocks.size(), |
| 194 | unordered_parameter_blocks.size()); |
| 195 | |
| 196 | EXPECT_THAT(unordered_parameter_blocks, |
| 197 | ::testing::UnorderedElementsAreArray(ordered_parameter_blocks)); |
| 198 | } |
| 199 | |
| 200 | ProblemImpl problem_; |
| 201 | double x_; |
| 202 | double y_; |
| 203 | double z_; |
| 204 | }; |
| 205 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 206 | TEST_F(ReorderProgramFoSparseCholeskyUsingSuiteSparseTest, |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 207 | EverythingInGroupZero) { |
| 208 | ParameterBlockOrdering linear_solver_ordering; |
| 209 | linear_solver_ordering.AddElementToGroup(&x_, 0); |
| 210 | linear_solver_ordering.AddElementToGroup(&y_, 0); |
| 211 | linear_solver_ordering.AddElementToGroup(&z_, 0); |
| 212 | |
| 213 | ComputeAndValidateOrdering(linear_solver_ordering); |
| 214 | } |
| 215 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 216 | TEST_F(ReorderProgramFoSparseCholeskyUsingSuiteSparseTest, ContiguousGroups) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 217 | ParameterBlockOrdering linear_solver_ordering; |
| 218 | linear_solver_ordering.AddElementToGroup(&x_, 0); |
| 219 | linear_solver_ordering.AddElementToGroup(&y_, 1); |
| 220 | linear_solver_ordering.AddElementToGroup(&z_, 2); |
| 221 | |
| 222 | ComputeAndValidateOrdering(linear_solver_ordering); |
| 223 | } |
| 224 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 225 | TEST_F(ReorderProgramFoSparseCholeskyUsingSuiteSparseTest, GroupsWithGaps) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 226 | ParameterBlockOrdering linear_solver_ordering; |
| 227 | linear_solver_ordering.AddElementToGroup(&x_, 0); |
| 228 | linear_solver_ordering.AddElementToGroup(&y_, 2); |
| 229 | linear_solver_ordering.AddElementToGroup(&z_, 2); |
| 230 | |
| 231 | ComputeAndValidateOrdering(linear_solver_ordering); |
| 232 | } |
| 233 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 234 | TEST_F(ReorderProgramFoSparseCholeskyUsingSuiteSparseTest, |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 235 | NonContiguousStartingAtTwo) { |
| 236 | ParameterBlockOrdering linear_solver_ordering; |
| 237 | linear_solver_ordering.AddElementToGroup(&x_, 2); |
| 238 | linear_solver_ordering.AddElementToGroup(&y_, 4); |
| 239 | linear_solver_ordering.AddElementToGroup(&z_, 4); |
| 240 | |
| 241 | ComputeAndValidateOrdering(linear_solver_ordering); |
| 242 | } |
| 243 | #endif // CERES_NO_SUITESPARSE |
| 244 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 245 | TEST(_, ReorderResidualBlocksbyPartition) { |
| 246 | ProblemImpl problem; |
| 247 | double x; |
| 248 | double y; |
| 249 | double z; |
| 250 | |
| 251 | problem.AddParameterBlock(&x, 1); |
| 252 | problem.AddParameterBlock(&y, 1); |
| 253 | problem.AddParameterBlock(&z, 1); |
| 254 | |
| 255 | problem.AddResidualBlock(new UnaryCostFunction(), nullptr, &x); |
| 256 | problem.AddResidualBlock(new BinaryCostFunction(), nullptr, &z, &x); |
| 257 | problem.AddResidualBlock(new BinaryCostFunction(), nullptr, &z, &y); |
| 258 | problem.AddResidualBlock(new UnaryCostFunction(), nullptr, &z); |
| 259 | problem.AddResidualBlock(new BinaryCostFunction(), nullptr, &x, &y); |
| 260 | problem.AddResidualBlock(new UnaryCostFunction(), nullptr, &y); |
| 261 | |
| 262 | std::vector<ResidualBlockId> residual_block_ids; |
| 263 | problem.GetResidualBlocks(&residual_block_ids); |
| 264 | std::vector<ResidualBlock*> residual_blocks = |
| 265 | problem.program().residual_blocks(); |
| 266 | auto rng = std::default_random_engine{}; |
| 267 | for (int i = 1; i < 6; ++i) { |
| 268 | std::shuffle( |
| 269 | std::begin(residual_block_ids), std::end(residual_block_ids), rng); |
| 270 | std::unordered_set<ResidualBlockId> bottom(residual_block_ids.begin(), |
| 271 | residual_block_ids.begin() + i); |
| 272 | const int start_bottom = |
| 273 | ReorderResidualBlocksByPartition(bottom, problem.mutable_program()); |
| 274 | std::vector<ResidualBlock*> actual_residual_blocks = |
| 275 | problem.program().residual_blocks(); |
| 276 | EXPECT_THAT(actual_residual_blocks, |
| 277 | testing::UnorderedElementsAreArray(residual_blocks)); |
| 278 | EXPECT_EQ(start_bottom, residual_blocks.size() - i); |
| 279 | for (int j = start_bottom; j < residual_blocks.size(); ++j) { |
| 280 | EXPECT_THAT(bottom, ::testing::Contains(actual_residual_blocks[j])); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 285 | } // namespace internal |
| 286 | } // namespace ceres |