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;
    }
};

Reference

Leave a comment