| 1 | #ifndef ROSENBROCK_HH |
| 2 | #define ROSENBROCK_HH |
| 3 | |
| 4 | #include "NRJ.hpp" |
| 5 | #include "Param.hpp" |
| 6 | //#include "mvvtp.h" |
| 7 | //#include "mvblas.h" |
| 8 | |
| 9 | template< class Real, class Mat > |
| 10 | class RosenBrock : public tNRJ< Param< Real >, KN< Real >, Mat, Real > { |
| 11 | private: |
| 12 | protected: |
| 13 | public: |
| 14 | // Constructors and Destructors |
| 15 | explicit RosenBrock(int); |
| 16 | ~RosenBrock( ); |
| 17 | |
| 18 | Real Val(const Param< Real >&); |
| 19 | KN< Real >* Gradient(const Param< Real >&); |
| 20 | Mat* Hessian(const Param< Real >&); |
| 21 | }; |
| 22 | |
| 23 | template< class Real, class Mat > |
| 24 | RosenBrock< Real, Mat >::RosenBrock(int n) |
| 25 | : tNRJ< Param< Real >, KN< Real >, Mat, Real >(n) { // On initialise le gradient |
| 26 | this->grad = new KN< Real >(n); |
| 27 | // On initialise le hessien |
| 28 | // Si on utlise pas le hessien, il suffit qu'il existe un constructeur |
| 29 | // pour le type Mat qui prend un parametre entier. C'est vrai pour les |
| 30 | // doubles par exemple... |
| 31 | this->hess = new Mat(n); |
| 32 | (*this->hess) = 0; |
| 33 | } |
| 34 | |
| 35 | template< class Real, class Mat > |
| 36 | RosenBrock< Real, Mat >::~RosenBrock( ) { |
| 37 | ; |
| 38 | } |
| 39 | |
| 40 | template< class Real, class Mat > |
| 41 | Real RosenBrock< Real, Mat >::Val(const Param< Real >& param) { |
| 42 | Real ti_1, ti, tt; |
| 43 | Real d = 0; |
| 44 | |
| 45 | ti_1 = param[0]; |
| 46 | |
| 47 | for (int i = 1; i < this->nparam; i++) { |
| 48 | ti = param[i]; |
| 49 | tt = 1 - ti_1; |
| 50 | d += tt * tt; |
| 51 | tt = ti - ti_1 * ti_1; |
| 52 | d += 100 * tt * tt; |
| 53 | |
| 54 | ti_1 = ti; |
| 55 | } |
| 56 | |
| 57 | this->val = d; |
| 58 | |
| 59 | return d; |
| 60 | } |
| 61 | |
| 62 | template< class Real, class Mat > |
| 63 | KN< Real >* RosenBrock< Real, Mat >::Gradient(const Param< Real >& param) { |
| 64 | Real ti_1, ti; |
| 65 | ti_1 = param[0]; |
| 66 | ti = param[1]; |
| 67 | |
| 68 | (*this->grad)[0] = -200 * 2 * ti_1 * (ti - ti_1 * ti_1) - 2 * (1 - ti_1); |
| 69 | |
| 70 | for (int i = 1; i < this->nparam - 1; i++) { |
| 71 | (*this->grad)[i] = 200 * (ti - ti_1 * ti_1); |
| 72 | ti_1 = ti; |
| 73 | ti = param[i + 1]; |
| 74 | (*this->grad)[i] += -200 * 2 * ti_1 * (ti - ti_1 * ti_1) - 2 * (1 - ti_1); |
| 75 | } |
| 76 | |
| 77 | (*this->grad)[this->nparam - 1] = 200 * (ti - ti_1 * ti_1); |
| 78 | |
| 79 | return this->grad; |
| 80 | } |
| 81 | |
| 82 | template< class Real, class Mat > |
| 83 | Mat* RosenBrock< Real, Mat >::Hessian(const Param< Real >& param) { |
| 84 | Real ti_1, ti; |
| 85 | ti_1 = param[0]; |
| 86 | ti = param[1]; |
| 87 | |
| 88 | (*this->hess)(0, 0) = -200 * 2 * (ti - ti_1 * ti_1) + 200 * 2 * 2 * ti_1 * ti_1 + 2; |
| 89 | (*this->hess)(0, 1) = -200 * 2 * ti_1; |
| 90 | |
| 91 | for (int i = 1; i < this->nparam - 1; i++) { |
| 92 | (*this->hess)(i, i) = 200; |
| 93 | (*this->hess)(i, i - 1) = -200 * 2 * ti_1; |
| 94 | ti_1 = ti; |
| 95 | ti = param[i + 1]; |
| 96 | (*this->hess)(i, i) += -200 * 2 * (ti - ti_1 * ti_1) + 200 * 2 * 2 * ti_1 * ti_1 + 2; |
| 97 | (*this->hess)(i, i + 1) = -200 * 2 * ti_1; |
| 98 | } |
| 99 | |
| 100 | (*this->hess)(this->nparam - 1, this->nparam - 1) = 200; |
| 101 | (*this->hess)(this->nparam - 1, this->nparam - 2) = -200 * 2 * ti_1; |
| 102 | |
| 103 | return this->hess; |
| 104 | } |
| 105 | |
| 106 | #endif |