Optimist  0.0.0
A C++ library for optimization
Loading...
Searching...
No Matches
LineSearch.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
2 * Copyright (c) 2026, Davide Stocco and Enrico Bertolazzi. *
3 * *
4 * The Optimist project is distributed under the BSD 2-Clause License. *
5 * *
6 * Davide Stocco Enrico Bertolazzi *
7 * University of Trento University of Trento *
8 * davide.stocco@unitn.it enrico.bertolazzi@unitn.it *
9\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
10
11#pragma once
12
13#ifndef OPTIMIST_LINESEARCH_HH
14#define OPTIMIST_LINESEARCH_HH
15
16#include "Optimist.hh"
17
18namespace Optimist {
19
36 namespace LineSearch {
37
41 using Method = enum class Method : Integer {
42 ARMIJO = 0,
43 GOLDSTEIN = 1,
44 STRONG_WOLFE = 2,
45 MORE_THUENTE = 3
46 };
47
51 inline std::string name(const Method method) {
52 switch (method) {
53 case Method::ARMIJO:
54 return "Armijo";
55 case Method::GOLDSTEIN:
56 return "Goldstein";
57 case Method::STRONG_WOLFE:
58 return "StrongWolfe";
59 case Method::MORE_THUENTE:
60 return "MoreThuente";
61 default:
62 return "Unknown";
63 }
64 }
65
70 template <typename Scalar>
71 struct Settings {
72 Scalar c1{1.0e-4};
73 Scalar c2{0.9};
74 Scalar goldstein{0.25};
75 Scalar shrink{0.5};
76 Scalar alpha_max{1.0e10};
78 Scalar f_slack{
79 100 * std::numeric_limits<Scalar>::epsilon()};
80
88 std::numeric_limits<Scalar>::epsilon()};
89
91 };
92
96 template <typename Scalar>
97 Scalar armijo_bound(const Scalar phi_0,
98 const Scalar dphi_0,
99 const Scalar alpha,
100 const Settings<Scalar> &s,
101 const Scalar c) {
102 return phi_0 + c * alpha * dphi_0 + s.f_slack * std::abs(phi_0);
103 }
104
110 template <typename Scalar>
111 Scalar cubic_minimizer(const Scalar a,
112 const Scalar phi_a,
113 const Scalar dphi_a,
114 const Scalar b,
115 const Scalar phi_b,
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};
125 if (den != 0) {
126 const Scalar t{b - (b - a) * (dphi_b + d2 - d1) / den};
127 if (std::isfinite(t)) {
128 x = t;
129 }
130 }
131 }
132 return std::clamp(x, lo + margin, hi - margin);
133 }
134
148 template <typename Scalar, typename Phi>
149 bool armijo(Phi &&phi,
150 const Scalar phi_0,
151 const Scalar dphi_0,
152 Scalar &alpha,
153 Scalar &phi_alpha,
154 const Settings<Scalar> &s = Settings<Scalar>()) {
155 if (!(dphi_0 < 0)) {
156 return false;
157 }
158 for (Integer i{0}; i < s.max_iterations; ++i) {
159 if (!phi(alpha, phi_alpha, nullptr) || !std::isfinite(phi_alpha)) {
160 alpha *= s.shrink;
161 continue;
162 }
163 if (phi_alpha <= armijo_bound(phi_0, dphi_0, alpha, s, s.c1)) {
164 return true;
165 }
166 // Minimizer of the quadratic through phi(0), phi'(0) and phi(alpha),
167 // safeguarded in [0.1, 0.5] * alpha
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);
173 }
174 return false;
175 }
176
191 template <typename Scalar, typename Phi>
192 bool goldstein(Phi &&phi,
193 const Scalar phi_0,
194 const Scalar dphi_0,
195 Scalar &alpha,
196 Scalar &phi_alpha,
197 const Settings<Scalar> &s = Settings<Scalar>()) {
198 if (!(dphi_0 < 0)) {
199 return false;
200 }
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)) {
207 hi = alpha; // Too long
208 } else if (phi_alpha < phi_0 + (1 - c) * alpha * dphi_0 -
209 s.f_slack * std::abs(phi_0)) {
210 // Too short (decreases below the rounding level of phi are not
211 // significant and are accepted as they are)
212 lo = alpha;
213 } else {
214 return true;
215 }
216 alpha = std::isfinite(hi) ? static_cast<Scalar>(0.5) * (lo + hi)
217 : std::min(2 * alpha, s.alpha_max);
218 }
219 // Accept the last point satisfying the sufficient decrease condition
220 if (lo > 0) {
221 alpha = lo;
222 return phi(alpha, phi_alpha, nullptr) && std::isfinite(phi_alpha);
223 }
224 return false;
225 }
226
242 template <typename Scalar, typename Phi>
243 bool strong_wolfe(Phi &&phi,
244 const Scalar phi_0,
245 const Scalar dphi_0,
246 Scalar &alpha,
247 Scalar &phi_alpha,
248 Scalar &dphi_alpha,
249 const Settings<Scalar> &s = Settings<Scalar>()) {
250 if (!(dphi_0 < 0)) {
251 return false;
252 }
253 Integer evaluations{0};
254 const Scalar noise{s.f_slack * std::abs(phi_0)};
255 auto eval = [&](const Scalar a, Scalar &v, Scalar &d) -> bool {
256 ++evaluations;
257 return phi(a, v, &d) && std::isfinite(v) && std::isfinite(d);
258 };
259
260 // Zoom phase: lo satisfies sufficient decrease and has the lowest value
261 // so far, the interval [lo, hi] contains points satisfying the strong
262 // Wolfe conditions
263 auto zoom = [&](Scalar lo,
264 Scalar phi_lo,
265 Scalar dphi_lo,
266 Scalar hi,
267 Scalar phi_hi,
268 Scalar dphi_hi) -> bool {
269 while (evaluations < s.max_iterations) {
270 Scalar a{cubic_minimizer(lo, phi_lo, dphi_lo, hi, phi_hi, dphi_hi)};
271 Scalar v, d;
272 if (!eval(a, v, d)) {
273 hi = a;
274 phi_hi = std::numeric_limits<Scalar>::infinity();
275 continue;
276 }
277 // Value comparisons are only meaningful above the rounding level of
278 // phi: below it, the derivative decides
279 if (v > armijo_bound(phi_0, dphi_0, a, s, s.c1) || v > phi_lo + noise) {
280 hi = a;
281 phi_hi = v;
282 dphi_hi = d;
283 } else {
284 if (std::abs(d) <= -s.c2 * dphi_0) {
285 alpha = a;
286 phi_alpha = v;
287 dphi_alpha = d;
288 return true;
289 }
290 if (d * (hi - lo) >= 0) {
291 hi = lo;
292 phi_hi = phi_lo;
293 dphi_hi = dphi_lo;
294 }
295 lo = a;
296 phi_lo = v;
297 dphi_lo = d;
298 }
299 if (std::abs(hi - lo) <=
300 std::numeric_limits<Scalar>::epsilon() * std::max(Scalar(1), lo)) {
301 break;
302 }
303 }
304 // Return the best point found if it gives sufficient decrease
305 if (lo > 0) {
306 alpha = lo;
307 return eval(alpha, phi_alpha, dphi_alpha);
308 }
309 return false;
310 };
311
312 // Bracketing phase
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) {
316 Scalar v, d;
317 if (!eval(a, v, d)) {
318 // Undefined function: shrink towards the last good point
319 a = a_prev + static_cast<Scalar>(0.5) * (a - a_prev);
320 continue;
321 }
322 if (v > armijo_bound(phi_0, dphi_0, a, s, s.c1) ||
323 (a_prev > 0 && v > phi_prev + noise)) {
324 return zoom(a_prev, phi_prev, dphi_prev, a, v, d);
325 }
326 if (std::abs(d) <= -s.c2 * dphi_0) {
327 alpha = a;
328 phi_alpha = v;
329 dphi_alpha = d;
330 return true;
331 }
332 if (d >= 0) {
333 return zoom(a, v, d, a_prev, phi_prev, dphi_prev);
334 }
335 a_prev = a;
336 phi_prev = v;
337 dphi_prev = d;
338 if (a >= s.alpha_max) {
339 break;
340 }
341 a = std::min(2 * a, s.alpha_max);
342 }
343 if (a_prev > 0) {
344 alpha = a_prev;
345 return eval(alpha, phi_alpha, dphi_alpha);
346 }
347 return false;
348 }
349
357 template <typename Scalar>
358 void more_thuente_step(Scalar &stx,
359 Scalar &fx,
360 Scalar &dx,
361 Scalar &sty,
362 Scalar &fy,
363 Scalar &dy,
364 Scalar &stp,
365 const Scalar fp,
366 const Scalar dp,
367 bool &brackt,
368 const Scalar stpmin,
369 const Scalar stpmax,
370 const Scalar noise = 0) {
371 const Scalar sgnd{dp * (dx / std::abs(dx))};
372 Scalar stpf;
373 // Function values within the rounding level are considered equal
374 const bool higher{fp > fx + noise};
375 if (higher) {
376 // Case 1: higher function value, the minimum is bracketed
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(
380 Scalar(0),
381 (theta / s) * (theta / s) - (dx / s) * (dp / s)))};
382 if (stp < stx) {
383 gamma = -gamma;
384 }
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) *
389 (stp - stx)};
390 stpf = std::abs(stpc - stx) < std::abs(stpq - stx)
391 ? stpc
392 : stpc + (stpq - stpc) / 2;
393 brackt = true;
394 } else if (sgnd < 0) {
395 // Case 2: lower function value and derivatives of opposite sign
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(
399 Scalar(0),
400 (theta / s) * (theta / s) - (dx / s) * (dp / s)))};
401 if (stp > stx) {
402 gamma = -gamma;
403 }
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;
409 brackt = true;
410 } else if (std::abs(dp) < std::abs(dx)) {
411 // Case 3: lower function value, same sign, decreasing magnitude
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(
415 Scalar(0),
416 (theta / s) * (theta / s) - (dx / s) * (dp / s)))};
417 if (stp > stx) {
418 gamma = -gamma;
419 }
420 const Scalar p{(gamma - dp) + theta};
421 const Scalar q{(gamma + (dx - dp)) + gamma};
422 const Scalar r{p / q};
423 Scalar stpc;
424 if (r < 0 && gamma != 0) {
425 stpc = stp + r * (stx - stp);
426 } else {
427 stpc = stp > stx ? stpmax : stpmin;
428 }
429 const Scalar stpq{stp + (dp / (dp - dx)) * (stx - stp)};
430 if (brackt) {
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);
434 } else {
435 stpf = std::abs(stpc - stp) > std::abs(stpq - stp) ? stpc : stpq;
436 stpf = std::clamp(stpf, stpmin, stpmax);
437 }
438 } else {
439 // Case 4: lower function value, same sign, non-decreasing magnitude
440 if (brackt) {
441 const Scalar theta{3 * (fp - fy) / (sty - stp) + dy + dp};
442 const Scalar s{
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)))};
447 if (stp > sty) {
448 gamma = -gamma;
449 }
450 const Scalar p{(gamma - dp) + theta};
451 const Scalar q{((gamma - dp) + gamma) + dy};
452 stpf = stp + (p / q) * (sty - stp);
453 } else {
454 stpf = stp > stx ? stpmax : stpmin;
455 }
456 }
457
458 // Update the interval that contains a minimizer
459 if (higher) {
460 sty = stp;
461 fy = fp;
462 dy = dp;
463 } else {
464 if (sgnd < 0) {
465 sty = stx;
466 fy = fx;
467 dy = dx;
468 }
469 stx = stp;
470 fx = fp;
471 dx = dp;
472 }
473 stp = stpf;
474 }
475
492 template <typename Scalar, typename Phi>
493 bool more_thuente(Phi &&phi,
494 const Scalar phi_0,
495 const Scalar dphi_0,
496 Scalar &alpha,
497 Scalar &phi_alpha,
498 Scalar &dphi_alpha,
499 const Settings<Scalar> &s = Settings<Scalar>()) {
500 if (!(dphi_0 < 0) || !(alpha > 0)) {
501 return false;
502 }
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};
513
514 // Best point satisfying the sufficient decrease condition
515 Scalar best_stp{0}, best_f{phi_0}, best_g{dphi_0};
516
517 for (Integer i{0}; i < s.max_iterations; ++i) {
518 if (!phi(stp, f, &g) || !std::isfinite(f) || !std::isfinite(g)) {
519 // Undefined function: move back towards the best endpoint
520 stp = stx + (stp - stx) / 2;
521 continue;
522 }
523 const Scalar ftest{armijo_bound(phi_0, dphi_0, stp, s, s.c1)};
524 if (f <= ftest && (best_stp == 0 || f < best_f)) {
525 best_stp = stp;
526 best_f = f;
527 best_g = g;
528 }
529
530 // Convergence: strong Wolfe conditions
531 if (f <= ftest && std::abs(g) <= -s.c2 * dphi_0) {
532 alpha = stp;
533 phi_alpha = f;
534 dphi_alpha = g;
535 return true;
536 }
537
538 // Termination by rounding errors or interval collapse
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))) {
543 break;
544 }
545
546 // Use the auxiliary function in the first stage, as long as the
547 // sufficient decrease condition has not been met with g >= 0
548 if (stage1 && f <= ftest && g >= std::min(s.c1, s.c2) * dphi_0) {
549 stage1 = false;
550 }
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;
559 gx = gxm + gtest;
560 gy = gym + gtest;
561 } else {
562 more_thuente_step(stx, fx, gx, sty, fy, gy, stp, f, g, brackt, stmin,
563 stmax, noise);
564 }
565
566 // Force a sufficient decrease of the interval width
567 if (brackt) {
568 if (std::abs(sty - stx) >= Scalar(0.66) * width1) {
569 stp = stx + (sty - stx) / 2;
570 }
571 width1 = width;
572 width = std::abs(sty - stx);
573 stmin = std::min(stx, sty);
574 stmax = std::max(stx, sty);
575 } else {
576 stmin = stp + Scalar(1.1) * (stp - stx);
577 stmax = stp + 4 * (stp - stx);
578 }
579 stp = std::clamp(stp, stpmin, stpmax);
580 if ((brackt && (stp <= stmin || stp >= stmax)) ||
581 (brackt && stmax - stmin <= s.x_tolerance * stmax)) {
582 stp = stx;
583 }
584 }
585
586 // Fall back to the best point giving sufficient decrease
587 if (best_stp > 0) {
588 alpha = best_stp;
589 phi_alpha = best_f;
590 dphi_alpha = best_g;
591 return true;
592 }
593 return false;
594 }
595
613 template <typename Scalar, typename Phi>
614 bool search(const Method method,
615 Phi &&phi,
616 const Scalar phi_0,
617 const Scalar dphi_0,
618 Scalar &alpha,
619 Scalar &phi_alpha,
620 Scalar &dphi_alpha,
621 const Settings<Scalar> &s) {
622 switch (method) {
623 case Method::ARMIJO:
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,
629 s);
630 case Method::STRONG_WOLFE:
631 default:
632 return strong_wolfe(phi, phi_0, dphi_0, alpha, phi_alpha, dphi_alpha,
633 s);
634 }
635 }
636
637 } // namespace LineSearch
638
639} // namespace Optimist
640
641#endif // OPTIMIST_LINESEARCH_HH
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