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
14
15#include "MantidKernel/Logger.h"
16
18namespace {
20Kernel::Logger g_log("SimplexMinimizer");
21} // namespace
22
23DECLARE_FUNCMINIMIZER(SimplexMinimizer, Simplex)
24
25
31double SimplexMinimizer::fun(const gsl_vector *x, void *params) {
32 SimplexMinimizer &minimizer = *static_cast<SimplexMinimizer *>(params);
33 // update function parameters
34 if (x->data) {
35 for (size_t i = 0; i < minimizer.m_costFunction->nParams(); ++i) {
36 minimizer.m_costFunction->setParameter(i, gsl_vector_get(x, i));
37 }
38 }
39 std::shared_ptr<CostFunctions::CostFuncFitting> fitting =
40 std::dynamic_pointer_cast<CostFunctions::CostFuncFitting>(minimizer.m_costFunction);
41 if (fitting) {
42 fitting->getFittingFunction()->applyTies();
43 }
44 return minimizer.m_costFunction->val();
45}
46
48 : m_epsabs(epsabs), m_costFunction(), m_size(1.0), m_simplexStepSize(nullptr), m_startGuess(nullptr),
49 m_gslSolver(nullptr) {
50 gslContainer.f = nullptr;
51 gslContainer.n = -1;
52 gslContainer.params = nullptr;
53}
54
55void SimplexMinimizer::initialize(API::ICostFunction_sptr function, size_t /*maxIterations*/) {
56 m_costFunction = function;
57
58 const gsl_multimin_fminimizer_type *T = gsl_multimin_fminimizer_nmsimplex;
59
60 size_t np = function->nParams();
61 // step size for simplex
62 m_simplexStepSize = gsl_vector_alloc(np);
63 gsl_vector_set_all(m_simplexStepSize, m_size);
64
65 // setup simplex container
66 gslContainer.n = np;
67 gslContainer.f = &fun;
68 gslContainer.params = this;
69
70 // fill in parameter values
71 m_startGuess = gsl_vector_alloc(np);
72 for (size_t i = 0; i < np; ++i) {
73 gsl_vector_set(m_startGuess, i, function->getParameter(i));
74 }
75
76 // setup minimizer
77 m_gslSolver = gsl_multimin_fminimizer_alloc(T, np);
78 gsl_multimin_fminimizer_set(m_gslSolver, &gslContainer, m_startGuess, m_simplexStepSize);
79}
80
85bool SimplexMinimizer::iterate(size_t /*iteration*/) {
86 int status = gsl_multimin_fminimizer_iterate(m_gslSolver);
87 if (status) {
88 m_errorString = gsl_strerror(status);
89 return false;
90 }
91 double size = gsl_multimin_fminimizer_size(m_gslSolver);
92 status = gsl_multimin_test_size(size, m_epsabs);
93 if (status != GSL_CONTINUE) {
94 m_errorString = gsl_strerror(status);
95 return false;
96 }
97 return true;
98}
99
101void SimplexMinimizer::resetSize(const double &size) {
102 m_size = size;
103 clearMemory();
105}
106
108
111 if (m_simplexStepSize) {
112 gsl_vector_free(m_simplexStepSize);
113 }
114 if (m_startGuess) {
115 gsl_vector_free(m_startGuess);
116 }
117 if (m_gslSolver) {
118 gsl_multimin_fminimizer_free(m_gslSolver);
119 }
120}
121
123
124} // 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.
bool iterate(size_t) override
Do one iteration.
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.
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< ICostFunction > ICostFunction_sptr
define a shared pointer to a cost function
Definition: ICostFunction.h:60