Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
miscmaths
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
FSL
miscmaths
Commits
d3274330
Commit
d3274330
authored
17 years ago
by
Jesper Andersson
Browse files
Options
Downloads
Patches
Plain Diff
Added new functionality to SpMat class + Improved the Levenberg-Marquardt code in nonlin
parent
eebc1c58
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
SpMat.h
+1
-0
1 addition, 0 deletions
SpMat.h
nonlin.cpp
+53
-1
53 additions, 1 deletion
nonlin.cpp
with
54 additions
and
1 deletion
SpMat.h
+
1
−
0
View file @
d3274330
...
@@ -90,6 +90,7 @@ public:
...
@@ -90,6 +90,7 @@ public:
SpMat
(
unsigned
int
m
,
unsigned
int
n
)
:
_m
(
m
),
_n
(
n
),
_nz
(
0
),
_ri
(
n
),
_val
(
n
)
{}
SpMat
(
unsigned
int
m
,
unsigned
int
n
)
:
_m
(
m
),
_n
(
n
),
_nz
(
0
),
_ri
(
n
),
_val
(
n
)
{}
SpMat
(
unsigned
int
m
,
unsigned
int
n
,
const
unsigned
int
*
irp
,
const
unsigned
int
*
jcp
,
const
double
*
sp
);
SpMat
(
unsigned
int
m
,
unsigned
int
n
,
const
unsigned
int
*
irp
,
const
unsigned
int
*
jcp
,
const
double
*
sp
);
SpMat
(
const
NEWMAT
::
GeneralMatrix
&
M
);
SpMat
(
const
NEWMAT
::
GeneralMatrix
&
M
);
~
SpMat
()
{}
unsigned
int
Nrows
()
const
{
return
(
_m
);}
unsigned
int
Nrows
()
const
{
return
(
_m
);}
unsigned
int
Ncols
()
const
{
return
(
_n
);}
unsigned
int
Ncols
()
const
{
return
(
_n
);}
...
...
This diff is collapsed.
Click to expand it.
nonlin.cpp
+
53
−
1
View file @
d3274330
...
@@ -290,6 +290,58 @@ NonlinOut nonlin(const NonlinParam& p, const NonlinCF& cfo)
...
@@ -290,6 +290,58 @@ NonlinOut nonlin(const NonlinParam& p, const NonlinCF& cfo)
// Main routine for Levenberg-Marquardt optimisation
// Main routine for Levenberg-Marquardt optimisation
NonlinOut
levmar
(
const
NonlinParam
&
p
,
const
NonlinCF
&
cfo
)
{
// Calculate initial values
p
.
SetCF
(
cfo
.
cf
(
p
.
Par
()));
// Cost-function evaluated at current parameters
bool
success
=
true
;
// True if last step decreased CF
double
olambda
=
0.0
;
// How much the diagonal of H was nudged last time
ColumnVector
g
;
// Gradient
boost
::
shared_ptr
<
BFMatrix
>
H
;
// Hessian
while
(
p
.
NextIter
(
success
))
{
if
(
success
)
{
// If last attempt decreased cost-function
g
=
cfo
.
grad
(
p
.
Par
());
// Gradient evaluated at current parameters
H
=
cfo
.
hess
(
p
.
Par
(),
H
);
// Hessian evaluated at current parameters
}
for
(
int
i
=
1
;
i
<=
p
.
NPar
();
i
++
)
{
// Nudge it
if
(
p
.
GaussNewtonType
()
==
LM_LM
)
{
// If Levenberg-Marquardt
H
->
AddTo
(
i
,
i
,(
p
.
Lambda
()
-
olambda
)
*
H
->
Peek
(
i
,
i
));
}
else
if
(
p
.
GaussNewtonType
()
==
LM_L
)
{
// If Levenberg
H
->
AddTo
(
i
,
i
,
p
.
Lambda
()
-
olambda
);
}
}
ColumnVector
step
=
-
H
->
SolveForx
(
g
,
SYM_POSDEF
,
p
.
EquationSolverTol
(),
p
.
EquationSolverMaxIter
());
double
ncf
=
cfo
.
cf
(
p
.
Par
()
+
step
);
if
(
success
=
(
ncf
<
p
.
CF
()))
{
// If last step successful
olambda
=
0.0
;
// Pristine Hessian, so no need to undo old lambda
p
.
SetPar
(
p
.
Par
()
+
step
);
// Set attempt as new parameters
p
.
SetLambda
(
p
.
Lambda
()
/
10.0
);
// Decrease nudge factor
// Check for convergence based on small decrease of cf
if
(
zero_cf_diff_conv
(
p
.
CF
(),
ncf
,
p
.
FractionalCFTolerance
()))
{
p
.
SetCF
(
ncf
);
p
.
SetStatus
(
NL_CFCONV
);
return
(
p
.
Status
());
}
p
.
SetCF
(
ncf
);
// Store value of cost-function
}
else
{
// If last step was unsuccesful
olambda
=
p
.
Lambda
();
// Returning to same H, so must undo old lambda
p
.
SetLambda
(
10.0
*
p
.
Lambda
());
// Increase nudge factor
p
.
SetCF
(
p
.
CF
());
// Push another copy of best cost function value thus far
// Check for convergence based on _really_ large lambda
if
(
p
.
Lambda
()
>
p
.
LambdaConvergenceCriterion
())
{
p
.
SetStatus
(
NL_LCONV
);
return
(
p
.
Status
());
}
}
}
// Getting here means we did too many iterations
p
.
SetStatus
(
NL_MAXITER
);
return
(
p
.
Status
());
}
/*
// Main routine for Levenberg-Marquardt optimisation
NonlinOut levmar(const NonlinParam& p, const NonlinCF& cfo)
NonlinOut levmar(const NonlinParam& p, const NonlinCF& cfo)
{
{
// Calculate initial values
// Calculate initial values
...
@@ -338,7 +390,7 @@ NonlinOut levmar(const NonlinParam& p, const NonlinCF& cfo)
...
@@ -338,7 +390,7 @@ NonlinOut levmar(const NonlinParam& p, const NonlinCF& cfo)
return(p.Status());
return(p.Status());
}
}
*/
// Main routine for conjugate-gradient optimisation. The
// Main routine for conjugate-gradient optimisation. The
// implementation follows that of Numerical Recipies
// implementation follows that of Numerical Recipies
// reasonably closely.
// reasonably closely.
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment