Debian Codegraph @

src/Algo/lgalgo.cpp

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 : Frederic Hecht
21 // E-MAIL : [email protected]
22
23 #include <iostream>
24 #include <cfloat>
25 using namespace std;
26 #include "ff++.hpp"
27 #include "NRJ.hpp"
28 #include "RosenBrock.hpp"
29 #include "LineSearch.hpp"
30 #include "CubicLS.hpp"
31 #include "BrentLS.hpp"
32 #include "Optima.hpp"
33 #include "BFGS.hpp"
34 //#include "CG.hpp"
35 #include "NewtonRaphson.hpp"
36
37 // template<class R>
38 extern Block *currentblock;
39
40 typedef double R;
41
42 class OptimAlgo : public OneOperator {
43 public:
44 typedef KN< R > Kn;
45 typedef KN_< R > Kn_;
46 typedef R REAL;
47 typedef Param< REAL > PARAM;
48 typedef KN< REAL > VECT;
49 typedef KNM< REAL > MAT;
50 typedef Matrice_Creuse< R >::VMat VMAT;
51
52 const int cas;
53
54 class E_LCG : public E_F0mps {
55 public:
56 const int cas;
57 static basicAC_F0::name_and_type name_param[];
58 static const int n_name_param = 3;
59 Expression nargs[n_name_param];
60 Expression X;
61 C_F0 inittheparam, theparam, closetheparam;
62 Expression J, dJ, hJ;
63 long arg(int i, Stack stack, long a) const {
64 return nargs[i] ? GetAny< long >((*nargs[i])(stack)) : a;
65 }
66 R arg(int i, Stack stack, R a) const { return nargs[i] ? GetAny< R >((*nargs[i])(stack)) : a; }
67
68 class lgNRJ : public tNRJ< PARAM, VECT, VMAT, REAL > {
69 private:
70 Stack stack;
71 Expression J, dJ, hJ, theparame;
72 VECT *gg;
73
74 protected:
75 void setparam(const Param< R > &x) {
76 KN< double > *p = GetAny< KN< double > * >((*theparame)(stack));
77 ffassert(p->N( ) == x.N( ));
78 *p = x;
79 }
80
81 public:
82 lgNRJ(Stack s, int n, Expression t, Expression JJ, Expression dJJ, Expression hJJ)
83 : tNRJ< PARAM, VECT, VMAT, REAL >(n), stack(s), J(JJ), dJ(dJJ), hJ(hJJ), theparame(t),
84 gg(0) {
85 if (dJ) gg = new VECT(n);
86 }
87
88 ~lgNRJ( ) {
89 if (gg) delete gg;
90 }
91
92 REAL Val(const Param< R > &x) {
93 setparam(x);
94 assert(J);
95 R ret = GetAny< R >((*J)(stack));
96 WhereStackOfPtr2Free(stack)->clean( );
97 return ret;
98 }
99
100 VECT *Gradient(const Param< R > &x) {
101 setparam(x);
102 if (dJ) {
103 *gg = GetAny< Kn_ >((*dJ)(stack));
104 WhereStackOfPtr2Free(stack)->clean( );
105 }
106 return gg; // dJ ? GetAny<Kn*>( (*dJ)(stack)) :0;}
107 }
108
109 VMAT *Hessian(const Param< R > &x) {
110 setparam(x);
111 if (!hJ) return 0;
112 Matrice_Creuse< R > *M = GetAny< Matrice_Creuse< R > * >((*hJ)(stack));
113 WhereStackOfPtr2Free(stack)->clean( );
114 assert(M && M->A);
115 return (VMAT *)M->A;
116 }
117 };
118
119 E_LCG(const basicAC_F0 &args, int cc) : cas(cc) {
120 int nbj = args.size( ) - 1;
121 Block::open(currentblock); // make a new block to
122 X = to< Kn * >(args[nbj]);
123 C_F0 X_n(args[nbj], "n");
124 // the expression to init the theparam of all
125 inittheparam =
126 currentblock->NewVar< LocalVariable >("the parameter", atype< KN< R > * >( ), X_n);
127 theparam = currentblock->Find("the parameter"); // the expression for the parameter
128 args.SetNameParam(n_name_param, name_param, nargs);
129 const Polymorphic *opJ = 0;
130 const Polymorphic *opdJ = 0;
131 const Polymorphic *ophJ = 0;
132 if (nbj > 0) {
133 opJ = dynamic_cast< const Polymorphic * >(args[0].LeftValue( ));
134 assert(opJ);
135 }
136 if (nbj > 1) {
137 opdJ = dynamic_cast< const Polymorphic * >(args[1].LeftValue( ));
138 assert(opdJ);
139 }
140 if (nbj > 2) {
141 ophJ = dynamic_cast< const Polymorphic * >(args[2].LeftValue( ));
142 assert(ophJ);
143 }
144 J = dJ = hJ = 0;
145
146 J = to< R >(C_F0(opJ, "(", theparam));
147 if (opdJ)
148 dJ = to< Kn_ >(C_F0(
149 opdJ, "(", theparam)); // May2019 (bug?) Modif FH 17102005 (to verify) to<Kn*> ->to<Kn>
150 if (ophJ) hJ = to< Matrice_Creuse< R > * >(C_F0(ophJ, "(", theparam));
151
152 closetheparam = C_F0((Expression)Block::snewclose(currentblock),
153 atype< void >( )); // The cleanning block expression
154 /*
155 if (nargs[2]) {
156 const Polymorphic *op = dynamic_cast<const Polymorphic *>(nargs[2]);
157 assert(op);
158 C = op->Find("(", ArrayOfaType(atype<Kn* >(), false));
159 } else C = 0;
160 */
161 }
162
163 virtual AnyType operator( )(Stack stack) const {
164 WhereStackOfPtr2Free(stack) = new StackOfPtr2Free(stack);
165 typedef LineSearch< PARAM, VECT, MAT, VMAT, REAL > LS;
166 typedef CubicLineSearch< LS > CLS;
167 KN< double > *delta = 0;
168 R tol = arg(0, stack, 1E-6);
169 int itMax = arg(1, stack, 100L);
170 int itMaxLine = arg(2, stack, 100L);
171 bool verbose = verbosity > 3;
172
173 try {
174 Kn &x = *GetAny< Kn * >((*X)(stack));
175
176 const int n = x.N( );
177 GetAny< KN< double > * >(inittheparam.eval(stack)); // do allocation
178
179 Param< R > param(x);
180 lgNRJ nrj1(stack, n, theparam, J, dJ, hJ);
181 if (!dJ) delta = new KN< double >(n);
182 CLS ls(&nrj1, itMaxLine, delta);
183 REAL initialNrj = nrj1.getVal(param);
184 Optima< LS > *opt = 0;
185 if (cas == 1)
186 opt = new BFGS< LS >((CLS *)&ls, itMax, tol, verbose);
187 else if (cas == 2)
188 opt = new Newt< LS >((CLS *)&ls, itMax, tol, verbose);
189 else
190 ErrorExec("lgalgo: Case no available. Sorry, internal bug", cas);
191
192 param = opt->optimizer(param);
193
194 REAL finalNrj = nrj1.getVal(param);
195
196 if (verbosity) cout << endl << "*** RESULTS SUMMARY ***" << endl;
197
198 if (verbosity > 1) {
199 cout << " The number of call to NRJ : " << nrj1.Appel_Val( ) << endl;
200 cout << " The number of call to gradient : " << nrj1.Appel_Grad( ) << endl;
201 cout << " The number of call to hessian : " << nrj1.Appel_Hess( ) << endl;
202 }
203 if (verbosity > 2) {
204 cout << "Normalized residue : ";
205 display(opt->allResidue( ));
206 }
207 if (verbosity) {
208 cout << " Initial NRJ value : " << initialNrj << endl;
209 cout << " Final NRJ value : " << finalNrj << endl;
210 }
211 delete opt;
212
213 // cout << "The final optimized parameters : " << param;
214 x = param;
215 } catch (...) {
216 closetheparam.eval(stack); // clean memory
217 WhereStackOfPtr2Free(stack)->clean( ); // FH mars 2005
218 if (delta) delete delta;
219
220 throw;
221 }
222
223 if (delta) delete delta;
224 closetheparam.eval(stack); // clean memory
225 WhereStackOfPtr2Free(stack)->clean( ); // FH mars 2005
226
227 return 0L; // SetAny<long>(0); Modif FH july 2005
228 }
229
230 operator aType( ) const { return atype< long >( ); }
231 };
232
233 E_F0 *code(const basicAC_F0 &args) const { return new E_LCG(args, cas); }
234
235 explicit OptimAlgo(int c)
236 : OneOperator(atype< long >( ), atype< Polymorphic * >( ), atype< KN< R > * >( )), cas(c) {}
237
238 OptimAlgo(int c, int cc)
239 : OneOperator(atype< long >( ), atype< Polymorphic * >( ), atype< Polymorphic * >( ),
240 atype< KN< R > * >( )),
241 cas(c) {}
242
243 OptimAlgo(int c, int cc, int ccc)
244 : OneOperator(atype< long >( ), atype< Polymorphic * >( ), atype< Polymorphic * >( ),
245 atype< Polymorphic * >( ), atype< KN< R > * >( )),
246 cas(c) {}
247 };
248
249 basicAC_F0::name_and_type OptimAlgo::E_LCG::name_param[] = {
250 {"eps", &typeid(double)}, {"nbiter", &typeid(long)}, {"nbiterline", &typeid(long)}};
251 void init_algo( );
252
253 void init_algo( ) {
254 Global.Add("BFGS", "(", new OptimAlgo(1, 1)); // j + dJ
255 Global.Add("Newton", "(", new OptimAlgo(2, 2, 2)); // j + dJ
256
257 // Start msh3 debug
258 // extern void init_msh3();
259 // init_msh3();
260 // End mesh3 debug
261 }
262
263 //#include "InitFunct.hpp"
264 // static addingInitFunct TheaddingInitFunct(-9, init_algo);