使用Ceres解算器求解非线性系统:编译问题

时间:2019-06-03 20:47:46

标签: c++ c++11 ceres-solver

我正在尝试使用Google的Ceres Solver解决非线性系统。以下示例来自此页面:http://terpconnect.umd.edu/~petersd/460/html/newtonex1z.html

我首先创建一个名为MatlabExample的类,在其中计算residualsjacobians

class MatlabExample
: public SizedCostFunction<2,2> {
public:
virtual ~MatlabExample() {}
virtual bool Evaluate(double const* const* parameters,
                    double* residuals,
                    double** jacobians) const {

double x1 = parameters[0][0];
double x2 = parameters[0][1];

residuals[0] = 2*x1+x1*x2-2;
residuals[1] = 2*x2-x1*pow(x2,2)-2 ;

if (jacobians != NULL && jacobians[0] != NULL) {
  jacobians[0][0] = 2+x2;
  jacobians[0][1] = x1;
  jacobians[1][0] = -pow(x2,2);
  jacobians[1][1] = 2-2*x1*x2;
}

return true;

} };

主文件如下:

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

  double x[1][2]={{1.00,1.00}};

  Problem problem;
  CostFunction* cost_function = new MatlabExample;
  problem.AddResidualBlock(cost_function, NULL, &x);
  Solver::Options options;
  options.minimizer_progress_to_stdout = true;
  Solver::Summary summary;
  Solve(options, &problem, &summary);

  std::cout << summary.BriefReport() << "\n";
  std::cout << "x[0]= " << x[0][0]
            << "; x[1]= " << x[0][1] << "\n";

  return 0;
}

编译时出现以下错误:

mytest_ceres.cpp:262:11: error: no matching member function for call to     'AddResidualBlock'
problem.AddResidualBlock(cost_function, NULL, &x);
/usr/local/include/ceres/problem.h:222:19: note: candidate function not   viable: cannot convert argument of incomplete type
  'void *' to 'const std::vector<double *>' for 3rd argument
ResidualBlockId AddResidualBlock(

这是我第一次使用Ceres Solver,所以我确定自己缺少某些东西...但是我不知道是什么。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您错误地调用了AddResidualBlock。改为这样做。

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

  double x[]= {1.00,1.00};

  Problem problem;
  CostFunction* cost_function = new MatlabExample;
  problem.AddResidualBlock(cost_function, NULL, x);
  Solver::Options options;
  options.minimizer_progress_to_stdout = true;
  Solver::Summary summary;
  Solve(options, &problem, &summary);

  std::cout << summary.BriefReport() << "\n";
  std::cout << "x[0]= " << x[0]
            << "; x[1]= " << x[1] << "\n";

  return 0;
}