48 template <
typename NumberType>
49 std::optional<NumberType>
51 const NumberType f_low,
52 const NumberType g_low,
53 const NumberType x_hi,
54 const NumberType f_hi);
65 template <
typename NumberType>
66 std::optional<NumberType>
68 const NumberType f_low,
69 const NumberType g_low,
70 const NumberType x_hi,
71 const NumberType f_hi,
72 const NumberType g_hi);
81 template <
typename NumberType>
82 std::optional<NumberType>
84 const NumberType f_low,
85 const NumberType g_low,
86 const NumberType x_hi,
87 const NumberType f_hi,
88 const NumberType x_rec,
89 const NumberType f_rec);
102 template <
typename NumberType>
105 const NumberType f_low,
106 const NumberType g_low,
107 const NumberType x_hi,
108 const NumberType f_hi,
109 const NumberType g_hi,
113 const std::pair<NumberType, NumberType> bounds);
119 template <
typename NumberType>
122 const NumberType f_low,
123 const NumberType g_low,
124 const NumberType x_hi,
125 const NumberType f_hi,
126 const NumberType g_hi,
130 const std::pair<NumberType, NumberType> bounds);
300 template <
typename NumberType>
301 std::pair<NumberType, unsigned int>
303 const std::function<std::pair<NumberType, NumberType>(
const NumberType x)>
308 NumberType(
const NumberType x_low,
309 const NumberType f_low,
310 const NumberType g_low,
311 const NumberType x_hi,
312 const NumberType f_hi,
313 const NumberType g_hi,
317 const std::pair<NumberType, NumberType> bounds)> &interpolate,
319 const NumberType eta = 0.9,
320 const NumberType mu = 0.01,
321 const NumberType a_max = std::numeric_limits<NumberType>::max(),
322 const unsigned int max_evaluations = 20,
323 const bool debug_output =
false);
332 template <
typename NumberType>
333 std::optional<NumberType>
341 const NumberType denom = (2. * g1 * x2 - 2. * g1 * x1 - 2. * f2 + 2. * f1);
345 return (g1 * (x2 * x2 - x1 * x1) + 2. * (f1 - f2) * x1) / denom;
350 template <
typename NumberType>
351 std::optional<NumberType>
360 const NumberType beta1 = g1 + g2 - 3. * (f1 - f2) / (x1 - x2);
361 const NumberType s = beta1 * beta1 - g1 * g2;
366 const NumberType denom =
367 x1 < x2 ? g2 - g1 + 2. * beta2 : g1 - g2 + 2. * beta2;
371 return x1 < x2 ? x2 - (x2 - x1) * (g2 + beta2 - beta1) / denom :
372 x1 - (x1 - x2) * (g1 + beta2 - beta1) / denom;
377 template <
typename NumberType>
378 std::optional<NumberType>
396 const NumberType x2_shift = x2 - x1;
397 const NumberType x3_shift = x3 - x1;
398 const NumberType r1 = f2 - f1 - g1 * x2_shift;
399 const NumberType r2 = f3 - f1 - g1 * x3_shift;
400 const NumberType denom =
401 Utilities::fixed_power<2>(x2_shift * x3_shift) * (x2_shift - x3_shift);
405 const NumberType
A = (r1 * Utilities::fixed_power<2>(x3_shift) -
406 r2 * Utilities::fixed_power<2>(x2_shift)) /
408 const NumberType B = (r2 * Utilities::fixed_power<3>(x2_shift) -
409 r1 * Utilities::fixed_power<3>(x3_shift)) /
411 const NumberType &
C = g1;
414 const NumberType radical = B * B -
A *
C * 3;
418 return x1 + (-B +
std::sqrt(radical)) / (A * 3);
423 template <
typename NumberType>
434 const std::pair<NumberType, NumberType> bounds)
443 std::optional<NumberType> res =
cubic_fit(x1, f1, g1, x2, f2, g2);
444 if (res && *res >= bounds.first && *res <= bounds.second)
449 if (res && *res >= bounds.first && *res <= bounds.second)
454 return (bounds.first + bounds.second) * 0.5;
459 template <
typename NumberType>
470 const std::pair<NumberType, NumberType> bounds)
479 std::optional<NumberType> res =
482 std::optional<NumberType>{};
483 if (res && *res >= bounds.first && *res <= bounds.second)
488 if (res && *res >= bounds.first && *res <= bounds.second)
493 return (bounds.first + bounds.second) * 0.5;
498 template <
typename NumberType>
499 std::pair<NumberType, unsigned int>
501 const std::function<std::pair<NumberType, NumberType>(
const NumberType x)>
506 NumberType(
const NumberType x_low,
507 const NumberType f_low,
508 const NumberType g_low,
509 const NumberType x_hi,
510 const NumberType f_hi,
511 const NumberType g_hi,
515 const std::pair<NumberType, NumberType> bounds)> &choose,
517 const NumberType eta,
519 const NumberType a_max,
520 const unsigned int max_evaluations,
521 const bool debug_output)
532 const NumberType tau1 = 9.;
537 const NumberType tau2 = 0.1;
538 const NumberType tau3 = 0.5;
540 const NumberType g0_abs =
std::abs(g0);
541 const NumberType f_min = f0 + a_max *
mu * g0;
545 const auto w1 = [&](
const NumberType a,
const NumberType f) {
546 return f <= f0 + a *
mu * g0;
551 const auto w2 = [&](
const NumberType g) {
558 const NumberType x = std::numeric_limits<NumberType>::signaling_NaN();
559 NumberType a_lo = x, f_lo = x, g_lo = x;
560 NumberType a_hi = x, f_hi = x, g_hi = x;
561 NumberType ai = x, fi = x, gi = x;
566 NumberType f_prev, g_prev, a_prev;
572 while (i < max_evaluations)
574 const auto fgi = func(ai);
580 deallog <<
"Bracketing phase: " << i << std::endl
581 << ai <<
' ' << fi <<
' ' << gi <<
' ' << w1(ai, fi) <<
' '
582 << w2(gi) <<
' ' << f_min << std::endl;
585 if (fi <= f_min || ai == a_max)
588 deallog <<
"Reached the maximum step size." << std::endl;
589 return std::make_pair(ai, i);
593 (fi >= f_prev && i > 1))
608 deallog <<
"Satisfied both Wolfe conditions during Bracketing."
612 return std::make_pair(ai, i);
629 std::make_pair(2. * ai - a_prev,
630 std::min(a_max, ai + tau1 * (ai - a_prev)));
647 "Could not find the initial bracket within the given number of iterations."));
664 if (
std::abs(a_lo) > std::numeric_limits<NumberType>::epsilon() &&
665 std::abs(a_hi) > std::numeric_limits<NumberType>::epsilon())
673 while (i < max_evaluations)
675 const NumberType a_lo_safe = a_lo + tau2 * (a_hi - a_lo);
676 const NumberType a_hi_safe = a_hi - tau3 * (a_hi - a_lo);
677 const auto bounds = std::minmax(a_lo_safe, a_hi_safe);
680 a_lo, f_lo, g_lo, a_hi, f_hi, g_hi, a_rec, f_rec, g_rec, bounds);
682 const std::pair<NumberType, NumberType> fgi = func(ai);
688 deallog <<
"Sectioning phase: " << i << std::endl
689 << a_lo <<
' ' << f_lo <<
' ' << g_lo <<
' ' << w1(a_lo, f_lo)
690 <<
' ' << w2(g_lo) << std::endl
691 << a_hi <<
' ' << f_hi <<
' ' << g_hi <<
' ' << w1(a_hi, f_hi)
692 <<
' ' << w2(g_hi) << std::endl
693 << ai <<
' ' << fi <<
' ' << gi <<
' ' << w1(ai, fi) <<
' '
694 << w2(gi) << std::endl;
696 if (!w1(ai, fi) || fi >= f_lo)
712 deallog <<
"Satisfied both Wolfe conditions." << std::endl;
714 return std::make_pair(ai, i);
717 if (gi * (a_hi - a_lo) >= 0)
746 "Could not could complete the sectioning phase within the given number of iterations."));
747 return std::make_pair(std::numeric_limits<NumberType>::signaling_NaN(), i);
std::pair< NumberType, unsigned int > line_search(const std::function< std::pair< NumberType, NumberType >(const NumberType x)> &func, const NumberType f0, const NumberType g0, const std::function< NumberType(const NumberType x_low, const NumberType f_low, const NumberType g_low, const NumberType x_hi, const NumberType f_hi, const NumberType g_hi, const FiniteSizeHistory< NumberType > &x_rec, const FiniteSizeHistory< NumberType > &f_rec, const FiniteSizeHistory< NumberType > &g_rec, const std::pair< NumberType, NumberType > bounds)> &interpolate, const NumberType a1, const NumberType eta=0.9, const NumberType mu=0.01, const NumberType a_max=std::numeric_limits< NumberType >::max(), const unsigned int max_evaluations=20, const bool debug_output=false)