1

我想使用 Ceres 来优化 n 个参数的函数。如何找到这个函数的梯度尚不清楚,尽管我确实有一个明确的成本。到目前为止,我一直在使用 GSL,但我想我会尝试使用带有自动差异的 Ceres。

我看过玩具示例helloworld_analytic_diff.cc,其中他们使用 AutoDiff 来最小化函数 f(x) = 0.5 (10 - x)^2 并阅读教程,所以我想我会尝试将其扩展到二维函数 f(x,y) = (10-x)^2 +(20- y)^2,在 x, y = 10, 20 处具有全局最小值。但我对此有点卡住:

#include "ceres/ceres.h"
#include "glog/logging.h"

using ceres::AutoDiffCostFunction;
using ceres::CostFunction;
using ceres::Problem;
using ceres::Solver;
using ceres::Solve;


struct CostFunctor {
  template <typename T> bool operator()(const T* const x, T* residual) const {
      const T x1 = x[0];
      const T y1 = x[1];
      residual[0] = (10.0-x[0]) + (20.0-x[1]);

    return true;
  }
};

int main(int argc, char** argv) {
  google::InitGoogleLogging(argv[0]);

    double x[2] = {0.5, -3.0};
    const double initial_x[2] = {0.5, -3.0};
    Problem problem;

    CostFunction* cost_function = new AutoDiffCostFunction<CostFunctor, 1, 2>(new CostFunctor);
          problem.AddResidualBlock(cost_function, NULL, &x[0]);

    // Run the solver!
    Solver::Options options;
    options.minimizer_progress_to_stdout = true;
    Solver::Summary summary;
    Solve(options, &problem, &summary);

    std::cout << summary.BriefReport() << "\n";
    std::cout << "x : " << initial_x[0] << ", " << initial_x[0]
        << " -> " << x[0] << ", " << x[1]<< "\n";

    return 0;
}

但是,如果我运行它,它最终会收敛到一些不正确的东西,这取决于最初的猜测:

Ceres Solver Report: Iterations: 3, Initial cost: 5.281250e+02, Final cost: 3.667046e-16, Termination: CONVERGENCE
x : 0.5, 0.5 -> 16.75, 13.25

关于我在这里做错了什么的任何想法?非常感谢!

4

1 回答 1

0

您的成本函子是错误的。您正在解决的优化问题是

[(10.0-x) + (20.0-y)]^2

并不是

(10-x)^2 +(20- y)^2,

你应该在这里做的是有两个成本函子,如下所示:

struct CostFunctor1 {
  template <typename T> bool operator()(const T* const x, T* residual) const {
      residual[0] = 10.0-x[0];
      return true;
  }
};

struct CostFunctor2 {
  template <typename T> bool operator()(const T* const x, T* residual) const {
      residual[0] = 20.0-x[1];
      return true;
  }
};

int main(int argc, char** argv) {
  google::InitGoogleLogging(argv[0]);

    double x[2] = {0.5, -3.0};
    const double initial_x[2] = {0.5, -3.0};
    Problem problem;
    problem.AddResidualBlock(new AutoDiffCostFunction<CostFunctor1, 1, 2>(new 
    CostFunctor1), NULL, &x[0]);
    problem.AddResidualBlock(new AutoDiffCostFunction<CostFunctor2, 1, 2>(new 
    CostFunctor2), NULL, &x[0]);

    // Run the solver!
    Solver::Options options;
    options.minimizer_progress_to_stdout = true;
    Solver::Summary summary;
    Solve(options, &problem, &summary);

    std::cout << summary.BriefReport() << "\n";
    std::cout << "x : " << initial_x[0] << ", " << initial_x[0]
        << " -> " << x[0] << ", " << x[1]<< "\n";

    return 0;
}
于 2018-08-28T19:53:49.813 回答