25Kernel::Logger
g_log(
"LevenbergMarquardMD");
34 :
IFuncMinimizer(), m_tau(1e-6), m_mu(1e-6), m_nu(2.0), m_rho(1.0), m_F(0.0) {
35 declareProperty(
"MuMax", 1e6,
"Maximum value of mu - a stopping parameter in failure.");
36 declareProperty(
"AbsError", 0.0001,
37 "Absolute error allowed for parameters - "
38 "a stopping parameter in success.");
39 declareProperty(
"Verbose",
false,
"Make output more verbose.");
44 m_costFunction = std::dynamic_pointer_cast<CostFunctions::CostFuncFitting>(function);
46 throw std::invalid_argument(
"Levenberg-Marquardt minimizer works only with "
47 "functions which define the Hessian. Different function was given.");
61 throw std::runtime_error(
"Cost function isn't set up.");
91 g_log.
warning() <<
"===========================================================\n";
104 std::vector<double> sf(
n);
106 for (
size_t i = 0; i <
n; ++i) {
111 double tmp = H.get(i, i) +
m_mu *
d;
121 for (
size_t i = 0; i <
n; ++i) {
122 double d = dd.
get(i);
123 dd.
set(i,
d / sf[i]);
124 for (
size_t j = i; j <
n; ++j) {
125 const double f = sf[i] * sf[j];
126 double tmp = H.get(i, j);
127 H.set(i, j,
tmp / f);
130 H.set(j, i,
tmp / f);
135 if (verbose &&
m_rho > 0) {
138 for (
size_t j = 0; j <
n; ++j) {
151 }
catch (std::runtime_error &
error) {
158 for (
size_t j = 0; j <
n; ++j) {
163 for (
size_t j = 0; j <
n; ++j) {
170 for (
size_t i = 0; i <
n; ++i) {
171 double d = dx.
get(i);
172 dx.
set(i,
d / sf[i]);
174 dd.
set(i,
d * sf[i]);
185 for (
size_t i = 0; i <
n; ++i) {
186 g_log.
warning() <<
"Parameter(" << i <<
")=" << parameters[i] <<
'\n';
214 double dx_norm = dx.
norm();
215 if (dx_norm < absError) {
217 g_log.
warning() <<
"Successful fit, parameters changed by less than " << absError <<
'\n';
226 g_log.
warning() <<
"Successful fit, cost function didn't change.\n";
232 if (
fabs(dL) == 0.0) {
251 const double I3 = 1.0 / 3.0;
260 g_log.
warning() <<
"Good iteration, accept new parameters.\n";
272 g_log.
warning() <<
"Bad iteration, increase mu and revert changes to parameters.\n";
282 throw std::runtime_error(
"Cost function isn't set up.");
#define DECLARE_FUNCMINIMIZER(classname, username)
Macro for declaring a new type of minimizers to be used with the FuncMinimizerFactory.
An interface for function minimizers.
std::string m_errorString
Error string.
A wrapper around Eigen::Matrix.
A wrapper around Eigen::Vector.
void set(const size_t i, const double value)
Set an element.
double dot(const EigenVector &v) const
Calculate the dot product.
double get(const size_t i) const
Get an element.
double norm() const
Get vector norm (length)
Implementing Levenberg-Marquardt algorithm.
double m_mu
The damping mu parameter in the Levenberg-Marquardt method.
std::shared_ptr< CostFunctions::CostFuncFitting > m_costFunction
Pointer to the cost function.
double m_rho
The rho parameter in the Levenberg-Marquardt method.
std::vector< double > m_D
bool iterate(size_t iteration) override
Do one iteration.
void initialize(API::ICostFunction_sptr function, size_t maxIterations=0) override
Initialize minimizer, i.e. pass a function to minimize.
double m_nu
The nu parameter in the Levenberg-Marquardt method.
double m_F
To keep function value.
double costFunctionVal() override
Return current value of the cost function.
double m_tau
The tau parameter in the Levenberg-Marquardt method.
void warning(const std::string &msg)
Logs at warning level.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< ICostFunction > ICostFunction_sptr
define a shared pointer to a cost function