00001 #ifndef MINIMIZATION_H 00002 #define MINIMIZATION_H 00003 using namespace std; 00004 00005 // ABSTRACT class, can't be instantiated to be an object itself. Only good for a base(parent) class. 00006 class CMinimization{ 00007 public : 00008 int n; //dimension 00009 double TOL_CG, TOL_Brent; 00010 double * vec_x; // variables 00011 double * vec_dx; // derivatives of variables 00012 double * * H; // Hessian matrix as of d2f/dxidxj 00013 virtual double fn( double * vec_x)=0; // get function's value with the current *x value. 00014 virtual bool dfn( double * vec_x)=0; // calculate the gradient of the function and the value is stored in vec_dx; 00015 00016 double fn1( double x); //get function value with the current x value, this is one dimensional function. 00017 // get function value at vec_x+x*vec_dir. vec_x is the starting point and x*vec_dir is the step x along vec_dir direction. 00018 // This one-dimensional function will use fn(double *x) to evaluate function and used in one-dimensional minimization search. 00019 // As an abstract class, this will not be used. Only for demonstration purpose and test. 00020 CMinimization( int dimension); 00021 // default constructor. Only to define the object. Dimension is 1. 00022 //Use SetDimension() to set multi-dimensional search. 00023 CMinimization(); 00024 ~CMinimization(); 00025 00026 bool SetDimension( int dimension); 00027 00028 // One dimensional search 00029 // Given distinct points ax and bx, searches for new points ax, bx and cx so that they bracket a minimum of the function. 00030 bool bracket( double &ax, double &bx, double &cx, double &fa, double &fb, double &fc); 00031 double brent( double ax, double bx, double cx, double &xmin); 00032 00033 bool conjugate_gradient( double * initial_x, int &iteration, double &fmin); // input the initial guess of x 00034 // return the minimum point by reset initial_x and return minimum fmin by reference. Times of iteration is returned by iteration. 00035 // the minimum point can also be found from vec_x; 00036 protected : 00037 bool AllocVectors(); 00038 bool TagMultiD; 00039 // "linear_" means only used in one-dimensional minimization search 00040 bool linear_Min( double &fmin); // fmin returns the minimum value of function. vec_x is reset to the local minimum. 00041 double * linear_dir; // direction used in one-dimensional minimization search 00042 double * linear_trial_x; // To avoid using new operator each time when try new x 00043 00044 // book keeping utilities 00045 inline void shift2( double &a, double &b, double c) 00046 { 00047 a=b; 00048 b=c; 00049 } 00050 inline void shift3( double &a, double &b, double &c, double d) 00051 { 00052 a=b; 00053 b=c; 00054 c=d; 00055 } 00056 inline void swap2( double &a, double &b) 00057 { 00058 double c=b; 00059 b=a; 00060 a=c; 00061 } 00062 00063 }; 00064 00065 #endif 00066