123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- #include <iostream>
- #include "Eigen/Core"
- #include "ceres/ceres.h"
- #include "glog/logging.h"
- const int kNumObservations = 67;
- const double data[] = {
- 0.000000e+00, 1.133898e+00,
- 7.500000e-02, 1.334902e+00,
- 1.500000e-01, 1.213546e+00,
- 2.250000e-01, 1.252016e+00,
- 3.000000e-01, 1.392265e+00,
- 3.750000e-01, 1.314458e+00,
- 4.500000e-01, 1.472541e+00,
- 5.250000e-01, 1.536218e+00,
- 6.000000e-01, 1.355679e+00,
- 6.750000e-01, 1.463566e+00,
- 7.500000e-01, 1.490201e+00,
- 8.250000e-01, 1.658699e+00,
- 9.000000e-01, 1.067574e+00,
- 9.750000e-01, 1.464629e+00,
- 1.050000e+00, 1.402653e+00,
- 1.125000e+00, 1.713141e+00,
- 1.200000e+00, 1.527021e+00,
- 1.275000e+00, 1.702632e+00,
- 1.350000e+00, 1.423899e+00,
- 1.425000e+00, 1.543078e+00,
- 1.500000e+00, 1.664015e+00,
- 1.575000e+00, 1.732484e+00,
- 1.650000e+00, 1.543296e+00,
- 1.725000e+00, 1.959523e+00,
- 1.800000e+00, 1.685132e+00,
- 1.875000e+00, 1.951791e+00,
- 1.950000e+00, 2.095346e+00,
- 2.025000e+00, 2.361460e+00,
- 2.100000e+00, 2.169119e+00,
- 2.175000e+00, 2.061745e+00,
- 2.250000e+00, 2.178641e+00,
- 2.325000e+00, 2.104346e+00,
- 2.400000e+00, 2.584470e+00,
- 2.475000e+00, 1.914158e+00,
- 2.550000e+00, 2.368375e+00,
- 2.625000e+00, 2.686125e+00,
- 2.700000e+00, 2.712395e+00,
- 2.775000e+00, 2.499511e+00,
- 2.850000e+00, 2.558897e+00,
- 2.925000e+00, 2.309154e+00,
- 3.000000e+00, 2.869503e+00,
- 3.075000e+00, 3.116645e+00,
- 3.150000e+00, 3.094907e+00,
- 3.225000e+00, 2.471759e+00,
- 3.300000e+00, 3.017131e+00,
- 3.375000e+00, 3.232381e+00,
- 3.450000e+00, 2.944596e+00,
- 3.525000e+00, 3.385343e+00,
- 3.600000e+00, 3.199826e+00,
- 3.675000e+00, 3.423039e+00,
- 3.750000e+00, 3.621552e+00,
- 3.825000e+00, 3.559255e+00,
- 3.900000e+00, 3.530713e+00,
- 3.975000e+00, 3.561766e+00,
- 4.050000e+00, 3.544574e+00,
- 4.125000e+00, 3.867945e+00,
- 4.200000e+00, 4.049776e+00,
- 4.275000e+00, 3.885601e+00,
- 4.350000e+00, 4.110505e+00,
- 4.425000e+00, 4.345320e+00,
- 4.500000e+00, 4.161241e+00,
- 4.575000e+00, 4.363407e+00,
- 4.650000e+00, 4.161576e+00,
- 4.725000e+00, 4.619728e+00,
- 4.800000e+00, 4.737410e+00,
- 4.875000e+00, 4.727863e+00,
- 4.950000e+00, 4.669206e+00,
- };
- class MyEvaluationCallback : public ceres::EvaluationCallback {
- public:
-
-
- MyEvaluationCallback(const double& m, const double& c) : m_(m), c_(c) {
- x_ = Eigen::VectorXd::Zero(kNumObservations);
- y_ = Eigen::VectorXd::Zero(kNumObservations);
- residuals_ = Eigen::VectorXd::Zero(kNumObservations);
- jacobians_ = Eigen::MatrixXd::Zero(kNumObservations, 2);
- for (int i = 0; i < kNumObservations; ++i) {
- x_[i] = data[2 * i];
- y_[i] = data[2 * i + 1];
- }
- PrepareForEvaluation(true, true);
- }
- void PrepareForEvaluation(bool evaluate_jacobians,
- bool new_evaluation_point) final {
- if (new_evaluation_point) {
- ComputeResidualAndJacobian(evaluate_jacobians);
- jacobians_are_stale_ = !evaluate_jacobians;
- } else {
- if (evaluate_jacobians && jacobians_are_stale_) {
- ComputeResidualAndJacobian(evaluate_jacobians);
- jacobians_are_stale_ = false;
- }
- }
- }
- const Eigen::VectorXd& residuals() const { return residuals_; }
- const Eigen::MatrixXd& jacobians() const { return jacobians_; }
- bool jacobians_are_stale() const { return jacobians_are_stale_; }
- private:
- void ComputeResidualAndJacobian(bool evaluate_jacobians) {
- residuals_ = -(m_ * x_.array() + c_).exp();
- if (evaluate_jacobians) {
- jacobians_.col(0) = residuals_.array() * x_.array();
- jacobians_.col(1) = residuals_;
- }
- residuals_ += y_;
- }
- const double& m_;
- const double& c_;
- Eigen::VectorXd x_;
- Eigen::VectorXd y_;
- Eigen::VectorXd residuals_;
- Eigen::MatrixXd jacobians_;
-
-
-
- bool jacobians_are_stale_ = true;
- };
- class CostAndJacobianCopyingCostFunction
- : public ceres::SizedCostFunction<1, 1, 1> {
- public:
- CostAndJacobianCopyingCostFunction(
- int index, const MyEvaluationCallback& evaluation_callback)
- : index_(index), evaluation_callback_(evaluation_callback) {}
- ~CostAndJacobianCopyingCostFunction() override = default;
- bool Evaluate(double const* const* parameters,
- double* residuals,
- double** jacobians) const final {
- residuals[0] = evaluation_callback_.residuals()(index_);
- if (!jacobians) return true;
-
- CHECK(!evaluation_callback_.jacobians_are_stale());
- if (jacobians[0] != nullptr)
- jacobians[0][0] = evaluation_callback_.jacobians()(index_, 0);
- if (jacobians[1] != nullptr)
- jacobians[1][0] = evaluation_callback_.jacobians()(index_, 1);
- return true;
- }
- private:
- int index_ = -1;
- const MyEvaluationCallback& evaluation_callback_;
- };
- int main(int argc, char** argv) {
- google::InitGoogleLogging(argv[0]);
- const double initial_m = 0.0;
- const double initial_c = 0.0;
- double m = initial_m;
- double c = initial_c;
- MyEvaluationCallback evaluation_callback(m, c);
- ceres::Problem::Options problem_options;
- problem_options.evaluation_callback = &evaluation_callback;
- ceres::Problem problem(problem_options);
- for (int i = 0; i < kNumObservations; ++i) {
- problem.AddResidualBlock(
- new CostAndJacobianCopyingCostFunction(i, evaluation_callback),
- nullptr,
- &m,
- &c);
- }
- ceres::Solver::Options options;
- options.max_num_iterations = 25;
- options.linear_solver_type = ceres::DENSE_QR;
- options.minimizer_progress_to_stdout = true;
- ceres::Solver::Summary summary;
- ceres::Solve(options, &problem, &summary);
- std::cout << summary.BriefReport() << "\n";
- std::cout << "Initial m: " << initial_m << " c: " << initial_c << "\n";
- std::cout << "Final m: " << m << " c: " << c << "\n";
- return 0;
- }
|