/** \file powell_method.hpp \brief powerll optimization method \author Junhua Gu */ #ifndef POWELL_METHOD #define POWELL_METHOD #define OPT_HEADER #include //#include #include #include #include #include "../linmin/linmin.hpp" #include #include namespace opt_utilities { /** \brief Impliment of an optimization method \tparam rT return type of the object function \tparam pT parameter type of the object function */ template class powell_method :public opt_method { public: typedef pT array1d_type; typedef rT T; private: func_obj* p_fo; optimizer* p_optimizer; mutable bool bstop; //typedef blitz::Array array2d_type; const char* do_get_type_name()const { return "powell method"; } private: array1d_type start_point; array1d_type end_point; private: int ncom; array1d_type pcom_p; array1d_type xicom_p; rT threshold; T** xi; T* xi_1d; private: rT func(const pT& x) { assert(p_fo!=0); return p_fo->eval(x); } private: void clear_xi() { if(xi_1d!=0) { delete[] xi_1d; } if(xi!=0) { delete[] xi; } } void init_xi(int n) { clear_xi(); xi_1d=new T[n*n]; xi=new T*[n]; for(int i=0;i!=n;++i) { xi[i]=xi_1d+i*n; } for(int i=0;i!=n;++i) { for(int j=0;j!=n;++j) { xi[i][j]=(j==i?1:0); } } } void powell(array1d_type& p,const T ftol, int& iter,T& fret) { const int ITMAX=200; const T TINY=std::numeric_limits::epsilon(); int i,j,ibig; T del,fp,fptt,t; int n=(int)get_size(p); array1d_type pt(n); array1d_type ptt(n); array1d_type xit(n); fret=p_fo->eval(p); for(j=0;jdel) { del=fptt-fret; ibig=i+1; } } if(T(2.)*(fp-fret)<=ftol*(tabs(fp)+tabs(fret))+TINY) { return; } if(iter==ITMAX) { std::cerr<<"powell exceeding maximun iterations."<& rhs) :opt_method(rhs),p_fo(rhs.p_fo),p_optimizer(rhs.p_optimizer), start_point(rhs.start_point), end_point(rhs.end_point), ncom(rhs.ncom), threshold(rhs.threshold),xi(0),xi_1d(0) { } powell_method& operator=(const powell_method& rhs) { threshold=rhs.threshold; xi=0; xi_1d=0; p_fo=rhs.p_fo; p_optimizer=rhs.p_optimizer; start_point=rhs.start_point; end_point=rhs.end_point; ncom=rhs.ncom; threshold=rhs.threshold; } opt_method* do_clone()const { return new powell_method(*this); } void do_set_start_point(const array1d_type& p) { resize(start_point,get_size(p)); opt_eq(start_point,p); } array1d_type do_get_start_point()const { return start_point; } void do_set_lower_limit(const array1d_type& p) {} void do_set_upper_limit(const array1d_type& p) {} void do_set_precision(rT t) { threshold=t; } rT do_get_precision()const { return threshold; } void do_set_optimizer(optimizer& o) { p_optimizer=&o; p_fo=p_optimizer->ptr_func_obj(); } pT do_optimize() { bstop=false; init_xi((int)get_size(start_point)); for(int i=0;i<(int)get_size(start_point);++i) { for(int j=0;j<(int)get_size(start_point);++j) { xi[i][j]=(i==j)?1:0; } } int iter=100; opt_eq(end_point,start_point); rT fret; powell(end_point,threshold,iter,fret); return end_point; } void do_stop() { bstop=true; } }; } #endif //EOF