123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- #include <cmath>
- #include <cstdlib>
- #include "ceres/autodiff_cost_function.h"
- #include "ceres/internal/config.h"
- #include "ceres/problem.h"
- #include "ceres/solver.h"
- #include "ceres/test_util.h"
- #include "ceres/types.h"
- #include "glog/logging.h"
- #include "gtest/gtest.h"
- namespace ceres::internal {
- class PowellsFunction {
- public:
- PowellsFunction() {
- x_[0] = 3.0;
- x_[1] = -1.0;
- x_[2] = 0.0;
- x_[3] = 1.0;
- problem_.AddResidualBlock(
- new AutoDiffCostFunction<F1, 1, 1, 1>(new F1), nullptr, &x_[0], &x_[1]);
- problem_.AddResidualBlock(
- new AutoDiffCostFunction<F2, 1, 1, 1>(new F2), nullptr, &x_[2], &x_[3]);
- problem_.AddResidualBlock(
- new AutoDiffCostFunction<F3, 1, 1, 1>(new F3), nullptr, &x_[1], &x_[2]);
- problem_.AddResidualBlock(
- new AutoDiffCostFunction<F4, 1, 1, 1>(new F4), nullptr, &x_[0], &x_[3]);
-
- options_.linear_solver_type = ceres::DENSE_QR;
- options_.max_num_iterations = 10;
- options_.num_threads = 1;
- }
- Problem* mutable_problem() { return &problem_; }
- Solver::Options* mutable_solver_options() { return &options_; }
- static double kResidualTolerance;
- private:
-
-
- class F1 {
- public:
- template <typename T>
- bool operator()(const T* const x1, const T* const x2, T* residual) const {
-
- *residual = x1[0] + 10.0 * x2[0];
- return true;
- }
- };
- class F2 {
- public:
- template <typename T>
- bool operator()(const T* const x3, const T* const x4, T* residual) const {
-
- *residual = sqrt(5.0) * (x3[0] - x4[0]);
- return true;
- }
- };
- class F3 {
- public:
- template <typename T>
- bool operator()(const T* const x2, const T* const x3, T* residual) const {
-
- residual[0] = (x2[0] - 2.0 * x3[0]) * (x2[0] - 2.0 * x3[0]);
- return true;
- }
- };
- class F4 {
- public:
- template <typename T>
- bool operator()(const T* const x1, const T* const x4, T* residual) const {
-
- residual[0] = sqrt(10.0) * (x1[0] - x4[0]) * (x1[0] - x4[0]);
- return true;
- }
- };
- double x_[4];
- Problem problem_;
- Solver::Options options_;
- };
- double PowellsFunction::kResidualTolerance = 1e-8;
- using PowellTest = SystemTest<PowellsFunction>;
- TEST_F(PowellTest, DenseQR) {
- PowellsFunction powells_function;
- Solver::Options* options = powells_function.mutable_solver_options();
- options->linear_solver_type = DENSE_QR;
- RunSolverForConfigAndExpectResidualsMatch(*options,
- powells_function.mutable_problem());
- }
- TEST_F(PowellTest, DenseNormalCholesky) {
- PowellsFunction powells_function;
- Solver::Options* options = powells_function.mutable_solver_options();
- options->linear_solver_type = DENSE_NORMAL_CHOLESKY;
- RunSolverForConfigAndExpectResidualsMatch(*options,
- powells_function.mutable_problem());
- }
- TEST_F(PowellTest, DenseSchur) {
- PowellsFunction powells_function;
- Solver::Options* options = powells_function.mutable_solver_options();
- options->linear_solver_type = DENSE_SCHUR;
- RunSolverForConfigAndExpectResidualsMatch(*options,
- powells_function.mutable_problem());
- }
- TEST_F(PowellTest, IterativeSchurWithJacobi) {
- PowellsFunction powells_function;
- Solver::Options* options = powells_function.mutable_solver_options();
- options->linear_solver_type = ITERATIVE_SCHUR;
- options->sparse_linear_algebra_library_type = NO_SPARSE;
- options->preconditioner_type = JACOBI;
- RunSolverForConfigAndExpectResidualsMatch(*options,
- powells_function.mutable_problem());
- }
- #ifndef CERES_NO_SUITESPARSE
- TEST_F(PowellTest, SparseNormalCholeskyUsingSuiteSparse) {
- PowellsFunction powells_function;
- Solver::Options* options = powells_function.mutable_solver_options();
- options->linear_solver_type = SPARSE_NORMAL_CHOLESKY;
- options->sparse_linear_algebra_library_type = SUITE_SPARSE;
- RunSolverForConfigAndExpectResidualsMatch(*options,
- powells_function.mutable_problem());
- }
- #endif
- #ifndef CERES_NO_ACCELERATE_SPARSE
- TEST_F(PowellTest, SparseNormalCholeskyUsingAccelerateSparse) {
- PowellsFunction powells_function;
- Solver::Options* options = powells_function.mutable_solver_options();
- options->linear_solver_type = SPARSE_NORMAL_CHOLESKY;
- options->sparse_linear_algebra_library_type = ACCELERATE_SPARSE;
- RunSolverForConfigAndExpectResidualsMatch(*options,
- powells_function.mutable_problem());
- }
- #endif
- #ifdef CERES_USE_EIGEN_SPARSE
- TEST_F(PowellTest, SparseNormalCholeskyUsingEigenSparse) {
- PowellsFunction powells_function;
- Solver::Options* options = powells_function.mutable_solver_options();
- options->linear_solver_type = SPARSE_NORMAL_CHOLESKY;
- options->sparse_linear_algebra_library_type = EIGEN_SPARSE;
- RunSolverForConfigAndExpectResidualsMatch(*options,
- powells_function.mutable_problem());
- }
- #endif
- }
|