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 multivariable 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 | //! Wrapper class 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 | //! Return the parameter space dimension
|
---|
64 | inline int NDim() { return mZF->NVar(); }
|
---|
65 | // Simplex initial
|
---|
66 | //! Defines the initial point (center of simplex figure)
|
---|
67 | inline void SetInitialPoint(Vector& point) { mPoint0 = point; }
|
---|
68 | //! Defines the step along each dimension to construct the simplex from initial point
|
---|
69 | inline void SetInitialStep(Vector& step) { mStep0 = step; }
|
---|
70 |
|
---|
71 | //! Set the info/debug print level
|
---|
72 | inline void SetPrtLevel(int lev=0) { mPrt = lev; }
|
---|
73 | //! Return the current print level
|
---|
74 | inline int PrtLevel() { return mPrt; }
|
---|
75 |
|
---|
76 | //! Set the maximum number of iteration
|
---|
77 | inline void SetMaxIter(int max = 100000) { mMaxIter = max; }
|
---|
78 | //! Return the current max iter
|
---|
79 | inline int MaxIter() { return mMaxIter; }
|
---|
80 | //! Return the number of iterations performed
|
---|
81 | inline int NbIter() { return mIter; }
|
---|
82 | //! Return the stop reason
|
---|
83 | inline int StopReason() { return mStop; }
|
---|
84 | //! Return the stop reason and a description string (\b s)
|
---|
85 | int StopReason(string& s);
|
---|
86 |
|
---|
87 | // On minimise f(x) f=mZF->Value() ,
|
---|
88 | // f_max = max(f) sur simplex , f_min = min(f) sur simplex
|
---|
89 | // fm = (abs(f_max)+abs(f_min))
|
---|
90 | // [Delta f] = abs(f_max-f_min)
|
---|
91 | // [Delta f/f]simplex = 2.*Delta f / fm
|
---|
92 | // fm2 = (abs(f_max)+abs(f_max(iter-1)))
|
---|
93 | // [Delta f_max/f_max]iter = [f_max(iter-1)-f_max]/fm2
|
---|
94 | // Test d'arret :
|
---|
95 | // fm < mTol0 OU
|
---|
96 | // [Delta f/f]simplex < mTol1 mRep1 fois de suite OU
|
---|
97 | // [Delta f_max/f_max]iter < mTol2 mRep2 fois de suite
|
---|
98 |
|
---|
99 | //! Define the tolerances for the various convergence tests
|
---|
100 | inline void SetStopTolerance(double tol0=1.e-39, double tol1 = 1.e-3, int rep1=5,
|
---|
101 | double tol2=1.e-4, int rep2=5)
|
---|
102 | { mTol0 = tol0; mTol1 = tol1; mRep1 = rep1; mTol2 = tol2, mRep2 = rep2; }
|
---|
103 |
|
---|
104 | // Controle des facteurs de deformation du simplex:
|
---|
105 | // Alpha = Facteur d'homothetie pour la reflexion simple (Reflection)
|
---|
106 | // Gamma = Facteur d'homothetie pour la reflexion+expansion (ReflecExpand)
|
---|
107 | // Beta = Facteur d'homothetie pour la contraction pour le sommet haut f_max (ContractHigh)
|
---|
108 | // Beta2 = Facteur d'homothetie pour la contraction vers le sommet bas f_min (ContractLow)
|
---|
109 | // Gamma2 = Facteur d'homothetie pour la l'extension pour le sommet haut ExpandHigh
|
---|
110 |
|
---|
111 | //! Define the similarity (homothetic) factors for the different simplex transformations
|
---|
112 | inline void SetControls(double alpha=1., double beta=0.5, double beta2=0.5,
|
---|
113 | double gamma=2.0, double gamma2=2.0)
|
---|
114 | { mAlpha = alpha; mBeta = beta; mBeta2 = beta2; mGamma = gamma; mGamma2 = gamma2;}
|
---|
115 |
|
---|
116 | //! Return the the homothetic factor for Reflection
|
---|
117 | inline double Alpha() { return mAlpha; }
|
---|
118 | //! Return the the homothetic factor for ContractHigh (contraction away from high point)
|
---|
119 | inline double Beta() { return mBeta; }
|
---|
120 | //! Return the the homothetic factor for ContractLow (contraction toward the low point)
|
---|
121 | inline double Beta2() { return mBeta2; }
|
---|
122 | //! Return the the homothetic factor for ReflecExpand (reflection+expansion)
|
---|
123 | inline double Gamma() { return mGamma; }
|
---|
124 | //! Return the the homothetic factor for ExpandHigh (expansion along high point)
|
---|
125 | inline double Gamma2() { return mGamma2; }
|
---|
126 |
|
---|
127 | // Fonction de minimisation - Rc=0, OK, convergence , Rc>0, Non convergence
|
---|
128 | // Voir StopReason(string& s)
|
---|
129 | virtual int Minimize(Vector& fpoint);
|
---|
130 |
|
---|
131 | protected:
|
---|
132 | int FindMinMax12(Vector& fval, int& ilo, int& ihi, int& inhi);
|
---|
133 | inline double Value(Vector& point) { return mZF->Value(point.Data()); }
|
---|
134 |
|
---|
135 | MinZFunction* mZF;
|
---|
136 | int mMaxIter, mIter;
|
---|
137 | int mStop;
|
---|
138 | int mPrt;
|
---|
139 |
|
---|
140 | Vector mPoint0;
|
---|
141 | Vector mStep0;
|
---|
142 | // vector< Vector > simplex0;
|
---|
143 | // vector< Vector > simplex_cur;
|
---|
144 | double mAlpha, mBeta, mBeta2, mGamma, mGamma2;
|
---|
145 | double mTol0, mTol1, mTol2;
|
---|
146 | int mRep1,mRep2;
|
---|
147 | };
|
---|
148 |
|
---|
149 | } // namespace SOPHYA
|
---|
150 |
|
---|
151 | #endif
|
---|