Mantid
Loading...
Searching...
No Matches
SLSQPMinimizer.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2013 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#pragma once
8
9#include "MantidKernel/DllConfig.h"
10#include "MantidKernel/Matrix.h"
11#include "MantidKernel/System.h"
12
13#include <vector>
14
15namespace Mantid {
16namespace Kernel {
17namespace Math {
44class MANTID_KERNEL_DLL SLSQPMinimizer {
45public:
55 template <typename T>
56 SLSQPMinimizer(const size_t nparams, const T &objfunc)
57 : m_nparams(nparams), m_neq(0), m_nineq(0), m_objfunc(objfunc), m_constraintNorms() {}
58
72 template <typename T>
73 SLSQPMinimizer(const size_t nparams, const T &objfunc, const DblMatrix &equality, const DblMatrix &inequality)
74 : m_nparams(nparams), m_neq(equality.numRows()), m_nineq(inequality.numRows()), m_objfunc(objfunc),
75 m_constraintNorms() {
76 initializeConstraints(equality, inequality);
77 }
78
80 SLSQPMinimizer() = delete;
81
83 SLSQPMinimizer(const SLSQPMinimizer &) = delete;
84
87
89 inline size_t numParameters() const { return m_nparams; }
91 inline size_t numEqualityConstraints() const { return m_neq; }
93 inline size_t numInequalityConstraints() const { return m_nineq; }
94
96 std::vector<double> minimize(const std::vector<double> &x0) const;
97
98private:
104 inline double fvalue(const std::vector<double> &x) const { return m_objfunc.eval(x); }
106 void fprime(std::vector<double> &grad, const std::vector<double> &x) const;
108 void evaluateConstraints(std::vector<double> &constrValues, const std::vector<double> &x) const;
109
111 void initializeConstraints(const DblMatrix &equality, const DblMatrix &inequality);
112
117 private:
119 public:
120 virtual ~BaseHolder() = default;
121 virtual double eval(const std::vector<double> &x) const = 0;
122 };
123 template <typename T> class TypeHolder : public BaseHolder {
124 public:
125 TypeHolder(const T &func) : func(func) {}
126 double eval(const std::vector<double> &x) const override { return func.eval(x); }
129 };
130
131 public:
133 template <typename T> FunctionWrapper(const T &func) : m_funcHolder(std::make_unique<TypeHolder<T>>(func)) {}
139 double eval(const std::vector<double> &x) const { return m_funcHolder->eval(x); }
141 std::unique_ptr<BaseHolder> m_funcHolder;
142 };
143
145 const size_t m_nparams;
147 const size_t m_neq;
149 const size_t m_nineq;
150
154 mutable std::vector<double> m_constraintNorms;
155};
156
157} // namespace Math
158} // namespace Kernel
159} // namespace Mantid
virtual double eval(const std::vector< double > &x) const =0
double eval(const std::vector< double > &x) const override
Non-templated wrapper for objective function object to allow it to be stored without templating the c...
double eval(const std::vector< double > &x) const
Calls user supplied function.
std::unique_ptr< BaseHolder > m_funcHolder
Templated holder.
Minimize an objective function using the SLSQP optimization subroutine originally implemented by Diet...
SLSQPMinimizer()=delete
Disable default constructor.
const size_t m_nineq
Number of inequality constraints.
SLSQPMinimizer & operator=(const SLSQPMinimizer &)=delete
Disable assignment operator.
const size_t m_neq
Number of equality constraints.
SLSQPMinimizer(const size_t nparams, const T &objfunc)
Constructor.
FunctionWrapper m_objfunc
User-defined function.
SLSQPMinimizer(const size_t nparams, const T &objfunc, const DblMatrix &equality, const DblMatrix &inequality)
Constructor with constraints.
const size_t m_nparams
Number of parameters under minimization.
SLSQPMinimizer(const SLSQPMinimizer &)=delete
Disable copy operator.
double fvalue(const std::vector< double > &x) const
Compute the value of the objective function.
std::vector< double > m_constraintNorms
Holder for constraint normals.
Helper class which provides the Collimation Length for SANS instruments.
STL namespace.