diff options
author | astrojhgu <astrojhgu@ed2142bd-67ad-457f-ba7c-d818d4011675> | 2009-04-22 16:47:04 +0000 |
---|---|---|
committer | astrojhgu <astrojhgu@ed2142bd-67ad-457f-ba7c-d818d4011675> | 2009-04-22 16:47:04 +0000 |
commit | 6d037d5c5b4c1c855c407e68d2e59965e4dcf67b (patch) | |
tree | 73c1b3276e749e40dde39b856e08a1cfe307b715 | |
parent | 6341c4acc757f6252f36156229212938357caa71 (diff) | |
download | opt-utilities-6d037d5c5b4c1c855c407e68d2e59965e4dcf67b.tar.bz2 |
git-svn-id: file:///home/svn/opt_utilities@28 ed2142bd-67ad-457f-ba7c-d818d4011675
-rw-r--r-- | test/makefile | 10 | ||||
-rw-r--r-- | test/test_optimizer.cpp | 155 |
2 files changed, 165 insertions, 0 deletions
diff --git a/test/makefile b/test/makefile new file mode 100644 index 0000000..9933620 --- /dev/null +++ b/test/makefile @@ -0,0 +1,10 @@ +targets=test_optimizer + +all:$(targets) + +test_optimizer:test_optimizer.cpp + g++ $< -o $@ -I .. -O3 + + +clean: + rm -f $(targets) *.o *~ diff --git a/test/test_optimizer.cpp b/test/test_optimizer.cpp new file mode 100644 index 0000000..b6d70a7 --- /dev/null +++ b/test/test_optimizer.cpp @@ -0,0 +1,155 @@ +#include <core/optimizer.hpp> +#include <methods/powell/powell_method.hpp> +//#include <methods/gsl_simplex/gsl_simplex.hpp> +#include <vector> +#include <iostream> +using namespace opt_utilities; +using namespace std; + + +class foo1 + :public func_obj<double,vector<double> > +{ + foo1* do_clone()const + { + return new foo1(*this); + } + + double do_eval(const vector<double>& p) + { + double result=0; + for(int i=0;i!=p.size();++i) + { + result+=p[i]*p[i]; + } + return result; + } +}; + +class foo2 + :public func_obj<double,vector<double> > +{ + foo2* do_clone()const + { + return new foo2(*this); + } + + double do_eval(const vector<double>& p) + { + double result=0; + for(int i=0;i!=p.size();++i) + { + result+=(i+1)*p[i]*p[i]; + } + return result; + } +}; + +class foo3 + :public func_obj<double,vector<double> > +{ + foo3* do_clone()const + { + return new foo3(*this); + } + + double do_eval(const vector<double>& p) + { + double result=0; + for(int i=0;i<p.size();++i) + { + double temp=0; + for(int j=0;j<=i;++j) + { + temp+=p[j]; + } + result+=temp*temp; + } + + return result; + } +}; + + +class foo4 + :public func_obj<double,vector<double> > +{ + foo4* do_clone()const + { + return new foo4(*this); + } + + double do_eval(const vector<double>& p) + { + double result=0; + for(int i=0;i<p.size()-1;++i) + { + result+=100*(p[i+1]-p[i])*(p[i+1]-p[i]) + +(1-p[i])*(1-p[i]); + } + + return result; + } +}; + +class foo5 + :public func_obj<double,vector<double> > +{ + foo5* do_clone()const + { + return new foo5(*this); + } + + double do_eval(const vector<double>& p) + { + const double pi=3.14159265358979323846; + double result=0; + result+=10*p.size(); + for(int i=0;i<p.size();++i) + { + result+=(p[i]*p[i]-10*cos(2*pi*p[i])); + } + + return result; + } +}; + + +void test_opt(const func_obj<double,vector<double> >& fo, + const opt_method<double,vector<double> >& optm) +{ + const int problem_size=5; + optimizer<double,vector<double> > opt; + opt.set_opt_method(optm); + opt.set_func_obj(fo); + vector<double> p(problem_size); + for(int i=0;i<p.size();++i) + { + p[i]=1000; + } + opt.set_start_point(p); + opt.set_precision(1E-6); + p=opt.optimize(); + + + cout<<"the result is:\n"; + for(int i=0;i<p.size();++i) + { + cout<<p[i]<<endl; + } + cout<<"The result of the object function is:\n" + <<opt.eval(p)<<endl; + +} + + +int main() +{ + // gsl_simplex<double,vector<double> > pm_simplex; + powell_method<double,vector<double> > pm_powell; + test_opt(foo1(),pm_powell); + test_opt(foo2(),pm_powell); + test_opt(foo3(),pm_powell); + test_opt(foo4(),pm_powell); + test_opt(foo5(),pm_powell); +} |