proj home

Files   Classes   Functions   Hierarchy  

lineoptimizergold2.h

Go to the documentation of this file.
00001 #ifndef LINEOPTIMIZERGOLD2_H
00002 #define LINEOPTIMIZERGOLD2_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 
00046 template< typename LNPATH, typename T >
00047 class lineoptimizergold2
00048 {
00049 public:
00050 
00052   LNPATH linepath;
00053 
00055   T ti[4];
00056 
00058   T fti[4];
00059 
00061   uint ai[4];
00062 
00064   T lengthgold;
00065 
00067   static T goldratio;
00068 
00069   /* Initialize memory, does no computations. */
00070   lineoptimizergold2( LNPATH linepath_) :
00071     linepath(linepath_) {}
00072 
00074   ostream & print(ostream & os) const;
00075 
00077   void printstate() const;
00078 
00080   void reset( T const t0, T const t1 );
00081 
00083   void resetInnerTwoPoints();
00084 
00086   void operator ++ ();
00087 
00089   bool const verify() const;
00090 
00092   T const minimumrealize();
00093 
00094 };
00095 
00096 //-----------------------------------------------
00097 //  Implementation
00098 
00099 template< typename LNPATH, typename T >
00100 void lineoptimizergold2<LNPATH,T>::printstate() const
00101 {
00102   cout << "ti:  ";
00103   for (uint i=0; i<4; ++i)
00104     cout << ti[ai[i]] << " ";
00105   cout << endl;
00106   cout << "fti: ";
00107   for (uint i=0; i<4; ++i)
00108     cout << fti[ai[i]] << " ";
00109   cout << endl;
00110 }
00111 
00112 template< typename LNPATH, typename T >
00113 void lineoptimizergold2<LNPATH,T>::resetInnerTwoPoints()
00114 {
00115   lengthgold = goldratio*(ti[ai[3]]-ti[ai[0]]);
00116   ti[ai[1]] = ti[ai[3]]-lengthgold;
00117   linepath.eval(fti[ai[1]],ti[ai[1]]);
00118   ti[ai[2]] = ti[ai[0]]+lengthgold; 
00119   linepath.eval(fti[ai[2]],ti[ai[2]]);
00120 
00121   lengthgold *= goldratio;
00122 }
00123 
00124 
00125 template< typename LNPATH, typename T >
00126 void lineoptimizergold2<LNPATH,T>::reset( T const t0, T const t1 )
00127 {
00128   assert(t1>t0);
00129 
00130   ti[0] = t0;
00131   linepath.eval(fti[0],ti[0]);
00132 //cout << SHOW(fti[0]) << endl;
00133   ti[3] = t1;
00134   linepath.eval(fti[3],ti[3]);
00135 //cout << SHOW(fti[3]) << endl;
00136 
00137   lengthgold = goldratio*(t1-t0);
00138   ti[1] = ti[3]-lengthgold;
00139   linepath.eval(fti[1],ti[1]);
00140   ti[2] = ti[0]+lengthgold; 
00141   linepath.eval(fti[2],ti[2]);
00142 
00143   for (uint i=0; i<4; ++i)
00144     ai[i] = i;
00145 
00146   lengthgold *= goldratio;
00147 }
00148 
00149 
00150 
00151 template< typename LNPATH, typename T >
00152 T const lineoptimizergold2<LNPATH,T>::minimumrealize()
00153 {
00154   T t = ti[0];
00155   T f = fti[0]; 
00156   for (uint i=1; i<4; ++i)
00157   {
00158     if (fti[i]<f)
00159     {
00160       t = ti[i];
00161       f = fti[i];
00162     }
00163   }
00164 
00165   linepath.eval(t);
00166 
00167   return f;
00168 }
00169 
00170 template< typename LNPATH, typename T >
00171 bool const lineoptimizergold2<LNPATH,T>::verify() const
00172 {
00173   bool valid=true;
00174 
00175   if ( ti[ai[1]] < ti[ai[0]] )
00176     valid=false;
00177   if ( ti[ai[2]] < ti[ai[0]] )
00178     valid=false;
00179   if ( ti[ai[3]] < ti[ai[0]] )
00180     valid=false;
00181 
00182   if ( ti[ai[2]] < ti[ai[1]] )
00183     valid=false;
00184   if ( ti[ai[3]] < ti[ai[1]] )
00185     valid=false;
00186 
00187   if ( ti[ai[3]] < ti[ai[2]] )
00188     valid=false;
00189 
00190   if (valid==false)
00191   {
00192     cout << "error: ti invalid" << endl;
00193     for (uint i=0; i<4; ++i)
00194       cout << ti[ai[i]] << " ";
00195     cout << endl;
00196   }
00197 
00198   return valid;
00199 }
00200 
00201 
00202 
00203 template< typename LNPATH, typename T >
00204 void lineoptimizergold2<LNPATH,T>::operator ++ ()
00205 {
00206   lengthgold *= goldratio;
00207 //cout << SHOW(lengthgold) << endl;
00208 
00209   // Minimization
00210   uint rej;
00211 
00212   //printstate();
00213 
00214   // Test - reject x0 if successful.
00215   if (fti[ai[2]]<fti[ai[1]])
00216   {
00217 //cout << "Reject x0" << endl;
00218     rej = ai[0];
00219     ai[0] = ai[1];
00220 
00221     ai[1] = ai[2];
00222     ai[2] = rej;
00223     ti[ai[2]] = ti[ai[3]]-lengthgold;
00224     assert(verify());
00225     linepath.fn(fti[ai[2]],ti[ai[2]]);
00226 
00227 /*
00228     ti[ai[1]] = ti[ai[3]]-lengthgold;
00229     linepath.fneval(fti[ai[1]],ti[ai[1]]);
00230 */
00231   }
00232   else
00233   {
00234 //cout << "Reject x3" << endl;
00235     rej = ai[3];
00236     ai[3] = ai[2];
00237     ai[2] = ai[1];
00238     ai[1] = rej;
00239     ti[ai[1]] = ti[ai[0]]+lengthgold;
00240     //printstate();
00241     assert(verify());
00242     linepath.fn(fti[ai[1]],ti[ai[1]]);
00243   }
00244 
00245 }
00246 
00247 template< typename LNPATH, typename T >
00248 T lineoptimizergold2<LNPATH,T>::goldratio = 0.61803398875;
00249 
00250 template< typename LNPATH, typename T >
00251 ostream & lineoptimizergold2<LNPATH,T>::print(ostream & os) const
00252 {
00253   //os << "ti[]=" << printvecfunc(ti,4) << endl; 
00254   os << "ti[]=" << ::print(ti,ti+4) << endl; 
00255   //os << "fti[]=" << printvecfunc(fti,4) << endl; 
00256   os << "fti[]=" << ::print(fti,fti+4) << endl; 
00257   //os << "ai[]=" << printvecfunc(ai,4) << endl; 
00258   os << "ai[]=" << ::print(ai,ai+4) << endl; 
00259   os << SHOW(goldratio) << endl;
00260 
00261   return os;
00262 }
00263 
00264 #endif
00265 
00266 

Generated on Fri Mar 4 00:49:30 2011 for Chelton Evans Source by  doxygen 1.5.8