cMHN 1.2
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
bracketing.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef pRC_ALGORITHMS_OPTIMIZER_LINE_SEARCH_BRACKETING_H
4#define pRC_ALGORITHMS_OPTIMIZER_LINE_SEARCH_BRACKETING_H
5
9
11{
13 {
14 private:
15 static constexpr Bool defaultStrongWolfeConditions()
16 {
17 return true;
18 }
19
20 static constexpr Float<> defaultDecreasingStepScale()
21 {
22 return 0.5;
23 }
24
25 static constexpr Float<> defaultIncreasingStepScale()
26 {
27 return 2.1;
28 }
29
30 static constexpr Size defaultMaxIterations()
31 {
32 return 20;
33 }
34
35 static constexpr Float<> defaultC1()
36 {
37 return 1e-4;
38 }
39
40 static constexpr Float<> defaultC2()
41 {
42 return 0.9;
43 }
44
45 public:
46 constexpr Bracketing(
47 Bool const strongWolfeConditions = defaultStrongWolfeConditions(),
48 Float<> const decreasingStepScale = defaultDecreasingStepScale(),
49 Float<> const increasingStepScale = defaultIncreasingStepScale(),
50 Size const maxIterations = defaultMaxIterations(),
51 Float<> const c1 = defaultC1(), Float<> const c2 = defaultC2())
52 : mStrongWolfeConditions(strongWolfeConditions)
53 , mDecreasingStepScale(decreasingStepScale)
54 , mIncreasingStepScale(increasingStepScale)
55 , mMaxIterations(maxIterations)
56 , mC1(c1)
57 , mC2(c2)
58 {
59 }
60
61 constexpr auto strongWolfeConditions() const
62 {
63 return mStrongWolfeConditions;
64 }
65
66 template<class T = Float<>>
67 constexpr decltype(auto) decreasingStepScale() const
68 {
69 return cast<T>(mDecreasingStepScale);
70 }
71
72 template<class T = Float<>>
73 constexpr decltype(auto) increasingStepScale() const
74 {
75 return cast<T>(mIncreasingStepScale);
76 }
77
78 constexpr auto maxIterations() const
79 {
80 return mMaxIterations;
81 }
82
83 template<class T = Float<>>
84 constexpr decltype(auto) c1() const
85 {
86 return cast<T>(mC1);
87 }
88
89 template<class T = Float<>>
90 constexpr decltype(auto) c2() const
91 {
92 return cast<T>(mC2);
93 }
94
95 template<IsTensor X, IsFloat T = Value<X>, class F, class FC>
100 constexpr auto operator()(X &x, ResultOf<F, X const &, X &> &f, X &g,
101 typename ResultOf<ScalarProduct, X, X>::Type &d, F &&function,
102 FC &&constraint, X const &p, T alpha = identity<T>(),
103 T const alphaMin = zero<T>(),
104 T const alphaMax = identity<T>(NumericLimits<T>::max())) const
105 {
106 if constexpr(cDebugLevel >= DebugLevel::High)
107 {
108 if(alphaMin < zero<T>())
109 {
110 Logging::error("LS-Bracketing: Minimum alpha < 0.");
111 }
112
113 if(alphaMax < zero<T>())
114 {
115 Logging::error("LS-Bracketing: Maximum alpha < 0.");
116 }
117
118 if(alphaMax < alphaMin)
119 {
121 "LS-Bracketing: Minimum alpha > Maximum alpha.");
122 }
123
124 if(alpha <= alphaMin || alpha > alphaMax)
125 {
126 Logging::error("Initial alpha not in range (min, max]");
127 }
128 }
129
130 Tensor const x0 = x;
131 auto const f0 = f;
132 auto const d0 = d;
133
134 auto low = alphaMin;
135 auto high = alphaMax;
136
137 for(Index iteration = 0;; ++iteration,
138 alpha = min(decreasingStepScale<T>() * (low + high),
139 increasingStepScale<T>() * alpha))
140 {
141 if(iteration > maxIterations())
142 {
143 Logging::debug("Line search: Max iterations reached.");
144 break;
145 }
146
147 x = constraint(x0 + alpha * p);
148 f = function(x, g);
149
150 if(f > f0 + c1<T>() * alpha * d0)
151 {
152 high = alpha;
154 "First Wolfe condition not satisfied, trying again");
155 continue;
156 }
157
158 d = scalarProduct(g, p)();
159 if(d < c2<T>() * d0)
160 {
161 low = alpha;
163 "Second Wolfe condition not satisfied, trying again");
164 continue;
165 }
166
168 {
169 if(abs(d) > abs(c2<T>() * d0))
170 {
171 high = alpha;
173 "Strong Wolfe condition not satisfied, trying "
174 "again");
175 continue;
176 }
177 }
178
179 break;
180 }
181
182 return alpha;
183 }
184
185 template<IsTensor X, IsFloat T = Value<X>, class F>
188 constexpr auto operator()(X &x, ResultOf<F, X const &, X &> &f, X &g,
189 typename ResultOf<ScalarProduct, X, X>::Type &d, F &&function,
190 X const &p, T alpha = identity<T>(), T const alphaMin = zero<T>(),
191 T const alphaMax = identity<T>(NumericLimits<T>::max())) const
192 {
193 return operator()(
194 x, f, g, d, forward<F>(function),
195 [](auto &&x) -> decltype(auto)
196 {
197 return forward<decltype(x)>(x);
198 },
199 p, alpha, alphaMin, alphaMax);
200 }
201
202 private:
203 Bool const mStrongWolfeConditions;
204 Float<> const mDecreasingStepScale;
205 Float<> const mIncreasingStepScale;
206 Size const mMaxIterations;
207 Float<> const mC1;
208 Float<> const mC2;
209 };
210}
211#endif // pRC_ALGORITHMS_OPTIMIZER_LINE_SEARCH_BRACKETING_H
Definition value.hpp:12
Definition tensor.hpp:25
Definition concepts.hpp:43
Definition value.hpp:24
Definition concepts.hpp:31
int x
Definition gmock-matchers-containers_test.cc:376
const char * p
Definition gmock-matchers-containers_test.cc:379
static void info(Xs &&...args)
Definition log.hpp:27
static void debug(Xs &&...args)
Definition log.hpp:33
static void error(Xs &&...args)
Definition log.hpp:14
Definition bracketing.hpp:11
static constexpr auto cast(T const &a)
Definition cast.hpp:11
Size Index
Definition basics.hpp:32
std::size_t Size
Definition basics.hpp:31
std::invoke_result_t< F, Args... > ResultOf
Definition basics.hpp:59
static constexpr auto abs(T const &a)
Definition abs.hpp:11
static constexpr decltype(auto) min(X &&a)
Definition min.hpp:13
static constexpr auto scalarProduct(TA const &a, TB const &b)
Definition scalar_product.hpp:11
constexpr auto cDebugLevel
Definition config.hpp:48
static constexpr auto identity()
Definition identity.hpp:13
static constexpr auto zero()
Definition zero.hpp:12
Definition gtest_pred_impl_unittest.cc:54
Definition limits.hpp:13
Definition bracketing.hpp:13
constexpr decltype(auto) decreasingStepScale() const
Definition bracketing.hpp:67
constexpr auto operator()(X &x, ResultOf< F, X const &, X & > &f, X &g, typename ResultOf< ScalarProduct, X, X >::Type &d, F &&function, X const &p, T alpha=identity< T >(), T const alphaMin=zero< T >(), T const alphaMax=identity< T >(NumericLimits< T >::max())) const
Definition bracketing.hpp:188
constexpr auto strongWolfeConditions() const
Definition bracketing.hpp:61
constexpr decltype(auto) c2() const
Definition bracketing.hpp:90
constexpr decltype(auto) increasingStepScale() const
Definition bracketing.hpp:73
constexpr auto operator()(X &x, ResultOf< F, X const &, X & > &f, X &g, typename ResultOf< ScalarProduct, X, X >::Type &d, F &&function, FC &&constraint, X const &p, T alpha=identity< T >(), T const alphaMin=zero< T >(), T const alphaMax=identity< T >(NumericLimits< T >::max())) const
Definition bracketing.hpp:100
constexpr Bracketing(Bool const strongWolfeConditions=defaultStrongWolfeConditions(), Float<> const decreasingStepScale=defaultDecreasingStepScale(), Float<> const increasingStepScale=defaultIncreasingStepScale(), Size const maxIterations=defaultMaxIterations(), Float<> const c1=defaultC1(), Float<> const c2=defaultC2())
Definition bracketing.hpp:46
constexpr auto maxIterations() const
Definition bracketing.hpp:78
constexpr decltype(auto) c1() const
Definition bracketing.hpp:84