[2650] | 1 | #ifndef SIMPLEX_SEEN
|
---|
| 2 | #define SIMPLEX_SEEN
|
---|
| 3 |
|
---|
| 4 | #include "machdefs.h"
|
---|
| 5 | #include "tvector.h"
|
---|
| 6 | #include "generalfit.h"
|
---|
| 7 | #include <string>
|
---|
| 8 | #include <vector>
|
---|
| 9 |
|
---|
| 10 | // ---------------------------------------------------------------
|
---|
| 11 | // (C) UPS-LAL R. Ansari , Aout 2004
|
---|
| 12 | // ---------------------------------------------------------------
|
---|
| 13 | // Classe de minimisation (optimisation) non lineaire dans
|
---|
| 14 | // un espace multidimensionnel suivant la methode des simplex.
|
---|
| 15 | // Simplex : Figure geometrique formee de N+1 sommets (points)
|
---|
| 16 | // (points) dans un espace a N dimension
|
---|
| 17 | // La methode d'optimisation codee ici dans la classe MinZSimplex
|
---|
| 18 | // est basee sur celle decrite dans Numerical Recipes ,
|
---|
| 19 | // Chapitre X, Minimization or Maximization of Functions
|
---|
| 20 | // L'algorithme est ameliore sur deux aspects :
|
---|
| 21 | // - Test d'arret plus complexe
|
---|
| 22 | // - Ajout d'une transformation du simplex (ExpandHigh)
|
---|
| 23 | // Reflection,ReflecExpand,ContractHigh,ContractLow+ExpandHigh
|
---|
| 24 | // ----------------------------------------------------------------
|
---|
| 25 |
|
---|
| 26 | using namespace std;
|
---|
| 27 |
|
---|
| 28 | namespace SOPHYA {
|
---|
| 29 |
|
---|
| 30 | //! Interface definition for multivaruable function (used by SimplexMinmizer)
|
---|
| 31 | class MinZFunction {
|
---|
| 32 | public:
|
---|
| 33 | //! Constructor with nVar defining the number of variables
|
---|
| 34 | MinZFunction(unsigned int nVar);
|
---|
| 35 | virtual ~MinZFunction();
|
---|
| 36 | //! Returns the value of the function for the point defined by xp[]
|
---|
| 37 | virtual double Value(double const xp[])=0;
|
---|
| 38 | //! Returns the number of variables (dimension of xp[]) in method Value()
|
---|
| 39 | inline int NVar() const {return mNVar;}
|
---|
| 40 | protected:
|
---|
| 41 | const int mNVar; //!< nombre de variables f(x,y,z,...)
|
---|
| 42 | };
|
---|
| 43 |
|
---|
| 44 | //! Classe implementing MinZFunction for a GeneralXi2 associated with a GeneralFitData
|
---|
| 45 | class MinZFuncXi2 : public MinZFunction {
|
---|
| 46 | public:
|
---|
| 47 | MinZFuncXi2(GeneralXi2* gxi2, GeneralFitData* gd);
|
---|
| 48 | virtual ~MinZFuncXi2();
|
---|
| 49 | virtual double Value(double const xp[]);
|
---|
| 50 | protected:
|
---|
| 51 | GeneralXi2 *mGXi2;
|
---|
| 52 | GeneralFitData *mGData;
|
---|
| 53 | };
|
---|
| 54 |
|
---|
| 55 | //! Class implementing the Simplex minimisation method
|
---|
| 56 | class MinZSimplex {
|
---|
| 57 | public:
|
---|
| 58 | static int AutoTest(int tsel=-1, int prtlev=0);
|
---|
| 59 |
|
---|
| 60 | MinZSimplex(MinZFunction *mzf);
|
---|
| 61 | virtual ~MinZSimplex();
|
---|
| 62 |
|
---|
| 63 | inline int NDim() { return mZF->NVar(); }
|
---|
| 64 | // Simplex initial
|
---|
| 65 | inline void SetInitialPoint(Vector& point) { mPoint0 = point; }
|
---|
| 66 | inline void SetInitialStep(Vector& step) { mStep0 = step; }
|
---|
| 67 |
|
---|
| 68 | inline void SetPrtLevel(int lev=0) { mPrt = lev; }
|
---|
| 69 | inline int PrtLevel() { return mPrt; }
|
---|
| 70 |
|
---|
| 71 | inline void SetMaxIter(int max = 100000) { mMaxIter = max; }
|
---|
| 72 | inline int MaxIter() { return mMaxIter; }
|
---|
| 73 | inline int NbIter() { return mIter; }
|
---|
| 74 | inline int StopReason() { return mStop; }
|
---|
| 75 | int StopReason(string& s);
|
---|
| 76 |
|
---|
| 77 | // On minimise f(x) f=mZF->Value() ,
|
---|
| 78 | // f_max = max(f) sur simplex , f_min = min(f) sur simplex
|
---|
| 79 | // fm = (abs(f_max)+abs(f_min))
|
---|
| 80 | // [Delta f] = abs(f_max-f_min)
|
---|
| 81 | // [Delta f/f]simplex = 2.*Delta f / fm
|
---|
| 82 | // fm2 = (abs(f_max)+abs(f_max(iter-1)))
|
---|
| 83 | // [Delta f_max/f_max]iter = [f_max(iter-1)-f_max]/fm2
|
---|
| 84 | // Test d'arret :
|
---|
| 85 | // fm < mTol0 OU
|
---|
| 86 | // [Delta f/f]simplex < mTol1 mRep1 fois de suite OU
|
---|
| 87 | // [Delta f_max/f_max]iter < mTol2 mRep2 fois de suite
|
---|
| 88 | //
|
---|
| 89 | inline void SetStopTolerance(double tol0=1.e-39, double tol1 = 1.e-3, int rep1=5,
|
---|
| 90 | double tol2=1.e-4, int rep2=5)
|
---|
| 91 | { mTol0 = tol0; mTol1 = tol1; mRep1 = rep1; mTol2 = tol2, mRep2 = rep2; }
|
---|
| 92 |
|
---|
| 93 | // Controle des facteurs de deformation du simplex:
|
---|
| 94 | // Alpha = Facteur d'homothetie pour la reflexion simple (Reflection)
|
---|
| 95 | // Gamma = Facteur d'homothetie pour la reflexion+expansion (ReflecExpand)
|
---|
| 96 | // Beta = Facteur d'homothetie pour la contraction pour le sommet haut f_max (ContractHigh)
|
---|
| 97 | // Beta2 = Facteur d'homothetie pour la contraction vers le sommet bas f_min (ContractLow)
|
---|
| 98 | // Gamma2 = Facteur d'homothetie pour la l'extension pour le sommet haut ExpandHigh
|
---|
| 99 | inline void SetControls(double alpha=1., double beta=0.5, double beta2=0.5,
|
---|
| 100 | double gamma=2.0, double gamma2=2.0)
|
---|
| 101 | { mAlpha = alpha; mBeta = beta; mBeta2 = beta2; mGamma = gamma; mGamma2 = gamma2;}
|
---|
| 102 |
|
---|
| 103 | inline double Alpha() { return mAlpha; }
|
---|
| 104 | inline double Beta() { return mBeta; }
|
---|
| 105 | inline double Beta2() { return mBeta2; }
|
---|
| 106 | inline double Gamma() { return mGamma; }
|
---|
| 107 | inline double Gamma2() { return mGamma2; }
|
---|
| 108 |
|
---|
| 109 | // Fonction de minimisation - Rc=0, OK, convergence , Rc>0, Non convergence
|
---|
| 110 | // Voir StopReason(string& s)
|
---|
| 111 | virtual int Minimize(Vector& fpoint);
|
---|
| 112 |
|
---|
| 113 | protected:
|
---|
| 114 | int FindMinMax12(Vector& fval, int& ilo, int& ihi, int& inhi);
|
---|
| 115 | inline double Value(Vector& point) { return mZF->Value(point.Data()); }
|
---|
| 116 |
|
---|
| 117 | MinZFunction* mZF;
|
---|
| 118 | int mMaxIter, mIter;
|
---|
| 119 | int mStop;
|
---|
| 120 | int mPrt;
|
---|
| 121 |
|
---|
| 122 | Vector mPoint0;
|
---|
| 123 | Vector mStep0;
|
---|
| 124 | // vector< Vector > simplex0;
|
---|
| 125 | // vector< Vector > simplex_cur;
|
---|
| 126 | double mAlpha, mBeta, mBeta2, mGamma, mGamma2;
|
---|
| 127 | double mTol0, mTol1, mTol2;
|
---|
| 128 | int mRep1,mRep2;
|
---|
| 129 | };
|
---|
| 130 |
|
---|
| 131 | } // namespace SOPHYA
|
---|
| 132 |
|
---|
| 133 | #endif
|
---|