add_residuals.cpp
Ceres solver ์ฌ์ฉ๋ฒ ์ ๋ฆฌ
์ด ๊ธ์ ์ํํ๋์ helloceres git ์ฝ๋๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์์ฑ๋์์ต๋๋ค
Example Code
//
// Tutorial Author: shapelim@kaist.ac.kr (์ํํ)
#include "ceres/ceres.h"
#include "glog/logging.h"
using ceres::AutoDiffCostFunction;
using ceres::CostFunction;
using ceres::Problem;
using ceres::Solver;
using ceres::Solve;
// ์์ 1. Cost function์ 2๊ฐ ์์ฑํด์ addResidualBlock ํด๋ณด๊ธฐ
// ์ฒซ ๋ฒ์งธ cost function: 0.5 (10 - x)^2
struct CostFunctor1 {
template<typename T>
bool operator()(const T *const x, T *residual) const {
residual[0] = 10.0 - x[0];
return true;
}
};
// ๋ ๋ฒ์งธ cost function: 0.5 (x)^2
struct CostFunctor2 {
template<typename T>
bool operator()(const T *const x, T *residual) const {
residual[0] = x[0];
return true;
}
};
int main(int argc, char **argv) {
google::InitGoogleLogging(argv[0]);
// ์ด๊ธฐ x ๊ฐ ์ ์ธ
double x = 0.5;
// ์ด๊ธฐ์ ๊ฐ๊ณผ optimization์ ์๋ฃํ ๊ฐ์ ํ์ธํด๋ณด๊ธฐ ์ํด ์ด๊ธฐ x ๊ฐ์ const๋ก ๊ณ ์ ์์ผ ๋
const double initial_x = x;
// {1, 1} <- ์ด ์ซ์๋ค์ ์๋ฏธ๊ฐ ๊ฐ๊ฐ Cost function์ {residual์ ์, ๋ณ์์ ์}๋ฅผ ๋ํ๋ด๋ ๊ฒ๊ณผ ๊ฐ๋ค.
Problem problem;
// ์ฒซ ๋ฒ์งธ cost function ์ ์ธ
CostFunction *cost_function1 =
new AutoDiffCostFunction<CostFunctor1, 1, 1>(new CostFunctor1);
// ๋ ๋ฒ์งธ cost function ์ ์ธ
CostFunction *cost_function2 =
new AutoDiffCostFunction<CostFunctor2, 1, 1>(new CostFunctor2);
problem.AddResidualBlock(cost_function1, NULL, &x);
problem.AddResidualBlock(cost_function2, NULL, &x);
// Run the solver!
Solver::Options options;
options.eta = 1e-4;
options.function_tolerance = 1e-10;
options.minimizer_progress_to_stdout = true;
Solver::Summary summary;
Solve(options, &problem, &summary);
std::cout << summary.BriefReport() << "\n";
std::cout << "x : " << initial_x
<< " -> " << x << "\n";
return 0;
}
Break the code
Two cost function
// ์ฒซ ๋ฒ์งธ cost function: 0.5 (10 - x)^2
struct CostFunctor1 {
template<typename T>
bool operator()(const T *const x, T *residual) const {
residual[0] = 10.0 - x[0];
return true;
}
};
// ๋ ๋ฒ์งธ cost function: 0.5 (x)^2
struct CostFunctor2 {
template<typename T>
bool operator()(const T *const x, T *residual) const {
residual[0] = x[0];
return true;
}
};
Leave a comment