123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- #include <vector>
- #include "ceres/ceres.h"
- #include "gflags/gflags.h"
- #include "glog/logging.h"
- struct F1 {
- template <typename T>
- bool operator()(const T* const x1, const T* const x2, T* residual) const {
-
- residual[0] = x1[0] + 10.0 * x2[0];
- return true;
- }
- };
- struct F2 {
- template <typename T>
- bool operator()(const T* const x3, const T* const x4, T* residual) const {
-
- residual[0] = sqrt(5.0) * (x3[0] - x4[0]);
- return true;
- }
- };
- struct F3 {
- 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;
- }
- };
- struct F4 {
- 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;
- }
- };
- DEFINE_string(minimizer,
- "trust_region",
- "Minimizer type to use, choices are: line_search & trust_region");
- int main(int argc, char** argv) {
- GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true);
- google::InitGoogleLogging(argv[0]);
- double x1 = 3.0;
- double x2 = -1.0;
- double x3 = 0.0;
- double x4 = 1.0;
- ceres::Problem problem;
-
-
-
- problem.AddResidualBlock(
- new ceres::AutoDiffCostFunction<F1, 1, 1, 1>(new F1), nullptr, &x1, &x2);
- problem.AddResidualBlock(
- new ceres::AutoDiffCostFunction<F2, 1, 1, 1>(new F2), nullptr, &x3, &x4);
- problem.AddResidualBlock(
- new ceres::AutoDiffCostFunction<F3, 1, 1, 1>(new F3), nullptr, &x2, &x3);
- problem.AddResidualBlock(
- new ceres::AutoDiffCostFunction<F4, 1, 1, 1>(new F4), nullptr, &x1, &x4);
- ceres::Solver::Options options;
- LOG_IF(FATAL,
- !ceres::StringToMinimizerType(CERES_GET_FLAG(FLAGS_minimizer),
- &options.minimizer_type))
- << "Invalid minimizer: " << CERES_GET_FLAG(FLAGS_minimizer)
- << ", valid options are: trust_region and line_search.";
- options.max_num_iterations = 100;
- options.linear_solver_type = ceres::DENSE_QR;
- options.minimizer_progress_to_stdout = true;
-
- std::cout << "Initial x1 = " << x1
- << ", x2 = " << x2
- << ", x3 = " << x3
- << ", x4 = " << x4
- << "\n";
-
-
- ceres::Solver::Summary summary;
- ceres::Solve(options, &problem, &summary);
- std::cout << summary.FullReport() << "\n";
-
- std::cout << "Final x1 = " << x1
- << ", x2 = " << x2
- << ", x3 = " << x3
- << ", x4 = " << x4
- << "\n";
-
- return 0;
- }
|