This forum has been archived. All content is frozen. Please use KDE Discuss instead.

LevenbergMarquardt question

Tags: None
(comma "," separated)
Jack701
Registered Member
Posts
5
Karma
0

LevenbergMarquardt question

Fri Mar 01, 2013 8:44 am
Hi,
I've looked the code of your LevenbergMarquardt algorithm, and found quite different of the classical LM algorithm described in "Methods for non linear least square problems" by Madsen (the algorithm is page 27). Especially, it takes much less iteration to converge than other LM algorithm and i don't know why. Is there a paper describing your version of the LM algo ?
(I'am not a math guru so maybe i'm wrong)
Thanks.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: LevenbergMarquardt question

Fri Mar 01, 2013 1:08 pm
It is a port of MINPACK, so you will find more details on the underlying algorithm there:

http://devernay.free.fr/hacks/cminpack/index.html
http://en.wikipedia.org/wiki/MINPACK
igorm
Registered Member
Posts
13
Karma
0

Re: LevenbergMarquardt question

Tue Jul 01, 2014 4:57 pm
This is my code for 2 equations:
Code: Select all
struct MyFunctor
{
  int operator()(const Eigen::VectorXf &x, Eigen::VectorXf &fvec) const
  {
    // Implement y = x^2
    fvec(0) = x(0)*x(0) - 5.0 * x(0);
    fvec(1) = x(1)*x(1) - 5.0 * x(1);
    return 0;
  }
 
  int df(const Eigen::VectorXf &x, Eigen::MatrixXf &fjac) const
  {
    // Implement Jacobian
    fjac(0,0) = 2.0f * x(0) - 5.0;    fjac(1,0) = 0; 
    fjac(0,1) = 0;                    fjac(1,1) = 2.0f * x(1) - 5.0;;
    return 0;
  }
 
  int inputs() const { return 1; }
  int values() const { return 2; } // number of constraints
};
 
 
int main(int argc, char *argv[])
{
  Eigen::VectorXf x(2);
  x(0) = 4;
  x(1) = -1;
  std::cout << "x: " << x << std::endl;
 
  MyFunctor functor;
  Eigen::LevenbergMarquardt<MyFunctor, float> lm(functor);
  lm.minimize(x);
 
  std::cout << "x that minimizes the function: " << x << std::endl;
 
  return 0;
}


Q1: how can I return the value of iterations done by solver?
Q2: How can I pass some constants (for instance: a,b,c in the equations)?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: LevenbergMarquardt question

Wed Jul 02, 2014 7:02 am
Q1: cout << lm.iter;
Q2: That's typically the role of MyFunctor to store the constants.
hnokhostin
Registered Member
Posts
5
Karma
0

Re: LevenbergMarquardt question

Wed Jul 02, 2014 9:26 am
is there possible to use LM in 3D (plane fitting)?
For example: F(x,y,a1,a2) = a1*exp(x)*a2*exp(y)
igorm
Registered Member
Posts
13
Karma
0

Re: LevenbergMarquardt question

Wed Jul 02, 2014 10:40 am
Thank you for such fast response!
ggael wrote:Q2: That's typically the role of MyFunctor to store the constants.

Could you please be more detailed?
I need to solve tons of the sets of quadratic equations and they differ only by some constants:

for instance, equations for 1st set will be:
Code: Select all
fvec(0) = a0*x(0)*x(n) - 5.0 * x(0);
.....
fvec(n) = a0*x(n)*x(0) - 5.0 * x(n-5);

for the 2nd set:
Code: Select all
fvec(0) = a1*x(0)*x(n) - 5.0 * x(0);
.....
fvec(n) = a1*x(n)*x(0) - 5.0 * x(n-5);

etc...

How can pass this variable into struct? Sorry for nooby questions.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: LevenbergMarquardt question

Wed Jul 02, 2014 11:19 am
This is not related to Eigen. That's basic C/C++ practice. For instance you could add an attribute to your class with the associated getter/setter.
Code: Select all
class MyFunc {
  ...
  double m_value;
public:
  double value() const {return m_value;}
  void setValue(double v) {m_value = v;}
  ...
};

and operatro() and df() will use m_value instead of the hard-coded one.
User avatar
VictorL
Registered Member
Posts
14
Karma
0
OS

Re: LevenbergMarquardt question

Wed Jul 02, 2014 11:23 am
igorm wrote:Thank you for such fast response!
ggael wrote:Q2: That's typically the role of MyFunctor to store the constants.

Could you please be more detailed?
I need to solve tons of the sets of quadratic equations and they differ only by some constants:

How can pass this variable into struct? Sorry for nooby questions.

This question on stackoverflow is about Functors, you should read it
http://stackoverflow.com/questions/3569 ... their-uses

Bye!
igorm
Registered Member
Posts
13
Karma
0

Re: LevenbergMarquardt question

Fri Jul 04, 2014 1:30 pm
Thank you very much!

I've implemented everything that I was going to do. Results little bit different than compare to Matlab. Matlab gives better results and I dont know why. By the way, what means output "7" by the info, for instance the following code:
Code: Select all
int info = lm.minimize(x0);
std::cout << info;

prints 7 on the screen.

What does this mean? Where I can find the information about output infos?
igorm
Registered Member
Posts
13
Karma
0

Re: LevenbergMarquardt question

Fri Jul 04, 2014 1:50 pm
Oh I found in the topic the answer in the related topic here:
Code: Select all
std::string
printExitStatus (int status)
{
  switch (status)
  {
    case -2:
      return ("NotStarted");
      break;
    case -1:
      return ("Running");
      break;
    case 0:
      return ("ImproperInputParameters");
      break;
    case 1:
      return ("RelativeReductionTooSmall");
      break;
    case 2:
      return ("RelativeErrorTooSmall");
      break;
    case 3:
      return ("RelativeErrorAndReductionTooSmall");
      break;
    case 4:
      return ("CosinusTooSmall");
      break;
    case 5:
      return ("TooManyFunctionEvaluation");
      break;
    case 6:
      return ("FtolTooSmall");
      break;
    case 7:
      return ("XtolTooSmall");
      break;
    case 8:
      return ("GtolTooSmall");
      break;
    case 9:
      return ("UserAsked");
      break;
    default:
      return ("Unknown exit code");
      break;
  }
  return ("error");
}
User avatar
VictorL
Registered Member
Posts
14
Karma
0
OS

Re: LevenbergMarquardt question

Mon Jul 07, 2014 3:33 pm
Unfortunately I can't find a more detailed description of these exit statuses.
Take a look at the "documentation":
http://eigen.tuxfamily.org/dox/unsuppor ... uardt.html

You can probably improve your results by tweaking the parameters :
Code: Select all
lm.parameters.maxfev = 2000;   // Maximum number of evaluations
lm.parameters.xtol = 1.0e-10;  // Tolerance

Bye


Bookmarks



Who is online

Registered users: Bing [Bot], claydoh, Google [Bot], rblackwell, Yahoo [Bot]