Mantid
Loading...
Searching...
No Matches
SimplexMinimizer.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
12
15
16#include "MantidKernel/Logger.h"
17
19namespace {
21Kernel::Logger g_log("SimplexMinimizer");
22} // namespace
23
24DECLARE_FUNCMINIMIZER(SimplexMinimizer, Simplex)
25
26
32double SimplexMinimizer::fun(const gsl_vector *x, void *params) {
33 SimplexMinimizer &minimizer = *static_cast<SimplexMinimizer *>(params);
34 // update function parameters
35 if (x->data) {
36 for (size_t i = 0; i < minimizer.m_costFunction->nParams(); ++i) {
37 minimizer.m_costFunction->setParameter(i, gsl_vector_get(x, i));
38 }
39 }
40 std::shared_ptr<CostFunctions::CostFuncFitting> fitting =
41 std::dynamic_pointer_cast<CostFunctions::CostFuncFitting>(minimizer.m_costFunction);
42 if (fitting) {
43 fitting->getFittingFunction()->applyTies();
44 }
45 return minimizer.m_costFunction->val();
46}
47
49 : m_epsabs(epsabs), m_costFunction(), m_size(1.0), m_simplexStepSize(nullptr), m_startGuess(nullptr),
50 m_gslSolver(nullptr) {
51 gslContainer.f = nullptr;
52 gslContainer.n = -1;
53 gslContainer.params = nullptr;
54}
55
56void SimplexMinimizer::initialize(API::ICostFunction_sptr function, size_t /*maxIterations*/) {
57 m_costFunction = function;
58
59 const gsl_multimin_fminimizer_type *T = gsl_multimin_fminimizer_nmsimplex;
60
61 size_t np = function->nParams();
62 // step size for simplex
63 m_simplexStepSize = gsl_vector_alloc(np);
64 gsl_vector_set_all(m_simplexStepSize, m_size);
65
66 // setup simplex container
67 gslContainer.n = np;
68 gslContainer.f = &fun;
69 gslContainer.params = this;
70
71 // fill in parameter values
72 m_startGuess = gsl_vector_alloc(np);
73 for (size_t i = 0; i < np; ++i) {
74 gsl_vector_set(m_startGuess, i, function->getParameter(i));
75 }
76
77 // setup minimizer
78 m_gslSolver = gsl_multimin_fminimizer_alloc(T, np);
79 gsl_multimin_fminimizer_set(m_gslSolver, &gslContainer, m_startGuess, m_simplexStepSize);
80}
81
86bool SimplexMinimizer::iterate(size_t /*iteration*/) {
87 int status = gsl_multimin_fminimizer_iterate(m_gslSolver);
88 if (status) {
89 m_errorString = gsl_strerror(status);
90 return false;
91 }
92 double size = gsl_multimin_fminimizer_size(m_gslSolver);
93 status = gsl_multimin_test_size(size, m_epsabs);
94 if (status != GSL_CONTINUE) {
95 m_errorString = (status == GSL_SUCCESS) ? API::MinimizerStatus::SUCCESS : gsl_strerror(status);
96 return false;
97 }
98 return true;
99}
100
102void SimplexMinimizer::resetSize(const double &size) {
103 m_size = size;
104 clearMemory();
106}
107
109
112 if (m_simplexStepSize) {
113 gsl_vector_free(m_simplexStepSize);
114 }
115 if (m_startGuess) {
116 gsl_vector_free(m_startGuess);
117 }
118 if (m_gslSolver) {
119 gsl_multimin_fminimizer_free(m_gslSolver);
120 }
121}
122
124
125} // namespace Mantid::CurveFitting::FuncMinimisers
size_t m_size
Maximum size of the store.
CostFunctions::CostFuncFitting & m_costFunction
The cost function.
#define DECLARE_FUNCMINIMIZER(classname, username)
Macro for declaring a new type of minimizers to be used with the FuncMinimizerFactory.
std::string m_errorString
Error string.
Implementing Simplex by wrapping the IFuncMinimizer interface around the GSL implementation of this a...
double costFunctionVal() override
Return current value of the cost function.
gsl_multimin_fminimizer * m_gslSolver
pointer to the GSL solver doing the work
static double fun(const gsl_vector *x, void *params)
Used by the GSL to evaluate the function.
double m_epsabs
Absolute value of the error that is considered a fit.
gsl_vector * m_startGuess
Starting parameter values.
SimplexMinimizer(const double epsabs=1e-2)
Constructor setting a value for the relative error acceptance (default=0.01)
gsl_multimin_function gslContainer
GSL simplex minimizer container.
void resetSize(const double &size)
resets the size
void initialize(API::ICostFunction_sptr function, size_t maxIterations=0) override
Initialize minimizer, i.e. pass a function to minimize.
API::ICostFunction_sptr m_costFunction
Function to minimize.
const std::string SUCCESS
Reported when a minimizer has fully converged.
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< ICostFunction > ICostFunction_sptr
define a shared pointer to a cost function