From 3a8b1ca89c9b62e35e24d414d7a79f4c325dfd09 Mon Sep 17 00:00:00 2001 From: astrojhgu Date: Sun, 1 Apr 2012 18:17:41 +0000 Subject: git-svn-id: file:///home/svn/opt_utilities@230 ed2142bd-67ad-457f-ba7c-d818d4011675 --- core/fp_fo_adapter.hpp | 33 ++++ methods/conjugate_gradient/conjugate_gradient.hpp | 188 ++++++++++++++++++++++ test/test_cg.cpp | 55 +++++++ 3 files changed, 276 insertions(+) create mode 100644 core/fp_fo_adapter.hpp create mode 100644 methods/conjugate_gradient/conjugate_gradient.hpp create mode 100644 test/test_cg.cpp diff --git a/core/fp_fo_adapter.hpp b/core/fp_fo_adapter.hpp new file mode 100644 index 0000000..0c5740b --- /dev/null +++ b/core/fp_fo_adapter.hpp @@ -0,0 +1,33 @@ +#ifndef FP_FO_ADAPTER_HPP +#define FP_FO_ADAPTER_HPP +#include "optimizer.hpp" + +namespace opt_utilities +{ + template + class fp_fo_adapter + :public func_obj + { + private: + fp_fo_adapter(); + rT (*fp)(const pT&); + public: + fp_fo_adapter(rT (*_fp)(const pT&)) + :fp(_fp) + {} + + fp_fo_adapter* do_clone()const + { + return new fp_fo_adapter(*this); + } + + rT do_eval(const pT& x) + { + return fp(x); + } + }; +}; + + +#endif + diff --git a/methods/conjugate_gradient/conjugate_gradient.hpp b/methods/conjugate_gradient/conjugate_gradient.hpp new file mode 100644 index 0000000..3fb0f8b --- /dev/null +++ b/methods/conjugate_gradient/conjugate_gradient.hpp @@ -0,0 +1,188 @@ +/** + \file conjugate_gradient.hpp + \brief powerll optimization method + \author Junhua Gu + */ + +#ifndef CONJUGATE_GRADIENT +#define CONJUGATE_GRADIENT +#define OPT_HEADER +#include +//#include +#include +#include +#include +#include "../linmin/linmin.hpp" +#include +#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 conjugate_gradient + :public opt_method + { + public: + typedef pT array1d_type; + typedef rT T; + private: + func_obj* p_fo; + optimizer* p_optimizer; + volatile bool bstop; + + const char* do_get_type_name()const + { + return "conjugate gradient"; + } + 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); + } + + + private: + + + + public: + + conjugate_gradient() + :threshold(1e-4) + {} + + virtual ~conjugate_gradient() + { + }; + + conjugate_gradient(const conjugate_gradient& rhs) + :opt_method(rhs),p_fo(rhs.p_fo),p_optimizer(rhs.p_optimizer), + start_point(rhs.start_point), + end_point(rhs.end_point), + threshold(rhs.threshold) + { + } + + conjugate_gradient& operator=(const conjugate_gradient& rhs) + { + threshold=rhs.threshold; + p_fo=rhs.p_fo; + p_optimizer=rhs.p_optimizer; + start_point=rhs.start_point; + end_point=rhs.end_point; + threshold=rhs.threshold; + } + + opt_method* do_clone()const + { + return new conjugate_gradient(*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; + opt_eq(end_point,start_point); + pT xn; + opt_eq(xn,start_point); + pT Delta_Xn1(gradient(*p_fo,start_point)); + for(size_t i=0;i +#include +#include +using namespace std; + +using namespace opt_utilities; + +class foo + :public diff_func_obj > +{ + foo* do_clone()const + { + return new foo(*this); + } + + double do_eval(const vector& p) + { + static int call_cnt=0; + ++call_cnt; + cerr< do_gradient(const vector& p) + { + static int call_cnt=0; + ++call_cnt; + cerr< result(p.size()); + for(int i=0;i > opt; + //opt.set_opt_method(conjugate_gradient >()); + opt.set_opt_method(powell_method >()); + opt.set_func_obj(foo()); + std::vector p(2); + p[0]=p[1]=4; + opt.set_start_point(p); + p=opt.optimize(); + cout<