[英]How to use ceres-solver to solve high dimensional non-linear problem?
I need to solve the optimization problem:我需要解决优化问题:
.
.
A
and b
are known. A
和b
已知。 I use Zero
to represent A
and b
to facilate the expression in the following code.我使用
Zero
来表示A
和b
以方便以下代码中的表达式。 The error is caused by problem.AddResidualBlock(cost_function, nullptr, &X);
该错误是由
problem.AddResidualBlock(cost_function, nullptr, &X);
引起的because the third argument needs to be double type and X is a vector with 50 elements.因为第三个参数需要是 double 类型并且 X 是一个包含 50 个元素的向量。 Can you give me some advice?
你能给我一些建议吗?
#include <cmath>
#include <ceres/ceres.h>
#include <Eigen/Core>
#include <Eigen/Eigen>
#include <Eigen/Core>
#include <Eigen/Dense>
#include <Eigen/StdVector>
#define PhaseNums 25
using namespace std;
using namespace ceres;
using namespace Eigen;
struct GammaResidual
{
GammaResidual(const MatrixXf A, const VectorXf b) : A_(A), b_(b) {}
template <typename T>
bool operator()(const T* const x, T* residual) const {
residual[0] = (A_ * x[0] - b_).transpose() * (A_ * x[0] - b_);
return true;
}
private:
const MatrixXf A_;
const VectorXf b_;
};
int main()
{
MatrixXf A = MatrixXf::Zero(2 * PhaseNums, 2 * PhaseNums);
VectorXf b = VectorXf::Zero(2 * PhaseNums);
VectorXf X = VectorXf::Zero(2 * PhaseNums);
Problem problem;
CostFunction* cost_function = new AutoDiffCostFunction<GammaResidual, 1, 1>(
new GammaResidual(A, b));
problem.AddResidualBlock(cost_function, nullptr, &X);
ceres::Solver::Options options;
options.minimizer_progress_to_stdout = true;
ceres::Solver::Summary summary;
ceres::Solve(options, &problem, &summary);
cout << summary.BriefReport() << endl;
}
I guess that If your X is a vector, you need to loop through it and add a residual a residual block for each x.我猜如果你的 X 是一个向量,你需要遍历它并为每个 x 添加一个残差 a residual block。 Makes sense?
说得通?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.