-
Notifications
You must be signed in to change notification settings - Fork 39
/
LevenbergMarquardtMinimizer.h
183 lines (159 loc) · 5.36 KB
/
LevenbergMarquardtMinimizer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/***********************************************************************
LevenbergMarquardtMinimizer - Class to implement n-dimensional least-
squares minimization using a modified Levenberg-Marquardt algorithm.
Copyright (c) 2007-2008 Oliver Kreylos
This file is part of the LiDAR processing and analysis package.
The LiDAR processing and analysis package is free software; you can
redistribute it and/or modify it under the terms of the GNU General
Public License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
The LiDAR processing and analysis package is distributed in the hope
that it will be useful, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with the LiDAR processing and analysis package; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA
***********************************************************************/
#ifndef LEVENBERGMARQUARDTMINIMIZER_INCLUDED
#define LEVENBERGMARQUARDTMINIMIZER_INCLUDED
#include <Math/Math.h>
#include <Geometry/ComponentArray.h>
#include <Geometry/Matrix.h>
template <class FitterFunctor>
class LevenbergMarquardtMinimizer
{
/* Embedded classes: */
public:
typedef FitterFunctor Fitter; // Functor class for the fitting geometry
typedef typename FitterFunctor::Scalar Scalar; // Scalar type
static const int dimension=Fitter::dimension; // Dimension of the optimization space
typedef typename FitterFunctor::Derivative Derivative; // Type for distance function derivatives
typedef Geometry::Matrix<Scalar,dimension,dimension> Matrix; // Type for matrices
typedef Geometry::ComponentArray<Scalar,dimension> Vector; // Type for vectors (in the matrix sense)
/* Methods: */
static Scalar minimize(Fitter& fitter); // Minimizes the target function by manipulating the given fitter
};
/********************************************
Methods of class LevenbergMarquardtMinimizer:
********************************************/
template <class FitterParam>
inline
typename LevenbergMarquardtMinimizer<FitterParam>::Scalar
LevenbergMarquardtMinimizer<FitterParam>::minimize(
typename LevenbergMarquardtMinimizer<FitterParam>::Fitter& fitter)
{
/* Initialize the optimizer: */
Scalar tau=Scalar(1.0e-3);
Scalar epsilon1=Scalar(1.0e-12);
Scalar epsilon2=Scalar(1.0e-12);
int maxNumIterations=1000;
/* Compute the Jacobian matrix, the error vector, and the initial target function value: */
Matrix A;
Vector g;
for(int i=0;i<dimension;++i)
{
for(int j=0;j<dimension;++j)
A(i,j)=Scalar(0);
g[i]=Scalar(0);
}
Scalar F(0);
for(size_t index=0;index<fitter.getNumPoints();++index)
{
Derivative dp=fitter.calcDistanceDerivative(index);
Scalar d=fitter.calcDistance(index);
for(int i=0;i<dimension;++i)
{
for(int j=0;j<dimension;++j)
A(i,j)+=dp[i]*dp[j];
g[i]+=dp[i]*d;
}
F+=Math::sqr(d);
}
F*=Scalar(0.5);
/* Compute the initial damping factor: */
Scalar maxA=A(0,0);
for(int i=1;i<dimension;++i)
if(maxA<A(i,i))
maxA=A(i,i);
Scalar mu=tau*maxA;
Scalar nu=Scalar(2);
/* Check for convergence: */
bool found=true;
for(int i=0;i<dimension;++i)
if(Math::abs(g[i])>epsilon1)
found=false;
for(int iteration=0;!found&&iteration<maxNumIterations;++iteration)
{
/* Calculate step direction: */
Matrix H=A;
for(int i=0;i<dimension;++i)
H(i,i)+=mu;
Vector h=g/H; // h is actually the negative of hlm in the pseudo-code
/* Check for convergence: */
if(Geometry::mag(h)<=epsilon2*(fitter.calcMag()+epsilon2))
break;
/* Try updating the current state: */
fitter.save();
fitter.increment(h); // Subtracts h instead of adding (h is negative, see above)
fitter.normalize();
/* Calculate the new target function value: */
Scalar newF(0);
for(size_t index=0;index<fitter.getNumPoints();++index)
newF+=Math::sqr(fitter.calcDistance(index));
newF*=Scalar(0.5);
/* Calculate the gain value: */
Scalar denom(0);
for(int i=0;i<dimension;++i)
denom+=h[i]*(mu*h[i]+g[i]); // Adds g instead of subtracting (h is negative, see above)
denom*=Scalar(0.5);
Scalar rho=(F-newF)/denom;
/* Accept or deny the step: */
if(rho>Scalar(0))
{
/* Compute the new Jacobian matrix and the new error vector: */
for(int i=0;i<dimension;++i)
{
for(int j=0;j<dimension;++j)
A(i,j)=Scalar(0);
g[i]=Scalar(0);
}
for(size_t index=0;index<fitter.getNumPoints();++index)
{
Derivative dp=fitter.calcDistanceDerivative(index);
Scalar d=fitter.calcDistance(index);
for(int i=0;i<dimension;++i)
{
for(int j=0;j<dimension;++j)
A(i,j)+=dp[i]*dp[j];
g[i]+=dp[i]*d;
}
}
/* Update the target function value: */
F=newF;
/* Check for convergence: */
found=true;
for(int i=0;i<dimension;++i)
if(Math::abs(g[i])>epsilon1)
found=false;
/* Update the damping factor: */
Scalar rhof=Scalar(2)*rho-Scalar(1);
Scalar factor=Scalar(1)-rhof*rhof*rhof;
if(factor<Scalar(1)/Scalar(3))
factor=Scalar(1)/Scalar(3);
mu*=factor;
nu=Scalar(2);
}
else
{
/* Deny the step: */
fitter.restore();
/* Update the damping factor: */
mu*=nu;
nu*=Scalar(2);
}
}
return F;
}
#endif