From e1be7dc1247d604ea8c914ee1ec01b0b009cdb98 Mon Sep 17 00:00:00 2001 From: astrojhgu Date: Sat, 29 Aug 2009 10:26:50 +0000 Subject: git-svn-id: file:///home/svn/opt_utilities@56 ed2142bd-67ad-457f-ba7c-d818d4011675 --- core/fitter.hpp | 2 +- example/Makefile | 9 ++++++-- example/test_fitter.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ methods/aga/aga.hpp | 1 + 4 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 example/test_fitter.cpp diff --git a/core/fitter.hpp b/core/fitter.hpp index 119c930..874e4d0 100644 --- a/core/fitter.hpp +++ b/core/fitter.hpp @@ -1455,9 +1455,9 @@ namespace opt_utilities //return start_point; return p_model->get_all_params(); } - optengine.set_start_point(start_point); optengine.set_lower_limit(lower_limits); optengine.set_upper_limit(upper_limits); + optengine.set_start_point(start_point); Tp result; opt_eq(result,optengine.optimize()); 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 +#include +#include +#include +#include +#include +#include +#include + +using namespace opt_utilities; +//declear a class derived from func_obj +class lin1d + :public opt_utilities::model,std::string> + { + private: + model >* do_clone()const + { + return new lin1d(*this); + } + public: + lin1d() + { + this->push_param_info(param_info >("k",1,-10,10)); + this->push_param_info(param_info >("b",0,-10,10)); + } + + public: + double do_eval(const double& x,const std::vector& 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,std::string> f; + f.set_model(lin1d()); + //f.set_method(powell_method >()); + f.set_method(aga_method >()); + default_data_set ds; + + for(int i=0;i<100;++i) + { + ds.add_data(data(i,i*3+5,.1,.1,0,0)); + } + f.load_data(ds); + f.set_statistic(chisq,double,std::string>()); + f.fit(); + cout<