diff options
author | astrojhgu <astrojhgu@ed2142bd-67ad-457f-ba7c-d818d4011675> | 2009-08-29 10:26:50 +0000 |
---|---|---|
committer | astrojhgu <astrojhgu@ed2142bd-67ad-457f-ba7c-d818d4011675> | 2009-08-29 10:26:50 +0000 |
commit | e1be7dc1247d604ea8c914ee1ec01b0b009cdb98 (patch) | |
tree | 5d944579807f2e9dbd5265113120bf73da415467 /example | |
parent | 13c74917ecea5c151120678aff05edcc4ea682d5 (diff) | |
download | opt-utilities-e1be7dc1247d604ea8c914ee1ec01b0b009cdb98.tar.bz2 |
git-svn-id: file:///home/svn/opt_utilities@56 ed2142bd-67ad-457f-ba7c-d818d4011675
Diffstat (limited to 'example')
-rw-r--r-- | example/Makefile | 9 | ||||
-rw-r--r-- | example/test_fitter.cpp | 59 |
2 files changed, 66 insertions, 2 deletions
diff --git a/example/Makefile b/example/Makefile index 24fc4f0..dc05276 100644 --- a/example/Makefile +++ b/example/Makefile @@ -1,8 +1,13 @@ -target=test_optimizer +target=test_optimizer test_fitter + +all:$(target) test_optimizer:test_optimizer.cpp - g++ -o $@ $< -I ../ -O2 + g++ -o $@ $< -I ../ -O2 -g + +test_fitter:test_fitter.cpp + g++ -o $@ $< -I ../ -O2 -g clean: rm -f $(target) diff --git a/example/test_fitter.cpp b/example/test_fitter.cpp new file mode 100644 index 0000000..48c4d8c --- /dev/null +++ b/example/test_fitter.cpp @@ -0,0 +1,59 @@ +#include <core/optimizer.hpp> +#include <methods/powell/powell_method.hpp> +#include <methods/aga/aga.hpp> +#include <core/fitter.hpp> +#include <vector> +#include <iostream> +#include <data_sets/default_data_set.hpp> +#include <statistics/chisq.hpp> + +using namespace opt_utilities; +//declear a class derived from func_obj +class lin1d + :public opt_utilities::model<double,double,std::vector<double>,std::string> + { + private: + model<double,double,std::vector<double> >* do_clone()const + { + return new lin1d(*this); + } + public: + lin1d() + { + this->push_param_info(param_info<std::vector<double> >("k",1,-10,10)); + this->push_param_info(param_info<std::vector<double> >("b",0,-10,10)); + } + + public: + double do_eval(const double& x,const std::vector<double>& param) + { + return x*get_element(param,0)+get_element(param,1); + } + + private: + std::string do_to_string()const + { + return "linear model\n" + "y=k*x+b\n"; + } + }; + + +int main() +{ + fitter<double,double,std::vector<double>,double,std::string> f; + f.set_model(lin1d()); + //f.set_method(powell_method<double,std::vector<double> >()); + f.set_method(aga_method<double,std::vector<double> >()); + default_data_set<double,double> ds; + + for(int i=0;i<100;++i) + { + ds.add_data(data<double,double>(i,i*3+5,.1,.1,0,0)); + } + f.load_data(ds); + f.set_statistic(chisq<double,double,std::vector<double>,double,std::string>()); + f.fit(); + cout<<f.get_param_value("k")<<endl; + cout<<f.get_param_value("b")<<endl; +} |