Files Classes Functions Hierarchy
00001 #ifndef POWELL_H 00002 #define POWELL_H 00003 #include <cassert> 00004 #include <iostream> 00005 using namespace std 00006 00007 #include <print.h> 00008 #include <typeop.h> 00009 00010 #include <cirbuffarr.h> 00011 00012 #include <linepathd1.h> 00013 00014 ; 00022 template< typename EXP, typename LNM > 00023 class powell02 00024 { 00025 public: 00026 00027 typedef typename typeop<EXP>::Tbare::Ttype T; 00028 typedef typename typeop<EXP>::Tbare::XItype XI; 00029 typedef typename typeop<EXP>::Tbare::FNtype FN; 00030 00032 uintc N; 00033 00035 EXP exp; 00036 00038 cirbuffarr< T > xi0; 00039 00041 cirbuffarr< T > di0; 00042 00044 T* pi; 00045 00047 T* linex0; 00049 T* linedi; 00051 T linet0; 00053 T linet1; 00054 00055 00057 linepathd1<FN,XI,XI,T> path; 00059 LNM linemin; 00060 00061 uint lineiterations; 00062 00064 powell02 00065 ( 00066 uintc N_, 00067 uintc lineiterations_, 00068 T const h0step=20.0, 00069 uintc indexmax_=20 00070 ); 00071 00073 powell02 00074 ( 00075 FN fn_, 00076 uintc N_, 00077 uintc lineiterations_, 00078 T const h0step=20.0, 00079 uintc indexmax_=20 00080 ); 00081 00083 ~powell02(); 00084 00086 void reset( T const * x0 ); 00087 00089 void operator ++ (); 00090 00091 void pushbackadirection(); 00092 00093 }; 00094 00095 00096 //---------------------------------------------------------- 00097 // Implementation 00098 00099 template < typename EXP, typename LNM > 00100 void powell02<EXP,LNM>::pushbackadirection() 00101 { 00102 static T * p0; 00103 static T * pN; 00104 static T * d0; 00105 00106 ++di0; 00107 00108 p0 = xi0[0]; 00109 pN = xi0[N]; 00110 //cout << "p0: " << printvecfunc(p0,N) << endl; 00111 //cout << "pN: " << printvecfunc(pN,N) << endl; 00112 d0 = di0[0]; 00113 for (uint i=0; i<N; ++i) 00114 d0[i] = p0[i] - pN[i]; 00115 } 00116 00117 00118 template < typename EXP, typename LNM > 00119 powell02<EXP,LNM>::powell02 00120 ( 00121 uintc N_, 00122 uintc lineiterations_, 00123 T const h0step, 00124 uintc indexmax_ 00125 ) 00126 : N(N_), exp(N_,h0step,indexmax_), xi0(N_*N_*N_,N_), 00127 di0(N_,N_), 00128 linex0(new T[N_]), linedi(new T[N_]), 00129 linet0(-1.0), linet1(1.0), 00130 path(N_,exp.fn,exp.xi,linex0,linedi), 00131 linemin(path), 00132 lineiterations(lineiterations_) 00133 { 00134 } 00135 00136 template < typename EXP, typename LNM > 00137 powell02<EXP,LNM>::powell02 00138 ( 00139 FN fn_, 00140 uintc N_, 00141 uintc lineiterations_, 00142 T const h0step, 00143 uintc indexmax_ 00144 ) 00145 : N(N_), exp(fn_,N_,h0step,indexmax_), xi0(N_*N_*N_,N_), 00146 di0(N_,N_), 00147 linex0(new T[N_]), linedi(new T[N_]), 00148 linet0(-1.0), linet1(1.0), 00149 path(N_,EXP::fn,EXP::xi,linex0,linedi), 00150 linemin(path), 00151 lineiterations(lineiterations_) 00152 { 00153 } 00154 00155 00156 template < typename EXP, typename LNM > 00157 powell02<EXP,LNM>::~powell02() 00158 { 00159 delete[] linex0; 00160 delete[] linedi; 00161 } 00162 00163 00164 00165 template < typename EXP, typename LNM > 00166 void powell02<EXP,LNM>::reset( T const * x0 ) 00167 { 00168 //cout << "powell02<EXP,LNM>::reset" << endl; 00169 00170 exp.reset(x0); 00171 //cout << printvecfunc(exp.xi,N) << endl; 00172 xi0.push(exp.xi); 00173 for (uint i=1; i<N; ++i) 00174 { 00175 exp.move(); 00176 //cout << SHOW(exp.fn.counter) << endl; 00177 xi0.push(exp.xi); 00178 //cout << printvecfunc(exp.xi,N) << endl; 00179 } 00180 00181 for (uint i=0; i<N; ++i) 00182 { 00183 exp.move(); 00184 xi0.push(exp.xi); 00185 pushbackadirection(); 00186 } 00187 //cout << "xi0" << endl; 00188 //cout << xi0 << endl; 00189 //cout << "di0" << endl; 00190 //cout << di0 << endl; 00191 00192 } 00193 00194 template < typename EXP, typename LNM > 00195 void powell02<EXP,LNM>::operator ++ () 00196 { 00197 T* p0 = & exp.xi[0]; 00198 T* d0; 00199 00200 T fval0=exp.fmin; 00201 00202 //cout << "powell ++ operator" << endl; 00203 00204 for (uint i=0; i<N; ++i) 00205 { 00206 d0 = di0[N-i-1]; 00207 00208 for (uint k=0; k<N; ++k) 00209 { 00210 linex0[i] = p0[i]; 00211 linedi[i] = d0[i]; 00212 00213 linemin.reset(linet0,linet1); 00214 for (uint j=0; j<lineiterations; ++j) 00215 { 00216 ++linemin; 00217 } 00218 //cout << "***" << printvecfunc(exp.xi,N) << endl; 00219 } 00220 00221 } 00222 //exp.move(); 00223 00224 if (fval0<exp.fmin) 00225 { 00226 cout << "fmin failed" << endl; 00227 xi0.copyto(exp.xi); 00228 exp.move(); 00229 } 00230 00231 xi0.push(exp.xi); 00232 pushbackadirection(); 00233 } 00234 00235 00236 #endif
1.5.8