From 109473cacf85c7e2a2831d56531399ab4621654e Mon Sep 17 00:00:00 2001 From: astrojhgu Date: Thu, 3 Jun 2010 17:55:42 +0000 Subject: git-svn-id: file:///home/svn/opt_utilities@118 ed2142bd-67ad-457f-ba7c-d818d4011675 --- methods/linmin/bas_util.hpp | 65 ++++++++++++++++++++++++++ methods/linmin/brent.hpp | 111 ++++++++++++++++++++++++++++++++++++++++++++ methods/linmin/linmin.hpp | 106 ++++++++++++++++++++++++++++++++++++++++++ methods/linmin/mnbrak.hpp | 82 ++++++++++++++++++++++++++++++++ 4 files changed, 364 insertions(+) create mode 100644 methods/linmin/bas_util.hpp create mode 100644 methods/linmin/brent.hpp create mode 100644 methods/linmin/linmin.hpp create mode 100644 methods/linmin/mnbrak.hpp (limited to 'methods/linmin') diff --git a/methods/linmin/bas_util.hpp b/methods/linmin/bas_util.hpp new file mode 100644 index 0000000..ccdce9e --- /dev/null +++ b/methods/linmin/bas_util.hpp @@ -0,0 +1,65 @@ +#ifndef BAS_UTIL +#define BAS_UTIL +#define OPT_HEADER +#include +#include +namespace opt_utilities +{ + template + T tabs(T x) + { + return T(x) + T sqr(T x) + { + return x*x; + } + + + template + void shft3(T&a,T& b,T& c,T d) + { + opt_eq(a,b); + opt_eq(b,c); + opt_eq(c,d); + } + + template + void shft(T& a,T& b,T& c,T d) + { + opt_eq(a,b); + opt_eq(b,c); + opt_eq(c,d); + } + // template + // void swap(T& ax,T& bx) + //{ + // swap(ax,bx); + // T temp; + //opt_eq(temp,ax); + //opt_eq(ax,bx); + //opt_eq(bx,temp); + //} + + template + T sign(const T& a,const T& b) + { + return b>=0?T(a>=0?T(a):T(-a)):T(a>=0?T(-a):T(a)); + } + + template + T max(T a,T b) + { + return b>a?T(b):T(a); + } + + template + void mov3(T& a,T& b,T& c, T& d,T& e,T& f) + { + opt_eq(a,d);opt_eq(b,e);opt_eq(c,f); + } +} + +#endif diff --git a/methods/linmin/brent.hpp b/methods/linmin/brent.hpp new file mode 100644 index 0000000..1a0ee39 --- /dev/null +++ b/methods/linmin/brent.hpp @@ -0,0 +1,111 @@ +#ifndef BRENT_HPP +#define BRENT_HPP +#define OPT_HEADER +#include +#include "bas_util.hpp" +//#include "optimizer.hpp" +namespace opt_utilities +{ + template + T brent(T ax,T bx,T cx,func_obj& f,T tol,T& xmin) + { + const int ITMAX=100; + const T CGOLD=0.3819660; + const T ZEPS=std::numeric_limits::epsilon()*1.e-3; + + int iter; + T a=0,b=0,d(0),etemp=0,fu=0,fv=0,fw=0,fx=0,p=0,q=0 + ,r=0,tol1=0,tol2=0,u=0,v=0,w=0,x=0,xm=0; + T e=0.; + a=(axcx?ax:cx); + x=w=v=bx; + fw=fv=fx=f.eval(x); + for(iter=0;itertol1) + { + r=(x-w)*(fx-fv); + q=(x-v)*(fx-fw); + p=(x-v)*q-(x-w)*r; + q=2.*(q-r); + if(q>0.) + { + p=-p; + } + q=tabs(q); + etemp=e; + e=d; + if(tabs(p)>=tabs(T(T(.5)*p*etemp))||p<=q*(a-x)||p>=q*(b-x)) + { + d=CGOLD*(e=(x>=xm?a-x:b-x)); + } + else + { + d=p/q; + u=x+d; + if(u-a=xm?a-x:b-x)); + } + u=(tabs(d)>=tol1?x+d:x+sign(tol1,d)); + fu=f.eval(u); + if(fu<=fx) + { + if(u>=x) + { + a=x; + } + else + { + b=x; + } + shft3(v,w,x,u); + shft3(fv,fw,fx,fu); + } + else + { + if(u +#include + +namespace opt_utilities +{ + template + class func_adaptor + :public func_obj + { + private: + const pT p1,xi1; + const func_obj* pfoo; + func_adaptor(){} + func_adaptor(const func_adaptor&) + :func_obj(),p1(),xi1(),pfoo(0) + {} + + public: + /* + void set_origin(pT& p2) + { + p1=p2; + } + + void set_direction(pT& xi2) + { + xi1=xi2; + } + + void set_func_obj(func_obj& foo) + { + pfoo=&foo; + }*/ + public: + func_adaptor(const pT& _p,const pT& _xi,const func_obj& pf) + :p1(_p),xi1(_xi),pfoo(&pf) + {} + + private: + func_obj* do_clone()const + { + return new func_adaptor(*this); + } + + rT do_eval(const rT& x) + { + //assert(p1.size()==xi1.size()); + + pT xt; + opt_eq(xt,p1); + for(size_t i=0;i&>(*pfoo).eval(xt); + //return x; + } + }; + + + template + void linmin(pT& p,pT& xi,rT& fret,func_obj& func) + { + + // assert(p.size()==10); + //assert(xi.size()==10); + func_adaptor fadpt(p,xi,func); + + int j=0; + const rT TOL=std::sqrt(std::numeric_limits::epsilon()); + rT xx=0,xmin=0,fx=0,fb=0,fa=0,bx=0,ax=0; + int n=(int)get_size(p); + + + ax=0.; + xx=1.; + + + mnbrak(ax,xx,bx,fa,fx,fb,fadpt); + //cout< + void mnbrak(T& ax,T& bx,T& cx,T& fa,T& fb,T& fc,func_obj& func) + { + const T GOLD=1.618034; + const T GLIMIT=100; + const T TINY=std::numeric_limits::epsilon(); + T ulim,u,r,q,fu; + fa=func.eval(ax); + fb=func.eval(bx); + + if(fb>fa) + { + //shft(dum,ax,bx,dum); + //shft(dum,fb,fa,dum); + std::swap(ax,bx); + std::swap(fa,fb); + } + + cx=bx+GOLD*(bx-ax); + fc=func.eval(cx); + while(fb>fc) + { + r=(bx-ax)*(fb-fc); + q=(bx-cx)*(fb-fa); + u=bx-T((bx-cx)*q-(bx-ax)*r)/ + T(T(2.)*sign(T(max(T(tabs(T(q-r))),T(TINY))),T(q-r))); + ulim=bx+GLIMIT*(cx-bx); + if((bx-u)*(u-cx)>0.) + { + fu=func.eval(u); + if(fufb) + { + cx=u; + fc=fu; + return; + } + u=cx+GOLD*(cx-bx); + fu=func.eval(u); + } + else if((cx-u)*(u-ulim)>0.) + { + fu=func.eval(u); + if(fu=0) + { + u=ulim; + fu=func.eval(u); + } + else + { + u=cx+GOLD*(cx-bx); + fu=func.eval(u); + } + shft3(ax,bx,cx,u); + shft3(fa,fb,fc,fu); + } + } +} + +#endif -- cgit v1.2.2