define and use struct Parameters

This commit is contained in:
Thomas Capricelli 2009-08-25 21:50:01 +02:00
parent d13bcdc891
commit e465ea82e1
3 changed files with 187 additions and 147 deletions

View File

@ -17,6 +17,16 @@ public:
UserAksed = 6 UserAksed = 6
}; };
struct Parameters {
Parameters()
: factor(Scalar(100.))
, maxfev(1000)
, xtol(ei_sqrt(epsilon<Scalar>())) {}
Scalar factor;
int maxfev; // maximum number of function evaluation
Scalar xtol;
};
Status solve( Status solve(
Matrix< Scalar, Dynamic, 1 > &x, Matrix< Scalar, Dynamic, 1 > &x,
const Scalar tol = ei_sqrt(epsilon<Scalar>()) const Scalar tol = ei_sqrt(epsilon<Scalar>())
@ -25,10 +35,8 @@ public:
Matrix< Scalar, Dynamic, 1 > &x, Matrix< Scalar, Dynamic, 1 > &x,
int &nfev, int &njev, int &nfev, int &njev,
Matrix< Scalar, Dynamic, 1 > &diag, Matrix< Scalar, Dynamic, 1 > &diag,
const int mode=1, const Parameters &parameters,
const int maxfev = 1000, const int mode=1
const Scalar factor = Scalar(100.),
const Scalar xtol = ei_sqrt(epsilon<Scalar>())
); );
Status solveNumericalDiff( Status solveNumericalDiff(
@ -39,12 +47,10 @@ public:
Matrix< Scalar, Dynamic, 1 > &x, Matrix< Scalar, Dynamic, 1 > &x,
int &nfev, int &nfev,
Matrix< Scalar, Dynamic, 1 > &diag, Matrix< Scalar, Dynamic, 1 > &diag,
const Parameters &parameters,
const int mode=1, const int mode=1,
int nb_of_subdiagonals = -1, int nb_of_subdiagonals = -1,
int nb_of_superdiagonals = -1, int nb_of_superdiagonals = -1,
const int maxfev = 2000,
const Scalar factor = Scalar(100.),
const Scalar xtol = ei_sqrt(epsilon<Scalar>()),
const Scalar epsfcn = Scalar(0.) const Scalar epsfcn = Scalar(0.)
); );
@ -68,6 +74,7 @@ HybridNonLinearSolver<FunctorType,Scalar>::solve(
const int n = x.size(); const int n = x.size();
int nfev=0, njev=0; int nfev=0, njev=0;
Matrix< Scalar, Dynamic, 1> diag; Matrix< Scalar, Dynamic, 1> diag;
Parameters parameters;
/* check the input parameters for errors. */ /* check the input parameters for errors. */
if (n <= 0 || tol < 0.) { if (n <= 0 || tol < 0.) {
@ -75,15 +82,15 @@ HybridNonLinearSolver<FunctorType,Scalar>::solve(
return ImproperInputParameters; return ImproperInputParameters;
} }
parameters.maxfev = 100*(n+1);
parameters.xtol = tol;
diag.setConstant(n, 1.); diag.setConstant(n, 1.);
return solve( return solve(
x, x,
nfev, njev, nfev, njev,
diag, diag,
2, parameters,
(n+1)*100, 2
100.,
tol
); );
} }
@ -96,10 +103,8 @@ HybridNonLinearSolver<FunctorType,Scalar>::solve(
int &nfev, int &nfev,
int &njev, int &njev,
Matrix< Scalar, Dynamic, 1 > &diag, Matrix< Scalar, Dynamic, 1 > &diag,
const int mode, const Parameters &parameters,
const int maxfev, const int mode
const Scalar factor,
const Scalar xtol
) )
{ {
const int n = x.size(); const int n = x.size();
@ -133,7 +138,7 @@ HybridNonLinearSolver<FunctorType,Scalar>::solve(
/* check the input parameters for errors. */ /* check the input parameters for errors. */
if (n <= 0 || xtol < 0. || maxfev <= 0 || factor <= 0. ) if (n <= 0 || parameters.xtol < 0. || parameters.maxfev <= 0 || parameters.factor <= 0. )
return RelativeErrorTooSmall; return RelativeErrorTooSmall;
if (mode == 2) if (mode == 2)
for (j = 0; j < n; ++j) for (j = 0; j < n; ++j)
@ -187,9 +192,9 @@ HybridNonLinearSolver<FunctorType,Scalar>::solve(
wa3 = diag.cwise() * x; wa3 = diag.cwise() * x;
xnorm = wa3.stableNorm(); xnorm = wa3.stableNorm();
delta = factor * xnorm; delta = parameters.factor * xnorm;
if (delta == 0.) if (delta == 0.)
delta = factor; delta = parameters.factor;
} }
/* form (q transpose)*fvec and store in qtf. */ /* form (q transpose)*fvec and store in qtf. */
@ -326,12 +331,12 @@ HybridNonLinearSolver<FunctorType,Scalar>::solve(
/* test for convergence. */ /* test for convergence. */
if (delta <= xtol * xnorm || fnorm == 0.) if (delta <= parameters.xtol * xnorm || fnorm == 0.)
return RelativeErrorTooSmall; return RelativeErrorTooSmall;
/* tests for termination and stringent tolerances. */ /* tests for termination and stringent tolerances. */
if (nfev >= maxfev) if (nfev >= parameters.maxfev)
return TooManyFunctionEvaluation; return TooManyFunctionEvaluation;
/* Computing MAX */ /* Computing MAX */
if (Scalar(.1) * std::max(Scalar(.1) * delta, pnorm) <= epsilon<Scalar>() * xnorm) if (Scalar(.1) * std::max(Scalar(.1) * delta, pnorm) <= epsilon<Scalar>() * xnorm)
@ -384,6 +389,7 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveNumericalDiff(
const int n = x.size(); const int n = x.size();
int nfev=0; int nfev=0;
Matrix< Scalar, Dynamic, 1> diag; Matrix< Scalar, Dynamic, 1> diag;
Parameters parameters;
/* check the input parameters for errors. */ /* check the input parameters for errors. */
if (n <= 0 || tol < 0.) { if (n <= 0 || tol < 0.) {
@ -391,16 +397,18 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveNumericalDiff(
return ImproperInputParameters; return ImproperInputParameters;
} }
parameters.maxfev = 200*(n+1);
parameters.xtol = tol;
diag.setConstant(n, 1.); diag.setConstant(n, 1.);
return solveNumericalDiff( return solveNumericalDiff(
x, x,
nfev, nfev,
diag, diag,
parameters,
2, 2,
-1, -1, -1, -1,
(n+1)*200, Scalar(0.)
100.,
tol, Scalar(0.)
); );
} }
@ -411,12 +419,10 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveNumericalDiff(
Matrix< Scalar, Dynamic, 1 > &x, Matrix< Scalar, Dynamic, 1 > &x,
int &nfev, int &nfev,
Matrix< Scalar, Dynamic, 1 > &diag, Matrix< Scalar, Dynamic, 1 > &diag,
const Parameters &parameters,
const int mode, const int mode,
int nb_of_subdiagonals, int nb_of_subdiagonals,
int nb_of_superdiagonals, int nb_of_superdiagonals,
const int maxfev,
const Scalar factor,
const Scalar xtol,
const Scalar epsfcn const Scalar epsfcn
) )
{ {
@ -454,7 +460,7 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveNumericalDiff(
/* check the input parameters for errors. */ /* check the input parameters for errors. */
if (n <= 0 || xtol < 0. || maxfev <= 0 || nb_of_subdiagonals < 0 || nb_of_superdiagonals < 0 || factor <= 0. ) if (n <= 0 || parameters.xtol < 0. || parameters.maxfev <= 0 || nb_of_subdiagonals < 0 || nb_of_superdiagonals < 0 || parameters.factor <= 0. )
return RelativeErrorTooSmall; return RelativeErrorTooSmall;
if (mode == 2) if (mode == 2)
for (j = 0; j < n; ++j) for (j = 0; j < n; ++j)
@ -514,9 +520,9 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveNumericalDiff(
wa3 = diag.cwise() * x; wa3 = diag.cwise() * x;
xnorm = wa3.stableNorm(); xnorm = wa3.stableNorm();
delta = factor * xnorm; delta = parameters.factor * xnorm;
if (delta == 0.) if (delta == 0.)
delta = factor; delta = parameters.factor;
} }
/* form (q transpose)*fvec and store in qtf. */ /* form (q transpose)*fvec and store in qtf. */
@ -653,12 +659,12 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveNumericalDiff(
/* test for convergence. */ /* test for convergence. */
if (delta <= xtol * xnorm || fnorm == 0.) if (delta <= parameters.xtol * xnorm || fnorm == 0.)
return RelativeErrorTooSmall; return RelativeErrorTooSmall;
/* tests for termination and stringent tolerances. */ /* tests for termination and stringent tolerances. */
if (nfev >= maxfev) if (nfev >= parameters.maxfev)
return TooManyFunctionEvaluation; return TooManyFunctionEvaluation;
/* Computing MAX */ /* Computing MAX */
if (Scalar(.1) * std::max(Scalar(.1) * delta, pnorm) <= epsilon<Scalar>() * xnorm) if (Scalar(.1) * std::max(Scalar(.1) * delta, pnorm) <= epsilon<Scalar>() * xnorm)

View File

@ -1,5 +1,4 @@
template<typename FunctorType, typename Scalar=double> template<typename FunctorType, typename Scalar=double>
class LevenbergMarquardt class LevenbergMarquardt
{ {
@ -21,6 +20,20 @@ public:
UserAsked = 9 UserAsked = 9
}; };
struct Parameters {
Parameters()
: factor(Scalar(100.))
, maxfev(400)
, ftol(ei_sqrt(epsilon<Scalar>()))
, xtol(ei_sqrt(epsilon<Scalar>()))
, gtol(Scalar(0.)) { }
Scalar factor;
int maxfev; // maximum number of function evaluation
Scalar ftol;
Scalar xtol;
Scalar gtol;
};
Status minimize( Status minimize(
Matrix< Scalar, Dynamic, 1 > &x, Matrix< Scalar, Dynamic, 1 > &x,
const Scalar tol = ei_sqrt(epsilon<Scalar>()) const Scalar tol = ei_sqrt(epsilon<Scalar>())
@ -31,12 +44,8 @@ public:
int &nfev, int &nfev,
int &njev, int &njev,
Matrix< Scalar, Dynamic, 1 > &diag, Matrix< Scalar, Dynamic, 1 > &diag,
const int mode=1, const Parameters &parameters,
const Scalar factor = Scalar(100.), const int mode=1
const int maxfev = 400,
const Scalar ftol = ei_sqrt(epsilon<Scalar>()),
const Scalar xtol = ei_sqrt(epsilon<Scalar>()),
const Scalar gtol = Scalar(0.)
); );
Status minimizeNumericalDiff( Status minimizeNumericalDiff(
@ -48,12 +57,8 @@ public:
Matrix< Scalar, Dynamic, 1 > &x, Matrix< Scalar, Dynamic, 1 > &x,
int &nfev, int &nfev,
Matrix< Scalar, Dynamic, 1 > &diag, Matrix< Scalar, Dynamic, 1 > &diag,
const Parameters &parameters,
const int mode=1, const int mode=1,
const Scalar factor = Scalar(100.),
const int maxfev = 400,
const Scalar ftol = ei_sqrt(epsilon<Scalar>()),
const Scalar xtol = ei_sqrt(epsilon<Scalar>()),
const Scalar gtol = Scalar(0.),
const Scalar epsfcn = Scalar(0.) const Scalar epsfcn = Scalar(0.)
); );
@ -67,12 +72,8 @@ public:
int &nfev, int &nfev,
int &njev, int &njev,
Matrix< Scalar, Dynamic, 1 > &diag, Matrix< Scalar, Dynamic, 1 > &diag,
const int mode=1, const Parameters &parameters,
const Scalar factor = Scalar(100.), const int mode=1
const int maxfev = 400,
const Scalar ftol = ei_sqrt(epsilon<Scalar>()),
const Scalar xtol = ei_sqrt(epsilon<Scalar>()),
const Scalar gtol = Scalar(0.)
); );
Matrix< Scalar, Dynamic, 1 > fvec; Matrix< Scalar, Dynamic, 1 > fvec;
@ -96,6 +97,7 @@ LevenbergMarquardt<FunctorType,Scalar>::minimize(
Matrix< Scalar, Dynamic, Dynamic > fjac(m, n); Matrix< Scalar, Dynamic, Dynamic > fjac(m, n);
Matrix< Scalar, Dynamic, 1> diag, qtf; Matrix< Scalar, Dynamic, 1> diag, qtf;
VectorXi ipvt; VectorXi ipvt;
Parameters parameters;
/* check the input parameters for errors. */ /* check the input parameters for errors. */
if (n <= 0 || m < n || tol < 0.) { if (n <= 0 || m < n || tol < 0.) {
@ -103,14 +105,16 @@ LevenbergMarquardt<FunctorType,Scalar>::minimize(
return ImproperInputParameters; return ImproperInputParameters;
} }
parameters.ftol = tol;
parameters.xtol = tol;
parameters.maxfev = 100*(n+1);
return minimize( return minimize(
x, x,
nfev, njev, nfev, njev,
diag, diag,
1, parameters,
100., 1
(n+1)*100,
tol, tol, Scalar(0.)
); );
} }
@ -122,12 +126,8 @@ LevenbergMarquardt<FunctorType,Scalar>::minimize(
int &nfev, int &nfev,
int &njev, int &njev,
Matrix< Scalar, Dynamic, 1 > &diag, Matrix< Scalar, Dynamic, 1 > &diag,
const int mode, const Parameters &parameters,
const Scalar factor, const int mode
const int maxfev,
const Scalar ftol,
const Scalar xtol,
const Scalar gtol
) )
{ {
const int n = x.size(); const int n = x.size();
@ -156,7 +156,7 @@ LevenbergMarquardt<FunctorType,Scalar>::minimize(
/* check the input parameters for errors. */ /* check the input parameters for errors. */
if (n <= 0 || m < n || ftol < 0. || xtol < 0. || gtol < 0. || maxfev <= 0 || factor <= 0.) if (n <= 0 || m < n || parameters.ftol < 0. || parameters.xtol < 0. || parameters.gtol < 0. || parameters.maxfev <= 0 || parameters.factor <= 0.)
return RelativeErrorTooSmall; return RelativeErrorTooSmall;
if (mode == 2) if (mode == 2)
@ -208,9 +208,9 @@ LevenbergMarquardt<FunctorType,Scalar>::minimize(
wa3 = diag.cwise() * x; wa3 = diag.cwise() * x;
xnorm = wa3.stableNorm(); xnorm = wa3.stableNorm();
delta = factor * xnorm; delta = parameters.factor * xnorm;
if (delta == 0.) if (delta == 0.)
delta = factor; delta = parameters.factor;
} }
/* form (q transpose)*fvec and store the first n components in */ /* form (q transpose)*fvec and store the first n components in */
@ -247,7 +247,7 @@ LevenbergMarquardt<FunctorType,Scalar>::minimize(
/* test for convergence of the gradient norm. */ /* test for convergence of the gradient norm. */
if (gnorm <= gtol) if (gnorm <= parameters.gtol)
return CosinusTooSmall; return CosinusTooSmall;
/* rescale if necessary. */ /* rescale if necessary. */
@ -341,16 +341,16 @@ LevenbergMarquardt<FunctorType,Scalar>::minimize(
/* tests for convergence. */ /* tests for convergence. */
if (ei_abs(actred) <= ftol && prered <= ftol && Scalar(.5) * ratio <= 1. && delta <= xtol * xnorm) if (ei_abs(actred) <= parameters.ftol && prered <= parameters.ftol && Scalar(.5) * ratio <= 1. && delta <= parameters.xtol * xnorm)
return RelativeErrorAndReductionTooSmall; return RelativeErrorAndReductionTooSmall;
if (ei_abs(actred) <= ftol && prered <= ftol && Scalar(.5) * ratio <= 1.) if (ei_abs(actred) <= parameters.ftol && prered <= parameters.ftol && Scalar(.5) * ratio <= 1.)
return RelativeReductionTooSmall; return RelativeReductionTooSmall;
if (delta <= xtol * xnorm) if (delta <= parameters.xtol * xnorm)
return RelativeErrorTooSmall; return RelativeErrorTooSmall;
/* tests for termination and stringent tolerances. */ /* tests for termination and stringent tolerances. */
if (nfev >= maxfev) if (nfev >= parameters.maxfev)
return TooManyFunctionEvaluation; return TooManyFunctionEvaluation;
if (ei_abs(actred) <= epsilon<Scalar>() && prered <= epsilon<Scalar>() && Scalar(.5) * ratio <= 1.) if (ei_abs(actred) <= epsilon<Scalar>() && prered <= epsilon<Scalar>() && Scalar(.5) * ratio <= 1.)
return FtolTooSmall; return FtolTooSmall;
@ -379,6 +379,7 @@ LevenbergMarquardt<FunctorType,Scalar>::minimizeNumericalDiff(
Matrix< Scalar, Dynamic, Dynamic > fjac(m, n); Matrix< Scalar, Dynamic, Dynamic > fjac(m, n);
Matrix< Scalar, Dynamic, 1> diag, qtf; Matrix< Scalar, Dynamic, 1> diag, qtf;
VectorXi ipvt; VectorXi ipvt;
Parameters parameters;
/* check the input parameters for errors. */ /* check the input parameters for errors. */
if (n <= 0 || m < n || tol < 0.) { if (n <= 0 || m < n || tol < 0.) {
@ -386,14 +387,17 @@ LevenbergMarquardt<FunctorType,Scalar>::minimizeNumericalDiff(
return ImproperInputParameters; return ImproperInputParameters;
} }
parameters.ftol = tol;
parameters.xtol = tol;
parameters.maxfev = 200*(n+1);
return minimizeNumericalDiff( return minimizeNumericalDiff(
x, x,
nfev, nfev,
diag, diag,
parameters,
1, 1,
100., Scalar(0.)
(n+1)*200,
tol, tol, Scalar(0.), Scalar(0.)
); );
} }
@ -403,12 +407,8 @@ LevenbergMarquardt<FunctorType,Scalar>::minimizeNumericalDiff(
Matrix< Scalar, Dynamic, 1 > &x, Matrix< Scalar, Dynamic, 1 > &x,
int &nfev, int &nfev,
Matrix< Scalar, Dynamic, 1 > &diag, Matrix< Scalar, Dynamic, 1 > &diag,
const Parameters &parameters,
const int mode, const int mode,
const Scalar factor,
const int maxfev,
const Scalar ftol,
const Scalar xtol,
const Scalar gtol,
const Scalar epsfcn const Scalar epsfcn
) )
{ {
@ -437,7 +437,7 @@ LevenbergMarquardt<FunctorType,Scalar>::minimizeNumericalDiff(
/* check the input parameters for errors. */ /* check the input parameters for errors. */
if (n <= 0 || m < n || ftol < 0. || xtol < 0. || gtol < 0. || maxfev <= 0 || factor <= 0.) if (n <= 0 || m < n || parameters.ftol < 0. || parameters.xtol < 0. || parameters.gtol < 0. || parameters.maxfev <= 0 || parameters.factor <= 0.)
return RelativeErrorTooSmall; return RelativeErrorTooSmall;
if (mode == 2) if (mode == 2)
for (j = 0; j < n; ++j) for (j = 0; j < n; ++j)
@ -488,9 +488,9 @@ LevenbergMarquardt<FunctorType,Scalar>::minimizeNumericalDiff(
wa3 = diag.cwise() * x; wa3 = diag.cwise() * x;
xnorm = wa3.stableNorm(); xnorm = wa3.stableNorm();
delta = factor * xnorm; delta = parameters.factor * xnorm;
if (delta == 0.) if (delta == 0.)
delta = factor; delta = parameters.factor;
} }
/* form (q transpose)*fvec and store the first n components in */ /* form (q transpose)*fvec and store the first n components in */
@ -527,7 +527,7 @@ LevenbergMarquardt<FunctorType,Scalar>::minimizeNumericalDiff(
/* test for convergence of the gradient norm. */ /* test for convergence of the gradient norm. */
if (gnorm <= gtol) if (gnorm <= parameters.gtol)
return CosinusTooSmall; return CosinusTooSmall;
/* rescale if necessary. */ /* rescale if necessary. */
@ -621,16 +621,16 @@ LevenbergMarquardt<FunctorType,Scalar>::minimizeNumericalDiff(
/* tests for convergence. */ /* tests for convergence. */
if (ei_abs(actred) <= ftol && prered <= ftol && Scalar(.5) * ratio <= 1. && delta <= xtol * xnorm) if (ei_abs(actred) <= parameters.ftol && prered <= parameters.ftol && Scalar(.5) * ratio <= 1. && delta <= parameters.xtol * xnorm)
return RelativeErrorAndReductionTooSmall; return RelativeErrorAndReductionTooSmall;
if (ei_abs(actred) <= ftol && prered <= ftol && Scalar(.5) * ratio <= 1.) if (ei_abs(actred) <= parameters.ftol && prered <= parameters.ftol && Scalar(.5) * ratio <= 1.)
return RelativeReductionTooSmall; return RelativeReductionTooSmall;
if (delta <= xtol * xnorm) if (delta <= parameters.xtol * xnorm)
return RelativeErrorTooSmall; return RelativeErrorTooSmall;
/* tests for termination and stringent tolerances. */ /* tests for termination and stringent tolerances. */
if (nfev >= maxfev) if (nfev >= parameters.maxfev)
return TooManyFunctionEvaluation; return TooManyFunctionEvaluation;
if (ei_abs(actred) <= epsilon<Scalar>() && prered <= epsilon<Scalar>() && Scalar(.5) * ratio <= 1.) if (ei_abs(actred) <= epsilon<Scalar>() && prered <= epsilon<Scalar>() && Scalar(.5) * ratio <= 1.)
return FtolTooSmall; return FtolTooSmall;
@ -660,6 +660,7 @@ LevenbergMarquardt<FunctorType,Scalar>::minimizeOptimumStorage(
Matrix< Scalar, Dynamic, Dynamic > fjac(m, n); Matrix< Scalar, Dynamic, Dynamic > fjac(m, n);
Matrix< Scalar, Dynamic, 1> diag, qtf; Matrix< Scalar, Dynamic, 1> diag, qtf;
VectorXi ipvt; VectorXi ipvt;
Parameters parameters;
/* check the input parameters for errors. */ /* check the input parameters for errors. */
if (n <= 0 || m < n || tol < 0.) { if (n <= 0 || m < n || tol < 0.) {
@ -667,14 +668,16 @@ LevenbergMarquardt<FunctorType,Scalar>::minimizeOptimumStorage(
return ImproperInputParameters; return ImproperInputParameters;
} }
parameters.ftol = tol;
parameters.xtol = tol;
parameters.maxfev = 100*(n+1);
return minimizeOptimumStorage( return minimizeOptimumStorage(
x, x,
nfev, njev, nfev, njev,
diag, diag,
1, parameters,
100., 1
(n+1)*100,
tol, tol, Scalar(0.)
); );
} }
@ -685,12 +688,8 @@ LevenbergMarquardt<FunctorType,Scalar>::minimizeOptimumStorage(
int &nfev, int &nfev,
int &njev, int &njev,
Matrix< Scalar, Dynamic, 1 > &diag, Matrix< Scalar, Dynamic, 1 > &diag,
const int mode, const Parameters &parameters,
const Scalar factor, const int mode
const int maxfev,
const Scalar ftol,
const Scalar xtol,
const Scalar gtol
) )
{ {
const int n = x.size(); const int n = x.size();
@ -720,7 +719,7 @@ LevenbergMarquardt<FunctorType,Scalar>::minimizeOptimumStorage(
/* check the input parameters for errors. */ /* check the input parameters for errors. */
if (n <= 0 || m < n || ftol < 0. || xtol < 0. || gtol < 0. || maxfev <= 0 || factor <= 0.) if (n <= 0 || m < n || parameters.ftol < 0. || parameters.xtol < 0. || parameters.gtol < 0. || parameters.maxfev <= 0 || parameters.factor <= 0.)
return RelativeErrorTooSmall; return RelativeErrorTooSmall;
if (mode == 2) if (mode == 2)
@ -805,9 +804,9 @@ LevenbergMarquardt<FunctorType,Scalar>::minimizeOptimumStorage(
wa3 = diag.cwise() * x; wa3 = diag.cwise() * x;
xnorm = wa3.stableNorm(); xnorm = wa3.stableNorm();
delta = factor * xnorm; delta = parameters.factor * xnorm;
if (delta == 0.) if (delta == 0.)
delta = factor; delta = parameters.factor;
} }
/* compute the norm of the scaled gradient. */ /* compute the norm of the scaled gradient. */
@ -827,7 +826,7 @@ LevenbergMarquardt<FunctorType,Scalar>::minimizeOptimumStorage(
/* test for convergence of the gradient norm. */ /* test for convergence of the gradient norm. */
if (gnorm <= gtol) if (gnorm <= parameters.gtol)
return CosinusTooSmall; return CosinusTooSmall;
/* rescale if necessary. */ /* rescale if necessary. */
@ -921,16 +920,16 @@ LevenbergMarquardt<FunctorType,Scalar>::minimizeOptimumStorage(
/* tests for convergence. */ /* tests for convergence. */
if (ei_abs(actred) <= ftol && prered <= ftol && Scalar(.5) * ratio <= 1. && delta <= xtol * xnorm) if (ei_abs(actred) <= parameters.ftol && prered <= parameters.ftol && Scalar(.5) * ratio <= 1. && delta <= parameters.xtol * xnorm)
return RelativeErrorAndReductionTooSmall; return RelativeErrorAndReductionTooSmall;
if (ei_abs(actred) <= ftol && prered <= ftol && Scalar(.5) * ratio <= 1.) if (ei_abs(actred) <= parameters.ftol && prered <= parameters.ftol && Scalar(.5) * ratio <= 1.)
return RelativeReductionTooSmall; return RelativeReductionTooSmall;
if (delta <= xtol * xnorm) if (delta <= parameters.xtol * xnorm)
return RelativeErrorTooSmall; return RelativeErrorTooSmall;
/* tests for termination and stringent tolerances. */ /* tests for termination and stringent tolerances. */
if (nfev >= maxfev) if (nfev >= parameters.maxfev)
return TooManyFunctionEvaluation; return TooManyFunctionEvaluation;
if (ei_abs(actred) <= epsilon<Scalar>() && prered <= epsilon<Scalar>() && Scalar(.5) * ratio <= 1.) if (ei_abs(actred) <= epsilon<Scalar>() && prered <= epsilon<Scalar>() && Scalar(.5) * ratio <= 1.)
return FtolTooSmall; return FtolTooSmall;

View File

@ -181,7 +181,8 @@ void testLmder()
// do the computation // do the computation
lmder_functor functor; lmder_functor functor;
LevenbergMarquardt<lmder_functor> lm(functor); LevenbergMarquardt<lmder_functor> lm(functor);
info = lm.minimize(x, nfev, njev, diag); LevenbergMarquardt<lmder_functor>::Parameters parameters;
info = lm.minimize(x, nfev, njev, diag, parameters);
// check return values // check return values
VERIFY( 1 == info); VERIFY( 1 == info);
@ -290,19 +291,19 @@ void testHybrj1()
void testHybrj() void testHybrj()
{ {
const int n=9; const int n=9;
int info, nfev=0, njev=0, mode; int info, nfev=0, njev=0;
VectorXd x(n), diag(n); VectorXd x(n), diag(n);
/* the following starting values provide a rough fit. */ /* the following starting values provide a rough fit. */
x.setConstant(n, -1.); x.setConstant(n, -1.);
mode = 2;
diag.setConstant(n, 1.); diag.setConstant(n, 1.);
// do the computation // do the computation
hybrj_functor functor; hybrj_functor functor;
HybridNonLinearSolver<hybrj_functor> solver(functor); HybridNonLinearSolver<hybrj_functor> solver(functor);
info = solver.solve(x, nfev, njev, diag, mode); HybridNonLinearSolver<hybrj_functor>::Parameters parameters;
info = solver.solve(x, nfev, njev, diag, parameters, 2);
// check return value // check return value
VERIFY( 1 == info); VERIFY( 1 == info);
@ -372,7 +373,7 @@ void testHybrd1()
void testHybrd() void testHybrd()
{ {
const int n=9; const int n=9;
int info, nfev=0, ml, mu, mode; int info, nfev=0, ml, mu;
VectorXd x, diag(n); VectorXd x, diag(n);
/* the following starting values provide a rough fit. */ /* the following starting values provide a rough fit. */
@ -380,13 +381,13 @@ void testHybrd()
ml = 1; ml = 1;
mu = 1; mu = 1;
mode = 2;
diag.setConstant(n, 1.); diag.setConstant(n, 1.);
// do the computation // do the computation
hybrd_functor functor; hybrd_functor functor;
HybridNonLinearSolver<hybrd_functor> solver(functor); HybridNonLinearSolver<hybrd_functor> solver(functor);
info = solver.solveNumericalDiff(x, nfev, diag, mode, ml, mu); HybridNonLinearSolver<hybrd_functor>::Parameters parameters;
info = solver.solveNumericalDiff(x, nfev, diag, parameters, 2, ml, mu);
// check return value // check return value
VERIFY( 1 == info); VERIFY( 1 == info);
@ -484,7 +485,8 @@ void testLmstr()
// do the computation // do the computation
lmstr_functor functor; lmstr_functor functor;
LevenbergMarquardt<lmstr_functor> lm(functor); LevenbergMarquardt<lmstr_functor> lm(functor);
info = lm.minimizeOptimumStorage(x, nfev, njev, diag); LevenbergMarquardt<lmstr_functor>::Parameters parameters;
info = lm.minimizeOptimumStorage(x, nfev, njev, diag, parameters);
// check return values // check return values
VERIFY( 1 == info); VERIFY( 1 == info);
@ -570,7 +572,8 @@ void testLmdif()
// do the computation // do the computation
lmdif_functor functor; lmdif_functor functor;
LevenbergMarquardt<lmdif_functor> lm(functor); LevenbergMarquardt<lmdif_functor> lm(functor);
info = lm.minimizeNumericalDiff(x, nfev, diag); LevenbergMarquardt<lmdif_functor>::Parameters parameters;
info = lm.minimizeNumericalDiff(x, nfev, diag, parameters);
// check return values // check return values
VERIFY( 1 == info); VERIFY( 1 == info);
@ -655,7 +658,8 @@ void testNistChwirut2(void)
// do the computation // do the computation
chwirut2_functor functor; chwirut2_functor functor;
LevenbergMarquardt<chwirut2_functor> lm(functor); LevenbergMarquardt<chwirut2_functor> lm(functor);
info = lm.minimize(x, nfev, njev, diag); LevenbergMarquardt<chwirut2_functor>::Parameters parameters;
info = lm.minimize(x, nfev, njev, diag, parameters);
// check return value // check return value
VERIFY( 1 == info); VERIFY( 1 == info);
@ -673,8 +677,10 @@ void testNistChwirut2(void)
*/ */
x<< 0.15, 0.008, 0.010; x<< 0.15, 0.008, 0.010;
// do the computation // do the computation
info = lm.minimize(x, nfev, njev, diag, parameters = LevenbergMarquardt<chwirut2_functor>::Parameters(); // get default back
1, 100., 400, 1.E6*epsilon<double>(), 1.E6*epsilon<double>()); parameters.ftol = 1.E6*epsilon<double>();
parameters.xtol = 1.E6*epsilon<double>();
info = lm.minimize(x, nfev, njev, diag, parameters);
// check return value // check return value
VERIFY( 1 == info); VERIFY( 1 == info);
@ -733,7 +739,8 @@ void testNistMisra1a(void)
// do the computation // do the computation
misra1a_functor functor; misra1a_functor functor;
LevenbergMarquardt<misra1a_functor> lm(functor); LevenbergMarquardt<misra1a_functor> lm(functor);
info = lm.minimize(x, nfev, njev, diag); LevenbergMarquardt<misra1a_functor>::Parameters parameters;
info = lm.minimize(x, nfev, njev, diag, parameters);
// check return value // check return value
VERIFY( 1 == info); VERIFY( 1 == info);
@ -750,7 +757,7 @@ void testNistMisra1a(void)
*/ */
x<< 250., 0.0005; x<< 250., 0.0005;
// do the computation // do the computation
info = lm.minimize(x, nfev, njev, diag); info = lm.minimize(x, nfev, njev, diag, parameters);
// check return value // check return value
VERIFY( 1 == info); VERIFY( 1 == info);
@ -819,7 +826,8 @@ void testNistHahn1(void)
// do the computation // do the computation
hahn1_functor functor; hahn1_functor functor;
LevenbergMarquardt<hahn1_functor> lm(functor); LevenbergMarquardt<hahn1_functor> lm(functor);
info = lm.minimize(x, nfev, njev, diag); LevenbergMarquardt<hahn1_functor>::Parameters parameters;
info = lm.minimize(x, nfev, njev, diag, parameters);
// check return value // check return value
VERIFY( 1 == info); VERIFY( 1 == info);
@ -841,7 +849,7 @@ void testNistHahn1(void)
*/ */
x<< .1, -.1, .005, -.000001, -.005, .0001, -.0000001; x<< .1, -.1, .005, -.000001, -.005, .0001, -.0000001;
// do the computation // do the computation
info = lm.minimize(x, nfev, njev, diag); info = lm.minimize(x, nfev, njev, diag, parameters);
// check return value // check return value
VERIFY( 1 == info); VERIFY( 1 == info);
@ -905,7 +913,8 @@ void testNistMisra1d(void)
// do the computation // do the computation
misra1d_functor functor; misra1d_functor functor;
LevenbergMarquardt<misra1d_functor> lm(functor); LevenbergMarquardt<misra1d_functor> lm(functor);
info = lm.minimize(x, nfev, njev, diag); LevenbergMarquardt<misra1d_functor>::Parameters parameters;
info = lm.minimize(x, nfev, njev, diag, parameters);
// check return value // check return value
VERIFY( 3 == info); VERIFY( 3 == info);
@ -922,7 +931,7 @@ void testNistMisra1d(void)
*/ */
x<< 450., 0.0003; x<< 450., 0.0003;
// do the computation // do the computation
info = lm.minimize(x, nfev, njev, diag); info = lm.minimize(x, nfev, njev, diag, parameters);
// check return value // check return value
VERIFY( 1 == info); VERIFY( 1 == info);
@ -983,7 +992,8 @@ void testNistLanczos1(void)
// do the computation // do the computation
lanczos1_functor functor; lanczos1_functor functor;
LevenbergMarquardt<lanczos1_functor> lm(functor); LevenbergMarquardt<lanczos1_functor> lm(functor);
info = lm.minimize(x, nfev, njev, diag); LevenbergMarquardt<lanczos1_functor>::Parameters parameters;
info = lm.minimize(x, nfev, njev, diag, parameters);
// check return value // check return value
VERIFY( 2 == info); VERIFY( 2 == info);
@ -1004,7 +1014,7 @@ void testNistLanczos1(void)
*/ */
x<< 0.5, 0.7, 3.6, 4.2, 4., 6.3; x<< 0.5, 0.7, 3.6, 4.2, 4., 6.3;
// do the computation // do the computation
info = lm.minimize(x, nfev, njev, diag); info = lm.minimize(x, nfev, njev, diag, parameters);
// check return value // check return value
VERIFY( 2 == info); VERIFY( 2 == info);
@ -1069,7 +1079,8 @@ void testNistRat42(void)
// do the computation // do the computation
rat42_functor functor; rat42_functor functor;
LevenbergMarquardt<rat42_functor> lm(functor); LevenbergMarquardt<rat42_functor> lm(functor);
info = lm.minimize(x, nfev, njev, diag); LevenbergMarquardt<rat42_functor>::Parameters parameters;
info = lm.minimize(x, nfev, njev, diag, parameters);
// check return value // check return value
VERIFY( 1 == info); VERIFY( 1 == info);
@ -1087,7 +1098,7 @@ void testNistRat42(void)
*/ */
x<< 75., 2.5, 0.07; x<< 75., 2.5, 0.07;
// do the computation // do the computation
info = lm.minimize(x, nfev, njev, diag); info = lm.minimize(x, nfev, njev, diag, parameters);
// check return value // check return value
VERIFY( 1 == info); VERIFY( 1 == info);
@ -1147,7 +1158,8 @@ void testNistMGH10(void)
// do the computation // do the computation
MGH10_functor functor; MGH10_functor functor;
LevenbergMarquardt<MGH10_functor> lm(functor); LevenbergMarquardt<MGH10_functor> lm(functor);
info = lm.minimize(x, nfev, njev, diag); LevenbergMarquardt<MGH10_functor>::Parameters parameters;
info = lm.minimize(x, nfev, njev, diag, parameters);
// check return value // check return value
VERIFY( 2 == info); VERIFY( 2 == info);
@ -1165,7 +1177,7 @@ void testNistMGH10(void)
*/ */
x<< 0.02, 4000., 250.; x<< 0.02, 4000., 250.;
// do the computation // do the computation
info = lm.minimize(x, nfev, njev, diag); info = lm.minimize(x, nfev, njev, diag, parameters);
// check return value // check return value
VERIFY( 2 == info); VERIFY( 2 == info);
@ -1223,8 +1235,11 @@ void testNistBoxBOD(void)
// do the computation // do the computation
BoxBOD_functor functor; BoxBOD_functor functor;
LevenbergMarquardt<BoxBOD_functor> lm(functor); LevenbergMarquardt<BoxBOD_functor> lm(functor);
info = lm.minimize(x, nfev, njev, diag, LevenbergMarquardt<BoxBOD_functor>::Parameters parameters;
1, 10., 400, 1E6*epsilon<double>(), 1E6*epsilon<double>()); parameters.ftol = 1.E6*epsilon<double>();
parameters.xtol = 1.E6*epsilon<double>();
parameters.factor = 10.;
info = lm.minimize(x, nfev, njev, diag, parameters);
// check return value // check return value
VERIFY( 1 == info); VERIFY( 1 == info);
@ -1241,8 +1256,10 @@ void testNistBoxBOD(void)
*/ */
x<< 100., 0.75; x<< 100., 0.75;
// do the computation // do the computation
info = lm.minimize(x, nfev, njev, diag, parameters = LevenbergMarquardt<BoxBOD_functor>::Parameters(); // get default back
1, 100., 14000, epsilon<double>(), epsilon<double>()); parameters.ftol = epsilon<double>();
parameters.xtol = epsilon<double>();
info = lm.minimize(x, nfev, njev, diag, parameters);
// check return value // check return value
VERIFY( 1 == info); VERIFY( 1 == info);
@ -1301,8 +1318,11 @@ void testNistMGH17(void)
// do the computation // do the computation
MGH17_functor functor; MGH17_functor functor;
LevenbergMarquardt<MGH17_functor> lm(functor); LevenbergMarquardt<MGH17_functor> lm(functor);
info = lm.minimize(x, nfev, njev, diag, LevenbergMarquardt<MGH17_functor>::Parameters parameters;
1, 100., 5000, epsilon<double>(), epsilon<double>()); parameters.ftol = epsilon<double>();
parameters.xtol = epsilon<double>();
parameters.maxfev = 1000;
info = lm.minimize(x, nfev, njev, diag, parameters);
// check return value // check return value
VERIFY( 1 == info); VERIFY( 1 == info);
@ -1322,7 +1342,8 @@ void testNistMGH17(void)
*/ */
x<< 0.5 ,1.5 ,-1 ,0.01 ,0.02; x<< 0.5 ,1.5 ,-1 ,0.01 ,0.02;
// do the computation // do the computation
info = lm.minimize(x, nfev, njev, diag); parameters = LevenbergMarquardt<MGH17_functor>::Parameters(); // get default back
info = lm.minimize(x, nfev, njev, diag, parameters);
// check return value // check return value
VERIFY( 1 == info); VERIFY( 1 == info);
@ -1387,8 +1408,9 @@ void testNistMGH09(void)
// do the computation // do the computation
MGH09_functor functor; MGH09_functor functor;
LevenbergMarquardt<MGH09_functor> lm(functor); LevenbergMarquardt<MGH09_functor> lm(functor);
info = lm.minimize(x, nfev, njev, diag, LevenbergMarquardt<MGH09_functor>::Parameters parameters;
1, 100., 5000); parameters.maxfev = 1000;
info = lm.minimize(x, nfev, njev, diag, parameters);
// check return value // check return value
VERIFY( 1 == info); VERIFY( 1 == info);
@ -1407,7 +1429,8 @@ void testNistMGH09(void)
*/ */
x<< 0.25, 0.39, 0.415, 0.39; x<< 0.25, 0.39, 0.415, 0.39;
// do the computation // do the computation
info = lm.minimize(x, nfev, njev, diag); parameters = LevenbergMarquardt<MGH09_functor>::Parameters();
info = lm.minimize(x, nfev, njev, diag, parameters);
// check return value // check return value
VERIFY( 1 == info); VERIFY( 1 == info);
@ -1469,7 +1492,9 @@ void testNistBennett5(void)
// do the computation // do the computation
Bennett5_functor functor; Bennett5_functor functor;
LevenbergMarquardt<Bennett5_functor> lm(functor); LevenbergMarquardt<Bennett5_functor> lm(functor);
info = lm.minimize(x, nfev, njev, diag, 1, 100., 5000); LevenbergMarquardt<Bennett5_functor>::Parameters parameters;
parameters.maxfev = 1000;
info = lm.minimize(x, nfev, njev, diag, parameters);
// check return value // check return value
VERIFY( 1 == info); VERIFY( 1 == info);
@ -1486,7 +1511,8 @@ void testNistBennett5(void)
*/ */
x<< -1500., 45., 0.85; x<< -1500., 45., 0.85;
// do the computation // do the computation
info = lm.minimize(x, nfev, njev, diag); parameters = LevenbergMarquardt<Bennett5_functor>::Parameters();
info = lm.minimize(x, nfev, njev, diag, parameters);
// check return value // check return value
VERIFY( 1 == info); VERIFY( 1 == info);
@ -1554,8 +1580,10 @@ void testNistThurber(void)
// do the computation // do the computation
thurber_functor functor; thurber_functor functor;
LevenbergMarquardt<thurber_functor> lm(functor); LevenbergMarquardt<thurber_functor> lm(functor);
info = lm.minimize(x, nfev, njev, diag, LevenbergMarquardt<thurber_functor>::Parameters parameters;
1, 100., 400, 1.E4*epsilon<double>(), 1.E4*epsilon<double>()); parameters.ftol = 1.E4*epsilon<double>();
parameters.xtol = 1.E4*epsilon<double>();
info = lm.minimize(x, nfev, njev, diag, parameters);
// check return value // check return value
VERIFY( 1 == info); VERIFY( 1 == info);
@ -1577,8 +1605,10 @@ void testNistThurber(void)
*/ */
x<< 1300 ,1500 ,500 ,75 ,1 ,0.4 ,0.05 ; x<< 1300 ,1500 ,500 ,75 ,1 ,0.4 ,0.05 ;
// do the computation // do the computation
info = lm.minimize(x, nfev, njev, diag, parameters = LevenbergMarquardt<thurber_functor>::Parameters();
1, 100., 400, 1.E4*epsilon<double>(), 1.E4*epsilon<double>()); parameters.ftol = 1.E4*epsilon<double>();
parameters.xtol = 1.E4*epsilon<double>();
info = lm.minimize(x, nfev, njev, diag, parameters);
// check return value // check return value
VERIFY( 1 == info); VERIFY( 1 == info);
@ -1643,8 +1673,10 @@ void testNistRat43(void)
// do the computation // do the computation
rat43_functor functor; rat43_functor functor;
LevenbergMarquardt<rat43_functor> lm(functor); LevenbergMarquardt<rat43_functor> lm(functor);
info = lm.minimize(x, nfev, njev, diag, LevenbergMarquardt<rat43_functor>::Parameters parameters;
1, 100., 400, 1.E6*epsilon<double>(), 1.E6*epsilon<double>()); parameters.ftol = 1.E6*epsilon<double>();
parameters.xtol = 1.E6*epsilon<double>();
info = lm.minimize(x, nfev, njev, diag, parameters);
// check return value // check return value
VERIFY( 1 == info); VERIFY( 1 == info);
@ -1663,8 +1695,10 @@ void testNistRat43(void)
*/ */
x<< 700., 5., 0.75, 1.3; x<< 700., 5., 0.75, 1.3;
// do the computation // do the computation
info = lm.minimize(x, nfev, njev, diag, parameters = LevenbergMarquardt<rat43_functor>::Parameters(); // get default back
1, 100., 400, 1.E5*epsilon<double>(), 1.E5*epsilon<double>()); parameters.ftol = 1.E5*epsilon<double>();
parameters.xtol = 1.E5*epsilon<double>();
info = lm.minimize(x, nfev, njev, diag, parameters);
// check return value // check return value
VERIFY( 1 == info); VERIFY( 1 == info);
@ -1727,7 +1761,8 @@ void testNistEckerle4(void)
// do the computation // do the computation
eckerle4_functor functor; eckerle4_functor functor;
LevenbergMarquardt<eckerle4_functor> lm(functor); LevenbergMarquardt<eckerle4_functor> lm(functor);
info = lm.minimize(x, nfev, njev, diag); LevenbergMarquardt<eckerle4_functor>::Parameters parameters;
info = lm.minimize(x, nfev, njev, diag, parameters);
// check return value // check return value
VERIFY( 1 == info); VERIFY( 1 == info);
@ -1745,7 +1780,7 @@ void testNistEckerle4(void)
*/ */
x<< 1.5, 5., 450.; x<< 1.5, 5., 450.;
// do the computation // do the computation
info = lm.minimize(x, nfev, njev, diag); info = lm.minimize(x, nfev, njev, diag, parameters);
// check return value // check return value
VERIFY( 1 == info); VERIFY( 1 == info);