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>
75 rho(0.01), tau1(9), tau2(0.05), tau3(0.5), order(3), sigma(0.1), func(func), gradFunc(grad_func), status(
SUCCESS) {}
110 for (std::size_t i = 0; max_iter == 0 || i < max_iter; i++) {
111 status =
iterate(fValue, x, g);
116 if (g_norm >=
ValueType(0) && g0Norm <= g_norm)
119 if (delta_f >=
ValueType(0) && deltaF <= delta_f)
136 startF = gradFunc(x, g);
145 multiply(p, -1 / g0Norm);
176 alpha1 = std::min(
ValueType(1), 2 * del / -fp0);
188 updatePosition(alpha, x, f, g);
220 A = -(1 + dgnorm * dgnorm / dxdg) *
B + dgg / dxdg;
238 multiply(p, dir / pNorm);
252 void initFuncEvalCache()
287 return MinimizerVariableArrayTraits<VA>::template norm2<ValueType>(v);
292 return MinimizerVariableArrayTraits<VA>::template dot<ValueType>(v1, v2);
302 return dot(gAlpha, p);
307 if (alpha == xCacheKey)
314 axpy(alpha, p, xAlpha);
321 if (alpha == fCacheKey)
326 fAlpha = func(xAlpha);
334 if (alpha == dfCacheKey)
339 if (alpha != gCacheKey) {
340 fAlpha = gradFunc(xAlpha, gAlpha);
355 if (alpha == fCacheKey && alpha == dfCacheKey) {
361 if (alpha == fCacheKey || alpha == dfCacheKey) {
369 fAlpha = gradFunc(xAlpha, gAlpha);
386 getFDF(alpha, f_alpha, df_alpha);
393 void changeDirection()
476 ValueType fl = f0 + zl * (fp0 + zl * (f1 - f0 - fp0));
477 ValueType fh = f0 + zh * (fp0 + zh * (f1 - f0 - fp0));
490 if (z > zl && z < zh) {
491 ValueType f = f0 + z * (fp0 + z * (f1 - f0 - fp0));
517 return c0 + z * (c1 + z * (c2 + z * c3));
537 ValueType eta = 3 * (f1 - f0) - 2 * fp0 - fp1;
538 ValueType xi = fp0 + fp1 - 2 * (f1 - f0);
539 ValueType c0 = f0, c1 = fp0, c2 = eta, c3 = xi;
542 ValueType fmin = cubic(c0, c1, c2, c3, zl);
544 checkExtremum(c0, c1, c2, c3, zh, zmin, fmin);
547 std::size_t n = solveQuadratic(3 * c3, 2 * c2, c1, z0, z1);
550 if (z0 > zl && z0 < zh)
551 checkExtremum(c0, c1, c2, c3, z0, zmin, fmin);
553 if (z1 > zl && z1 < zh)
554 checkExtremum(c0, c1, c2, c3, z1, zmin, fmin);
557 if (z0 > zl && z0 < zh)
558 checkExtremum(c0, c1, c2, c3, z0, zmin, fmin);
581 if (order > 2 && std::isfinite(fpb))
582 z = interpolateCubic(fa, fpa * (b - a), fb, fpb * (b - a), zmin, zmax);
584 z = interpolateQuadratic(fa, fpa * (b - a), fb, zmin, zmax);
593 ValueType falpha, fpalpha, delta, alpha_next;
610 const std::size_t num_brack_iter = 100, section_iters = 100;
613 while (i++ < num_brack_iter) {
614 falpha = getF(alpha);
618 if (falpha > f0 + alpha * rho * fp0 || falpha >= falpha_prev) {
624 fpb = std::numeric_limits<ValueType>::quiet_NaN();
628 fpalpha = getDF(alpha);
647 delta = alpha - alpha_prev;
652 alpha_next = interpolate(alpha_prev, falpha_prev, fpalpha_prev,
653 alpha, falpha, fpalpha, lower, upper);
656 falpha_prev = falpha;
657 fpalpha_prev = fpalpha;
663 while (i++ < section_iters) {
669 alpha = interpolate(a, fa, fpa, b, fb, fpb, lower, upper);
670 falpha = getF(alpha);
672 if ((a - alpha) * fpa <= std::numeric_limits<ValueType>::epsilon()) {
677 if (falpha > f0 + rho * alpha * fp0 || falpha >= fa) {
681 fpb = std::numeric_limits<ValueType>::quiet_NaN();
684 fpalpha = getDF(alpha);
714 const std::size_t order;
Provides traits to flexibly handle different types of variable arrays in function optimization algori...
Definition of type traits.
Fletcher's implementation of the BFGS method.
Definition: BFGSMinimizer.hpp:54
Status iterate(ValueType &f, VariableArrayType &x, VariableArrayType &g)
Definition: BFGSMinimizer.hpp:161
std::function< FVT(const VA &)> ObjectiveFunction
Definition: BFGSMinimizer.hpp:62
ValueType getFunctionValue() const
Definition: BFGSMinimizer.hpp:87
Status getStatus() const
Definition: BFGSMinimizer.hpp:97
Status minimize(VariableArrayType &x, VariableArrayType &g, std::size_t max_iter, const ValueType &g_norm, const ValueType &delta_f, bool do_setup=true)
Definition: BFGSMinimizer.hpp:102
Status
Definition: BFGSMinimizer.hpp:65
@ NO_PROGRESS
Definition: BFGSMinimizer.hpp:68
@ SUCCESS
Definition: BFGSMinimizer.hpp:67
@ GNORM_REACHED
Definition: BFGSMinimizer.hpp:70
@ ITER_LIMIT_REACHED
Definition: BFGSMinimizer.hpp:69
@ DELTAF_REACHED
Definition: BFGSMinimizer.hpp:71
BFGSMinimizer(const ObjectiveFunction &func, const GradientFunction &grad_func)
Definition: BFGSMinimizer.hpp:74
ValueType getGradientNorm() const
Definition: BFGSMinimizer.hpp:77
std::function< FVT(const VA &, VA &)> GradientFunction
Definition: BFGSMinimizer.hpp:61
ValueType setup(const VariableArrayType &x, VariableArrayType &g, const ValueType &step_size=0.001, const ValueType &tol=0.15)
Definition: BFGSMinimizer.hpp:129
FVT FunctionValueType
Definition: BFGSMinimizer.hpp:59
ValueType getFunctionDelta() const
Definition: BFGSMinimizer.hpp:82
std::size_t getNumIterations() const
Definition: BFGSMinimizer.hpp:92
VT ValueType
Definition: BFGSMinimizer.hpp:58
VA VariableArrayType
Definition: BFGSMinimizer.hpp:57
constexpr unsigned int A
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)
Definition: MinimizerVariableArrayTraits.hpp:91
static void multiply(ArrayType &a, const T &v)
Definition: MinimizerVariableArrayTraits.hpp:107
static void sub(ArrayType &a1, const ArrayType &a2)
Definition: MinimizerVariableArrayTraits.hpp:112
static void assign(ArrayType &a1, const ArrayType &a2)
Definition: MinimizerVariableArrayTraits.hpp:101
static void clear(ArrayType &a)
Definition: MinimizerVariableArrayTraits.hpp:96
static RealType abs(ConstReference t)
Definition: TypeTraits.hpp:89
static ValueType sqrt(ConstReference t)
Definition: TypeTraits.hpp:94
Definition: TypeTraits.hpp:171