diff options
author | astrojhgu <astrojhgu@ed2142bd-67ad-457f-ba7c-d818d4011675> | 2009-08-23 13:08:26 +0000 |
---|---|---|
committer | astrojhgu <astrojhgu@ed2142bd-67ad-457f-ba7c-d818d4011675> | 2009-08-23 13:08:26 +0000 |
commit | f11adda11d1275bcff45b20c15194d7a76bb6b59 (patch) | |
tree | b57521b8eeef3fe85130ebb32a964a3dbac7aebd /example | |
parent | 1a63d3a669ebcff212e52ddb1e4efae473550f9f (diff) | |
download | opt-utilities-f11adda11d1275bcff45b20c15194d7a76bb6b59.tar.bz2 |
git-svn-id: file:///home/svn/opt_utilities@54 ed2142bd-67ad-457f-ba7c-d818d4011675
Diffstat (limited to 'example')
-rw-r--r-- | example/Makefile | 8 | ||||
-rw-r--r-- | example/test_optimizer.cpp | 73 |
2 files changed, 81 insertions, 0 deletions
diff --git a/example/Makefile b/example/Makefile new file mode 100644 index 0000000..24fc4f0 --- /dev/null +++ b/example/Makefile @@ -0,0 +1,8 @@ +target=test_optimizer + + +test_optimizer:test_optimizer.cpp + g++ -o $@ $< -I ../ -O2 + +clean: + rm -f $(target) diff --git a/example/test_optimizer.cpp b/example/test_optimizer.cpp new file mode 100644 index 0000000..99b9ec2 --- /dev/null +++ b/example/test_optimizer.cpp @@ -0,0 +1,73 @@ +#include <core/optimizer.hpp> +#include <methods/powell/powell_method.hpp> +#include <methods/aga/aga.hpp> +#include <vector> +#include <iostream> + + +//declear a class derived from func_obj +class foo + :public opt_utilities::func_obj<double,std::vector<double> > +// ^^^^^^^^double is the return type +// vector is the parameter type +{ + double do_eval(const std::vector<double>& p) + { + double result=0; + for(int i=0;i<p.size();++i) + { + result+=p[i]*p[i]; + } + return result; + } + + foo* do_clone()const + { + return new foo(*this);//default implement of the do_clone + } +}; + + +int main() +{ + //define an optimizer object + opt_utilities::optimizer<double,std::vector<double> > op; + //set the optimization method + op.set_opt_method(opt_utilities::powell_method<double,std::vector<double> >()); + //attach the object function + op.set_func_obj(foo()); + + //define the starting point + std::vector<double> p(3); + p[0]=p[1]=p[2]=10; + + //set the starting point + op.set_start_point(p); + std::cout<<"starting optimization from:"<< + p[0]<<" "<<p[1]<<" "<<p[2]<<std::endl; + + //set the precision + op.set_precision(1E-6); + //perform the optimization + p=op.optimize(); + + //output the result + std::cout<<"optimization result:"<< + p[0]<<" "<<p[1]<<" "<<p[2]<<std::endl; + + //Let's change another method + op.set_opt_method(opt_utilities::aga_method<double,std::vector<double> >()); + //define the lower and upper limit + p[0]=p[1]=p[2]=-10; + op.set_lower_limit(p); + p[0]=p[1]=p[2]=10; + op.set_upper_limit(p); + //set the start point + op.set_start_point(p); + //set precision + op.set_precision(1E-10); + //start optimize + p=op.optimize(); + std::cout<<"optimization result:"<< + p[0]<<" "<<p[1]<<" "<<p[2]<<std::endl; +} |