Ceres Solver C++:分段错误:11

游侠鲍勃

我正在尝试使用 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[] = { 0.0,0.0 };

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

  return 0;
}

编译时,我得到一个Segmentation fault: 11错误。有任何想法吗?

萨米尔·阿加瓦尔

您访问的 jacobians 数组是错误的。这就是为什么。

当您添加残差块时,您告诉 Ceres 成本函数仅取决于一个大小为 2 的参数块并产生大小为 2 的残差。

jacobians 数组是一个行优先 jacobians 数组。每个参数块一个。所以在这种情况下,它的大小为 1,并且包含一个指向大小为 4 的数组的指针,该数组应该包含一个行主雅可比行列式。

你的雅可比填充代码应该改为

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章