27 #ifndef CDPL_MATH_BFGSMINIMIZER_HPP
28 #define CDPL_MATH_BFGSMINIMIZER_HPP
52 template <typename VA, typename VT = typename MinimizerVariableArrayTraits<VA>::ValueType,
typename FVT = VT>
122 rho(0.01), tau1(9), tau2(0.05), tau3(0.5), order(3), sigma(0.1), func(func), gradFunc(grad_func), status(
SUCCESS) {}
187 for (std::size_t i = 0; max_iter == 0 || i < max_iter; i++) {
188 status =
iterate(fValue, x, g);
193 if (g_norm >=
ValueType(0) && g0Norm <= g_norm)
196 if (delta_f >=
ValueType(0) && deltaF <= delta_f)
221 startF = gradFunc(x, g);
230 multiply(p, -1 / g0Norm);
269 alpha1 = std::min(
ValueType(1), 2 * del / -fp0);
281 updatePosition(alpha, x, f, g);
313 A = -(1 + dgnorm * dgnorm / dxdg) *
B + dgg / dxdg;
331 multiply(p, dir / pNorm);
345 void initFuncEvalCache()
380 return MinimizerVariableArrayTraits<VA>::template norm2<ValueType>(v);
385 return MinimizerVariableArrayTraits<VA>::template dot<ValueType>(v1, v2);
395 return dot(gAlpha, p);
400 if (alpha == xCacheKey)
407 axpy(alpha, p, xAlpha);
414 if (alpha == fCacheKey)
419 fAlpha = func(xAlpha);
427 if (alpha == dfCacheKey)
432 if (alpha != gCacheKey) {
433 fAlpha = gradFunc(xAlpha, gAlpha);
448 if (alpha == fCacheKey && alpha == dfCacheKey) {
454 if (alpha == fCacheKey || alpha == dfCacheKey) {
462 fAlpha = gradFunc(xAlpha, gAlpha);
479 getFDF(alpha, f_alpha, df_alpha);
486 void changeDirection()
569 ValueType fl = f0 + zl * (fp0 + zl * (f1 - f0 - fp0));
570 ValueType fh = f0 + zh * (fp0 + zh * (f1 - f0 - fp0));
583 if (z > zl && z < zh) {
584 ValueType f = f0 + z * (fp0 + z * (f1 - f0 - fp0));
610 return c0 + z * (c1 + z * (c2 + z * c3));
630 ValueType eta = 3 * (f1 - f0) - 2 * fp0 - fp1;
631 ValueType xi = fp0 + fp1 - 2 * (f1 - f0);
632 ValueType c0 = f0, c1 = fp0, c2 = eta, c3 = xi;
635 ValueType fmin = cubic(c0, c1, c2, c3, zl);
637 checkExtremum(c0, c1, c2, c3, zh, zmin, fmin);
640 std::size_t n = solveQuadratic(3 * c3, 2 * c2, c1, z0, z1);
643 if (z0 > zl && z0 < zh)
644 checkExtremum(c0, c1, c2, c3, z0, zmin, fmin);
646 if (z1 > zl && z1 < zh)
647 checkExtremum(c0, c1, c2, c3, z1, zmin, fmin);
650 if (z0 > zl && z0 < zh)
651 checkExtremum(c0, c1, c2, c3, z0, zmin, fmin);
674 if (order > 2 && std::isfinite(fpb))
675 z = interpolateCubic(fa, fpa * (b - a), fb, fpb * (b - a), zmin, zmax);
677 z = interpolateQuadratic(fa, fpa * (b - a), fb, zmin, zmax);
686 ValueType falpha, fpalpha, delta, alpha_next;
703 const std::size_t num_brack_iter = 100, section_iters = 100;
706 while (i++ < num_brack_iter) {
707 falpha = getF(alpha);
711 if (falpha > f0 + alpha * rho * fp0 || falpha >= falpha_prev) {
717 fpb = std::numeric_limits<ValueType>::quiet_NaN();
721 fpalpha = getDF(alpha);
740 delta = alpha - alpha_prev;
745 alpha_next = interpolate(alpha_prev, falpha_prev, fpalpha_prev,
746 alpha, falpha, fpalpha, lower, upper);
749 falpha_prev = falpha;
750 fpalpha_prev = fpalpha;
756 while (i++ < section_iters) {
762 alpha = interpolate(a, fa, fpa, b, fb, fpb, lower, upper);
763 falpha = getF(alpha);
765 if ((a - alpha) * fpa <= std::numeric_limits<ValueType>::epsilon()) {
770 if (falpha > f0 + rho * alpha * fp0 || falpha >= fa) {
774 fpb = std::numeric_limits<ValueType>::quiet_NaN();
777 fpalpha = getDF(alpha);
807 const std::size_t order;
Provides traits to flexibly handle different types of variable arrays in function optimization algori...
Definition of type traits.
Implementation of the Broyden-Fletcher-Goldfarb-Shanno (BFGS) algorithm for solving unconstrained non...
Definition: BFGSMinimizer.hpp:54
Status iterate(ValueType &f, VariableArrayType &x, VariableArrayType &g)
Performs a single BFGS iteration: line search along the current search direction, BFGS update of the ...
Definition: BFGSMinimizer.hpp:254
std::function< FVT(const VA &)> ObjectiveFunction
Type of the objective function.
Definition: BFGSMinimizer.hpp:80
ValueType getFunctionValue() const
Returns the function value at the end of the most recent iterate() call.
Definition: BFGSMinimizer.hpp:146
Status getStatus() const
Returns the current status of the minimizer.
Definition: BFGSMinimizer.hpp:164
Status minimize(VariableArrayType &x, VariableArrayType &g, std::size_t max_iter, const ValueType &g_norm, const ValueType &delta_f, bool do_setup=true)
Runs the BFGS minimization loop on x.
Definition: BFGSMinimizer.hpp:179
Status
Status bitmask reported by minimize() and getStatus().
Definition: BFGSMinimizer.hpp:88
@ NO_PROGRESS
No more progress towards the solution can be made.
Definition: BFGSMinimizer.hpp:98
@ SUCCESS
Iteration step completed successfully (no termination condition met yet).
Definition: BFGSMinimizer.hpp:93
@ GNORM_REACHED
The configured gradient-norm threshold has been reached.
Definition: BFGSMinimizer.hpp:108
@ ITER_LIMIT_REACHED
The maximum number of minimization iterations has been reached.
Definition: BFGSMinimizer.hpp:103
@ DELTAF_REACHED
The configured function value delta between successive iterations has been reached.
Definition: BFGSMinimizer.hpp:113
BFGSMinimizer(const ObjectiveFunction &func, const GradientFunction &grad_func)
Constructs the BFGSMinimizer instance with the given objective and gradient functions.
Definition: BFGSMinimizer.hpp:121
ValueType getGradientNorm() const
Returns the L2 norm of the gradient at the end of the most recent iterate() call.
Definition: BFGSMinimizer.hpp:128
std::function< FVT(const VA &, VA &)> GradientFunction
Type of the gradient function (computes the objective value and writes the gradient into the second a...
Definition: BFGSMinimizer.hpp:75
ValueType setup(const VariableArrayType &x, VariableArrayType &g, const ValueType &step_size=0.001, const ValueType &tol=0.15)
Initializes the minimizer state for a subsequent iterate() / minimize() loop.
Definition: BFGSMinimizer.hpp:214
FVT FunctionValueType
The scalar return type of the objective and gradient functions.
Definition: BFGSMinimizer.hpp:70
ValueType getFunctionDelta() const
Returns the magnitude of the function value decrease produced by the most recent iterate() call.
Definition: BFGSMinimizer.hpp:137
std::size_t getNumIterations() const
Returns the number of iterations performed by the most recent minimize() or iterate() loop.
Definition: BFGSMinimizer.hpp:155
VT ValueType
The scalar value type of VariableArrayType.
Definition: BFGSMinimizer.hpp:65
VA VariableArrayType
The type of the variable array passed to the minimizer.
Definition: BFGSMinimizer.hpp:60
constexpr unsigned int A
Generic type that covers any element except hydrogen.
Definition: AtomType.hpp:637
constexpr unsigned int B
Specifies Boron.
Definition: AtomType.hpp:87
constexpr unsigned int r
Specifies that the stereocenter has r configuration.
Definition: CIPDescriptor.hpp:76
The namespace of the Chemical Data Processing Library.
static void axpy(const T &alpha, const ArrayType &x, ArrayType &y)
Performs the in-place BLAS-style axpy operation .
Definition: MinimizerVariableArrayTraits.hpp:131
static void multiply(ArrayType &a, const T &v)
Multiplies every element of a by the scalar v.
Definition: MinimizerVariableArrayTraits.hpp:162
static void sub(ArrayType &a1, const ArrayType &a2)
Subtracts a2 from a1 element-wise ( ).
Definition: MinimizerVariableArrayTraits.hpp:172
static void assign(ArrayType &a1, const ArrayType &a2)
Copies the contents of a2 into a1.
Definition: MinimizerVariableArrayTraits.hpp:150
static void clear(ArrayType &a)
Sets all elements of a to the default-constructed ValueType.
Definition: MinimizerVariableArrayTraits.hpp:140
static RealType abs(ConstReference t)
Returns the absolute value of t (std::abs for signed types, the identity for unsigned types).
Definition: TypeTraits.hpp:142
static ValueType sqrt(ConstReference t)
Returns the square root of t.
Definition: TypeTraits.hpp:152
Primary traits template for scalar arithmetic value types.
Definition: TypeTraits.hpp:307