From 568e1711e0db3986ef8733a319556cce83a5056c Mon Sep 17 00:00:00 2001 From: astrojhgu Date: Sat, 8 Sep 2012 04:58:34 +0000 Subject: New free function "optimize" added, which can accept function pointer or lambda expression as an objective function. git-svn-id: file:///home/svn/opt_utilities@243 ed2142bd-67ad-457f-ba7c-d818d4011675 --- interface/optimize.hpp | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 interface/optimize.hpp (limited to 'interface/optimize.hpp') diff --git a/interface/optimize.hpp b/interface/optimize.hpp new file mode 100644 index 0000000..f56642f --- /dev/null +++ b/interface/optimize.hpp @@ -0,0 +1,55 @@ +#ifndef OPTIMIZE_FUNC_HPP +#define OPTIMIZE_FUNC_HPP + +#if __cplusplus<201103L +#error This header must be used with C++ 11(0x) or newer +#endif + +#include +#include +#include +#include +#include "../core/optimizer.hpp" +#include "../methods/powell/powell_method.hpp" + +namespace opt_utilities +{ + + template + Tx optimize(const std::function& func,const Tx& start_point,const opt_utilities::opt_method& opm=opt_utilities::powell_method >()) + { + class func_wrapper + :public opt_utilities::func_obj + { + std::function f; + public: + func_wrapper(const std::function& f1) + :f(f1) + {}; + + func_wrapper* do_clone()const + { + return const_cast(this); + } + + void do_destroy() + { + //do nothing + } + + Ty do_eval(const Tx& x) + { + return f(x); + } + }foo(func); + opt_utilities::optimizer opt; + opt.set_opt_method(opm); + opt.set_func_obj(foo); + opt.set_start_point(start_point); + return opt.optimize(); + } + +} + +#endif +//EOF -- cgit v1.2.2