Files Classes Functions Hierarchy
00001 #ifndef LINEOPTIMIZERGOLD_H 00002 #define LINEOPTIMIZERGOLD_H 00003 00004 #include <cassert> 00005 #include <iostream> 00006 using namespace std; 00007 00008 typedef unsigned int const uintc; 00009 00010 #include <typeop.h> 00011 #include <print.h> 00012 00053 template< typename FN, typename XI, typename X, typename T > 00054 class lineoptimizergold 00055 { 00056 public: 00057 00059 uintc dim; 00060 00062 typename typeop<FN>::Tref fn; 00064 XI xi; 00066 X x0; 00068 X di; 00069 00071 T ti[4]; 00072 00074 T fti[4]; 00075 00077 uint ai[4]; 00078 00080 T lengthgold; 00081 00083 static T goldratio; 00084 00086 void fneval(T& fval, T const t) 00087 { 00088 for (uint i=0; i<dim; ++i) 00089 xi[i] = x0[i]+di[i]*t; 00090 //cout << "***"; 00091 //cout << printvecfunc(xi,dim) << endl; 00092 fn(fval); 00093 } 00094 00095 /* Initialize memory, does no computations. */ 00096 lineoptimizergold 00097 ( 00098 uintc dim_, 00099 typename typeop<FN>::Tref fn_, 00100 XI xi_, 00101 X x0_, 00102 X di_ 00103 ); 00104 00106 ostream & print(ostream & os) const; 00107 00109 void printstate() const 00110 { 00111 cout << "ti: "; 00112 for (uint i=0; i<4; ++i) 00113 cout << ti[ai[i]] << " "; 00114 cout << endl; 00115 cout << "fti: "; 00116 for (uint i=0; i<4; ++i) 00117 cout << fti[ai[i]] << " "; 00118 cout << endl; 00119 } 00120 00122 void reset( T const t0, T const t1 ); 00123 00125 void operator ++ (); 00126 00128 bool const verify() const; 00129 00131 T const minimumrealize(); 00132 00133 }; 00134 00135 //----------------------------------------------- 00136 // Implementation 00137 00138 00139 template< typename FN, typename XI, typename X, typename T > 00140 void lineoptimizergold<FN,XI,X,T>::reset( T const t0, T const t1 ) 00141 { 00142 assert(t1>t0); 00143 00144 ti[0] = t0; 00145 fneval(fti[0],ti[0]); 00146 //cout << SHOW(fti[0]) << endl; 00147 ti[3] = t1; 00148 fneval(fti[3],ti[3]); 00149 //cout << SHOW(fti[3]) << endl; 00150 00151 lengthgold = goldratio*(t1-t0); 00152 ti[1] = ti[3]-lengthgold; 00153 fneval(fti[1],ti[1]); 00154 ti[2] = ti[0]+lengthgold; 00155 fneval(fti[2],ti[2]); 00156 00157 for (uint i=0; i<4; ++i) 00158 ai[i] = i; 00159 00160 lengthgold *= goldratio; 00161 } 00162 00163 00164 00165 template< typename FN, typename XI, typename X, typename T > 00166 T const lineoptimizergold<FN,XI,X,T>::minimumrealize() 00167 { 00168 T t = ti[0]; 00169 T f = fti[0]; 00170 for (uint i=1; i<4; ++i) 00171 { 00172 if (fti[i]<f) 00173 { 00174 t = ti[i]; 00175 f = fti[i]; 00176 } 00177 } 00178 00179 for (uint i=0; i<dim; ++i) 00180 xi[i] = x0[i]+di[i]*t; 00181 00182 return f; 00183 } 00184 00185 template< typename FN, typename XI, typename X, typename T > 00186 bool const lineoptimizergold<FN,XI,X,T>::verify() const 00187 { 00188 bool valid=true; 00189 00190 if ( ti[ai[1]] < ti[ai[0]] ) 00191 valid=false; 00192 if ( ti[ai[2]] < ti[ai[0]] ) 00193 valid=false; 00194 if ( ti[ai[3]] < ti[ai[0]] ) 00195 valid=false; 00196 00197 if ( ti[ai[2]] < ti[ai[1]] ) 00198 valid=false; 00199 if ( ti[ai[3]] < ti[ai[1]] ) 00200 valid=false; 00201 00202 if ( ti[ai[3]] < ti[ai[2]] ) 00203 valid=false; 00204 00205 if (valid==false) 00206 { 00207 cout << "error: ti invalid" << endl; 00208 for (uint i=0; i<4; ++i) 00209 cout << ti[ai[i]] << " "; 00210 cout << endl; 00211 } 00212 00213 return valid; 00214 } 00215 00216 00217 00218 template< typename FN, typename XI, typename X, typename T > 00219 void lineoptimizergold<FN,XI,X,T>::operator ++ () 00220 { 00221 lengthgold *= goldratio; 00222 //cout << SHOW(lengthgold) << endl; 00223 00224 // Minimization 00225 uint rej; 00226 00227 //printstate(); 00228 00229 // Test - reject x0 if successful. 00230 if (fti[ai[2]]<fti[ai[1]]) 00231 { 00232 //cout << "Reject x0" << endl; 00233 rej = ai[0]; 00234 ai[0] = ai[1]; 00235 ai[1] = ai[2]; 00236 ai[2] = rej; 00237 ti[ai[2]] = ti[ai[3]]-lengthgold; 00238 //printstate(); 00239 assert(verify()); 00240 fneval(fti[ai[2]],ti[ai[2]]); 00241 } 00242 else 00243 { 00244 //cout << "Reject x3" << endl; 00245 rej = ai[3]; 00246 ai[3] = ai[2]; 00247 ai[2] = ai[1]; 00248 ai[1] = rej; 00249 ti[ai[1]] = ti[ai[0]]+lengthgold; 00250 //printstate(); 00251 assert(verify()); 00252 fneval(fti[ai[1]],ti[ai[1]]); 00253 } 00254 00255 } 00256 00257 template< typename FN, typename XI, typename X, typename T > 00258 lineoptimizergold<FN,XI,X,T>::lineoptimizergold 00259 ( 00260 uintc dim_, 00261 typename typeop<FN>::Tref fn_, 00262 XI xi_, 00263 X x0_, 00264 X di_ 00265 ) 00266 : dim(dim_), fn(fn_), xi(xi_), x0(x0_), di(di_) 00267 { 00268 } 00269 00270 template< typename FN, typename XI, typename X, typename T > 00271 T lineoptimizergold<FN,XI,X,T>::goldratio = 0.61803398875; 00272 00273 template< typename FN, typename XI, typename X, typename T > 00274 ostream & lineoptimizergold<FN,XI,X,T>::print(ostream & os) const 00275 { 00276 os << "dim=" << dim << endl; 00277 //os << "xi[]=" << printvecfunc(xi,dim) << endl; 00278 os << "xi[]=" << print(xi,xi+dim) << endl; 00279 //os << "x0[]=" << printvecfunc(x0,dim) << endl; 00280 os << "x0[]=" << print(x0,x0+dim) << endl; 00281 //os << "di[]=" << printvecfunc(di,dim) << endl; 00282 os << "di[]=" << print(di,di+dim) << endl; 00283 //os << "ti[]=" << printvecfunc(ti,4) << endl; 00284 os << "ti[]=" << print(ti,ti+4) << endl; 00285 //os << "fti[]=" << printvecfunc(fti,4) << endl; 00286 os << "fti[]=" << print(fti,fti+4) << endl; 00287 //os << "ai[]=" << printvecfunc(ai,4) << endl; 00288 os << "ai[]=" << print(ai,ai+4) << endl; 00289 os << SHOW(goldratio) << endl; 00290 00291 return os; 00292 } 00293 00294 #endif 00295 00296
1.5.8