GridSearch.h

00001 #ifndef GridSearch_hpp
00002 #define GridSearch_hpp
00003 
00004 #include <iostream>
00005 #include <vector>
00006 
00007 using namespace std;
00008 
00010 template<class functor, class real>
00011 class GridSearch {
00012  protected:
00013   // key attributes
00015   int _max_number_increment;
00016 
00018   real _X0;
00020   real _dX;
00021   
00023   real (functor::*_f)(real);
00024 
00026   functor& _obj;
00027 
00028   // derived attributes
00029 
00031   mutable real _x_max;
00033   mutable int _i_max;
00035   mutable real _f_x_max;
00036 
00037 
00039   mutable real _x_min;
00041   mutable int _i_min;
00043   mutable real _f_x_min;
00044 
00046   mutable vector<real> _x;
00047 
00049   mutable vector<real> _F_x;
00050 
00051  public:  
00052 
00054   bool get_boundary_max() const {
00055     return (_i_max == 0 || _i_max == _max_number_increment);
00056   }
00057 
00059   bool get_boundary_min() const {
00060     return (_i_min == 0 || _i_min == _max_number_increment);
00061   }
00062 
00064   functor& get_functor() { return _obj;}
00065 
00067   GridSearch( 
00068     int  Max_Number_Increment,
00069     real X0, 
00070     real dX,
00071     functor& obj, 
00072     real (functor::*f)(real))
00073     : _max_number_increment( Max_Number_Increment), _X0(X0), _dX(dX), _f(f), _obj(obj),
00074     _x(Max_Number_Increment+1), _F_x(Max_Number_Increment+1)
00075   {
00076 
00077   }
00078 
00080   void do_iteration( bool print_results) {
00081     int i;
00082     for (i=0; i<= _max_number_increment;i++) {
00083       _x[i] = _X0 + _dX * i;
00084       _F_x[i] = (_obj.*_f)( _x[i]);
00085       if (i==0) {
00086         _i_max = _i_min = 0;
00087         _x_max = _x_min = _X0;
00088         _f_x_max = _f_x_min = _F_x[0];
00089       } else {
00090         if (_F_x[i] > _f_x_max) {
00091           _i_max = i;
00092           _x_max = _x[i];
00093           _f_x_max = _F_x[i];
00094         } else if (_F_x[i] < _f_x_min) {
00095           _i_min = i;
00096           _x_min = _x[i];
00097           _f_x_min = _F_x[i];
00098         }
00099       }
00100       if (print_results) {
00101         cout << i << " " << _x[i] << " " << _F_x[i] << endl;
00102       }
00103     }
00104   }
00105 
00107   real get_x_max() const { return _x_max;}
00109   int get_i_max() const { return _i_max;}
00111   real get_f_x_max() const { return _f_x_max;}
00112 
00114   real get_x_min() const { return _x_min;}
00116   int get_i_min() const { return _i_min;}
00118   real get_f_x_min() const { return _f_x_min;}
00119 
00120 };
00121 
00122 
00123 #endif

Generated on Fri Jan 7 12:36:18 2011 for public_options by  doxygen 1.5.1