13#ifndef OPTIMIST_LINESEARCH_HH
14#define OPTIMIST_LINESEARCH_HH
55 case Method::GOLDSTEIN:
57 case Method::STRONG_WOLFE:
59 case Method::MORE_THUENTE:
70 template <
typename Scalar>
79 100 * std::numeric_limits<Scalar>::epsilon()};
88 std::numeric_limits<Scalar>::epsilon()};
96 template <
typename Scalar>
102 return phi_0 + c * alpha * dphi_0 + s.
f_slack * std::abs(phi_0);
110 template <
typename Scalar>
116 const Scalar dphi_b) {
117 const Scalar lo{std::min(a, b)}, hi{std::max(a, b)};
118 const Scalar margin{
static_cast<Scalar
>(0.1) * (hi - lo)};
119 const Scalar d1{dphi_a + dphi_b - 3 * (phi_a - phi_b) / (a - b)};
120 const Scalar disc{d1 * d1 - dphi_a * dphi_b};
121 Scalar x{
static_cast<Scalar
>(0.5) * (a + b)};
122 if (disc >= 0 && std::isfinite(disc)) {
123 const Scalar d2{(b > a ? 1 : -1) * std::sqrt(disc)};
124 const Scalar den{dphi_b - dphi_a + 2 * d2};
126 const Scalar t{b - (b - a) * (dphi_b + d2 - d1) / den};
127 if (std::isfinite(t)) {
132 return std::clamp(x, lo + margin, hi - margin);
148 template <
typename Scalar,
typename Phi>
158 for (
Integer i{0}; i < s.max_iterations; ++i) {
159 if (!phi(alpha, phi_alpha,
nullptr) || !std::isfinite(phi_alpha)) {
163 if (phi_alpha <=
armijo_bound(phi_0, dphi_0, alpha, s, s.c1)) {
168 const Scalar den{2 * (phi_alpha - phi_0 - dphi_0 * alpha)};
169 Scalar next{den > 0 ? -dphi_0 * alpha * alpha / den : s.shrink * alpha};
170 alpha = std::clamp(next,
171 static_cast<Scalar
>(0.1) * alpha,
172 static_cast<Scalar
>(0.5) * alpha);
191 template <
typename Scalar,
typename Phi>
201 const Scalar c{s.goldstein};
202 Scalar lo{0}, hi{std::numeric_limits<Scalar>::infinity()};
203 for (
Integer i{0}; i < s.max_iterations; ++i) {
204 const bool ok{phi(alpha, phi_alpha,
nullptr) &&
205 std::isfinite(phi_alpha)};
206 if (!ok || phi_alpha >
armijo_bound(phi_0, dphi_0, alpha, s, c)) {
208 }
else if (phi_alpha < phi_0 + (1 - c) * alpha * dphi_0 -
209 s.f_slack * std::abs(phi_0)) {
216 alpha = std::isfinite(hi) ?
static_cast<Scalar
>(0.5) * (lo + hi)
217 : std::min(2 * alpha, s.alpha_max);
222 return phi(alpha, phi_alpha,
nullptr) && std::isfinite(phi_alpha);
242 template <
typename Scalar,
typename Phi>
254 const Scalar noise{s.f_slack * std::abs(phi_0)};
255 auto eval = [&](
const Scalar a, Scalar &v, Scalar &d) ->
bool {
257 return phi(a, v, &d) && std::isfinite(v) && std::isfinite(d);
263 auto zoom = [&](Scalar lo,
268 Scalar dphi_hi) ->
bool {
269 while (evaluations < s.max_iterations) {
272 if (!eval(a, v, d)) {
274 phi_hi = std::numeric_limits<Scalar>::infinity();
279 if (v >
armijo_bound(phi_0, dphi_0, a, s, s.c1) || v > phi_lo + noise) {
284 if (std::abs(d) <= -s.c2 * dphi_0) {
290 if (d * (hi - lo) >= 0) {
299 if (std::abs(hi - lo) <=
300 std::numeric_limits<Scalar>::epsilon() * std::max(Scalar(1), lo)) {
307 return eval(alpha, phi_alpha, dphi_alpha);
313 Scalar a_prev{0}, phi_prev{phi_0}, dphi_prev{dphi_0};
314 Scalar a{std::min(alpha, s.alpha_max)};
315 while (evaluations < s.max_iterations) {
317 if (!eval(a, v, d)) {
319 a = a_prev +
static_cast<Scalar
>(0.5) * (a - a_prev);
323 (a_prev > 0 && v > phi_prev + noise)) {
324 return zoom(a_prev, phi_prev, dphi_prev, a, v, d);
326 if (std::abs(d) <= -s.c2 * dphi_0) {
333 return zoom(a, v, d, a_prev, phi_prev, dphi_prev);
338 if (a >= s.alpha_max) {
341 a = std::min(2 * a, s.alpha_max);
345 return eval(alpha, phi_alpha, dphi_alpha);
357 template <
typename Scalar>
370 const Scalar noise = 0) {
371 const Scalar sgnd{dp * (dx / std::abs(dx))};
374 const bool higher{fp > fx + noise};
377 const Scalar theta{3 * (fx - fp) / (stp - stx) + dx + dp};
378 const Scalar s{std::max({std::abs(theta), std::abs(dx), std::abs(dp)})};
379 Scalar gamma{s * std::sqrt(std::max(
381 (theta / s) * (theta / s) - (dx / s) * (dp / s)))};
385 const Scalar p{(gamma - dx) + theta};
386 const Scalar q{((gamma - dx) + gamma) + dp};
387 const Scalar stpc{stx + (p / q) * (stp - stx)};
388 const Scalar stpq{stx + ((dx / ((fx - fp) / (stp - stx) + dx)) / 2) *
390 stpf = std::abs(stpc - stx) < std::abs(stpq - stx)
392 : stpc + (stpq - stpc) / 2;
394 }
else if (sgnd < 0) {
396 const Scalar theta{3 * (fx - fp) / (stp - stx) + dx + dp};
397 const Scalar s{std::max({std::abs(theta), std::abs(dx), std::abs(dp)})};
398 Scalar gamma{s * std::sqrt(std::max(
400 (theta / s) * (theta / s) - (dx / s) * (dp / s)))};
404 const Scalar p{(gamma - dp) + theta};
405 const Scalar q{((gamma - dp) + gamma) + dx};
406 const Scalar stpc{stp + (p / q) * (stx - stp)};
407 const Scalar stpq{stp + (dp / (dp - dx)) * (stx - stp)};
408 stpf = std::abs(stpc - stp) > std::abs(stpq - stp) ? stpc : stpq;
410 }
else if (std::abs(dp) < std::abs(dx)) {
412 const Scalar theta{3 * (fx - fp) / (stp - stx) + dx + dp};
413 const Scalar s{std::max({std::abs(theta), std::abs(dx), std::abs(dp)})};
414 Scalar gamma{s * std::sqrt(std::max(
416 (theta / s) * (theta / s) - (dx / s) * (dp / s)))};
420 const Scalar p{(gamma - dp) + theta};
421 const Scalar q{(gamma + (dx - dp)) + gamma};
422 const Scalar r{p / q};
424 if (r < 0 && gamma != 0) {
425 stpc = stp + r * (stx - stp);
427 stpc = stp > stx ? stpmax : stpmin;
429 const Scalar stpq{stp + (dp / (dp - dx)) * (stx - stp)};
431 stpf = std::abs(stpc - stp) < std::abs(stpq - stp) ? stpc : stpq;
432 const Scalar bound{stp + Scalar(0.66) * (sty - stp)};
433 stpf = stp > stx ? std::min(bound, stpf) : std::max(bound, stpf);
435 stpf = std::abs(stpc - stp) > std::abs(stpq - stp) ? stpc : stpq;
436 stpf = std::clamp(stpf, stpmin, stpmax);
441 const Scalar theta{3 * (fp - fy) / (sty - stp) + dy + dp};
443 std::max({std::abs(theta), std::abs(dy), std::abs(dp)})};
444 Scalar gamma{s * std::sqrt(std::max(Scalar(0),
445 (theta / s) * (theta / s) -
446 (dy / s) * (dp / s)))};
450 const Scalar p{(gamma - dp) + theta};
451 const Scalar q{((gamma - dp) + gamma) + dy};
452 stpf = stp + (p / q) * (sty - stp);
454 stpf = stp > stx ? stpmax : stpmin;
492 template <
typename Scalar,
typename Phi>
500 if (!(dphi_0 < 0) || !(alpha > 0)) {
503 const Scalar stpmin{0}, stpmax{s.alpha_max};
504 const Scalar noise{s.f_slack * std::abs(phi_0)};
505 const Scalar gtest{s.c1 * dphi_0};
506 bool brackt{
false}, stage1{
true};
507 Scalar width{stpmax - stpmin}, width1{2 * width};
508 Scalar stx{0}, fx{phi_0}, gx{dphi_0};
509 Scalar sty{0}, fy{phi_0}, gy{dphi_0};
510 Scalar stp{std::min(alpha, stpmax)};
511 Scalar stmin{0}, stmax{stp + 4 * stp};
512 Scalar f{phi_0}, g{dphi_0};
515 Scalar best_stp{0}, best_f{phi_0}, best_g{dphi_0};
517 for (
Integer i{0}; i < s.max_iterations; ++i) {
518 if (!phi(stp, f, &g) || !std::isfinite(f) || !std::isfinite(g)) {
520 stp = stx + (stp - stx) / 2;
523 const Scalar ftest{
armijo_bound(phi_0, dphi_0, stp, s, s.c1)};
524 if (f <= ftest && (best_stp == 0 || f < best_f)) {
531 if (f <= ftest && std::abs(g) <= -s.c2 * dphi_0) {
539 if ((brackt && (stp <= stmin || stp >= stmax)) ||
540 (brackt && stmax - stmin <= s.x_tolerance * stmax) ||
541 (stp == stpmax && f <= ftest && g <= gtest) ||
542 (stp == stpmin && (f > ftest || g >= gtest))) {
548 if (stage1 && f <= ftest && g >= std::min(s.c1, s.c2) * dphi_0) {
551 if (stage1 && f <= fx && f > ftest) {
552 Scalar fm{f - stp * gtest}, gm{g - gtest};
553 Scalar fxm{fx - stx * gtest}, gxm{gx - gtest};
554 Scalar fym{fy - sty * gtest}, gym{gy - gtest};
555 more_thuente_step(stx, fxm, gxm, sty, fym, gym, stp, fm, gm, brackt,
556 stmin, stmax, noise);
557 fx = fxm + stx * gtest;
558 fy = fym + sty * gtest;
562 more_thuente_step(stx, fx, gx, sty, fy, gy, stp, f, g, brackt, stmin,
568 if (std::abs(sty - stx) >= Scalar(0.66) * width1) {
569 stp = stx + (sty - stx) / 2;
572 width = std::abs(sty - stx);
573 stmin = std::min(stx, sty);
574 stmax = std::max(stx, sty);
576 stmin = stp + Scalar(1.1) * (stp - stx);
577 stmax = stp + 4 * (stp - stx);
579 stp = std::clamp(stp, stpmin, stpmax);
580 if ((brackt && (stp <= stmin || stp >= stmax)) ||
581 (brackt && stmax - stmin <= s.x_tolerance * stmax)) {
613 template <
typename Scalar,
typename Phi>
624 return armijo(phi, phi_0, dphi_0, alpha, phi_alpha, s);
625 case Method::GOLDSTEIN:
626 return goldstein(phi, phi_0, dphi_0, alpha, phi_alpha, s);
627 case Method::MORE_THUENTE:
628 return more_thuente(phi, phi_0, dphi_0, alpha, phi_alpha, dphi_alpha,
630 case Method::STRONG_WOLFE:
632 return strong_wolfe(phi, phi_0, dphi_0, alpha, phi_alpha, dphi_alpha,
Namespace for the one-dimensional line searches used by the descent optimizers.
Definition LineSearch.hh:36
bool armijo(Phi &&phi, const Scalar phi_0, const Scalar dphi_0, Scalar &alpha, Scalar &phi_alpha, const Settings< Scalar > &s=Settings< Scalar >())
Backtracking line search enforcing the Armijo condition , with safeguarded quadratic interpolation of...
Definition LineSearch.hh:149
bool strong_wolfe(Phi &&phi, const Scalar phi_0, const Scalar dphi_0, Scalar &alpha, Scalar &phi_alpha, Scalar &dphi_alpha, const Settings< Scalar > &s=Settings< Scalar >())
Line search enforcing the strong Wolfe conditions and (Nocedal and Wright, Algorithms 3....
Definition LineSearch.hh:243
bool search(const Method method, Phi &&phi, const Scalar phi_0, const Scalar dphi_0, Scalar &alpha, Scalar &phi_alpha, Scalar &dphi_alpha, const Settings< Scalar > &s)
Run the selected line search.
Definition LineSearch.hh:614
void more_thuente_step(Scalar &stx, Scalar &fx, Scalar &dx, Scalar &sty, Scalar &fy, Scalar &dy, Scalar &stp, const Scalar fp, const Scalar dp, bool &brackt, const Scalar stpmin, const Scalar stpmax, const Scalar noise=0)
Safeguarded step of the Moré-Thuente line search (MINPACK-2 dcstep). Computes a new trial step from t...
Definition LineSearch.hh:358
Scalar cubic_minimizer(const Scalar a, const Scalar phi_a, const Scalar dphi_a, const Scalar b, const Scalar phi_b, const Scalar dphi_b)
Definition LineSearch.hh:111
Scalar armijo_bound(const Scalar phi_0, const Scalar dphi_0, const Scalar alpha, const Settings< Scalar > &s, const Scalar c)
Definition LineSearch.hh:97
bool goldstein(Phi &&phi, const Scalar phi_0, const Scalar dphi_0, Scalar &alpha, Scalar &phi_alpha, const Settings< Scalar > &s=Settings< Scalar >())
Line search enforcing the Goldstein conditions , by expansion and bisection. Only function values ar...
Definition LineSearch.hh:192
std::string name(const Method method)
Get the name of a line search strategy.
Definition LineSearch.hh:51
bool more_thuente(Phi &&phi, const Scalar phi_0, const Scalar dphi_0, Scalar &alpha, Scalar &phi_alpha, Scalar &dphi_alpha, const Settings< Scalar > &s=Settings< Scalar >())
Line search of Moré and Thuente (1994), as implemented in MINPACK-2 (dcsrch). It finds a step satisfy...
Definition LineSearch.hh:493
enum class Method :Integer { ARMIJO=0, GOLDSTEIN=1, STRONG_WOLFE=2, MORE_THUENTE=3 } Method
Available line search strategies.
Definition LineSearch.hh:41
Namespace for the Optimist library.
Definition Optimist.hh:90
OPTIMIST_DEFAULT_INTEGER_TYPE Integer
The Integer type as used for the API.
Definition Optimist.hh:98
Line search parameters and result.
Definition LineSearch.hh:71
Scalar alpha_max
Definition LineSearch.hh:76
Scalar goldstein
Definition LineSearch.hh:74
Scalar shrink
Definition LineSearch.hh:75
Scalar c1
Definition LineSearch.hh:72
Integer max_iterations
Definition LineSearch.hh:77
Scalar c2
Definition LineSearch.hh:73
Scalar x_tolerance
Definition LineSearch.hh:87
Scalar f_slack
Definition LineSearch.hh:78