Debian Codegraph @

src/Algo/BFGS.hpp

raw symbols SCIP
1 /****************************************************************************/cxx . . $ `<file>/src/Algo/BFGS.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 : BFGS Quasi-Newton Optimization Method
18 // LICENSE : LGPLv3
19 // ORG : LJLL Universite Pierre et Marie Curie, Paris, FRANCE
20 // AUTHORS : ...
21 // E-MAIL : ...
22
23 /* At the present version, you must use the CubicLineSearch procedure */
24
25 #ifndef _BFGS_HH_
26 #define _BFGS_HH_cxx . . $ `src/Algo/BFGS.hpp:26:9`!
27
28 #include "Optima.hpp"cxx . . $ `<file>/src/Algo/Optima.hpp`/
29 #include "defs.hpp"cxx . . $ `<file>/src/Algo/defs.hpp`/
30
31 // Need a "true" Matrix class
32 template< class LS >
33 class BFGScxx . . $ BFGS# : public Optimacxx . . $ Optima#< LS > {
34 typedef typename LS::Real Realcxx . . $ BFGS#Real#;
35 typedef typename LS::Param Paramcxx . . $ BFGS#Param#;
36 typedef typename LS::Vect Vectcxx . . $ BFGS#Vect#;
37 typedef typename LS::VMat VMatcxx . . $ BFGS#VMat#;
38 typedef typename LS::Mat Matcxx . . $ BFGS#Mat#;
39 typedef listcxx . . $ list#< Realcxx . . $ BFGS#Real# > mlistcxx . . $ BFGS#mlist#;
40
41 public:
42 BFGScxx . . $ BFGS#`BFGS<LS>`(4e2eacd0dafa8962).(LS* ls, // pointer to the line-search object
43 int iter, // Maximum number of iterations
44 Realcxx . . $ BFGS#Real# tol, // Minimum accepted gradient at optimum solution
45 int verb = 0 // vebose or quiet
46 );
47 ~cxx . . $ BFGS#`~BFGS<LS>`(49f6e7a06ebc5aa8).BFGS( ) { ; }
48
49 // the BFGS search starting from model0, returns an optimum model
50 Paramcxx . . $ BFGS#Param# optimizercxx . . $ BFGS#optimizer(e033336ccb1d726c).(Paramcxx . . $ BFGS#Param#& model0) override;
51 };
52
53 template< class LS >
54 BFGScxx . . $ BFGS#< LS >::BFGScxx . . $ BFGS#`BFGS<LS>`(4e2eacd0dafa8962).(LS* p, int it, Realcxx . . $ BFGS#Real# eps, int verb) : Optimacxx . . $ Optima#< LS >(verb) {
55 this->lscxx . . $ Optima#ls. = p;
56 this->iterMaxcxx . . $ Optima#iterMax. = it;
57 this->tolcxx . . $ Optima#tol. = eps;
58 this->iterNumcxx . . $ Optima#iterNum. = 0;
59 }
60
61 // Matrix class requirements:
62 // - A matrix constructor with n_rows, n_cols
63 // - A matrix constructor from diagonal
64 // - A matrix-vector product
65 // - An outProduct method: from two vector u, v, build the matrix A_{i,j}=u(i)v(j)
66 // - A t() method
67 // - An operator +
68 // - An operator = Real
69 // - An operator = Vect
70 template< class LS >
71 typename BFGScxx . . $ BFGS#< LS >::Paramcxx . . $ BFGS#Param# BFGScxx . . $ BFGS#< LS >::optimizercxx . . $ BFGS#optimizer(e033336ccb1d726c).(Paramcxx . . $ BFGS#Param#& model0) {
72 // reset the residue history for every new optimizer
73 this->iterNumcxx . . $ Optima#iterNum. = 0;
74 if (this->residuecxx . . $ Optima#residue. !=cxx . . $ std/`operator!=`(bf4c77cd91c4b201). NULLcxx . . $ `<external>/16391990256953433862/stddef.h:416:9`!) {
75 delete this->residuecxx . . $ Optima#residue.;
76 this->residuecxx . . $ Optima#residue. = new mlistcxx . . $ BFGS#mlist#;
77 }
78
79 // Initial settings for some parameters
80 int n = model0.size( );
81 Vectcxx . . $ BFGS#Vect# g0(n);
82 double lambda = 0.025;
83 double descent = 0.;
84
85 g0 = *cxx . . $ `operator*`(ca0d58f853b0940).(this->lscxx . . $ Optima#ls.->gradient(model0));
86
87 // Check the gradient, in case the initial model is the optimal
88 double err = (Realcxx . . $ BFGS#Real#)sqrt((g0,cxx . . $ `operator,`(5d54d1eb02c625aa). g0));
89
90 if (this->isVerbosecxx . . $ Optima#isVerbose.) cerrcxx . . $ `src/fflib/throwassert.hpp:33:9`! <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Initial residue : " <<cxx . . $ std/basic_ostream#`operator<<`(968942d578224538). err <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ endl(345bf97a19cc83a9).;
91
92 this->appendResiduecxx . . $ Optima#appendResidue(51e82fb6181a0d1d).(err); // residual
93
94 if (err <cxx . . $ std/`operator<`(c2ad8cf295ccec45). this->tolcxx . . $ Optima#tol.) {
95 if (this->isVerbosecxx . . $ Optima#isVerbose.) cerrcxx . . $ `src/fflib/throwassert.hpp:33:9`! <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Initial guess was great! \n";
96 this->isSuccesscxx . . $ Optima#isSuccess. = 1;
97 return model0;
98 }
99
100 // Initial identical matrix for estimating inverse of the Hessian
101 // Vect diag(n, 1.);
102
103 Matcxx . . $ BFGS#Mat# H(n, n);
104 H = 0;
105 diagonalcxx . . $ diagonal(b24acf50faa69958).(H) = 1;
106 Realcxx . . $ BFGS#Real# d, dd, scale;
107 Paramcxx . . $ BFGS#Param# model1(model0);
108 Vectcxx . . $ BFGS#Vect# s(n), gamma(n), delta(n), g1(n);
109
110 // Searching directions
111 s = Realcxx . . $ BFGS#Real#( );
112 s -= H *cxx . . $ `operator*`(ca0d58f853b0940). g0;
113 descent = (s,cxx . . $ `operator,`(5d54d1eb02c625aa). g0);
114 assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!(s.max( ) < 1e100);
115
116 // Cubic line search for a new model
117 model1 = this->lscxx . . $ Optima#ls.->search(model0, s, descent, lambda);
118 g1 = *cxx . . $ `operator*`(ca0d58f853b0940).(this->lscxx . . $ Optima#ls.->gradient(model1));
119 err = (Realcxx . . $ BFGS#Real#)sqrt((g1,cxx . . $ `operator,`(5d54d1eb02c625aa). g1));
120 if (this->isVerbosecxx . . $ Optima#isVerbose.)
121 cerrcxx . . $ `src/fflib/throwassert.hpp:33:9`! <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Iteration (" <<cxx . . $ `operator<<`(2587130c0179f881). this->iterNumcxx . . $ Optima#iterNum. <<cxx . . $ `operator<<`(2587130c0179f881). ") : "
122 <<cxx . . $ `operator<<`(2587130c0179f881). "current value of the objective function: " <<cxx . . $ `operator<<`(2587130c0179f881). this->lscxx . . $ Optima#ls.->currentValue( )
123 <<cxx . . $ `operator<<`(2587130c0179f881). "\t current residue: " <<cxx . . $ `operator<<`(2587130c0179f881). err <<cxx . . $ `operator<<`(2587130c0179f881). endlcxx . . $ endl(345bf97a19cc83a9).;
124
125 this->appendResiduecxx . . $ Optima#appendResidue(51e82fb6181a0d1d).(err); // residual
126 this->iterNumcxx . . $ Optima#iterNum.++;
127
128 Matcxx . . $ BFGS#Mat# B(n, n);
129
130 while (this->finalResiduecxx . . $ Optima#finalResidue(fffa23a80432b1b8).( ) >cxx . . $ std/`operator>`(c2ad8cf295ccec45). this->tolcxx . . $ Optima#tol. && this->iterNumcxx . . $ Optima#iterNum. <cxx . . $ std/`operator<`(c2ad8cf295ccec45). this->iterMaxcxx . . $ Optima#iterMax.) {
131 gamma = g1 -cxx . . $ operator-(4b6fd085471a9fb1). g0;
132 delta = model1 -cxx . . $ operator-(4b6fd085471a9fb1). model0;
133
134 // Replace the searching direction with temporal storage
135 s = H *cxx . . $ `operator*`(ca0d58f853b0940). gamma;
136
137 // Factor of the denominator
138 dd = (delta,cxx . . $ `operator,`(5d54d1eb02c625aa). gamma);
139
140 // check denominator
141 if (Abscxx . . $ `src/Algo/defs.hpp:66:9`!(dd) <cxx . . $ std/`operator<`(c2ad8cf295ccec45). 1e-20) {
142 // Re-initialize the Hessian Matrix
143 // It must be zeroed first (cf. Matrix.hpp)
144 H = 0.;
145 diagonalcxx . . $ diagonal(b24acf50faa69958).(H) = 1.;
146 } else {
147 assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!(dd);
148 d = 1. /cxx . . $ `operator/`(42306a5d7abcb141). dd;
149
150 scale = d *cxx . . $ `operator*`(ca0d58f853b0940). ((gamma,cxx . . $ `operator,`(5d54d1eb02c625aa). s));
151 scale +=cxx . . $ `operator+=`(d02e6a613fcf7dc7). 1;
152 scale *= d;
153
154 // Update the first term
155 H +=cxx . . $ `operator+=`(d02e6a613fcf7dc7). scale *cxx . . $ `operator*`(ca0d58f853b0940). delta *cxx . . $ `operator*`(ca0d58f853b0940). delta.t( );
156
157 // Update the second term
158 H -= d *cxx . . $ `operator*`(ca0d58f853b0940). s *cxx . . $ `operator*`(ca0d58f853b0940). delta.t( );
159 H -= d *cxx . . $ `operator*`(ca0d58f853b0940). delta *cxx . . $ `operator*`(ca0d58f853b0940). s.t( );
160
161 // Store the current model and gradient
162 g0 = g1;
163 model0 = model1;
164 }
165
166 s = Realcxx . . $ BFGS#Real#( );
167 s -= H *cxx . . $ `operator*`(ca0d58f853b0940). g0;
168 descent = (s,cxx . . $ `operator,`(5d54d1eb02c625aa). g0);
169 model1 = this->lscxx . . $ Optima#ls.->search(model0, s, descent, lambda);
170 g1 = *cxx . . $ `operator*`(ca0d58f853b0940).(this->lscxx . . $ Optima#ls.->gradient(model1));
171 err = (Realcxx . . $ BFGS#Real#)sqrt((g1,cxx . . $ `operator,`(5d54d1eb02c625aa). g1));
172
173 if (this->isVerbosecxx . . $ Optima#isVerbose.)
174 cerrcxx . . $ `src/fflib/throwassert.hpp:33:9`! <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Iteration (" <<cxx . . $ `operator<<`(2587130c0179f881). this->iterNumcxx . . $ Optima#iterNum. <<cxx . . $ `operator<<`(2587130c0179f881). ") : "
175 <<cxx . . $ `operator<<`(2587130c0179f881). "current value of the objective function: " <<cxx . . $ `operator<<`(2587130c0179f881). this->lscxx . . $ Optima#ls.->currentValue( )
176 <<cxx . . $ `operator<<`(2587130c0179f881). "\t current residue: " <<cxx . . $ `operator<<`(2587130c0179f881). err <<cxx . . $ `operator<<`(2587130c0179f881). endlcxx . . $ endl(345bf97a19cc83a9).;
177
178 this->appendResiduecxx . . $ Optima#appendResidue(51e82fb6181a0d1d).(err); // residual
179 this->iterNumcxx . . $ Optima#iterNum.++;
180 }
181
182 return (model1);
183 }
184
185 #endif //_BFGS_HH_