Debian Codegraph @

src/Algo/BrentLS.hpp

raw symbols SCIP
1 /****************************************************************************/
2 /* This file is part of FreeFEM. */
3 /* */
4 /* FreeFEM is free software: you can redistribute it and/or modify */
5 /* it under the terms of the GNU Lesser General Public License as */
6 /* published by the Free Software Foundation, either version 3 of */
7 /* the License, or (at your option) any later version. */
8 /* */
9 /* FreeFEM is distributed in the hope that it will be useful, */
10 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
11 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
12 /* GNU Lesser General Public License for more details. */
13 /* */
14 /* You should have received a copy of the GNU Lesser General Public License */
15 /* along with FreeFEM. If not, see <http://www.gnu.org/licenses/>. */
16 /****************************************************************************/
17 // SUMMARY : ...
18 // LICENSE : LGPLv3
19 // ORG : LJLL Universite Pierre et Marie Curie, Paris, FRANCE
20 // AUTHORS : ...
21 // E-MAIL : ...
22
23 //======================================================================
24 // Definition of the BrentLineSearch class
25 // Brent's line search algorithm
26 // author: Wenceslau Gouveia, Adapted from Numerical Recipes in C
27 // Modified to fit into new classes. H. Lydia Deng, 02/21/94, 03/15/94
28 // Tony : cf. page 301 section 10.2
29 //========================================================================
30 // .B BrentLineSearch()
31 // This routine, inpired by the Numerical Recipes book, performs a unidimensional search
32 // for the minimum of the objective function along a specified direction. The minimum is
33 // at first bracket using the Golden search procedure. After bracketing the Brent's
34 // algorithm is used to isolate the minimum to a fractional precision of about the specified
35 // tolerance.
36 //
37 // .SECTION Description
38 // Public Operations
39 // Constructors:
40 // BrentLineSearch (ObjectiveFunction *f, int iter);
41 // Here:
42 // f: Define the objective function
43 // iter: Maximum number of iterations
44 // Methods:
45 // model <> BrentLineSearch::search(Model<double>& model0, Vector<double>& direction,
46 // double tol, double delta)
47 // Here:
48 // model0: Initial model to initiate the bracketing procedure
49 // direction: Vector that defines the direction of the line search
50 // tol: The minimum is within the returned this->value +/- tol
51 // delta: Used in the bracketing procedure. The initial interval
52 // for the bracketing is from 0 to delta * STEP_MAX, where
53 // STEP_MAX is hard coded to 5.
54 //
55 // The sought minimum is returned by the function.
56 //
57 // .SECTION Caveats
58 // This line search was not thoroughly tested. The CubicLineSearch procedure, that requires
59 // certain derivative information on the objective function (that can be provided by numerical
60 // methods) has demonstrated to be a more efficient line search procedure.
61
62 // Cela me semble louche... cf. les commentaires BIZARRE !
63 // Ceci dit, le premiers tests semblent OK...
64
65 #include "defs.hpp"
66 #include <cstdio>
67
68 #ifndef BRENT_LINE_SEARCH_HH
69 #define BRENT_LINE_SEARCH_HH
70
71 #include "LineSearch.hpp"
72
73 #define CGOLD .3819660
74 #define ZEPS 1.e-10
75 #define GOLD 1.618034
76 #define STEP_MAX 5
77 #define GLIMIT 100.
78
79 /*
80 BrentLineSearch class was inpired by the Numerical Recipes book,
81 performs an unidimensional search for the minimum of the objective
82 function along a specified direction. The minimum is at first
83 bracket using the Golden search procedure. After bracketing
84 the Brent's algorithm is used to isolate the minimum to a
85 fractional precision of about the specified tolerance.
86
87 This line search was not thoroughly tested. The
88 CubicLineSearch procedure, that requires certain
89 derivative information on the objective function
90 (that can be provided by numerical methods) has
91 demonstrated to be a more efficient line search procedure.
92
93 */
94
95 template< class LS >
96 class BrentLineSearch : public LS {
97 typedef typename LS::Real Real;
98 typedef typename LS::Param Param;
99 typedef typename LS::Vect Vect;
100 typedef typename LS::VMat VMat;
101 typedef typename LS::Mat Mat;
102 typedef typename LS::NRJ NRJ;
103
104 public:
105 BrentLineSearch(NRJ *f, int iter);
106 ~BrentLineSearch( );
107
108 // Implementation of the Brent Search
109 // search for minimum model along a 1-D direction
110 Param search(Param &m0, // initial model to initiate the bracketing procedure
111 Vect &direction, // the direction of the line search
112 Real tol, // the minimum is within the returned this->value +/- tol
113 double delta); // a parameter used in the bracketing procedure
114 /*The initial interval for the bracketing is from $0$ to
115 $delta \times STEP\_MAX$, where $STEP\_MAX$ is hard coded to $5$.
116 The sought minimum is returned by the function. */
117 //@ManMemo: search for minimum model along a 1-D direction
118 };
119
120 template< class LS >
121 BrentLineSearch< LS >::BrentLineSearch(NRJ *f, int it) : LS(f) {
122 this->iterMax = it;
123 }
124
125 template< class LS >
126 BrentLineSearch< LS >::~BrentLineSearch( ) {
127 ;
128 }
129
130 // Code for the BrentBrent line search
131 template< class LS >
132 typename BrentLineSearch< LS >::Param BrentLineSearch< LS >::search(Param &model0, Vect &direction,
133 Real tol, double delta) {
134 this->iterNum = 0;
135 KN< double > steps(3); // brackets
136 Vect of_values(3); // OF evaluated inside bracket
137 Real dum; // auxiliary quantity
138 Real r, q, ulim; // auxiliary quantity
139 Real u, fu; // define the new bracket limit
140
141 // Variables related to Brent's algorithm
142 Real d, fv, fw, fx, p; // auxiliary variables
143 double v, w; // auxiliary variables
144 double x, e = 0., etemp; // auxiliary variables
145 int i; // counter
146
147 // Ajout Tony
148 d = 0; // Sinon, il n'est pas initialise : BIZARRE !
149
150 // Beggining of the bracketing stage
151 steps[0] = 0.;
152 steps[1] = STEP_MAX * delta;
153
154 of_values[0] = this->nrj->getVal(model0);
155 of_values[1] = this->nrj->getVal(update(model0, 1, steps[1], direction));
156 this->iterNum += 2;
157
158 if (of_values[1] > of_values[0]) {
159 dum = steps[0];
160 steps[0] = steps[1];
161 steps[1] = dum;
162 dum = of_values[0];
163 of_values[0] = of_values[1];
164 of_values[1] = dum;
165 }
166
167 steps[2] = steps[1] + GOLD * (steps[1] - steps[0]);
168 of_values[2] = this->nrj->getVal(update(model0, 1, steps[2], direction));
169 this->iterNum++;
170 while (of_values[1] > of_values[2]) {
171 r = (steps[1] - steps[0]) * (of_values[1] - of_values[2]);
172 q = (steps[1] - steps[2]) * (of_values[1] - of_values[0]);
173 u = steps[1] - ((steps[1] - steps[2]) * q - (steps[1] - steps[0]) * r) /
174 (2. * Abs(Max(Abs(q - r), TINY)) / Sgn(q - r));
175 ulim = steps[1] + GLIMIT * (steps[2] - steps[1]);
176
177 if ((steps[1] - u) * (u - steps[2]) > 0.) {
178 fu = this->nrj->getVal(update(model0, 1, u, direction));
179 this->iterNum++;
180 if (fu < of_values[2]) {
181 steps[0] = steps[1];
182 steps[1] = u;
183 of_values[0] = of_values[1];
184 of_values[1] = fu;
185 break;
186 } else if (fu > of_values[1]) {
187 steps[2] = u;
188 of_values[2] = fu;
189 break;
190 }
191
192 u = steps[2] + GOLD * (steps[2] - steps[1]);
193 fu = this->nrj->getVal(update(model0, 1, u, direction));
194 this->iterNum++;
195 } else if ((steps[2] - u) * (u - ulim) > 0.) {
196 fu = this->nrj->getVal(update(model0, 1, u, direction));
197 this->iterNum++;
198 if (fu < of_values[2]) {
199 steps[1] = steps[2];
200 steps[2] = u;
201 u = steps[2] + GOLD * (steps[2] - steps[1]);
202
203 of_values[1] = of_values[2];
204 of_values[2] = fu;
205 fu = this->nrj->getVal(update(model0, 1, u, direction));
206 this->iterNum++;
207 }
208 } else if ((u - ulim) * (ulim * steps[2]) >= 0.) {
209 u = ulim;
210 fu = this->nrj->getVal(update(model0, 1, u, direction));
211 this->iterNum++;
212 } else {
213 u = steps[2] + GOLD * (steps[2] - steps[1]);
214 fu = this->nrj->getVal(update(model0, 1, u, direction));
215 this->iterNum++;
216 }
217
218 steps[0] = steps[1];
219 steps[1] = steps[2];
220 steps[2] = u;
221 of_values[0] = of_values[1];
222 of_values[1] = of_values[2];
223 of_values[2] = fu;
224 }
225
226 // Sorting STEPS in ascending order
227 for (i = 0; i < 2; i++) {
228 if (steps[0] > steps[i + 1]) {
229 dum = steps[0];
230 steps[0] = steps[i + 1];
231 steps[i + 1] = dum;
232
233 dum = of_values[0];
234 of_values[0] = of_values[i + 1];
235 of_values[i + 1] = dum;
236 }
237 }
238
239 if (steps[1] > steps[2]) {
240 dum = steps[1];
241 steps[1] = steps[2];
242 steps[2] = dum;
243
244 dum = of_values[1];
245 of_values[1] = of_values[2];
246 of_values[2] = dum;
247 }
248
249 // The line minimization will be performed now using as
250 // bracket the 3 first steps given in vector steps
251 // The algorithm is due to Brent
252
253 // initializations
254 x = w = v = steps[1];
255 fw = fv = fx = of_values[1];
256
257 for (; this->iterNum <= this->iterMax; this->iterNum++) {
258 double tol1, tol2, xm;
259 xm = .5 * (steps[0] + steps[2]);
260 tol1 = tol * Abs(x) + ZEPS;
261 tol2 = 2.0 * tol1;
262 if (Abs(x - xm) <= (tol2 - .5 * (steps[2] - steps[0]))) {
263 this->value = fx;
264 Param new_model(update(model0, 1, x, direction));
265
266 return new_model;
267 }
268 // minimum along a line
269 if (Abs(e) > tol1) {
270 r = (x - w) * (fx - fv);
271 q = (x - v) * (fx - fw);
272 p = (x - v) * q - (x - w) * r;
273 q = 2.0 * (q - r);
274 // Tony
275 // ??? ERREUR ???
276 // dans le numerical recipe, c'est p=-p...
277 // BIZARRE !
278 if (q > 0.) {
279 cerr << "BIZARRE BrentLS" << endl;
280 p = -q;
281 }
282
283 q = Abs(q);
284 etemp = e;
285 e = d;
286 if (Abs(p) >= Abs(.5 * q * etemp) || p <= q * (steps[0] - x) ||
287 p >= q * (steps[2] - x)) { // parabolic fit
288 if (x >= xm)
289 e = steps[0] - x;
290 else
291 e = steps[2] - x;
292 d = CGOLD * e;
293 } else {
294 d = p / q;
295 u = x + d;
296 if (u - steps[0] < tol2 || steps[2] - u < tol2) d = Abs(tol1) / Sgn(xm - x);
297 }
298 } else {
299 if (x >= xm)
300 e = steps[0] - x;
301 else
302 e = steps[2] - x;
303
304 d = CGOLD * e;
305 }
306
307 if (Abs(d) >= tol1)
308 u = x + d;
309 else
310 u = x + Abs(tol1) / Sgn(d);
311
312 fu = this->nrj->getVal(update(model0, 1, u, direction));
313
314 if (fu <= fx) {
315 if (u >= x)
316 steps[0] = x;
317 else
318 steps[2] = x;
319 v = w;
320 w = x;
321 x = u;
322 fv = fw;
323 fw = fx;
324 fx = fu;
325 } else {
326 if (u < x)
327 steps[0] = u;
328 else
329 steps[2] = u;
330
331 if (fu <= fw || w == x) {
332 v = w;
333 w = u;
334 fv = fw;
335 fw = fu;
336 } else if (fu <= fw || v == x || v == w) {
337 v = u;
338 fv = fu;
339 }
340 }
341 }
342
343 this->appendSearchNumber( );
344 // cout << " Maximum number of iterations reached " << endl;
345
346 Param new_model(update(model0, 1, x, direction));
347 this->value = this->nrj->getVal(new_model);
348 return new_model;
349 }
350
351 #endif