[C++]ceres编译完成后测试代码
/第一部分:构建代价函数 struct CostFunctor {template <typename T>bool operator()(const T* const x, T* residual) const {residual[0] = T(10.0) - x[0];} };//主函数 int main(int argc, char** argv) {google::InitGoogleLo
#include <iostream> #include <ceres/ceres.h>
using namespace std; using namespace ceres; //第一部分:构建代价函数 struct CostFunctor { template <typename T> bool operator()(const T* const x, T* residual) const { residual[0] = T(10.0) - x[0]; return true; } };
//主函数 int main(int argc, char** argv) { google::InitGoogleLogging(argv[0]);
// 寻优参数x的初始值,为5 double initial_x = 5.0; double x = initial_x;
// 第二部分:构建寻优问题 Problem problem; CostFunction* cost_function = new AutoDiffCostFunction<CostFunctor, 1, 1>(new CostFunctor); //使用自动求导,将之前的代价函数结构体传入,第一个1是输出维度,即残差的维度,第二个1是输入维度,即待寻优参数x的维度。 problem.AddResidualBlock(cost_function, NULL, &x); //向问题中添加误差项,本问题比较简单,添加一个就行。
//第三部分: 配置并运行求解器 Solver::Options options; options.linear_solver_type = ceres::DENSE_QR; //配置增量方程的解法 options.minimizer_progress_to_stdout = true;//输出到cout Solver::Summary summary;//优化信息 Solve(options, &problem, &summary);//求解!!!
std::cout << summary.BriefReport() << "\n";//输出优化的简要信息 //最终结果 std::cout << "x : " << initial_x << " -> " << x << "\n"; return 0; }
如果提示M_2_SQRTPI”: 找不到标识符,则需要
解决方法(二选一)
- 关闭
标准符合性或者符合模式选项,即设置为否

- 在属性->预处理器下添加宏
_USE_MATH_DEFINES。注意,最佳方案就是在属性页面里添加此宏。如果只是在项目的某个文件头部加了一句#define _USE_MATH_DEFINES,很可能是没用的。

最后测试结果:

更多推荐



所有评论(0)