aboutsummaryrefslogtreecommitdiffstats
path: root/core/optimizer.hpp
blob: e953b2dc77bc22d44254cc07f09c2b44fdf5401b (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#ifndef OPTIMZER_H_
#define OPTIMZER_H_
//#define DEBUG
#include <cstddef>
#include "opt_traits.hpp"
#include "opt_exception.hpp"
#include <cstdlib>
#include <functional>
#ifdef DEBUG
#include <iostream>
using namespace std;
#endif

namespace opt_utilities
{
  /////////Forward declare///////////////////////////////////
  template <typename rT,typename pT>
  class optimizer;
  
  template <typename rT,typename pT>
  class func_obj;
  
  template <typename rT,typename pT>
  class opt_method;


  //////////////Target Function/////////////////////
  ///An eval function should be implemented/////////
  ///The eval function return the function value////
  ///which is wrapped by the func_obj///////////////
  //////////////////////////////////////////////////
  template <typename rT,typename pT>
  class func_obj
    :public std::unary_function<pT,rT>
  {
  private:
    virtual rT do_eval(const pT&)=0;
    virtual func_obj<rT,pT>* do_clone()const=0;
    virtual void do_destroy()
    {
      delete this;
    }

  public:
  public:
    func_obj<rT,pT>* clone()const
    {
      return do_clone();
    }

    void destroy()
    {
      do_destroy();
    }

    rT operator()(const pT& p)
    {
      return do_eval(p);
    }
    

    rT eval(const pT& p)
    {
      return do_eval(p);
    };
    virtual ~func_obj(){};
    //    virtual XT walk(XT,YT)=0;
  };
  

  ///////////////Optimization method//////////////////////
  
  template <typename rT,typename pT>
  class opt_method
  {
  public:
    virtual void do_set_optimizer(optimizer<rT,pT>&)=0;
    virtual void do_set_precision(rT)=0;
    virtual pT do_optimize()=0;
    virtual void do_set_start_point(const pT& p)=0;
    virtual opt_method<rT,pT>* do_clone()const=0;

    virtual void do_destroy()
    {
      delete this;
    }
  public:
    void  set_optimizer(optimizer<rT,pT>& op)
      {
	do_set_optimizer(op);
      };

    void set_precision(rT x)
    {
      do_set_precision(x);
    }

    void set_start_point(const pT& p)
    {
      do_set_start_point(p);
    }
    
    pT optimize()
      {
	return do_optimize();
      };
    
    opt_method<rT,pT>* clone()const
    {
      return do_clone();
    }

    void destroy()
    {
      do_destroy();
    }

    virtual ~opt_method(){};
  };
  
  
  ///////////Optimizer////////////////////////////////////
  template <typename rT,typename pT>
  class optimizer
  {
  public:

  private:

    ////////////pointer to an optimization method objection////////////
    ////////////The optimization method implements a certain method ///
    ////////////Currently only Mont-carlo method is implemented////////
    opt_method<rT,pT>* p_opt_method;
    func_obj<rT,pT>* p_func_obj;
    
  public:
     optimizer()
      :p_opt_method(0),p_func_obj(0)
    {}

    optimizer(func_obj<rT,pT>& fc,const opt_method<rT,pT>& om)
      :p_func_obj(fc.clone()),p_opt_method(om.clone())
    {
      p_opt_method->set_optimizer(*this);
    }
    
    optimizer(const optimizer& rhs)
      :p_opt_method(0),p_func_obj(0)
    {
      if(rhs.p_func_obj!=0)
	{
	  set_func_obj(*(rhs.p_func_obj));
	}
      if(rhs.p_opt_method!=0)
	{
	  set_opt_method(*(rhs.p_opt_method));
	}
    }

    optimizer& operator=(const optimizer& rhs)
    {
      if(this==&rhs)
	{
	  return *this;
	}
      if(rhs.p_func_obj!=0)
	{
	  set_func_obj(*(rhs.p_func_obj));
	}
      if(rhs.p_opt_method!=0)
	{
	  set_opt_method(*(rhs.p_opt_method));
	}
      return *this;
    }
    

    virtual ~optimizer()
    {
      if(p_func_obj!=0)
	{
	  //delete p_func_obj;
	  p_func_obj->destroy();
	}
      if(p_opt_method!=0)
	{
	  //delete p_opt_method;
	  p_opt_method->destroy();
	}
    };

  public:
    ////////////Re-set target function object///////////////////////////
    void set_func_obj(const func_obj<rT,pT>& fc)
    {
      if(p_func_obj!=0)
	{
	  //delete p_func_obj;
	  p_func_obj->destroy();
	}
      p_func_obj=fc.clone();
      if(p_opt_method!=0)
	{
	  p_opt_method->set_optimizer(*this);
	}
    }

    ////////////Re-set optimization method//////////////////////////////
    void set_opt_method(const opt_method<rT,pT>& om)
    {
      if(p_opt_method!=0)
	{
	  //delete p_opt_method;
	  p_opt_method->destroy();
	}
      
      p_opt_method=om.clone();
      p_opt_method->set_optimizer(*this);
    }

    opt_method<rT,pT>& method()
    {
      if(p_opt_method==0)
	{
	  throw opt_method_undefined();
	}
      return *(this->p_opt_method);
    }

    void set_precision(rT x)
    {
      if(p_opt_method==0)
	{
	  throw opt_method_undefined();
	}
      p_opt_method->set_precision(x);
    }

    void set_start_point(const pT& x)
    {
      if(p_opt_method==0)
	{
	  throw opt_method_undefined();
	}
      p_opt_method->set_start_point(x);
    }
    
    ////////////Just call the eval function in the target function object///
    ////////////In case the pointer to a target function is uninitialed/////
    ////////////a zero-value is returned////////////////////////////////////
    rT eval(const pT& x)
    {
      if(p_func_obj==0)
	{
	  throw target_function_undefined();
	}
      return p_func_obj->eval(x);
    }

	

    ////////////Just call the optimize() function in the optimization method//
    ////////////If no optimization method is given, an zero-value is returned/
    pT optimize()
    {
      if(p_opt_method==0)
	{
	  throw opt_method_undefined();
	}
      if(p_func_obj==0)
	{
	  throw target_function_undefined();
	}
      return p_opt_method->optimize();
    }
    
    ////////////Function that offers the access to the target function object///
    func_obj<rT,pT>* ptr_func_obj()
    {
      return p_func_obj;
    }
    
  };
}
  
#endif
//EOF