使用谷神星优化多维函数
Optimising multi-dimensional functions with 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
关于我在这里做错了什么有什么想法吗? 非常感谢!
您的成本函子是错误的。 您正在解决的优化问题是
[(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;
}
相关文章:
- 使用仅使用一次的变量调用的复制构造函数.这可能是通过调用move构造函数进行编译器优化的情况吗
- 纯函数,为什么没有优化
- 线性优化目标函数中的绝对值
- 这个C++编译器优化(在自身的实例上调用对象自己的构造函数)的名称是什么,它是如何工作的?
- 何时允许编译器优化复制构造函数
- C++延迟后的优化器调用函数
- 未使用的C++未优化的静态成员函数/变量
- 我应该保留这个函数来查找第 n 个素数还是可以优化?
- 如何使用 g2o 优化多约束函数
- GCC 能否优化具有相同主体的函数的代码大小?
- 对于优化级别为 0 的 std::vector,析构函数被调用两次
- 尾递归函数未被 g++ 优化
- 函数优化传递
- 优化在网格图中查找哈密尔循环的函数?
- C++将 lambda 函数另存为成员变量,而不使用函数指针进行优化
- 使用谷神星优化多维函数
- 创建一个始终返回零但优化器不知道的函数
- C++生成器 10.2 基于函数的优化状态"unknown attribute 'optimize' ignored"
- 使用函数优化数组中的代码最大元素
- C++11移动构造函数优化