| 1 | #ifndef OPTIMA_HH |
| 2 | #define OPTIMA_HH |
| 3 | |
| 4 | #include "NRJ.hpp" |
| 5 | #include <limits.h> |
| 6 | #include "defs.hpp" |
| 7 | |
| 8 | #define MAX_IT_OPT 1000 |
| 9 | |
| 10 | //================== |
| 11 | // define the base class for optimization classes |
| 12 | // H. Lydia Deng, 03/14/94 |
| 13 | //====================== |
| 14 | |
| 15 | /* |
| 16 | This is the base class for general optimization classes. |
| 17 | Classes derived from this class inherit features of |
| 18 | Optima. This class cannot be instantiated directly. |
| 19 | */ |
| 20 | |
| 21 | // WARNING : |
| 22 | // No copy constructor |
| 23 | // No copy operator = |
| 24 | |
| 25 | template< class LS > |
| 26 | class Optima { |
| 27 | typedef typename LS::Real Real; |
| 28 | typedef typename LS::Param Param; |
| 29 | typedef typename LS::Vect Vect; |
| 30 | typedef typename LS::VMat VMat; |
| 31 | typedef LS LineSearch; |
| 32 | typedef list< Real > mlist; |
| 33 | |
| 34 | protected: |
| 35 | // maximum number of iterations |
| 36 | int iterMax; |
| 37 | int iterNum; |
| 38 | // tolerance error |
| 39 | // This is a tolerance on the euclidiean norm of the gradient |
| 40 | Real tol; |
| 41 | // mlist of residue |
| 42 | mlist* residue; |
| 43 | // pointer to LS |
| 44 | LS* ls; |
| 45 | // verbose or quiet |
| 46 | int isVerbose; |
| 47 | // the flag indicating if the search was a success |
| 48 | int isSuccess; |
| 49 | // append the new residue to the mlist |
| 50 | void appendResidue(Real res); |
| 51 | |
| 52 | public: |
| 53 | Optima(int verb = 0); |
| 54 | virtual ~Optima( ); |
| 55 | |
| 56 | virtual Param optimizer(Param&) = 0; |
| 57 | |
| 58 | // Output residues |
| 59 | |
| 60 | int ifSuccess( ); |
| 61 | // residue of the last iteration |
| 62 | Real finalResidue( ); |
| 63 | // residue of the first iteration |
| 64 | Real firstResidue( ); |
| 65 | // residues of all iterations |
| 66 | mlist allResidue( ); |
| 67 | // normalized residues of all iterations |
| 68 | mlist normResidue( ); |
| 69 | }; |
| 70 | |
| 71 | template< class LS > |
| 72 | Optima< LS >::Optima(int verbose) { |
| 73 | residue = new mlist; |
| 74 | isVerbose = verbose; |
| 75 | isSuccess = 0; |
| 76 | ls = NULL; |
| 77 | tol = 0.; |
| 78 | iterMax = MAX_IT_OPT; |
| 79 | } |
| 80 | |
| 81 | template< class LS > |
| 82 | Optima< LS >::~Optima( ) { |
| 83 | if (residue != NULL) delete residue; |
| 84 | } |
| 85 | |
| 86 | // Output residues |
| 87 | template< class LS > |
| 88 | int Optima< LS >::ifSuccess( ) { |
| 89 | return isSuccess; |
| 90 | } |
| 91 | |
| 92 | template< class LS > |
| 93 | typename Optima< LS >::Real Optima< LS >::finalResidue( ) { |
| 94 | return residue->back( ); |
| 95 | } |
| 96 | |
| 97 | template< class LS > |
| 98 | typename Optima< LS >::Real Optima< LS >::firstResidue( ) { |
| 99 | return residue->front( ); |
| 100 | } |
| 101 | |
| 102 | template< class LS > |
| 103 | typename Optima< LS >::mlist Optima< LS >::allResidue( ) { |
| 104 | return *residue; |
| 105 | } |
| 106 | |
| 107 | template< class LS > |
| 108 | typename Optima< LS >::mlist Optima< LS >::normResidue( ) { |
| 109 | return normalize(*residue); |
| 110 | } // cf. defs.hpp |
| 111 | |
| 112 | template< class LS > |
| 113 | void Optima< LS >::appendResidue(Real res) { |
| 114 | (residue[0]).push_back(res); |
| 115 | } |
| 116 | |
| 117 | #endif |