Debian Codegraph @

src/Algo/LineSearch.hpp

raw symbols SCIP
1 /****************************************************************************/cxx . . $ `<file>/src/Algo/LineSearch.hpp`/
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 #ifndef LINE_SEARCH_HH
24 #define LINE_SEARCH_HHcxx . . $ `src/Algo/LineSearch.hpp:24:9`!
25
26 #include <list>
27 #include "defs.hpp"cxx . . $ `<file>/src/Algo/defs.hpp`/
28 #include "NRJ.hpp"cxx . . $ `<file>/src/Algo/NRJ.hpp`/
29 #include <limits.h>
30 #include <float.h>
31
32 #define MAX_IT_LScxx . . $ `src/Algo/LineSearch.hpp:32:9`! 1000
33
34 /*
35 This is the base class for one dimensional
36 line search algorithms. Some optimization
37 algorithms need to find the optimimal point
38 on one searching directions.
39 Classes derived from this class inherit features of
40 LineSearch. This class cannot be used directly.
41 */
42
43 // Warning:
44 // no copy operator
45 // no = operator
46
47 template< class P, class V, class M, class VM, class R >
48 class LineSearchcxx . . $ LineSearch# {
49 public:
50 typedef R Realcxx . . $ LineSearch#Real#;
51 typedef VM VMatcxx . . $ LineSearch#VMat#;
52 typedef M Matcxx . . $ LineSearch#Mat#;
53 typedef P Paramcxx . . $ LineSearch#Param#;
54 typedef V Vectcxx . . $ LineSearch#Vect#;
55 typedef tNRJcxx . . $ tNRJ#< Paramcxx . . $ LineSearch#Param#, Vectcxx . . $ LineSearch#Vect#, VMatcxx . . $ LineSearch#VMat#, Realcxx . . $ LineSearch#Real# > NRJcxx . . $ LineSearch#NRJ#;
56
57 private:
58 // Used only if we calculate numerically the gradient
59 // in this case, we allocate memory
60 // else, this is only a pointer to the NRJ gradient
61 Vectcxx . . $ LineSearch#Vect# *gradcxx . . $ LineSearch#grad.;
62 int refcxx . . $ LineSearch#ref.; // ref equals 1 if we calcualte numerically the gradient
63
64 void numericalGradientcxx . . $ LineSearch#numericalGradient(77d9f17553126ecb).(const Paramcxx . . $ LineSearch#Param# &);
65
66 protected:
67 // maxinum number of iterations allowed
68 int iterMaxcxx . . $ LineSearch#iterMax.;
69 // the number iterations so far
70 // to reinitialize in inherited classes
71 int iterNumcxx . . $ LineSearch#iterNum.;
72 // the history of number of iteration of iteration
73 listcxx . . $ list#< int > *iterHistorycxx . . $ LineSearch#iterHistory.;
74 // the value at the minimum along the direction
75 // to update in inherited classes
76 Realcxx . . $ LineSearch#Real# valuecxx . . $ LineSearch#value.;
77 // the delta used for calculating numerical gradient
78 // same type as gradient
79 Vectcxx . . $ LineSearch#Vect# *stepcxx . . $ LineSearch#step.;
80 // pointer to the nrj
81 NRJcxx . . $ LineSearch#NRJ# *nrjcxx . . $ LineSearch#nrj.;
82
83 void appendSearchNumbercxx . . $ LineSearch#appendSearchNumber(49f6e7a06ebc5aa8).( );
84
85 public:
86 // a constructor with pointer to the nrj and
87 // to the step if numerical gradient is computed
88 explicit LineSearchcxx . . $ LineSearch#`LineSearch<P, V, M, VM, R>`(802eab7ca81a0004).(NRJcxx . . $ LineSearch#NRJ# *f, Vectcxx . . $ LineSearch#Vect# *interval = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:416:9`!);
89 virtual ~cxx . . $ LineSearch#`~LineSearch<P, V, M, VM, R>`(49f6e7a06ebc5aa8).LineSearch( );
90
91 // Start point and descent direction + parameters
92 virtual Paramcxx . . $ LineSearch#Param# searchcxx . . $ LineSearch#search(3433d6bc05064256).(const Paramcxx . . $ LineSearch#Param# &, Vectcxx . . $ LineSearch#Vect# &, Realcxx . . $ LineSearch#Real#, double);
93
94 // compute the gradient Vector
95 // linesearch gradient
96 Vectcxx . . $ LineSearch#Vect# *gradientcxx . . $ LineSearch#gradient(b8803fc2f699eb8b).(const Paramcxx . . $ LineSearch#Param# &m);
97 // linesearch hessian
98 VMatcxx . . $ LineSearch#VMat# *hessiancxx . . $ LineSearch#hessian(65162f56a443f074).(const Paramcxx . . $ LineSearch#Param# &m);
99 // evaluate the nrj
100 Realcxx . . $ LineSearch#Real# evaluatecxx . . $ LineSearch#evaluate(3363963b0cfc0bd7).(const Paramcxx . . $ LineSearch#Param# &m);
101
102 // number of iterations for all line searches
103 listcxx . . $ list#< int > allSearchIterationscxx . . $ LineSearch#allSearchIterations(34aa21dc5613b5d1).( );
104
105 // number of iterations in the current line search
106 int searchIterationscxx . . $ LineSearch#searchIterations(b126dc7c1de90089).( );
107 // value of the objective function for the current Model
108 Realcxx . . $ LineSearch#Real# currentValuecxx . . $ LineSearch#currentValue(71fc842744411a9a).( );
109
110 Paramcxx . . $ LineSearch#Param# updatecxx . . $ LineSearch#update(1c2e7b4031239c77).(const Paramcxx . . $ LineSearch#Param# &, Realcxx . . $ LineSearch#Real#, Realcxx . . $ LineSearch#Real#, const Vectcxx . . $ LineSearch#Vect# &) const;
111 };
112
113 template< class P, class V, class M, class VM, class R >
114 LineSearchcxx . . $ LineSearch#< P, V, M, VM, R >::LineSearchcxx . . $ LineSearch#`LineSearch<P, V, M, VM, R>`(802eab7ca81a0004).(NRJcxx . . $ LineSearch#NRJ# *p, Vectcxx . . $ LineSearch#Vect# *interval) {
115 iterMaxcxx . . $ LineSearch#iterMax. = MAX_IT_LScxx . . $ `src/Algo/LineSearch.hpp:32:9`!;
116 iterNumcxx . . $ LineSearch#iterNum. = 0;
117 nrjcxx . . $ LineSearch#nrj. = p;
118 stepcxx . . $ LineSearch#step. = interval;
119 iterHistorycxx . . $ LineSearch#iterHistory. = new listcxx . . $ list#< int >;
120 gradcxx . . $ LineSearch#grad. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:416:9`!;
121 refcxx . . $ LineSearch#ref. = 0;
122 valuecxx . . $ LineSearch#value. = 0.;
123 }
124
125 template< class P, class V, class M, class VM, class R >
126 LineSearchcxx . . $ LineSearch#< P, V, M, VM, R >::~cxx . . $ LineSearch#`~LineSearch<P, V, M, VM, R>`(49f6e7a06ebc5aa8).LineSearch( ) {
127 if (iterHistorycxx . . $ LineSearch#iterHistory. != NULLcxx . . $ `<external>/16391990256953433862/stddef.h:416:9`!) delete iterHistorycxx . . $ LineSearch#iterHistory.;
128 if (refcxx . . $ LineSearch#ref. == 1 && gradcxx . . $ LineSearch#grad. !=cxx . . $ std/`operator!=`(bf4c77cd91c4b201). NULLcxx . . $ `<external>/16391990256953433862/stddef.h:416:9`!) delete gradcxx . . $ LineSearch#grad.;
129 if (stepcxx . . $ LineSearch#step. !=cxx . . $ std/`operator!=`(bf4c77cd91c4b201). NULLcxx . . $ `<external>/16391990256953433862/stddef.h:416:9`!) delete stepcxx . . $ LineSearch#step.;
130 }
131
132 template< class P, class V, class M, class VM, class R >
133 void LineSearchcxx . . $ LineSearch#< P, V, M, VM, R >::appendSearchNumbercxx . . $ LineSearch#appendSearchNumber(49f6e7a06ebc5aa8).( ) {
134 iterHistorycxx . . $ LineSearch#iterHistory.[0].push_backcxx . . $ std/__cxx11/list#push_back(77d9f17553126ecb).(iterNumcxx . . $ LineSearch#iterNum.);
135 }
136
137 template< class P, class V, class M, class VM, class R >
138 listcxx . . $ list#< int > LineSearchcxx . . $ LineSearch#< P, V, M, VM, R >::allSearchIterationscxx . . $ LineSearch#allSearchIterations(34aa21dc5613b5d1).( ) {
139 return iterHistorycxx . . $ std/__cxx11/list#list(cb0950d04356f622).[0];
140 }
141
142 template< class P, class V, class M, class VM, class R >
143 int LineSearchcxx . . $ LineSearch#< P, V, M, VM, R >::searchIterationscxx . . $ LineSearch#searchIterations(b126dc7c1de90089).( ) {
144 return iterNumcxx . . $ LineSearch#iterNum.;
145 }
146
147 template< class P, class V, class M, class VM, class R >
148 typename LineSearchcxx . . $ LineSearch#< P, V, M, VM, R >::Realcxx . . $ LineSearch#Real# LineSearchcxx . . $ LineSearch#< P, V, M, VM, R >::currentValuecxx . . $ LineSearch#currentValue(71fc842744411a9a).( ) {
149 return valuecxx . . $ LineSearch#value.;
150 }
151
152 template< class P, class V, class M, class VM, class R >
153 typename LineSearchcxx . . $ LineSearch#< P, V, M, VM, R >::Paramcxx . . $ LineSearch#Param# LineSearchcxx . . $ LineSearch#< P, V, M, VM, R >::searchcxx . . $ LineSearch#search(3433d6bc05064256).(const Paramcxx . . $ LineSearch#Param# &m,
154 Vectcxx . . $ LineSearch#Vect# &v,
155 Realcxx . . $ LineSearch#Real# alpha,
156 double beta) {
157 cerrcxx . . $ `src/fflib/throwassert.hpp:33:9`! <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "You need to specify the LineSearch method!" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ endl(345bf97a19cc83a9).;
158 exit(1);
159 }
160
161 // Centered finite difference
162 template< class P, class V, class M, class VM, class R >
163 void LineSearchcxx . . $ LineSearch#< P, V, M, VM, R >::numericalGradientcxx . . $ LineSearch#numericalGradient(77d9f17553126ecb).(const Paramcxx . . $ LineSearch#Param# &m) {
164 int n = m.size( );
165
166 // At first call: gradient initialization
167 if (gradcxx . . $ LineSearch#grad. ==cxx . . $ std/`operator==`(bf4c77cd91c4b201). NULLcxx . . $ `<external>/16391990256953433862/stddef.h:416:9`!) {
168 gradcxx . . $ LineSearch#grad. = new Vectcxx . . $ LineSearch#Vect#(n);
169 refcxx . . $ LineSearch#ref. = 1;
170 }
171
172 Realcxx . . $ LineSearch#Real# diffValue, newValue, off;
173 Paramcxx . . $ LineSearch#Param# m1(m);
174
175 if (stepcxx . . $ LineSearch#step. !=cxx . . $ std/`operator!=`(bf4c77cd91c4b201). NULLcxx . . $ `<external>/16391990256953433862/stddef.h:416:9`!) {
176 for (int i = 0; i < n; i++) {
177 off = stepcxx . . $ LineSearch#step.[0][i];
178 m1[i] -= off;
179 newValue = nrjcxx . . $ LineSearch#nrj.->getValcxx . . $ tNRJ#getVal(f500175d9f5e9e70).(m1);
180 m1[i] +=cxx . . $ `operator+=`(d02e6a613fcf7dc7). 2 *cxx . . $ `operator*`(ca0d58f853b0940). off;
181 diffValue = nrjcxx . . $ LineSearch#nrj.->getValcxx . . $ tNRJ#getVal(f500175d9f5e9e70).(m1);
182 diffValue -= newValue;
183 (*gradcxx . . $ LineSearch#grad.)[i] = diffValue /cxx . . $ `operator/`(42306a5d7abcb141). (2 *cxx . . $ `operator*`(ca0d58f853b0940). off);
184 m1[i] = m[i];
185 }
186 } else { // Default: step of relative size 10^-3
187 for (int i = 0; i < n; i++) {
188 off = m[i] /cxx . . $ `operator/`(42306a5d7abcb141). 1000;
189 m1[i] -= off;
190 newValue = nrjcxx . . $ LineSearch#nrj.->getValcxx . . $ tNRJ#getVal(f500175d9f5e9e70).(m1);
191 m1[i] +=cxx . . $ `operator+=`(d02e6a613fcf7dc7). 2 *cxx . . $ `operator*`(ca0d58f853b0940). off;
192 diffValue = nrjcxx . . $ LineSearch#nrj.->getValcxx . . $ tNRJ#getVal(f500175d9f5e9e70).(m1);
193 diffValue -= newValue;
194 (*gradcxx . . $ LineSearch#grad.)[i] = diffValue /cxx . . $ `operator/`(42306a5d7abcb141). (2 *cxx . . $ `operator*`(ca0d58f853b0940). off);
195 m1[i] = m[i];
196 }
197 }
198 }
199
200 template< class P, class V, class M, class VM, class R >
201 typename LineSearchcxx . . $ LineSearch#< P, V, M, VM, R >::Vectcxx . . $ LineSearch#Vect# *LineSearchcxx . . $ LineSearch#< P, V, M, VM, R >::gradientcxx . . $ LineSearch#gradient(b8803fc2f699eb8b).(
202 const Paramcxx . . $ LineSearch#Param# &m) {
203 Vectcxx . . $ LineSearch#Vect# *g;
204
205 g = nrjcxx . . $ LineSearch#nrj.->getGradientcxx . . $ tNRJ#getGradient(b8803fc2f699eb8b).(m);
206
207 if (g ==cxx . . $ std/`operator==`(bf4c77cd91c4b201). NULLcxx . . $ `<external>/16391990256953433862/stddef.h:416:9`!) {
208 cerrcxx . . $ `src/fflib/throwassert.hpp:33:9`! <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Undefined gradient for this NRJ function!" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ endl(345bf97a19cc83a9).;
209 cerrcxx . . $ `src/fflib/throwassert.hpp:33:9`! <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "will use a numerical gradient..." <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ endl(345bf97a19cc83a9).;
210 numericalGradientcxx . . $ LineSearch#numericalGradient(77d9f17553126ecb).(m);
211 } else
212 gradcxx . . $ LineSearch#grad. = g;
213
214 return gradcxx . . $ LineSearch#grad.;
215 }
216
217 template< class P, class V, class M, class VM, class R >
218 typename LineSearchcxx . . $ LineSearch#< P, V, M, VM, R >::VMatcxx . . $ LineSearch#VMat# *LineSearchcxx . . $ LineSearch#< P, V, M, VM, R >::hessiancxx . . $ LineSearch#hessian(65162f56a443f074).(const Paramcxx . . $ LineSearch#Param# &m) {
219 VMatcxx . . $ LineSearch#VMat# *h;
220
221 h = nrjcxx . . $ LineSearch#nrj.->getHessiancxx . . $ tNRJ#getHessian(41e6e37e6163bc37).(m);
222
223 if (h ==cxx . . $ std/`operator==`(bf4c77cd91c4b201). NULLcxx . . $ `<external>/16391990256953433862/stddef.h:416:9`!) {
224 cerrcxx . . $ `src/fflib/throwassert.hpp:33:9`! <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Hessien is undefined for this NRJ function!" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ endl(345bf97a19cc83a9).;
225 exit(1);
226 }
227
228 return h;
229 }
230
231 template< class P, class V, class M, class VM, class R >
232 typename LineSearchcxx . . $ LineSearch#< P, V, M, VM, R >::Realcxx . . $ LineSearch#Real# LineSearchcxx . . $ LineSearch#< P, V, M, VM, R >::evaluatecxx . . $ LineSearch#evaluate(3363963b0cfc0bd7).(const Paramcxx . . $ LineSearch#Param# &m) {
233 return nrjcxx . . $ LineSearch#nrj.->getValcxx . . $ tNRJ#getVal(f500175d9f5e9e70).(m);
234 }
235
236 // We moving forward from alpha*m, m is the start point in the direction dmod, of beta
237 // We check we are in the bounds
238 template< class P, class V, class M, class VM, class R >
239 typename LineSearchcxx . . $ LineSearch#< P, V, M, VM, R >::Paramcxx . . $ LineSearch#Param# LineSearchcxx . . $ LineSearch#< P, V, M, VM, R >::updatecxx . . $ LineSearch#update(1c2e7b4031239c77).(
240 const Paramcxx . . $ LineSearch#Param# &m, Realcxx . . $ LineSearch#Real# alpha, Realcxx . . $ LineSearch#Real# beta, const Vectcxx . . $ LineSearch#Vect# &dmod) const {
241 Paramcxx . . $ LineSearch#Param# newparam(m);
242 long ndim = m.size( );
243
244 Vectcxx . . $ LineSearch#Vect# direction(dmod);
245
246 if (direction.size( ) !=cxx . . $ std/`operator!=`(bf4c77cd91c4b201). ndim) {
247 cerrcxx . . $ `src/fflib/throwassert.hpp:33:9`! <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Size of direction is different from dimensions" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ endl(345bf97a19cc83a9).;
248 exit(1);
249 }
250
251 // check to see if the new model is out of the upper bound
252 coutcxx . . $ cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "update " <<cxx . . $ `operator<<`(2587130c0179f881). m.min( ) <<cxx . . $ `operator<<`(2587130c0179f881). " " <<cxx . . $ `operator<<`(2587130c0179f881). m.max( ) <<cxx . . $ `operator<<`(2587130c0179f881). " " <<cxx . . $ `operator<<`(2587130c0179f881). direction.max( ) <<cxx . . $ `operator<<`(2587130c0179f881). " " <<cxx . . $ `operator<<`(2587130c0179f881). dmod.max( );
253 direction = alpha *cxx . . $ `operator*`(ca0d58f853b0940). m +cxx . . $ operator+(1f62e43363bf5779). beta *cxx . . $ `operator*`(ca0d58f853b0940). direction;
254 newparam = Paramcxx . . $ LineSearch#Param#(direction);
255 coutcxx . . $ cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " ; " <<cxx . . $ `operator<<`(2587130c0179f881). newparam.min( ) <<cxx . . $ `operator<<`(2587130c0179f881). " " <<cxx . . $ `operator<<`(2587130c0179f881). newparam.max( ) <<cxx . . $ `operator<<`(2587130c0179f881). endlcxx . . $ endl(345bf97a19cc83a9).;
256 if ((m.modMax( ) !=cxx . . $ std/`operator!=`(bf4c77cd91c4b201). NULLcxx . . $ `<external>/16391990256953433862/stddef.h:416:9`!) && (m.modMin( ) !=cxx . . $ std/`operator!=`(bf4c77cd91c4b201). NULLcxx . . $ `<external>/16391990256953433862/stddef.h:416:9`!)) {
257 for (long i = 0; i < ndim; i++) {
258 if ((newparam[i] -cxx . . $ operator-(4b6fd085471a9fb1). (*cxx . . $ `operator*`(ca0d58f853b0940).(m.modMax( )))[i]) >cxx . . $ std/`operator>`(c2ad8cf295ccec45). 0) newparam[i] = (*cxx . . $ `operator*`(ca0d58f853b0940).(m.modMax( )))[i];
259
260 if ((newparam[i] -cxx . . $ operator-(4b6fd085471a9fb1). (*cxx . . $ `operator*`(ca0d58f853b0940).(m.modMin( )))[i]) <cxx . . $ std/`operator<`(c2ad8cf295ccec45). 0) newparam[i] = (*cxx . . $ `operator*`(ca0d58f853b0940).(m.modMin( )))[i];
261 }
262 }
263
264 return newparam;
265 }
266
267 #endif