diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/fitter.hpp | 319 | ||||
-rw-r--r-- | core/optimizer.hpp | 213 |
2 files changed, 488 insertions, 44 deletions
diff --git a/core/fitter.hpp b/core/fitter.hpp index 8e145a5..87e316e 100644 --- a/core/fitter.hpp +++ b/core/fitter.hpp @@ -1,3 +1,7 @@ +/**
+ \file fitter.hpp
+ */
+
#ifndef FITTER_HPP
#define FITTER_HPP
#include "opt_exception.hpp"
@@ -21,6 +25,11 @@ namespace opt_utilities class param_modifier;
+ /**
+ representing a single data point
+ \tparam Ty the type of y
+ \tparam Tx the type of x
+ */
template<typename Ty,typename Tx>
class data
{
@@ -28,6 +37,15 @@ namespace opt_utilities Tx x,x_lower_err,x_upper_err;
Ty y,y_lower_err,y_upper_err;
public:
+ /**
+ construct function
+ \param _x x
+ \param _y y
+ \param _y_lower_err lower y error
+ \param _y_upper_err upper y error
+ \param _x_lower_err lower x error
+ \param _x_upper_err upper x error
+ */
data(const Tx& _x,const Ty& _y,
const Ty& _y_lower_err,
const Ty& _y_upper_err,const Tx& _x_lower_err,const Tx& _x_upper_err)
@@ -41,6 +59,10 @@ namespace opt_utilities }
+
+ /**
+ default construct
+ */
data()
:x(),
x_lower_err(),
@@ -50,6 +72,10 @@ namespace opt_utilities y_upper_err()
{}
+
+ /**
+ copy construct
+ */
data(const data& rhs)
{
opt_eq(x,rhs.x);
@@ -60,6 +86,10 @@ namespace opt_utilities opt_eq(y_upper_err,rhs.y_upper_err);
}
+
+ /**
+ Assignment operator
+ */
data& operator=(const data& rhs)
{
opt_eq(x,rhs.x);
@@ -72,30 +102,52 @@ namespace opt_utilities }
public:
+
+ /**
+ set x
+ \return x
+ */
const Tx& get_x()const
{
return x;
}
+ /**
+ \return x lower error
+ */
const Tx& get_x_lower_err()const
{
return x_lower_err;
}
+ /**
+ \return x upper error
+ */
const Tx& get_x_upper_err()const
{
return x_upper_err;
}
+ /**
+ \return y
+ */
const Ty& get_y()const
{
return y;
}
+
+ /**
+ \return y lower error
+ */
const Ty& get_y_lower_err()const
{
return y_lower_err;
}
+
+ /**
+ \return y upper error
+ */
const Ty& get_y_upper_err()const
{
@@ -103,31 +155,59 @@ namespace opt_utilities }
+ /**
+ set x
+ \param _x x
+ */
+
void set_x(const Tx& _x)
{
opt_eq(x,_x);
}
+ /**
+ set x lower error
+ \param _x x lower error
+ */
void set_x_lower_err(const Tx& _x)
{
opt_eq(x_lower_err,_x);
}
+
+ /**
+ set x upper error
+ \param _x x upper error
+ */
void set_x_upper_err(const Tx& _x)
{
opt_eq(x_upper_err,_x);
}
+
+ /**
+ set y
+ \param _y y
+ */
void set_y(const Ty& _y)
{
opt_eq(y,_y);
}
+
+ /**
+ set y lower error
+ \param _y y lower error
+ */
void set_y_lower_err(const Ty& _y)
{
opt_eq(y_lower_err,_y);
}
+ /**
+ set y upper error
+ \param _y y upper error
+ */
void set_y_upper_err(const Ty& _y)
{
opt_eq(y_upper_err,_y);
@@ -136,12 +216,11 @@ namespace opt_utilities };
- ////////////////////////////////////
- ///class data_set///////////////////
- ///contain a set of data////////////
- ////////////////////////////////////
-
-
+ /**
+ virtual class representing a set of data
+ \tparam Ty type of y
+ \tparam Tx type of x
+ */
template <typename Ty,typename Tx>
class data_set
{
@@ -158,44 +237,75 @@ namespace opt_utilities delete this;
}
public:
+ /**
+ get data
+ \param i the order of the data point
+ \return the const reference of a class data point
+ */
const data<Ty,Tx>& get_data(size_t i)const
{
return this->do_get_data(i);
}
+
+ /**
+ \return the size of the data set
+ */
size_t size()const
{
return do_size();
}
+
+ /**
+ add data point
+ \param d data point
+ */
void add_data(const data<Ty,Tx>& d)
{
return do_add_data(d);
}
+
+ /**
+ clear the data set
+ */
void clear()
{
do_clear();
}
+
+ /**
+ clone self
+ \return a clone of self
+ */
data_set<Ty,Tx>* clone()const
{
return this->do_clone();
}
+ /**
+ destroy the cloned object
+ */
+
void destroy()
{
do_destroy();
}
+ /**
+ destruct function
+ */
+
virtual ~data_set(){}
};
- ///////////////////////////////////////////////
- /////class param_info//////////////////////////
- /////record the information of one parameter///
- /////including the name, default value/////////
- ///////////////////////////////////////////////
-
+
+ /**
+ the information of a model parameter
+ \tparam Tp type of model param type
+ \tparam Tstr the type of string type used
+ */
template <typename Tp,typename Tstr=std::string>
class param_info
{
@@ -207,6 +317,14 @@ namespace opt_utilities typename element_type_trait<Tp>::element_type upper_limit;
public:
+
+ /**
+ construct function
+ \param _name the name of the param
+ \param _v the value of the param
+ \param _l the lower boundary of the param
+ \param _u the upper boundary of the param
+ */
param_info(const Tstr& _name,
const typename element_type_trait<Tp>::element_type& _v,
const typename element_type_trait<Tp>::element_type& _l=0,
@@ -214,10 +332,17 @@ namespace opt_utilities )
:name(_name),value(_v),lower_limit(_l),upper_limit(_u){}
+ /**
+ default construct
+ */
param_info()
:name()
{}
+
+ /**
+ copy construct function
+ */
param_info(const param_info& rhs)
:name(rhs.name)
{
@@ -225,7 +350,11 @@ namespace opt_utilities opt_eq(lower_limit,rhs.lower_limit);
opt_eq(upper_limit,rhs.upper_limit);
}
+
+ /**
+ assignment operator
+ */
param_info& operator=(const param_info& rhs)
{
name=rhs.name;
@@ -235,43 +364,73 @@ namespace opt_utilities return *this;
}
+
+ /**
+ \return the name of the parameter
+ */
const Tstr& get_name()const
{
return this->name;
}
+ /**
+ \return the current value of the parameter
+ */
const typename element_type_trait<Tp>::element_type& get_value()const
{
return value;
}
+ /**
+ \return the lower boundary
+ */
const typename element_type_trait<Tp>::element_type& get_lower_limit()const
{
return lower_limit;
}
+ /**
+ \return the upper boundary
+ */
const typename element_type_trait<Tp>::element_type& get_upper_limit()const
{
return upper_limit;
}
-
-
+
+ /**
+ set the value
+ \param x the value of the parameter
+ */
void set_value(const typename element_type_trait<Tp>::element_type& x)
{
opt_eq(value,x);
}
+
+ /**
+ set the lower boundary
+ \param x the lower boundary
+ */
void set_lower_limit(const typename element_type_trait<Tp>::element_type& x)
{
opt_eq(lower_limit,x);
}
+ /**
+ set the upper limit
+ \param x the upper boundary
+ */
void set_upper_limit(const typename element_type_trait<Tp>::element_type& x)
{
opt_eq(upper_limit,x);
}
+ /**
+ set the name of the parameter
+ \param _name the name of the parameter
+ */
+
void set_name(const Tstr& _name)
{
name=_name;
@@ -281,7 +440,13 @@ namespace opt_utilities -
+ /**
+ virtual class representing a model
+ \tparam Ty the type of the returned value of the model
+ \tparam Tx the type of the self-var
+ \tparam Tp the type of the model param
+ \tparam Tstr the type of the string used
+ */
template <typename Ty,typename Tx,typename Tp,typename Tstr=std::string>
class model
{
@@ -298,20 +463,35 @@ namespace opt_utilities delete this;
}
public:
+
+ /**
+ \return the cloned object
+ */
model<Ty,Tx,Tp,Tstr>* clone()const
{
return do_clone();
}
+ /**
+ destroy the cloned object
+ */
void destroy()
{
do_destroy();
}
public:
+
+ /**
+ default construct function
+ */
model()
:p_param_modifier(0)
{}
+
+ /**
+ copy construct
+ */
model(const model& rhs)
:p_param_modifier(0)
{
@@ -324,6 +504,10 @@ namespace opt_utilities }
+
+ /**
+ assignment operator
+ */
model& operator=(const model& rhs)
{
if(this==&rhs)
@@ -339,6 +523,10 @@ namespace opt_utilities return *this;
}
+
+ /**
+ destructure function
+ */
virtual ~model()
{
if(p_param_modifier)
@@ -348,6 +536,11 @@ namespace opt_utilities }
}
+
+ /**
+ set the param modifier
+ \param pm param modifier
+ */
void set_param_modifier(const param_modifier<Ty,Tx,Tp,Tstr>& pm)
{
if(p_param_modifier!=0)
@@ -359,6 +552,9 @@ namespace opt_utilities p_param_modifier->set_model(*this);
}
+ /**
+ clear the param modifier
+ */
void set_param_modifier()
{
if(p_param_modifier!=0)
@@ -369,6 +565,10 @@ namespace opt_utilities p_param_modifier=0;
}
+
+ /**
+ \return the param_modifier
+ */
param_modifier<Ty,Tx,Tp,Tstr>& get_param_modifier()
{
if(p_param_modifier==0)
@@ -378,6 +578,11 @@ namespace opt_utilities return *p_param_modifier;
}
+
+ /**
+ report the param status
+ \return the param status
+ */
Tstr report_param_status(const Tstr& s)const
{
if(p_param_modifier==0)
@@ -390,6 +595,10 @@ namespace opt_utilities }
+ /**
+ \param pname the name of the param
+ \return the param info
+ */
const param_info<Tp,Tstr>& get_param_info(const Tstr& pname)
{
for(typename std::vector<param_info<Tp,Tstr> >::iterator i=param_info_list.begin();
@@ -406,12 +615,20 @@ namespace opt_utilities return null_param;
}
+
+ /**
+ \param n the order of the parameter
+ \return the param info
+ */
const param_info<Tp,Tstr>& get_param_info(size_t n)const
{
return param_info_list[n%get_num_params()];
}
+ /**
+ \return the full parameter vector
+ */
Tp get_all_params()const
{
Tp result;
@@ -425,6 +642,10 @@ namespace opt_utilities return result;
}
+
+ /**
+ \return the lower limit
+ */
Tp get_all_lower_limits()const
{
Tp result;
@@ -438,6 +659,9 @@ namespace opt_utilities return result;
}
+ /**
+ \return the upper limit
+ */
Tp get_all_upper_limits()const
{
Tp result;
@@ -452,11 +676,18 @@ namespace opt_utilities }
+ /**
+ \return the number of parameters
+ */
size_t get_num_params()const
{
return param_info_list.size();
}
+
+ /**
+ \return the number of free parameters
+ */
size_t get_num_free_params()const
{
if(p_param_modifier)
@@ -466,6 +697,11 @@ namespace opt_utilities return get_num_params();
}
+
+ /**
+ \param pname the name of the parameter
+ \param v the value of the pearameter
+ */
void set_param_value(const Tstr& pname,
const typename element_type_trait<Tp>::element_type& v)
{
@@ -484,6 +720,11 @@ namespace opt_utilities }
+ /**
+ set the lower limit
+ \param pname the parameter name
+ \param v the value of the lower limit
+ */
void set_param_lower_limit(const Tstr& pname,
const typename element_type_trait<Tp>::element_type& v)
{
@@ -501,6 +742,12 @@ namespace opt_utilities throw param_not_found();
}
+
+ /**
+ set the upper limit
+ \param pname the parameter name
+ \param v the value of the upper limit
+ */
void set_param_upper_limit(const Tstr& pname,
const typename element_type_trait<Tp>::element_type& v)
{
@@ -519,6 +766,10 @@ namespace opt_utilities }
+ /**
+ set param
+ \param param the values of the parameter
+ */
void set_param_value(const Tp& param)
{
for(size_t i=0;i<param_info_list.size();++i)
@@ -527,6 +778,11 @@ namespace opt_utilities }
}
+
+ /**
+ set lower limit
+ \param param the lower limit of the parameter
+ */
void set_param_lower_limit(const Tp& param)
{
for(size_t i=0;i<param_info_list.size();++i)
@@ -535,6 +791,11 @@ namespace opt_utilities }
}
+
+ /**
+ set upper limit
+ \param param the upper limit of the parameter
+ */
void set_param_upper_limit(const Tp& param)
{
for(size_t i=0;i<param_info_list.size();++i)
@@ -544,7 +805,11 @@ namespace opt_utilities }
-
+ /**
+ get the order of a parameter
+ \param pname the name of the parameter
+ \return the order of the parameter
+ */
size_t get_param_order(const Tstr& pname)const
{
for(size_t i=0;i<param_info_list.size();++i)
@@ -563,12 +828,20 @@ namespace opt_utilities protected:
+
+ /**
+ add param info
+ \param pinfo the param info to be added
+ */
void push_param_info(const param_info<Tp,Tstr>& pinfo)
{
param_info_list.push_back(pinfo);
// this->num_free_params++;
}
+ /**
+ clear the param information list
+ */
void clear_param_info()
{
// this->num_free_params=0;
@@ -578,11 +851,17 @@ namespace opt_utilities public:
+
+ /**
+ \return the description of the model
+ */
Tstr to_string()const
{
return do_to_string();
}
+
+
Tp reform_param(const Tp& p)const
{
if(p_param_modifier==0)
@@ -601,12 +880,18 @@ namespace opt_utilities return p_param_modifier->deform(p);
}
-
+ /**
+ evaluate the model
+ \param x the self var
+ \param p the parameter
+ \return the model value
+ */
Ty eval(const Tx& x,const Tp& p)
{
return do_eval(x,reform_param(p));
}
+
Ty eval_raw(const Tx& x,const Tp& p)
{
return do_eval(x,reform_param(p));
diff --git a/core/optimizer.hpp b/core/optimizer.hpp index 70f35f7..7492bab 100644 --- a/core/optimizer.hpp +++ b/core/optimizer.hpp @@ -1,3 +1,9 @@ +/** + \file optimizer.hpp + \brief the definition of classes optimizer, func_obj, and opt_method + */ + + #ifndef OPTIMZER_H_ #define OPTIMZER_H_ //#define DEBUG @@ -23,115 +29,212 @@ namespace opt_utilities 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/////////////// - ////////////////////////////////////////////////// + /** + 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: - virtual rT do_eval(const pT&)=0; + /** + \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; } public: - 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(); } + + /** + 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; }; - ///////////////Optimization method////////////////////// - + /** + virtual class representing optimization methods + \tparam rT the return type + \tparam pT the self-varible type + */ 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; + 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; + /** + 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& p){}; + /** + \param p the upper limit + */ virtual void do_set_upper_limit(const pT& p){}; + /** + \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; } 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); } + /** + Interface function to set start point + \param p start point + */ void set_start_point(const pT& p) { do_set_start_point(p); } + /** + Interface function to set lower limit + \param p the lower limit + */ + void set_lower_limit(const pT& p) { do_set_lower_limit(p); } + /** + Interface function to set upper limit + \param p upper limit + */ + void set_upper_limit(const pT& p) { do_set_upper_limit(p); } + + /** + 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(); } + + /** + destroy the cloned object. + */ void destroy() { do_destroy(); } + /** + deconstruct function + */ virtual ~opt_method(){}; }; - ///////////Optimizer//////////////////////////////////// + /** + The manager for performing the manager + \tparam rT the return type + \tparam pT the self-varible type + */ template <typename rT,typename pT> class optimizer { @@ -139,23 +242,38 @@ namespace opt_utilities private: - ////////////pointer to an optimization method objection//////////// - ////////////The optimization method implements a certain method /// - ////////////Currently only Mont-carlo method is implemented//////// + /** + 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(0),p_func_obj(0) {} + /** + 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(0),p_func_obj(0) { @@ -169,6 +287,10 @@ namespace opt_utilities } } + + /** + Assignment operator + */ optimizer& operator=(const optimizer& rhs) { if(this==&rhs) @@ -186,7 +308,9 @@ namespace opt_utilities return *this; } - + /** + destruct function + */ virtual ~optimizer() { if(p_func_obj!=0) @@ -202,7 +326,10 @@ namespace opt_utilities }; public: - ////////////Re-set target function object/////////////////////////// + /** + set objection function + \param fc objection function + */ void set_func_obj(const func_obj<rT,pT>& fc) { if(p_func_obj!=0) @@ -217,7 +344,10 @@ namespace opt_utilities } } - ////////////Re-set optimization method////////////////////////////// + /** + set optimization method + \param om optmization method + */ void set_opt_method(const opt_method<rT,pT>& om) { if(p_opt_method!=0) @@ -230,6 +360,9 @@ namespace opt_utilities p_opt_method->set_optimizer(*this); } + /** + \return a reference of internally kept optimization method + */ opt_method<rT,pT>& method() { if(p_opt_method==0) @@ -239,6 +372,11 @@ namespace opt_utilities return *(this->p_opt_method); } + + /** + set precision + \param x threshold + */ void set_precision(rT x) { if(p_opt_method==0) @@ -248,6 +386,11 @@ namespace opt_utilities p_opt_method->set_precision(x); } + + /** + set start point + \param x start point + */ void set_start_point(const pT& x) { if(p_opt_method==0) @@ -257,6 +400,11 @@ namespace opt_utilities p_opt_method->set_start_point(x); } + + /** + set lower limit + \param x lower limit + */ void set_lower_limit(const pT& x) { if(p_opt_method==0) @@ -266,6 +414,11 @@ namespace opt_utilities p_opt_method->set_lower_limit(x); } + + /** + set upper limit + \param x upper limit + */ void set_upper_limit(const pT& x) { if(p_opt_method==0) @@ -276,9 +429,11 @@ namespace opt_utilities } - ////////////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//////////////////////////////////// + /** + call the objection function + \param self var + \return function value + */ rT eval(const pT& x) { if(p_func_obj==0) @@ -290,8 +445,10 @@ namespace opt_utilities - ////////////Just call the optimize() function in the optimization method// - ////////////If no optimization method is given, an zero-value is returned/ + /** + perform the optimization + \return the optimizated paramater + */ pT optimize() { if(p_opt_method==0) @@ -305,7 +462,9 @@ namespace opt_utilities return p_opt_method->optimize(); } - ////////////Function that offers the access to the target function object/// + /** + \return the pointer to the inner object function + */ func_obj<rT,pT>* ptr_func_obj() { return p_func_obj; |