From f11adda11d1275bcff45b20c15194d7a76bb6b59 Mon Sep 17 00:00:00 2001 From: astrojhgu Date: Sun, 23 Aug 2009 13:08:26 +0000 Subject: git-svn-id: file:///home/svn/opt_utilities@54 ed2142bd-67ad-457f-ba7c-d818d4011675 --- example/Makefile | 8 +++++ example/test_optimizer.cpp | 73 +++++++++++++++++++++++++++++++++++++++++++++ methods/aga/aga.hpp | 10 +++---- methods/powell/bas_util.hpp | 17 ++++++----- 4 files changed, 95 insertions(+), 13 deletions(-) create mode 100644 example/Makefile create mode 100644 example/test_optimizer.cpp 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 +#include +#include +#include +#include + + +//declear a class derived from func_obj +class foo + :public opt_utilities::func_obj > +// ^^^^^^^^double is the return type +// vector is the parameter type +{ + double do_eval(const std::vector& p) + { + double result=0; + for(int i=0;i > op; + //set the optimization method + op.set_opt_method(opt_utilities::powell_method >()); + //attach the object function + op.set_func_obj(foo()); + + //define the starting point + std::vector 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]<<" "< >()); + //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]<<" "< >()); + std::sort(samples.begin(),samples.end(),vp_comp()); if(sum2/samples.size()-pow(sum/samples.size(),2)p; } diff --git a/methods/powell/bas_util.hpp b/methods/powell/bas_util.hpp index 743dc3c..6a53f0a 100644 --- a/methods/powell/bas_util.hpp +++ b/methods/powell/bas_util.hpp @@ -1,6 +1,7 @@ #ifndef BAS_UTIL #define BAS_UTIL #include +#include namespace opt_utilities { template @@ -31,15 +32,15 @@ namespace opt_utilities opt_eq(b,c); opt_eq(c,d); } - template - void swap(T& ax,T& bx) - { + // template + // void swap(T& ax,T& bx) + //{ // swap(ax,bx); - T temp; - opt_eq(temp,ax); - opt_eq(ax,bx); - opt_eq(bx=temp); - } + // T temp; + //opt_eq(temp,ax); + //opt_eq(ax,bx); + //opt_eq(bx,temp); + //} template T sign(const T& a,const T& b) -- cgit v1.2.2