### How to set Eigen DesnseFunctor input and value sizes for use in Eigen Levenberg Marquardt

162 观看

1回复

405 作者的声誉

Problem: I do not always know the exact size of the Jacobian or Function vector that I am going to use Levenberg Marquardt on. Therefore, I need to set the dimensions of them at compile time.

Expected: After I declare an instance of MyFunctorDense. I could set the "InputsAtCompileTime" to my input size and set "ValuesAtCompileTime" to my values size. Then my Jacobian ,aFjac, should have the dimensions tValues x tInputs, and my function vector, aH, should have the dimensions tValues x 1.

Observed:

.h file

`````` #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 file #pragma once #include "stdafx.h" #include "Main.h"

``````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

405 作者的声誉

Solution: I figured out my problem. I replaced:

``````const int m_inputs, m_values;
``````

with

``````int m_inputs, m_values;
``````

in the ".h" file this makes the member variable of the struct MyFunctorDense modifiable. So, then in the ".cpp" below the line

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

`````` Eigen::VectorXd tF(60928);
``````

because this is a test function vector of dimension 60928x1. Therefore, I could put in any arbitrary nx1 dimension.

Then below the line:

``````MyFunctorDense myDenseFunctor;
``````

``````  myDenseFunctor.m_inputs = tX.rows();