1 /*
2 * Licensed to the Hipparchus project under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The Hipparchus project licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.hipparchus.optim.nonlinear.vector.constrained;
18
19 import org.hipparchus.util.FastMath;
20
21 import java.util.LinkedList;
22 import java.util.Queue;
23 import org.hipparchus.util.Precision;
24
25 /**
26 * Robust Line Search strategy.
27 * <p>
28 * This class manages monotone and non-monotone line search. Switching is
29 * automatic after repeated failures of the monotone search.
30 * </p>
31 *
32 * <p>
33 * Typical parameter values used in Schittkowski's algorithms:
34 * </p>
35 * <ul>
36 * <li>maxHistory = 5 to 10 (memory for non-monotone search)</li>
37 * <li>sigma = 1e-4 or 1e-5 (Armijo sufficient decrease parameter)</li>
38 * <li>beta = 0.5 (step reduction factor)</li>
39 * <li>alphaMin (minimum allowed step length)</li>
40 * <li>alphaMax = 1.0 (maximum allowed step length)</li>
41 * <li>maxMonotoneFailures = 20 to 50 (failures before switching to
42 * non-monotone)</li>
43 * <li>maxBadSteps = 3 to 5 (allowed consecutive bad steps before Hessian
44 * reset)</li>
45 * </ul>
46 *
47 * @since 4.1
48 */
49 public class LineSearch {
50
51 /**
52 * Maximum penalty history size.
53 */
54 private final int maxHistory;
55
56 /**
57 * Parameter for evaluation of Armijo condition for descend direction.
58 */
59 private final double sigma;
60
61 /**
62 * Step reduction factor.
63 */
64 private final double beta;
65
66 /**
67 * Minimum step length.
68 */
69 private final double alphaMin;
70
71 /**
72 * Max step length.
73 */
74 private double alphaMax;
75
76 /**
77 * Failures before switching to non-monotone.
78 */
79 private final int maxMonotoneFailures;
80
81 /**
82 * Allowed consecutive bad steps before Hessian reset.
83 */
84 private final int maxBadSteps;
85
86 /**
87 * Penalty history.
88 */
89 private final Queue<Double> history;
90
91 /**
92 * Number of iterations in line search.
93 */
94 private int searchCount;
95
96 /**
97 * Number of monotone search failures.
98 */
99 private int monotoneFailures;
100
101 /**
102 * Number of consecutive bad steps.
103 */
104 private int badStepCount;
105
106 /**
107 * Indicator for non-monotone search.
108 */
109 private boolean nonMonotoneEnabled;
110
111 /**
112 * Indicator for detected bad step.
113 */
114 private boolean badStepDetected;
115
116 /**
117 * Indicator for too many consecutive bad steps.
118 */
119 private boolean badStepFailed;
120
121 /**
122 * Simple constructor.
123 *
124 * @param minAlpha minimum step for line search
125 * @param maxHistory maximum penalty history size
126 * @param mu parameter for evaluation of Armijo condition for descend
127 * direction
128 * @param beta step reduction factor
129 * @param maxMonotoneFailures failures before switching to non-monotone
130 * @param maxBadSteps allowed consecutive bad steps before Hessian reset
131 */
132 public LineSearch(final double minAlpha, final int maxHistory, final double mu, final double beta,
133 final int maxMonotoneFailures, final int maxBadSteps) {
134 this.maxHistory = maxHistory;
135 this.sigma = mu;
136 this.beta = beta;
137 this.alphaMin =minAlpha;
138 this.maxMonotoneFailures = maxMonotoneFailures;
139 this.maxBadSteps = maxBadSteps;
140 this.history = new LinkedList<>();
141 this.nonMonotoneEnabled = false;
142 resetBadStepCount();
143 }
144
145 /**
146 * Check if bad step has been detected.
147 *
148 * @return true if bad step has been detected
149 */
150 public boolean isBadStepDetected() {
151 return badStepDetected;
152 }
153
154 /**
155 * Check if too many consecutive bad step have been detected.
156 *
157 * @return true if too many consecutive bad step have been detected
158 */
159 public boolean isBadStepFailed() {
160 return badStepFailed;
161 }
162
163 /**
164 * Get number of iterations in line search.
165 *
166 * @return number of iterations in line search
167 */
168 public int getIteration() {
169 return searchCount;
170 }
171
172 /**
173 * Reset bad steps count and indicators.
174 */
175 public void resetBadStepCount() {
176 badStepCount = 0;
177 monotoneFailures = 0;
178 badStepDetected = false;
179 badStepFailed = false;
180 }
181
182 /**
183 * Save penalty value when step is accepted for reusing in case of
184 * non-monotone research.
185 *
186 * @param fx penalty
187 */
188 public void updateHistory(final double fx) {
189 if (nonMonotoneEnabled) {
190 history.add(fx);
191 if (history.size() > maxHistory) {
192 history.poll();
193 }
194 }
195 }
196
197 /**
198 * Verify Armijo condition for accept step.
199 *
200 * @param fxNew penalty at candidate point x+dx*alpha
201 * @param fxCurrent penalty at the current point
202 * @param alpha step length
203 * @param directionalDeriv penalty gradient
204 * @return true or false
205 */
206 public boolean acceptStep(double fxNew, double fxCurrent, double alpha, double directionalDeriv) {
207 double ref = fxCurrent;
208 if (nonMonotoneEnabled) {
209 for (double v : history) {
210 ref = FastMath.max(ref, v);
211 }
212
213 }
214 // alfaPenalty - currentPenalty > getSettings().getMu() * alpha * currentPenaltyGrad
215 return fxNew - ref <= sigma * alpha * directionalDeriv;
216 }
217
218 /**
219 * Mark Good Step if line search worked.
220 */
221 public void markGoodStep() {
222 nonMonotoneEnabled = false;
223 monotoneFailures = 0;
224 badStepCount = 0;
225 badStepDetected = false;
226 badStepFailed = false;
227 }
228
229 /**
230 * Mark Bad Step if line search failed.
231 */
232 private void markBadStep() {
233 nonMonotoneEnabled = false;
234 badStepCount++;
235 badStepDetected = true;
236 monotoneFailures = 0;
237 if (badStepCount > maxBadSteps) {
238 badStepFailed = true;
239 }
240 }
241
242 /**
243 * Update alpha qith quadratic curvature.
244 *
245 * @param alpha step length
246 * @param fxCurrent penalty at the current point
247 * @param fxNew penalty at candidate point x+dx*alpha
248 * @param directionalDeriv penalty gradient
249 * @return alpha
250 */
251 private double updateStepLength(final double alpha, final double fxCurrent, final double fxNew,
252 final double directionalDeriv) {
253
254 double numerator = alpha * directionalDeriv;
255 double denominator = alpha * directionalDeriv - fxNew + fxCurrent;
256
257 if (FastMath.abs(denominator) < Precision.SAFE_MIN) {
258 return alpha * beta;
259 }
260
261 double alphaStar = 0.5 * alpha * numerator / denominator;
262
263 return FastMath.max( alpha * beta, FastMath.min(alphaStar,0.5*alpha));
264
265 }
266
267 /**
268 * Main line search process: tries monotone first, then non-monotone.
269 *
270 * @param f penalty function
271 * @return alpha
272 */
273 public double search(final MeritFunctionL2 f) {
274 searchCount = 0;
275 final double fxCurrent = f.value0();
276 final double directionalDeriv = f.getGradient();
277
278 alphaMax = 1.0;
279
280 // Monotone Search
281 nonMonotoneEnabled = false;
282 double alpha = searchLoop(f, fxCurrent, directionalDeriv);
283 if (!Double.isNaN(alpha)) {
284 // monotone search succeeded
285 return alpha;
286 }
287
288 // Non-monotone search
289 nonMonotoneEnabled = true;
290 alpha = searchLoop(f, fxCurrent, directionalDeriv);
291 if (!Double.isNaN(alpha)) {
292 // non-monotone search succeeded
293 return alpha;
294 }
295
296 // last attempt before we declare bad step
297 final double fxNew = f.value(alphaMin);
298 searchCount++;
299 if (acceptStep(fxNew, fxCurrent, alphaMin, directionalDeriv)) {
300 markGoodStep();
301 updateHistory(fxNew);
302 } else {
303 markBadStep();
304 }
305 if (badStepFailed) {
306 f.value(0.0);
307 return 0.0;
308 }
309
310 return alphaMin;
311 }
312
313 /**
314 * Iterative search (either monotone or non-monotone).
315 *
316 * @param f penalty function
317 * @param fxCurrent penalty function at current point
318 * @param directionalDeriv directional derivative
319 * @return accepted alpha, or NaN if search failed
320 */
321 private double searchLoop(final MeritFunctionL2 f, final double fxCurrent,
322 final double directionalDeriv) {
323
324 // start value for step length
325 double alpha = alphaMax;
326
327 // step length reduction loop
328 while (alpha >= alphaMin) {
329
330 final double fxNew = f.value(alpha);
331 if (acceptStep(fxNew, fxCurrent, alpha, directionalDeriv)) {
332 // we have found an acceptable step length
333 markGoodStep();
334 updateHistory(fxNew);
335 return alpha;
336 }
337
338 // step length was not accepted, continue iteration
339 alpha = updateStepLength(alpha, fxCurrent, fxNew, directionalDeriv);
340 monotoneFailures++;
341 if (monotoneFailures >= maxMonotoneFailures) {
342 nonMonotoneEnabled = !nonMonotoneEnabled;
343 break;
344 }
345 searchCount++;
346
347 }
348
349 // we failed to find an acceptable step length
350 return Double.NaN;
351
352 }
353
354 }