| 1 | /****************************************************************************/cxx . . $ `<file>/src/Algo/NewtonRaphson.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 NEWTONRAPHSON_HH |
| 24 | #define NEWTONRAPHSON_HHcxx . . $ `src/Algo/NewtonRaphson.hpp:24:9`! |
| 25 | |
| 26 | #include "Optima.hpp"cxx . . $ `<file>/src/Algo/Optima.hpp`/ |
| 27 | #include "defs.hpp"cxx . . $ `<file>/src/Algo/defs.hpp`/ |
| 28 | |
| 29 | /* At the present version, you must use the CubicLS procedure */ |
| 30 | |
| 31 | // Need a "real" Matrix classe |
| 32 | template< class LS > |
| 33 | class Newtcxx . . $ Newt# : public Optimacxx . . $ Optima#< LS > { |
| 34 | typedef typename LS::Real Realcxx . . $ Newt#Real#; |
| 35 | typedef typename LS::Param Paramcxx . . $ Newt#Param#; |
| 36 | typedef typename LS::Vect Vectcxx . . $ Newt#Vect#; |
| 37 | typedef typename LS::VMat VMatcxx . . $ Newt#VMat#; |
| 38 | typedef typename LS::Mat Matcxx . . $ Newt#Mat#; |
| 39 | typedef listcxx . . $ list#< Realcxx . . $ Newt#Real# > mlistcxx . . $ Newt#mlist#; |
| 40 | |
| 41 | public: |
| 42 | Newtcxx . . $ Newt#`Newt<LS>`(4e2eacd0dafa8962).(LS* ls, // pointer to the line-search object. |
| 43 | int iter, // Maximum number of iterations |
| 44 | Realcxx . . $ Newt#Real# tol, // minimum accepted gradient at optimum solution |
| 45 | int verb = 0); // verbose or quiet |
| 46 | ~cxx . . $ Newt#`~Newt<LS>`(49f6e7a06ebc5aa8).Newt( ) { ; } |
| 47 | |
| 48 | // the Newt search starting from model0, returns an optimum model |
| 49 | Paramcxx . . $ Newt#Param# optimizercxx . . $ Newt#optimizer(e033336ccb1d726c).(Paramcxx . . $ Newt#Param#& model0) override; |
| 50 | }; |
| 51 | |
| 52 | template< class LS > |
| 53 | Newtcxx . . $ Newt#< LS >::Newtcxx . . $ Newt#`Newt<LS>`(4e2eacd0dafa8962).(LS* p, int it, Realcxx . . $ Newt#Real# eps, int verb) : Optimacxx . . $ Optima#< LS >(verb) { |
| 54 | this->lscxx . . $ Optima#ls. = p; |
| 55 | this->iterMaxcxx . . $ Optima#iterMax. = it; |
| 56 | this->tolcxx . . $ Optima#tol. = eps; |
| 57 | this->iterNumcxx . . $ Optima#iterNum. = 0; |
| 58 | } |
| 59 | |
| 60 | // MAT class needs: |
| 61 | // - Ax=b resolution method |
| 62 | template< class LS > |
| 63 | typename Newtcxx . . $ Newt#< LS >::Paramcxx . . $ Newt#Param# Newtcxx . . $ Newt#< LS >::optimizercxx . . $ Newt#optimizer(e033336ccb1d726c).(Paramcxx . . $ Newt#Param#& model0) { |
| 64 | // reset the this->residue history for every new optimizer |
| 65 | this->iterNumcxx . . $ Optima#iterNum. = 0; |
| 66 | if (this->residuecxx . . $ Optima#residue. !=cxx . . $ std/`operator!=`(bf4c77cd91c4b201). NULLcxx . . $ `<external>/16391990256953433862/stddef.h:416:9`!) { |
| 67 | delete this->residuecxx . . $ Optima#residue.; |
| 68 | this->residuecxx . . $ Optima#residue. = new mlistcxx . . $ Newt#mlist#; |
| 69 | } |
| 70 | |
| 71 | // Initial settings for some parameters |
| 72 | int n = model0.size( ); |
| 73 | Vectcxx . . $ Newt#Vect# g0(n); |
| 74 | double lambda = 0.025; |
| 75 | |
| 76 | g0 = *cxx . . $ `operator*`(ca0d58f853b0940).(this->lscxx . . $ Optima#ls.->gradient(model0)); |
| 77 | |
| 78 | // check the gradient, in case the initial model is the optimal |
| 79 | double err = (Realcxx . . $ Newt#Real#)sqrt((g0,cxx . . $ `operator,`(5d54d1eb02c625aa). g0)); |
| 80 | |
| 81 | if (this->isVerbosecxx . . $ Optima#isVerbose.) cerrcxx . . $ `src/fflib/throwassert.hpp:33:9`! <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Initial this->residue : " <<cxx . . $ std/basic_ostream#`operator<<`(968942d578224538). err <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ endl(345bf97a19cc83a9).; |
| 82 | |
| 83 | this->appendResiduecxx . . $ Optima#appendResidue(51e82fb6181a0d1d).(err); // residual |
| 84 | |
| 85 | if (err <cxx . . $ std/`operator<`(c2ad8cf295ccec45). this->tolcxx . . $ Optima#tol.) { |
| 86 | if (this->isVerbosecxx . . $ Optima#isVerbose.) cerrcxx . . $ `src/fflib/throwassert.hpp:33:9`! <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Initial guess was great!" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ endl(345bf97a19cc83a9).; |
| 87 | this->isSuccesscxx . . $ Optima#isSuccess. = 1; |
| 88 | return model0; |
| 89 | } |
| 90 | |
| 91 | Vectcxx . . $ Newt#Vect# g1(n); |
| 92 | Vectcxx . . $ Newt#Vect# s(n); |
| 93 | Paramcxx . . $ Newt#Param# model1(model0); |
| 94 | |
| 95 | 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.) { |
| 96 | // searching directions |
| 97 | // s = g0/(*this->ls->hessian(model0)); // LU reinitialization at each step |
| 98 | this->lscxx . . $ Optima#ls.->hessian(model0)->Solve(s, g0); |
| 99 | s = -1. *cxx . . $ `operator*`(ca0d58f853b0940). s; |
| 100 | |
| 101 | double descent = (s,cxx . . $ `operator,`(5d54d1eb02c625aa). g0); |
| 102 | // Cubic Line Search |
| 103 | model1 = this->lscxx . . $ Optima#ls.->search(model0, s, descent, lambda); |
| 104 | g1 = *cxx . . $ `operator*`(ca0d58f853b0940).(this->lscxx . . $ Optima#ls.->gradient(model1)); |
| 105 | err = (Realcxx . . $ Newt#Real#)sqrt((g1,cxx . . $ `operator,`(5d54d1eb02c625aa). g1)); |
| 106 | |
| 107 | if (this->isVerbosecxx . . $ Optima#isVerbose.) |
| 108 | cerrcxx . . $ `src/fflib/throwassert.hpp:33:9`! <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Iteration (" <<cxx . . $ `operator<<`(2587130c0179f881). this->iterNumcxx . . $ Optima#iterNum. <<cxx . . $ `operator<<`(2587130c0179f881). ") : " |
| 109 | <<cxx . . $ `operator<<`(2587130c0179f881). "current value of the objective function: " <<cxx . . $ `operator<<`(2587130c0179f881). this->lscxx . . $ Optima#ls.->currentValue( ) |
| 110 | <<cxx . . $ `operator<<`(2587130c0179f881). "\t current this->residue: " <<cxx . . $ `operator<<`(2587130c0179f881). err <<cxx . . $ `operator<<`(2587130c0179f881). endlcxx . . $ endl(345bf97a19cc83a9).; |
| 111 | |
| 112 | this->appendResiduecxx . . $ Optima#appendResidue(51e82fb6181a0d1d).(err); // residual |
| 113 | |
| 114 | g0 = g1; |
| 115 | model0 = model1; |
| 116 | |
| 117 | this->iterNumcxx . . $ Optima#iterNum.++; |
| 118 | } |
| 119 | |
| 120 | return (model1); |
| 121 | } |
| 122 | |
| 123 | #endif |