简体   繁体   English

如何使用ceres-solver解决高维非线性问题?

[英]How to use ceres-solver to solve high dimensional non-linear problem?

I need to solve the optimization problem:我需要解决优化问题: 在此处输入图像描述 . . A and b are known. Ab已知。 I use Zero to represent A and b to facilate the expression in the following code.我使用Zero来表示Ab以方便以下代码中的表达式。 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.

相关问题 Ceres Solver:对非线性最小二乘法使用平滑逼近 - Ceres Solver : using smooth approximations for non-linear least squares 我使用Ceres Solver时如何解决以下问题 - How to solve the following problem when I use Ceres Solver 如何在 CMake 中链接第 3 方库(ceres-solver) - How to link 3rd party library (ceres-solver) in CMake Ceres-solver 给出错误的三边形结果 - Ceres-solver giving wrong results for trilateraion 修改平方和[ceres-solver] - modify the squared sum [ceres-solver] Ceres-Solver成本函数继承错误:模板可能不是虚拟的 - Ceres-Solver cost function inheritance error: Templates may not be virtual 匹配Eigen和Ceres-Solver版本的最简单方法是什么? - What is the simplest way to match the versions for Eigen and Ceres-Solver? 如何修复 CERES_USE_OPENMP、CERES_USE_CXX11_THREADS 或 CERES_NO_THREADS 中的错误之一必须在 Ceres Solver Android 中定义 - How to Fix the Error One of CERES_USE_OPENMP, CERES_USE_CXX11_THREADS or CERES_NO_THREADS must be defined in Ceres Solver Android 在centOS7上安装ceres-solver并让helloworld.cc运行 - Installing ceres-solver on centOS7 and getting helloworld.cc to work 谷神胶(Ceres-Solver):残留的杀菌剂是否有用? 还有哪些其他选择? - Ceres-Solver: is a mutable used by a residual functor good pratice? What are other alternatives?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM