aboutsummaryrefslogtreecommitdiffstats
path: root/interface/optimize.hpp
blob: f56642f712361209d66a1accda812e7cc729d519 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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