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/visibility_based_preconditioner.h" |
| 32 | |
| 33 | #include <memory> |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 34 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 35 | #include "Eigen/Dense" |
| 36 | #include "ceres/block_random_access_dense_matrix.h" |
| 37 | #include "ceres/block_random_access_sparse_matrix.h" |
| 38 | #include "ceres/block_sparse_matrix.h" |
| 39 | #include "ceres/casts.h" |
| 40 | #include "ceres/file.h" |
| 41 | #include "ceres/internal/eigen.h" |
| 42 | #include "ceres/linear_least_squares_problems.h" |
| 43 | #include "ceres/schur_eliminator.h" |
| 44 | #include "ceres/stringprintf.h" |
| 45 | #include "ceres/test_util.h" |
| 46 | #include "ceres/types.h" |
| 47 | #include "glog/logging.h" |
| 48 | #include "gtest/gtest.h" |
| 49 | |
| 50 | namespace ceres { |
| 51 | namespace internal { |
| 52 | |
| 53 | // TODO(sameeragarwal): Re-enable this test once serialization is |
| 54 | // working again. |
| 55 | |
| 56 | // using testing::AssertionResult; |
| 57 | // using testing::AssertionSuccess; |
| 58 | // using testing::AssertionFailure; |
| 59 | |
| 60 | // static const double kTolerance = 1e-12; |
| 61 | |
| 62 | // class VisibilityBasedPreconditionerTest : public ::testing::Test { |
| 63 | // public: |
| 64 | // static const int kCameraSize = 9; |
| 65 | |
| 66 | // protected: |
| 67 | // void SetUp() { |
| 68 | // string input_file = TestFileAbsolutePath("problem-6-1384-000.lsqp"); |
| 69 | |
| 70 | // std::unique_ptr<LinearLeastSquaresProblem> problem( |
| 71 | // CHECK_NOTNULL(CreateLinearLeastSquaresProblemFromFile(input_file))); |
| 72 | // A_.reset(down_cast<BlockSparseMatrix*>(problem->A.release())); |
| 73 | // b_.reset(problem->b.release()); |
| 74 | // D_.reset(problem->D.release()); |
| 75 | |
| 76 | // const CompressedRowBlockStructure* bs = |
| 77 | // CHECK_NOTNULL(A_->block_structure()); |
| 78 | // const int num_col_blocks = bs->cols.size(); |
| 79 | |
| 80 | // num_cols_ = A_->num_cols(); |
| 81 | // num_rows_ = A_->num_rows(); |
| 82 | // num_eliminate_blocks_ = problem->num_eliminate_blocks; |
| 83 | // num_camera_blocks_ = num_col_blocks - num_eliminate_blocks_; |
| 84 | // options_.elimination_groups.push_back(num_eliminate_blocks_); |
| 85 | // options_.elimination_groups.push_back( |
| 86 | // A_->block_structure()->cols.size() - num_eliminate_blocks_); |
| 87 | |
| 88 | // vector<int> blocks(num_col_blocks - num_eliminate_blocks_, 0); |
| 89 | // for (int i = num_eliminate_blocks_; i < num_col_blocks; ++i) { |
| 90 | // blocks[i - num_eliminate_blocks_] = bs->cols[i].size; |
| 91 | // } |
| 92 | |
| 93 | // // The input matrix is a real jacobian and fairly poorly |
| 94 | // // conditioned. Setting D to a large constant makes the normal |
| 95 | // // equations better conditioned and makes the tests below better |
| 96 | // // conditioned. |
| 97 | // VectorRef(D_.get(), num_cols_).setConstant(10.0); |
| 98 | |
| 99 | // schur_complement_.reset(new BlockRandomAccessDenseMatrix(blocks)); |
| 100 | // Vector rhs(schur_complement_->num_rows()); |
| 101 | |
| 102 | // std::unique_ptr<SchurEliminatorBase> eliminator; |
| 103 | // LinearSolver::Options eliminator_options; |
| 104 | // eliminator_options.elimination_groups = options_.elimination_groups; |
| 105 | // eliminator_options.num_threads = options_.num_threads; |
| 106 | |
| 107 | // eliminator.reset(SchurEliminatorBase::Create(eliminator_options)); |
| 108 | // eliminator->Init(num_eliminate_blocks_, bs); |
| 109 | // eliminator->Eliminate(A_.get(), b_.get(), D_.get(), |
| 110 | // schur_complement_.get(), rhs.data()); |
| 111 | // } |
| 112 | |
| 113 | // AssertionResult IsSparsityStructureValid() { |
| 114 | // preconditioner_->InitStorage(*A_->block_structure()); |
| 115 | // const std::unordered_set<pair<int, int>, pair_hash>& cluster_pairs = |
| 116 | // get_cluster_pairs(); const vector<int>& cluster_membership = |
| 117 | // get_cluster_membership(); |
| 118 | |
| 119 | // for (int i = 0; i < num_camera_blocks_; ++i) { |
| 120 | // for (int j = i; j < num_camera_blocks_; ++j) { |
| 121 | // if (cluster_pairs.count(make_pair(cluster_membership[i], |
| 122 | // cluster_membership[j]))) { |
| 123 | // if (!IsBlockPairInPreconditioner(i, j)) { |
| 124 | // return AssertionFailure() |
| 125 | // << "block pair (" << i << "," << j << "missing"; |
| 126 | // } |
| 127 | // } else { |
| 128 | // if (IsBlockPairInPreconditioner(i, j)) { |
| 129 | // return AssertionFailure() |
| 130 | // << "block pair (" << i << "," << j << "should not be present"; |
| 131 | // } |
| 132 | // } |
| 133 | // } |
| 134 | // } |
| 135 | // return AssertionSuccess(); |
| 136 | // } |
| 137 | |
| 138 | // AssertionResult PreconditionerValuesMatch() { |
| 139 | // preconditioner_->Update(*A_, D_.get()); |
| 140 | // const std::unordered_set<pair<int, int>, pair_hash>& cluster_pairs = |
| 141 | // get_cluster_pairs(); const BlockRandomAccessSparseMatrix* m = get_m(); |
| 142 | // Matrix preconditioner_matrix; |
| 143 | // m->matrix()->ToDenseMatrix(&preconditioner_matrix); |
| 144 | // ConstMatrixRef full_schur_complement(schur_complement_->values(), |
| 145 | // m->num_rows(), |
| 146 | // m->num_rows()); |
| 147 | // const int num_clusters = get_num_clusters(); |
| 148 | // const int kDiagonalBlockSize = |
| 149 | // kCameraSize * num_camera_blocks_ / num_clusters; |
| 150 | |
| 151 | // for (int i = 0; i < num_clusters; ++i) { |
| 152 | // for (int j = i; j < num_clusters; ++j) { |
| 153 | // double diff = 0.0; |
| 154 | // if (cluster_pairs.count(make_pair(i, j))) { |
| 155 | // diff = |
| 156 | // (preconditioner_matrix.block(kDiagonalBlockSize * i, |
| 157 | // kDiagonalBlockSize * j, |
| 158 | // kDiagonalBlockSize, |
| 159 | // kDiagonalBlockSize) - |
| 160 | // full_schur_complement.block(kDiagonalBlockSize * i, |
| 161 | // kDiagonalBlockSize * j, |
| 162 | // kDiagonalBlockSize, |
| 163 | // kDiagonalBlockSize)).norm(); |
| 164 | // } else { |
| 165 | // diff = preconditioner_matrix.block(kDiagonalBlockSize * i, |
| 166 | // kDiagonalBlockSize * j, |
| 167 | // kDiagonalBlockSize, |
| 168 | // kDiagonalBlockSize).norm(); |
| 169 | // } |
| 170 | // if (diff > kTolerance) { |
| 171 | // return AssertionFailure() |
| 172 | // << "Preconditioner block " << i << " " << j << " differs " |
| 173 | // << "from expected value by " << diff; |
| 174 | // } |
| 175 | // } |
| 176 | // } |
| 177 | // return AssertionSuccess(); |
| 178 | // } |
| 179 | |
| 180 | // // Accessors |
| 181 | // int get_num_blocks() { return preconditioner_->num_blocks_; } |
| 182 | |
| 183 | // int get_num_clusters() { return preconditioner_->num_clusters_; } |
| 184 | // int* get_mutable_num_clusters() { return &preconditioner_->num_clusters_; } |
| 185 | |
| 186 | // const vector<int>& get_block_size() { |
| 187 | // return preconditioner_->block_size_; } |
| 188 | |
| 189 | // vector<int>* get_mutable_block_size() { |
| 190 | // return &preconditioner_->block_size_; } |
| 191 | |
| 192 | // const vector<int>& get_cluster_membership() { |
| 193 | // return preconditioner_->cluster_membership_; |
| 194 | // } |
| 195 | |
| 196 | // vector<int>* get_mutable_cluster_membership() { |
| 197 | // return &preconditioner_->cluster_membership_; |
| 198 | // } |
| 199 | |
| 200 | // const set<pair<int, int>>& get_block_pairs() { |
| 201 | // return preconditioner_->block_pairs_; |
| 202 | // } |
| 203 | |
| 204 | // set<pair<int, int>>* get_mutable_block_pairs() { |
| 205 | // return &preconditioner_->block_pairs_; |
| 206 | // } |
| 207 | |
| 208 | // const std::unordered_set<pair<int, int>, pair_hash>& get_cluster_pairs() { |
| 209 | // return preconditioner_->cluster_pairs_; |
| 210 | // } |
| 211 | |
| 212 | // std::unordered_set<pair<int, int>, pair_hash>* get_mutable_cluster_pairs() |
| 213 | // { |
| 214 | // return &preconditioner_->cluster_pairs_; |
| 215 | // } |
| 216 | |
| 217 | // bool IsBlockPairInPreconditioner(const int block1, const int block2) { |
| 218 | // return preconditioner_->IsBlockPairInPreconditioner(block1, block2); |
| 219 | // } |
| 220 | |
| 221 | // bool IsBlockPairOffDiagonal(const int block1, const int block2) { |
| 222 | // return preconditioner_->IsBlockPairOffDiagonal(block1, block2); |
| 223 | // } |
| 224 | |
| 225 | // const BlockRandomAccessSparseMatrix* get_m() { |
| 226 | // return preconditioner_->m_.get(); |
| 227 | // } |
| 228 | |
| 229 | // int num_rows_; |
| 230 | // int num_cols_; |
| 231 | // int num_eliminate_blocks_; |
| 232 | // int num_camera_blocks_; |
| 233 | |
| 234 | // std::unique_ptr<BlockSparseMatrix> A_; |
| 235 | // std::unique_ptr<double[]> b_; |
| 236 | // std::unique_ptr<double[]> D_; |
| 237 | |
| 238 | // Preconditioner::Options options_; |
| 239 | // std::unique_ptr<VisibilityBasedPreconditioner> preconditioner_; |
| 240 | // std::unique_ptr<BlockRandomAccessDenseMatrix> schur_complement_; |
| 241 | // }; |
| 242 | |
| 243 | // TEST_F(VisibilityBasedPreconditionerTest, OneClusterClusterJacobi) { |
| 244 | // options_.type = CLUSTER_JACOBI; |
| 245 | // preconditioner_.reset( |
| 246 | // new VisibilityBasedPreconditioner(*A_->block_structure(), options_)); |
| 247 | |
| 248 | // // Override the clustering to be a single clustering containing all |
| 249 | // // the cameras. |
| 250 | // vector<int>& cluster_membership = *get_mutable_cluster_membership(); |
| 251 | // for (int i = 0; i < num_camera_blocks_; ++i) { |
| 252 | // cluster_membership[i] = 0; |
| 253 | // } |
| 254 | |
| 255 | // *get_mutable_num_clusters() = 1; |
| 256 | |
| 257 | // std::unordered_set<pair<int, int>, pair_hash>& cluster_pairs = |
| 258 | // *get_mutable_cluster_pairs(); cluster_pairs.clear(); |
| 259 | // cluster_pairs.insert(make_pair(0, 0)); |
| 260 | |
| 261 | // EXPECT_TRUE(IsSparsityStructureValid()); |
| 262 | // EXPECT_TRUE(PreconditionerValuesMatch()); |
| 263 | |
| 264 | // // Multiplication by the inverse of the preconditioner. |
| 265 | // const int num_rows = schur_complement_->num_rows(); |
| 266 | // ConstMatrixRef full_schur_complement(schur_complement_->values(), |
| 267 | // num_rows, |
| 268 | // num_rows); |
| 269 | // Vector x(num_rows); |
| 270 | // Vector y(num_rows); |
| 271 | // Vector z(num_rows); |
| 272 | |
| 273 | // for (int i = 0; i < num_rows; ++i) { |
| 274 | // x.setZero(); |
| 275 | // y.setZero(); |
| 276 | // z.setZero(); |
| 277 | // x[i] = 1.0; |
| 278 | // preconditioner_->RightMultiply(x.data(), y.data()); |
| 279 | // z = full_schur_complement |
| 280 | // .selfadjointView<Eigen::Upper>() |
| 281 | // .llt().solve(x); |
| 282 | // double max_relative_difference = |
| 283 | // ((y - z).array() / z.array()).matrix().lpNorm<Eigen::Infinity>(); |
| 284 | // EXPECT_NEAR(max_relative_difference, 0.0, kTolerance); |
| 285 | // } |
| 286 | // } |
| 287 | |
| 288 | // TEST_F(VisibilityBasedPreconditionerTest, ClusterJacobi) { |
| 289 | // options_.type = CLUSTER_JACOBI; |
| 290 | // preconditioner_.reset( |
| 291 | // new VisibilityBasedPreconditioner(*A_->block_structure(), options_)); |
| 292 | |
| 293 | // // Override the clustering to be equal number of cameras. |
| 294 | // vector<int>& cluster_membership = *get_mutable_cluster_membership(); |
| 295 | // cluster_membership.resize(num_camera_blocks_); |
| 296 | // static const int kNumClusters = 3; |
| 297 | |
| 298 | // for (int i = 0; i < num_camera_blocks_; ++i) { |
| 299 | // cluster_membership[i] = (i * kNumClusters) / num_camera_blocks_; |
| 300 | // } |
| 301 | // *get_mutable_num_clusters() = kNumClusters; |
| 302 | |
| 303 | // std::unordered_set<pair<int, int>, pair_hash>& cluster_pairs = |
| 304 | // *get_mutable_cluster_pairs(); cluster_pairs.clear(); for (int i = 0; i < |
| 305 | // kNumClusters; ++i) { |
| 306 | // cluster_pairs.insert(make_pair(i, i)); |
| 307 | // } |
| 308 | |
| 309 | // EXPECT_TRUE(IsSparsityStructureValid()); |
| 310 | // EXPECT_TRUE(PreconditionerValuesMatch()); |
| 311 | // } |
| 312 | |
| 313 | // TEST_F(VisibilityBasedPreconditionerTest, ClusterTridiagonal) { |
| 314 | // options_.type = CLUSTER_TRIDIAGONAL; |
| 315 | // preconditioner_.reset( |
| 316 | // new VisibilityBasedPreconditioner(*A_->block_structure(), options_)); |
| 317 | // static const int kNumClusters = 3; |
| 318 | |
| 319 | // // Override the clustering to be 3 clusters. |
| 320 | // vector<int>& cluster_membership = *get_mutable_cluster_membership(); |
| 321 | // cluster_membership.resize(num_camera_blocks_); |
| 322 | // for (int i = 0; i < num_camera_blocks_; ++i) { |
| 323 | // cluster_membership[i] = (i * kNumClusters) / num_camera_blocks_; |
| 324 | // } |
| 325 | // *get_mutable_num_clusters() = kNumClusters; |
| 326 | |
| 327 | // // Spanning forest has structure 0-1 2 |
| 328 | // std::unordered_set<pair<int, int>, pair_hash>& cluster_pairs = |
| 329 | // *get_mutable_cluster_pairs(); cluster_pairs.clear(); for (int i = 0; i < |
| 330 | // kNumClusters; ++i) { |
| 331 | // cluster_pairs.insert(make_pair(i, i)); |
| 332 | // } |
| 333 | // cluster_pairs.insert(make_pair(0, 1)); |
| 334 | |
| 335 | // EXPECT_TRUE(IsSparsityStructureValid()); |
| 336 | // EXPECT_TRUE(PreconditionerValuesMatch()); |
| 337 | // } |
| 338 | |
| 339 | } // namespace internal |
| 340 | } // namespace ceres |