如何在Eigen Levenberg Marquardt中设置Eigen DesnseFunctor输入和值大小

时间:2017-12-27 17:13:31

标签: c++ eigen levenberg-marquardt

问题:我并不总是知道我将使用Levenberg Marquardt的雅可比行列式或函数向量的确切大小。因此,我需要在编译时设置它们的尺寸。

预期:声明MyFunctorDense的实例后。我可以设置" InputsAtCompileTime"我的输入大小和设置" ValuesAtCompileTime"我的价值观大小。然后我的Jacobian,aFjac应该有尺寸tValues x tInputs,而我的函数向量aH应该有尺寸tValues x 1.

观察到的enter image description here

.h文件

 #pragma once

 #include "stdafx.h"
 #include <iostream>

 #include <unsupported/Eigen/LevenbergMarquardt>
 #include <unsupported/Eigen/NumericalDiff>


 //Generic functor
 template <typename _Scalar, typename _Index>
 struct MySparseFunctor
 {
    typedef _Scalar Scalar;
    typedef _Index Index;
    typedef Eigen::Matrix<Scalar,Eigen::Dynamic,1> InputType;
    typedef Eigen::Matrix<Scalar,Eigen::Dynamic,1> ValueType;
    typedef Eigen::SparseMatrix<Scalar, Eigen::ColMajor, Index> 
  JacobianType;
     typedef Eigen::SparseQR<JacobianType, Eigen::COLAMDOrdering<int> > 
  QRSolver;
    enum {
       InputsAtCompileTime = Eigen::Dynamic,
       ValuesAtCompileTime = Eigen::Dynamic
     };

    MySparseFunctor(int inputs, int values) : m_inputs(inputs), 
m_values(values) {}

    int inputs() const { return m_inputs; }
    int values() const { return m_values; }

    const int m_inputs, m_values;


};

template <typename _Scalar, int NX=Eigen::Dynamic, int NY=Eigen::Dynamic>
struct MyDenseFunctor
{
  typedef _Scalar Scalar;
  enum {
    InputsAtCompileTime = NX,
    ValuesAtCompileTime = NY
  };
  typedef Eigen::Matrix<Scalar,InputsAtCompileTime,1> InputType;
  typedef Eigen::Matrix<Scalar,ValuesAtCompileTime,1> ValueType;
  typedef Eigen::Matrix<Scalar,ValuesAtCompileTime,InputsAtCompileTime> 
JacobianType;
   typedef Eigen::ColPivHouseholderQR<JacobianType> QRSolver;
  const int m_inputs, m_values;

   MyDenseFunctor() : m_inputs(InputsAtCompileTime), 
 m_values(ValuesAtCompileTime) {}
  MyDenseFunctor(int inputs, int values) : m_inputs(inputs), 
m_values(values) {}

  int inputs() const { return m_inputs; }
  int values() const { return m_values; }

};


 struct MyFunctorSparse : MySparseFunctor<double, int>
 {
    MyFunctorSparse(void) : MySparseFunctor<double, int>(2 , 2) {}

   int operator()(const Eigen::VectorXd &aX,  //Input 
              Eigen::VectorXd &aF) const; //Output 
   int df(const InputType &aF, JacobianType& aFjac);
};


struct MyFunctorDense : MyDenseFunctor<double>
 {
   MyFunctorDense(void) : MyDenseFunctor<double>( Eigen::Dynamic , 
Eigen::Dynamic) {}

   int operator()(const InputType &aX,  //Input 
              ValueType &aF) const; //Output 

   int df(const InputType &aX, JacobianType& aFjac);
 };

.cpp文件    #pragma一次    #include&#34; stdafx.h&#34;    #include&#34; Main.h&#34;

int MyFunctorSparse::operator()(const Eigen::VectorXd &aX,        //Input 
                            Eigen::VectorXd &aF) const        //Output 
{
         //F = aX0^2 + aX1^2  
     aF(0) = aX(0)*aX(0) +  aX(1)*aX(1);
     aF(1) = 0;

  return 0;
 }

int MyFunctorDense::operator()(const InputType &aX,        //Input 
                            ValueType &aF) const       //Output 
 {
//F = aX0^2 + aX1^2
for (int i = 0; i < aF.size(); i++)
{
  aF(i) = i*aX(0)*aX(0) + i*(aX(1)-1)*(aX(1)-1);
}


  return 0;
}


 int MyFunctorSparse::df(const InputType &aX, JacobianType& aFjac)
 {
   aFjac.coeffRef(0, 0) = 2*aX(0);
   aFjac.coeffRef(0, 1) = 2*aX(1);
   aFjac.coeffRef(1, 0) = 0.0;
   aFjac.coeffRef(1, 1) = 0.0;

   return 0;
}

 int MyFunctorDense::df(const InputType &aX, JacobianType& aFjac)
{
   for(int i = 0; i< aFjac.size(); i++)
   {
   aFjac(i, 0) = 2*i*aX(0);
   aFjac(i, 1) = 2*i*(aX(1)-1);
   }
   return 0; 
}

int main(int argc, char *argv[])
{
   int input;
   std::cout << "Enter 1 to run LM with DenseFunctor, Enter 2 to run LM with 
SparseFunctor: " << std::endl;
std::cin >> input;

   Eigen::VectorXd tX(2);
   tX(0) = 10;
   tX(1) = 0.5;
   int tInputs = tX.rows();
   int tValues = 60928;

   std::cout << "tX: " << tX << std::endl;

   if (input == 1)
   {
  MyFunctorDense myDenseFunctor;
  tInputs = myDenseFunctor.inputs();
  tValues = myDenseFunctor.values();

  std::cout << "tInputs  : " << tInputs << std::endl;
  std::cout << "tValues  : " << tValues << std::endl;

  Eigen::LevenbergMarquardt<MyFunctorDense> lm(myDenseFunctor);
  lm.setMaxfev(30);
  lm.setXtol(1e-5);
  lm.minimize(tX);
   }

  if (input == 2)
  {
  MyFunctorSparse myFunctorSparse;
  //Eigen::NumericalDiff<MyFunctor> numDiff(myFunctor);
  //Eigen::LevenbergMarquardt<Eigen::NumericalDiff<MyFunctor>,double> 
  lm(numDiff);

  Eigen::LevenbergMarquardt<MyFunctorSparse> lm(myFunctorSparse);
  lm.setMaxfev(2000);
  lm.setXtol(1e-10);

  lm.minimize(tX);
  }

   std::cout << "tX minimzed: " << tX << std::endl;

   return 0;
}

1 个答案:

答案 0 :(得分:0)

解决方案:我发现了我的问题。我换了:

const int m_inputs, m_values;

int m_inputs, m_values;

在“.h”文件中,这使得结构MyFunctorDense的成员变量可以修改。那么,然后在行下面的“.cpp”

std::cout << "tX: " << tX << std::endl;

我补充说:

 Eigen::VectorXd tF(60928);

因为这是尺寸为60928x1的测试函数向量。因此,我可以放入任意nx1维度。

然后在线下:

MyFunctorDense myDenseFunctor; 

我补充说:

  myDenseFunctor.m_inputs = tX.rows();
  myDenseFunctor.m_values = tF.rows();

现在我得到了结果:

enter image description here