From 967219fc1991d466ecd9ede8fa00083b46ebf2ed Mon Sep 17 00:00:00 2001 From: astrojhgu Date: Mon, 26 Jul 2010 14:20:25 +0000 Subject: git-svn-id: file:///home/svn/opt_utilities@127 ed2142bd-67ad-457f-ba7c-d818d4011675 --- math/num_diff.hpp | 70 ++++++++--- math/vector_operation.hpp | 26 ++-- methods/lsnewton_cg/lsnewton_cg.hpp | 238 ++++++++++++++++++++++++++++++++++++ 3 files changed, 306 insertions(+), 28 deletions(-) create mode 100644 methods/lsnewton_cg/lsnewton_cg.hpp diff --git a/math/num_diff.hpp b/math/num_diff.hpp index 0bd2937..2d2e67d 100644 --- a/math/num_diff.hpp +++ b/math/num_diff.hpp @@ -17,35 +17,60 @@ namespace opt_utilities /** calculate the numerical differential of a func_obj */ + template + class diff_func_obj + :public func_obj + { + private: + virtual pT do_gradient(const pT& p)=0; + public: + pT gradient(const pT& p) + { + return do_gradient(p); + } + }; + + + template - rT gradient(func_obj& f,const pT& p,size_t n) + rT gradient(func_obj& f,pT& p,size_t n) { rT ep=std::sqrt(std::numeric_limits::epsilon()); rT result; - pT p2; - resize(p2,get_size(p)); - pT p1; - resize(p1,get_size(p)); + pT p_tmp; + + typename element_type_trait::element_type old_value=get_element(p,n); - for(size_t i=0;i::element_type h= std::max(get_element(p,n),rT(1))*ep; - set_element(p2,n,get_element(p,n)+h); - set_element(p1,n,get_element(p,n)-h); - - rT v2=f(p2); - rT v1=f(p1); - + set_element(p,n,old_value+h); + rT v2=f(p); + set_element(p,n,old_value-h); + rT v1=f(p_tmp); + set_element(p,n,old_value); result=(v2-v1)/h/2; return result; } + template + pT gradient(func_obj& f,pT& p) + { + diff_func_obj* pdfo=0; + if(pdfo=dynamic_cast*>(&f)) + { + return pdfo->gradient(p); + } + pT result; + resize(result,get_size(p)); + for(int i=0;i rT hessian(func_obj& f,const pT& p,size_t m,size_t n) { @@ -83,6 +108,19 @@ namespace opt_utilities rT result=(f(p11)+f(p00)-f(p01)-f(p10))/(4*hm*hn); return result; } + + + template + rT div(func_obj& f,const pT& p) + { + rT result=0; + for(int i=0;i - -template -typename element_type_trait::element_type -inner_product(const pT& v1,const pT& v2) +namespace opt_utilities { - typename element_type_trait::element_type result; - for(int i=0;i + typename element_type_trait::element_type + inner_product(const pT& v1,const pT& v2) + { + typename element_type_trait::element_type result(0); + for(int i=0;i +#include +#include +#include +#include + +#include + +using std::cout; +using std::cerr; +using std::endl; + +namespace opt_utilities +{ + + template + void lsnewton_cg_f(func_obj& foo,pT& x0,const rT& threshold) + { + for(int k=0;;++k) + { + cerr<::epsilon()); + rT djBkdj=0; + pT Bkdj; + resize(Bkdj,get_size(x0)); + pT x1,x2; + typename element_type_trait::element_type h=1; + for(int i=0;i + class lsnewton_cg + :public opt_method + { + public: + typedef pT array1d_type; + typedef rT T; + private: + func_obj* p_fo; + optimizer* p_optimizer; + + //typedef blitz::Array array2d_type; + + + private: + array1d_type start_point; + array1d_type end_point; + + private: + rT threshold; + private: + rT func(const pT& x) + { + assert(p_fo!=0); + return p_fo->eval(x); + } + + + public: + lsnewton_cg() + :threshold(1e-4) + {} + + virtual ~lsnewton_cg() + { + }; + + lsnewton_cg(const lsnewton_cg& rhs) + :p_fo(rhs.p_fo),p_optimizer(rhs.p_optimizer), + start_point(rhs.start_point), + end_point(rhs.end_point), + threshold(rhs.threshold) + { + } + + lsnewton_cg& operator=(const lsnewton_cg& rhs) + { + threshold=rhs.threshold; + p_fo=rhs.p_fo; + p_optimizer=rhs.p_optimizer; + opt_eq(start_point,rhs.start_point); + opt_eq(end_point,rhs.end_point); + } + + opt_method* do_clone()const + { + return new lsnewton_cg(*this); + } + + void do_set_start_point(const array1d_type& p) + { + start_point.resize(get_size(p)); + opt_eq(start_point,p); + + } + + array1d_type do_get_start_point()const + { + return start_point; + } + + 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() + { + lsnewton_cg_f(*p_fo,start_point,threshold); + opt_eq(end_point,start_point); + return end_point; + } + }; + + +} + +#endif +//EOF -- cgit v1.2.2