123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- #include <vector>
- #include "ceres/ceres.h"
- #include "glog/logging.h"
- class QuadraticCostFunction
- : public ceres::SizedCostFunction<1 ,
- 1 > {
- public:
- bool Evaluate(double const* const* parameters,
- double* residuals,
- double** jacobians) const override {
- double x = parameters[0][0];
-
- residuals[0] = 10 - x;
-
-
-
-
-
-
-
-
-
-
-
-
- if (jacobians != nullptr && jacobians[0] != nullptr) {
- jacobians[0][0] = -1;
- }
- return true;
- }
- };
- int main(int argc, char** argv) {
- google::InitGoogleLogging(argv[0]);
-
-
- double x = 0.5;
- const double initial_x = x;
-
- ceres::Problem problem;
-
- ceres::CostFunction* cost_function = new QuadraticCostFunction;
- problem.AddResidualBlock(cost_function, nullptr, &x);
-
- ceres::Solver::Options options;
- options.minimizer_progress_to_stdout = true;
- ceres::Solver::Summary summary;
- ceres::Solve(options, &problem, &summary);
- std::cout << summary.BriefReport() << "\n";
- std::cout << "x : " << initial_x << " -> " << x << "\n";
- return 0;
- }
|