diff options
author | astrojhgu <astrojhgu@ed2142bd-67ad-457f-ba7c-d818d4011675> | 2012-09-08 04:58:34 +0000 |
---|---|---|
committer | astrojhgu <astrojhgu@ed2142bd-67ad-457f-ba7c-d818d4011675> | 2012-09-08 04:58:34 +0000 |
commit | 568e1711e0db3986ef8733a319556cce83a5056c (patch) | |
tree | 6a23eff5c67098e09c7f176ac11e4c4dd35ceda7 /interface | |
parent | b41a10e36629611fea88efe354e6e7deb66cd697 (diff) | |
download | opt-utilities-568e1711e0db3986ef8733a319556cce83a5056c.tar.bz2 |
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
Diffstat (limited to 'interface')
-rw-r--r-- | interface/optimize.hpp | 55 |
1 files changed, 55 insertions, 0 deletions
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 <vector> +#include <functional> +#include <cmath> +#include <utility> +#include "../core/optimizer.hpp" +#include "../methods/powell/powell_method.hpp" + +namespace opt_utilities +{ + + template <typename Ty,typename Tx> + Tx optimize(const std::function<Ty(Tx)>& func,const Tx& start_point,const opt_utilities::opt_method<Ty,Tx>& opm=opt_utilities::powell_method<double,std::vector<double> >()) + { + class func_wrapper + :public opt_utilities::func_obj<Ty,Tx> + { + std::function<Ty(Tx)> f; + public: + func_wrapper(const std::function<Ty(Tx)>& f1) + :f(f1) + {}; + + func_wrapper* do_clone()const + { + return const_cast<func_wrapper*>(this); + } + + void do_destroy() + { + //do nothing + } + + Ty do_eval(const Tx& x) + { + return f(x); + } + }foo(func); + opt_utilities::optimizer<Ty,Tx> opt; + opt.set_opt_method(opm); + opt.set_func_obj(foo); + opt.set_start_point(start_point); + return opt.optimize(); + } + +} + +#endif +//EOF |