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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
|
/**
\file optimizer.hpp
\brief the definition of classes optimizer, func_obj, and opt_method
\author Junhua Gu
*/
#ifndef OPTIMZER_HPP
#define OPTIMZER_HPP
#define OPT_HEADER
#include <cstddef>
#include "opt_traits.hpp"
#include "opt_exception.hpp"
#include <cstdlib>
#include <functional>
#include <typeinfo>
#ifdef DEBUG
#include <iostream>
using namespace std;
#endif
/**
The root namespace of the opt_utilities library
*/
namespace opt_utilities
{
/////////NULL_PTR//////////////////////////////////////////
#if __cplusplus<201103L
#define NULL_PTR 0
#else
const std::nullptr_t NULL_PTR=nullptr;
#endif
/////////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;
/**
\brief Virtual class representing an object function.
\tparam rT the return type
\tparam pT the self-varible type
*/
template <typename rT,typename pT>
class func_obj
:public std::unary_function<pT,rT>
{
private:
/**
\param x the self-varible
\return the value evaluated
*/
virtual rT do_eval(const pT& x)=0;
/**
\return the clone of an object.
*/
virtual func_obj<rT,pT>* do_clone()const=0;
/**
Destroy the object generated by clone function
*/
virtual void do_destroy()
{
delete this;
}
/**
\return the type name of self
*/
virtual const char* do_get_type_name()const
{
return typeid(*this).name();
}
public:
/**
Interface function to perform the clone
\return the clone of an pre-existed object.
*/
func_obj<rT,pT>* clone()const
{
return do_clone();
}
/**
Interface function to perform the destroy.
*/
void destroy()
{
do_destroy();
}
const char* get_type_name()const
{
return this->do_get_type_name();
}
/**
Makes the class object like a real function.
*/
rT operator()(const pT& p)
{
return do_eval(p);
}
/**
The same as operator().
*/
rT eval(const pT& p)
{
return do_eval(p);
};
/**
deconstruct function
*/
virtual ~func_obj(){};
// virtual XT walk(XT,YT)=0;
};
/**
\brief virtual class representing optimization methods
\tparam rT the return type
\tparam pT the self-varible type
*/
template <typename rT,typename pT>
class opt_method
{
private:
/**
Set the optimizer
\param op optimizer to be set
*/
virtual void do_set_optimizer(optimizer<rT,pT>& op)=0;
/**
Set the precision
\param th threshold
*/
virtual void do_set_precision(rT th)=0;
/**
get the precision
\return threshold
*/
virtual rT do_get_precision()const=0;
/**
perform the optimization
\return final optimized parameter.
*/
virtual pT do_optimize()=0;
/**
\param p start point
*/
virtual void do_set_start_point(const pT& p)=0;
/**
\param p the lower limit
*/
virtual void do_set_lower_limit(const pT&){};
/**
\param p the upper limit
*/
virtual void do_set_upper_limit(const pT&){};
/**
\return start point
*/
virtual pT do_get_start_point()const=0;
/**
\return the lower limit
*/
virtual pT do_get_lower_limit()const
{
return pT();
};
/**
\return the upper limit
*/
virtual pT do_get_upper_limit()const
{
return pT();
};
/**
\return the clone of current object
*/
virtual opt_method<rT,pT>* do_clone()const=0;
/**
destroy the object created by clone()
*/
virtual void do_destroy()
{
delete this;
}
/**
\return the type name of self
*/
virtual const char* do_get_type_name()const
{
return typeid(*this).name();
}
/**
interrupting the optimization
*/
virtual void do_stop()
{
}
public:
/**
Interface function for seting optimizer
\param op optimizer to be set
*/
void set_optimizer(optimizer<rT,pT>& op)
{
do_set_optimizer(op);
};
/**
Interface function to set precision
\param x the threshold
*/
void set_precision(rT x)
{
do_set_precision(x);
}
/**
\return precision
*/
rT get_precision()const
{
return do_get_precision();
}
/**
Interface function to set start point
\param p start point
*/
void set_start_point(const pT& p)
{
do_set_start_point(p);
}
/**
\return start point
*/
pT get_start_point()const
{
return do_get_start_point();
}
/**
Interface function to set lower limit
\param p the lower limit
*/
void set_lower_limit(const pT& p)
{
do_set_lower_limit(p);
}
/**
\return lower limit
*/
pT get_lower_limit()const
{
return do_get_lower_limit();
}
/**
Interface function to set upper limit
\param p upper limit
*/
void set_upper_limit(const pT& p)
{
do_set_upper_limit(p);
}
/**
\return upper limit
*/
pT get_upper_limit()const
{
return do_get_upper_limit();
}
/**
Interface function for performing the optimization
\return the optimized parameter.
*/
pT optimize()
{
return do_optimize();
};
/**
\return the cloned object.
*/
opt_method<rT,pT>* clone()const
{
return do_clone();
}
/**
stop the optimization
*/
void stop()
{
do_stop();
}
/**
destroy the cloned object.
*/
void destroy()
{
do_destroy();
}
/**
\return the type name of self
*/
virtual const char* get_type_name()const
{
return this->do_get_type_name();
}
/**
deconstruct function
*/
virtual ~opt_method(){};
};
/**
\brief The manager for performing the manager
\tparam rT the return type
\tparam pT the self-varible type
*/
template <typename rT,typename pT>
class optimizer
{
public:
private:
/**
pointer pointing a opt_method object
*/
opt_method<rT,pT>* p_opt_method;
/**
pointer pointing a func_obj object
*/
func_obj<rT,pT>* p_func_obj;
public:
/**
default construct function
*/
optimizer()
:p_opt_method(NULL_PTR),p_func_obj(NULL_PTR)
{}
/**
construct function
\param fc object function
\param om optimization method
*/
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);
}
/**
copy construct function
*/
optimizer(const optimizer& rhs)
:p_opt_method(NULL_PTR),p_func_obj(NULL_PTR)
{
if(rhs.p_func_obj!=NULL_PTR)
{
set_func_obj(*(rhs.p_func_obj));
}
if(rhs.p_opt_method!=NULL_PTR)
{
set_opt_method(*(rhs.p_opt_method));
}
}
/**
Assignment operator
*/
optimizer& operator=(const optimizer& rhs)
{
if(this==&rhs)
{
return *this;
}
if(rhs.p_func_obj!=NULL_PTR)
{
set_func_obj(*(rhs.p_func_obj));
}
if(rhs.p_opt_method!=NULL_PTR)
{
set_opt_method(*(rhs.p_opt_method));
}
return *this;
}
/**
destruct function
*/
virtual ~optimizer()
{
if(p_func_obj!=NULL_PTR)
{
//delete p_func_obj;
p_func_obj->destroy();
}
if(p_opt_method!=NULL_PTR)
{
//delete p_opt_method;
p_opt_method->destroy();
}
};
public:
/**
set objection function
\param fc objection function
*/
void set_func_obj(const func_obj<rT,pT>& fc)
{
if(p_func_obj!=NULL_PTR)
{
//delete p_func_obj;
p_func_obj->destroy();
}
p_func_obj=fc.clone();
if(p_opt_method!=NULL_PTR)
{
p_opt_method->set_optimizer(*this);
}
}
/**
set optimization method
\param om optmization method
*/
void set_opt_method(const opt_method<rT,pT>& om)
{
if(p_opt_method!=NULL_PTR)
{
//delete p_opt_method;
p_opt_method->destroy();
}
p_opt_method=om.clone();
p_opt_method->set_optimizer(*this);
}
/**
\return a reference of internally kept optimization method
*/
opt_method<rT,pT>& get_opt_method()
{
if(p_opt_method==NULL_PTR)
{
throw opt_method_not_defined();
}
return *(this->p_opt_method);
}
/**
\return a const reference of internally kept optimization method
*/
const opt_method<rT,pT>& get_opt_method()const
{
if(p_opt_method==NULL_PTR)
{
throw opt_method_not_defined();
}
return *(this->p_opt_method);
}
/**
set precision
\param x threshold
*/
void set_precision(rT x)
{
if(p_opt_method==NULL_PTR)
{
throw opt_method_not_defined();
}
p_opt_method->set_precision(x);
}
/**
\return precision
*/
rT get_precision()const
{
if(p_opt_method==NULL_PTR)
{
throw opt_method_not_defined();
}
return p_opt_method->get_precision();
}
/**
set start point
\param x start point
*/
void set_start_point(const pT& x)
{
if(p_opt_method==NULL_PTR)
{
throw opt_method_not_defined();
}
p_opt_method->set_start_point(x);
}
/**
\return start point
*/
pT get_start_point()const
{
if(p_opt_method==NULL_PTR)
{
throw opt_method_not_defined();
}
return p_opt_method->get_start_point();
}
/**
set lower limit
\param x lower limit
*/
void set_lower_limit(const pT& x)
{
if(p_opt_method==NULL_PTR)
{
throw opt_method_not_defined();
}
p_opt_method->set_lower_limit(x);
}
/**
\return lower limit
*/
pT get_lower_limit()const
{
if(p_opt_method==NULL_PTR)
{
throw opt_method_not_defined();
}
return p_opt_method->get_lower_limit();
}
/**
set upper limit
\param x upper limit
*/
void set_upper_limit(const pT& x)
{
if(p_opt_method==NULL_PTR)
{
throw opt_method_not_defined();
}
p_opt_method->set_upper_limit(x);
}
/**
\return upper limit
*/
pT get_upper_limit()const
{
if(p_opt_method==NULL_PTR)
{
throw opt_method_not_defined();
}
return p_opt_method->get_upper_limit();
}
/**
call the objection function
\param self var
\return function value
*/
rT eval(const pT& x)
{
if(p_func_obj==NULL_PTR)
{
throw object_function_not_defined();
}
return p_func_obj->eval(x);
}
/**
perform the optimization
\return the optimizated paramater
*/
pT optimize()
{
if(p_opt_method==NULL_PTR)
{
throw opt_method_not_defined();
}
if(p_func_obj==NULL_PTR)
{
throw object_function_not_defined();
}
return p_opt_method->optimize();
}
/**
stop the optimize
*/
void stop()
{
if(p_opt_method)
{
p_opt_method->stop();
}
}
/**
\return the pointer to the inner object function
*/
func_obj<rT,pT>* ptr_func_obj()
{
return p_func_obj;
}
/**
\return the reference of the internal kept func_obj object
*/
func_obj<rT,pT>& get_func_obj()
{
if(p_func_obj==NULL_PTR)
{
throw object_function_not_defined();
}
return *p_func_obj;
}
/**
\return the const reference of the internal kept func_obj object
*/
const func_obj<rT,pT>& get_func_obj()const
{
if(p_func_obj==NULL_PTR)
{
throw object_function_not_defined();
}
return *p_func_obj;
}
};
}
#endif
//EOF
|