/** \file aga.hpp \brief asexual genetic algorithm method \author Junhua Gu */ #ifndef AGA_METHOD #define AGA_METHOD #define OPT_HEADER #include //#include #include #include #include #include #include #include #include #include /* * */ #include using std::cout; using std::endl; namespace opt_utilities { template struct vp_pair { rT v; pT p; }; template class vp_comp { public: bool operator()(const vp_pair& x1, const vp_pair& x2) { return x1.v class aga_method :public opt_method { public: typedef pT array1d_type; private: int n1,n2,n0; func_obj* p_fo; optimizer* p_optimizer; rT threshold; pT lower_bound; pT upper_bound; typename element_type_trait::element_type decay_factor; pT reproduction_box; std::vector > samples; std::vector buffer; mutable bool bstop; private: typename element_type_trait::element_type uni_rand (typename element_type_trait::element_type x1, typename element_type_trait::element_type x2) { return rand()/(double)RAND_MAX*(x2-x1)+x1; } private: const char* do_get_type_name()const { return "asexual genetic algorithm"; } rT func(const pT& x) { assert(p_fo!=0); return p_fo->eval(x); } public: aga_method(int _n1,int _n2) :n1(_n1),n2(_n2),n0(n1*n2+n1), p_fo(0),p_optimizer(0),threshold(1e-4), decay_factor(.999), samples(n1*n2+n1) { } aga_method() :n1(50),n2(20),n0(n1*n2+n1), p_fo(0),p_optimizer(0),threshold(1e-4), decay_factor(.999), samples(n1*n2+n1) { } virtual ~aga_method() { }; aga_method(const aga_method& rhs) :n1(rhs.n1),n2(rhs.n2),n0(rhs.n0), p_fo(rhs.p_fo),p_optimizer(rhs.p_optimizer), threshold(rhs.threshold), decay_factor(rhs.decay_factor), samples(rhs.samples) { } aga_method& operator=(const aga_method& rhs) { threshold=rhs.threshold; p_fo=rhs.p_fo; p_optimizer=rhs.p_optimizer; samples=rhs.samples; n1=rhs.n1; n2=rhs.n2; n0=rhs.n0; } void set_decay_factor(typename element_type_trait::element_type _decay_factor) { decay_factor=_decay_factor; } opt_method* do_clone()const { return new aga_method(*this); } void do_set_start_point(const array1d_type& p) { for(size_t i=0;i& o) { p_optimizer=&o; p_fo=p_optimizer->ptr_func_obj(); } bool iter() { rT sum2=0; rT sum=0; for(size_t i=0;i()); if(sum2/samples.size()-pow(sum/samples.size(),2)get_element(upper_bound,j)) { set_element(p,j,get_element(upper_bound,j)); } if(get_element(p,j)p; } void do_stop() { bstop=true; } }; } #endif //EOF