Debian Codegraph @

src/Algo/LineSearch.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 #ifndef LINE_SEARCH_HH
24 #define LINE_SEARCH_HH
25
26 #include <list>
27 #include "defs.hpp"
28 #include "NRJ.hpp"
29 #include <limits.h>
30 #include <float.h>
31
32 #define MAX_IT_LS 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 LineSearch {
49 public:
50 typedef R Real;
51 typedef VM VMat;
52 typedef M Mat;
53 typedef P Param;
54 typedef V Vect;
55 typedef tNRJ< Param, Vect, VMat, Real > 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 Vect *grad;
62 int ref; // ref equals 1 if we calcualte numerically the gradient
63
64 void numericalGradient(const Param &);
65
66 protected:
67 // maxinum number of iterations allowed
68 int iterMax;
69 // the number iterations so far
70 // to reinitialize in inherited classes
71 int iterNum;
72 // the history of number of iteration of iteration
73 list< int > *iterHistory;
74 // the value at the minimum along the direction
75 // to update in inherited classes
76 Real value;
77 // the delta used for calculating numerical gradient
78 // same type as gradient
79 Vect *step;
80 // pointer to the nrj
81 NRJ *nrj;
82
83 void appendSearchNumber( );
84
85 public:
86 // a constructor with pointer to the nrj and
87 // to the step if numerical gradient is computed
88 explicit LineSearch(NRJ *f, Vect *interval = NULL);
89 virtual ~LineSearch( );
90
91 // Start point and descent direction + parameters
92 virtual Param search(const Param &, Vect &, Real, double);
93
94 // compute the gradient Vector
95 // linesearch gradient
96 Vect *gradient(const Param &m);
97 // linesearch hessian
98 VMat *hessian(const Param &m);
99 // evaluate the nrj
100 Real evaluate(const Param &m);
101
102 // number of iterations for all line searches
103 list< int > allSearchIterations( );
104
105 // number of iterations in the current line search
106 int searchIterations( );
107 // value of the objective function for the current Model
108 Real currentValue( );
109
110 Param update(const Param &, Real, Real, const Vect &) const;
111 };
112
113 template< class P, class V, class M, class VM, class R >
114 LineSearch< P, V, M, VM, R >::LineSearch(NRJ *p, Vect *interval) {
115 iterMax = MAX_IT_LS;
116 iterNum = 0;
117 nrj = p;
118 step = interval;
119 iterHistory = new list< int >;
120 grad = NULL;
121 ref = 0;
122 value = 0.;
123 }
124
125 template< class P, class V, class M, class VM, class R >
126 LineSearch< P, V, M, VM, R >::~LineSearch( ) {
127 if (iterHistory != NULL) delete iterHistory;
128 if (ref == 1 && grad != NULL) delete grad;
129 if (step != NULL) delete step;
130 }
131
132 template< class P, class V, class M, class VM, class R >
133 void LineSearch< P, V, M, VM, R >::appendSearchNumber( ) {
134 iterHistory[0].push_back(iterNum);
135 }
136
137 template< class P, class V, class M, class VM, class R >
138 list< int > LineSearch< P, V, M, VM, R >::allSearchIterations( ) {
139 return iterHistory[0];
140 }
141
142 template< class P, class V, class M, class VM, class R >
143 int LineSearch< P, V, M, VM, R >::searchIterations( ) {
144 return iterNum;
145 }
146
147 template< class P, class V, class M, class VM, class R >
148 typename LineSearch< P, V, M, VM, R >::Real LineSearch< P, V, M, VM, R >::currentValue( ) {
149 return value;
150 }
151
152 template< class P, class V, class M, class VM, class R >
153 typename LineSearch< P, V, M, VM, R >::Param LineSearch< P, V, M, VM, R >::search(const Param &m,
154 Vect &v,
155 Real alpha,
156 double beta) {
157 cerr << "You need to specify the LineSearch method!" << endl;
158 exit(1);
159 }
160
161 // Centered finite difference
162 template< class P, class V, class M, class VM, class R >
163 void LineSearch< P, V, M, VM, R >::numericalGradient(const Param &m) {
164 int n = m.size( );
165
166 // At first call: gradient initialization
167 if (grad == NULL) {
168 grad = new Vect(n);
169 ref = 1;
170 }
171
172 Real diffValue, newValue, off;
173 Param m1(m);
174
175 if (step != NULL) {
176 for (int i = 0; i < n; i++) {
177 off = step[0][i];
178 m1[i] -= off;
179 newValue = nrj->getVal(m1);
180 m1[i] += 2 * off;
181 diffValue = nrj->getVal(m1);
182 diffValue -= newValue;
183 (*grad)[i] = diffValue / (2 * 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] / 1000;
189 m1[i] -= off;
190 newValue = nrj->getVal(m1);
191 m1[i] += 2 * off;
192 diffValue = nrj->getVal(m1);
193 diffValue -= newValue;
194 (*grad)[i] = diffValue / (2 * off);
195 m1[i] = m[i];
196 }
197 }
198 }
199
200 template< class P, class V, class M, class VM, class R >
201 typename LineSearch< P, V, M, VM, R >::Vect *LineSearch< P, V, M, VM, R >::gradient(
202 const Param &m) {
203 Vect *g;
204
205 g = nrj->getGradient(m);
206
207 if (g == NULL) {
208 cerr << "Undefined gradient for this NRJ function!" << endl;
209 cerr << "will use a numerical gradient..." << endl;
210 numericalGradient(m);
211 } else
212 grad = g;
213
214 return grad;
215 }
216
217 template< class P, class V, class M, class VM, class R >
218 typename LineSearch< P, V, M, VM, R >::VMat *LineSearch< P, V, M, VM, R >::hessian(const Param &m) {
219 VMat *h;
220
221 h = nrj->getHessian(m);
222
223 if (h == NULL) {
224 cerr << "Hessien is undefined for this NRJ function!" << endl;
225 exit(1);
226 }
227
228 return h;
229 }
230
231 template< class P, class V, class M, class VM, class R >
232 typename LineSearch< P, V, M, VM, R >::Real LineSearch< P, V, M, VM, R >::evaluate(const Param &m) {
233 return nrj->getVal(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 LineSearch< P, V, M, VM, R >::Param LineSearch< P, V, M, VM, R >::update(
240 const Param &m, Real alpha, Real beta, const Vect &dmod) const {
241 Param newparam(m);
242 long ndim = m.size( );
243
244 Vect direction(dmod);
245
246 if (direction.size( ) != ndim) {
247 cerr << "Size of direction is different from dimensions" << endl;
248 exit(1);
249 }
250
251 // check to see if the new model is out of the upper bound
252 cout << "update " << m.min( ) << " " << m.max( ) << " " << direction.max( ) << " " << dmod.max( );
253 direction = alpha * m + beta * direction;
254 newparam = Param(direction);
255 cout << " ; " << newparam.min( ) << " " << newparam.max( ) << endl;
256 if ((m.modMax( ) != NULL) && (m.modMin( ) != NULL)) {
257 for (long i = 0; i < ndim; i++) {
258 if ((newparam[i] - (*(m.modMax( )))[i]) > 0) newparam[i] = (*(m.modMax( )))[i];
259
260 if ((newparam[i] - (*(m.modMin( )))[i]) < 0) newparam[i] = (*(m.modMin( )))[i];
261 }
262 }
263
264 return newparam;
265 }
266
267 #endif