| 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 NEWTONRAPHSON_HH |
| 24 | #define NEWTONRAPHSON_HH |
| 25 | |
| 26 | #include "Optima.hpp" |
| 27 | #include "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 Newt : 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 | Newt(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); // verbose or quiet |
| 46 | ~Newt( ) { ; } |
| 47 | |
| 48 | // the Newt search starting from model0, returns an optimum model |
| 49 | Param optimizer(Param& model0) override; |
| 50 | }; |
| 51 | |
| 52 | template< class LS > |
| 53 | Newt< LS >::Newt(LS* p, int it, Real eps, int verb) : Optima< LS >(verb) { |
| 54 | this->ls = p; |
| 55 | this->iterMax = it; |
| 56 | this->tol = eps; |
| 57 | this->iterNum = 0; |
| 58 | } |
| 59 | |
| 60 | // MAT class needs: |
| 61 | // - Ax=b resolution method |
| 62 | template< class LS > |
| 63 | typename Newt< LS >::Param Newt< LS >::optimizer(Param& model0) { |
| 64 | // reset the this->residue history for every new optimizer |
| 65 | this->iterNum = 0; |
| 66 | if (this->residue != NULL) { |
| 67 | delete this->residue; |
| 68 | this->residue = new mlist; |
| 69 | } |
| 70 | |
| 71 | // Initial settings for some parameters |
| 72 | int n = model0.size( ); |
| 73 | Vect g0(n); |
| 74 | double lambda = 0.025; |
| 75 | |
| 76 | g0 = *(this->ls->gradient(model0)); |
| 77 | |
| 78 | // check the gradient, in case the initial model is the optimal |
| 79 | double err = (Real)sqrt((g0, g0)); |
| 80 | |
| 81 | if (this->isVerbose) cerr << "Initial this->residue : " << err << endl; |
| 82 | |
| 83 | this->appendResidue(err); // residual |
| 84 | |
| 85 | if (err < this->tol) { |
| 86 | if (this->isVerbose) cerr << "Initial guess was great!" << endl; |
| 87 | this->isSuccess = 1; |
| 88 | return model0; |
| 89 | } |
| 90 | |
| 91 | Vect g1(n); |
| 92 | Vect s(n); |
| 93 | Param model1(model0); |
| 94 | |
| 95 | while (this->finalResidue( ) > this->tol && this->iterNum < this->iterMax) { |
| 96 | // searching directions |
| 97 | // s = g0/(*this->ls->hessian(model0)); // LU reinitialization at each step |
| 98 | this->ls->hessian(model0)->Solve(s, g0); |
| 99 | s = -1. * s; |
| 100 | |
| 101 | double descent = (s, g0); |
| 102 | // Cubic Line Search |
| 103 | model1 = this->ls->search(model0, s, descent, lambda); |
| 104 | g1 = *(this->ls->gradient(model1)); |
| 105 | err = (Real)sqrt((g1, g1)); |
| 106 | |
| 107 | if (this->isVerbose) |
| 108 | cerr << "Iteration (" << this->iterNum << ") : " |
| 109 | << "current value of the objective function: " << this->ls->currentValue( ) |
| 110 | << "\t current this->residue: " << err << endl; |
| 111 | |
| 112 | this->appendResidue(err); // residual |
| 113 | |
| 114 | g0 = g1; |
| 115 | model0 = model1; |
| 116 | |
| 117 | this->iterNum++; |
| 118 | } |
| 119 | |
| 120 | return (model1); |
| 121 | } |
| 122 | |
| 123 | #endif |