Debian Codegraph @

src/Algo/BFGS.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 : 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_
27
28 #include "Optima.hpp"
29 #include "defs.hpp"
30
31 // Need a "true" Matrix class
32 template< class LS >
33 class BFGS : public Optima< LS > {
34 typedef typename LS::Real Real;
35 typedef typename LS::Param Param;
36 typedef typename LS::Vect Vect;
37 typedef typename LS::VMat VMat;
38 typedef typename LS::Mat Mat;
39 typedef list< Real > mlist;
40
41 public:
42 BFGS(LS* ls, // pointer to the line-search object
43 int iter, // Maximum number of iterations
44 Real tol, // Minimum accepted gradient at optimum solution
45 int verb = 0 // vebose or quiet
46 );
47 ~BFGS( ) { ; }
48
49 // the BFGS search starting from model0, returns an optimum model
50 Param optimizer(Param& model0) override;
51 };
52
53 template< class LS >
54 BFGS< LS >::BFGS(LS* p, int it, Real eps, int verb) : Optima< LS >(verb) {
55 this->ls = p;
56 this->iterMax = it;
57 this->tol = eps;
58 this->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 BFGS< LS >::Param BFGS< LS >::optimizer(Param& model0) {
72 // reset the residue history for every new optimizer
73 this->iterNum = 0;
74 if (this->residue != NULL) {
75 delete this->residue;
76 this->residue = new mlist;
77 }
78
79 // Initial settings for some parameters
80 int n = model0.size( );
81 Vect g0(n);
82 double lambda = 0.025;
83 double descent = 0.;
84
85 g0 = *(this->ls->gradient(model0));
86
87 // Check the gradient, in case the initial model is the optimal
88 double err = (Real)sqrt((g0, g0));
89
90 if (this->isVerbose) cerr << "Initial residue : " << err << endl;
91
92 this->appendResidue(err); // residual
93
94 if (err < this->tol) {
95 if (this->isVerbose) cerr << "Initial guess was great! \n";
96 this->isSuccess = 1;
97 return model0;
98 }
99
100 // Initial identical matrix for estimating inverse of the Hessian
101 // Vect diag(n, 1.);
102
103 Mat H(n, n);
104 H = 0;
105 diagonal(H) = 1;
106 Real d, dd, scale;
107 Param model1(model0);
108 Vect s(n), gamma(n), delta(n), g1(n);
109
110 // Searching directions
111 s = Real( );
112 s -= H * g0;
113 descent = (s, g0);
114 assert(s.max( ) < 1e100);
115
116 // Cubic line search for a new model
117 model1 = this->ls->search(model0, s, descent, lambda);
118 g1 = *(this->ls->gradient(model1));
119 err = (Real)sqrt((g1, g1));
120 if (this->isVerbose)
121 cerr << "Iteration (" << this->iterNum << ") : "
122 << "current value of the objective function: " << this->ls->currentValue( )
123 << "\t current residue: " << err << endl;
124
125 this->appendResidue(err); // residual
126 this->iterNum++;
127
128 Mat B(n, n);
129
130 while (this->finalResidue( ) > this->tol && this->iterNum < this->iterMax) {
131 gamma = g1 - g0;
132 delta = model1 - model0;
133
134 // Replace the searching direction with temporal storage
135 s = H * gamma;
136
137 // Factor of the denominator
138 dd = (delta, gamma);
139
140 // check denominator
141 if (Abs(dd) < 1e-20) {
142 // Re-initialize the Hessian Matrix
143 // It must be zeroed first (cf. Matrix.hpp)
144 H = 0.;
145 diagonal(H) = 1.;
146 } else {
147 assert(dd);
148 d = 1. / dd;
149
150 scale = d * ((gamma, s));
151 scale += 1;
152 scale *= d;
153
154 // Update the first term
155 H += scale * delta * delta.t( );
156
157 // Update the second term
158 H -= d * s * delta.t( );
159 H -= d * delta * s.t( );
160
161 // Store the current model and gradient
162 g0 = g1;
163 model0 = model1;
164 }
165
166 s = Real( );
167 s -= H * g0;
168 descent = (s, g0);
169 model1 = this->ls->search(model0, s, descent, lambda);
170 g1 = *(this->ls->gradient(model1));
171 err = (Real)sqrt((g1, g1));
172
173 if (this->isVerbose)
174 cerr << "Iteration (" << this->iterNum << ") : "
175 << "current value of the objective function: " << this->ls->currentValue( )
176 << "\t current residue: " << err << endl;
177
178 this->appendResidue(err); // residual
179 this->iterNum++;
180 }
181
182 return (model1);
183 }
184
185 #endif //_BFGS_HH_