【发布时间】:2019-10-20 09:38:34
【问题描述】:
我正在尝试使用 Google 的 Ceres Solver 求解非线性系统。下面的例子来自这个页面:http://terpconnect.umd.edu/~petersd/460/html/newtonex1z.html
我首先创建了一个名为MatlabExample 的类,我在其中计算residuals 和jacobians:
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 错误。有什么想法吗?
【问题讨论】:
-
double const* const* parameters- 哇。为什么要这么做?为什么不直接传递std::array或std::vector(可能通过 const 引用)? -
吹毛求疵:更喜欢
nullptr而不是NULL。我们不再生活在 C++98 的土地上。 -
因为抽象类'SizedCostFunction'以这种方式定义了虚函数'Evaluate'。我无法改变它,因为它来自 Ceres Solver。
-
jacobians的空检查实际上毫无价值。这并不能充分确定jacobians对您的使用方式是否有效。
标签: c++ optimization google-analytics mathematical-optimization ceres-solver