proj home

Files   Classes   Functions   Hierarchy  

exploreline.h

Go to the documentation of this file.
00001 #ifndef EXPLORELINE_H
00002 #define EXPLORELINE_H
00003 
00004 #include <cassert>
00005 #include <iostream>
00006 using namespace std
00007 
00008 #include <typeop.h>
00009 #include <print.h>
00010 
00011 
00043 ;  //It has been years (10+) since I had to put such a hack into a compiler.
00044 template < typename EXP, typename LNM >
00045 class exploreline : public EXP
00046 {
00047 public:
00048 
00049   typedef typename typeop<EXP>::Targ::FNtype FN;
00050   typedef typename typeop<EXP>::Tbare::Ttype T;
00051 
00053   T* xi0;
00055   T* yi0;
00057   //T* hi0;
00059   T* linex0;
00061   T* linedi;
00063   T linet0;
00065   T linet1;
00066 
00068   LNM linemin;
00069 
00071   uint lineiterations;
00072 
00074   exploreline
00075   (
00076     uintc N_, 
00077     uintc lineiterations_,
00078     T const h0step=20.0, 
00079     uintc indexmax_=100
00080   );
00081 
00083   exploreline
00084   (
00085     FN fn_,
00086     uintc N_, 
00087     uintc lineiterations_,
00088     T const h0step=20.0, 
00089     uintc indexmax_=100
00090   );
00091 
00093   ~exploreline();
00094 
00095   void reset( T const * x0 );
00096 
00098   void operator ++ ();
00099 
00100 };
00101 
00102 
00103 //----------------------------------------------------------
00104 //  Implementation
00105 
00106 template < typename EXP, typename LNM >
00107 void exploreline<EXP,LNM>::reset( T const * x0 )
00108 {
00109 //cout << "exploreline<EXP,LNM>::reset(T const *)" << endl;
00110   EXP::reset(x0);
00111   EXP::move();
00112   for (uint i=0; i<EXP::N; ++i)
00113     xi0[i] = EXP::xi[i];
00114   EXP::move();
00115 }
00116 
00117 template < typename EXP, typename LNM >
00118 exploreline<EXP,LNM>::exploreline
00119 (
00120   uintc N_, 
00121   uintc lineiterations_,
00122   T const h0step, 
00123   uintc indexmax_
00124 )
00125   : EXP(N_,h0step,indexmax_),
00126   xi0(new T[N_]), yi0(new T[N_]), //hi0(new T[N_]), 
00127   linex0(new T[N_]), linedi(new T[N_]),
00128   linet0(-1.0), linet1(1.0), 
00129   linemin(N_,EXP::fn,EXP::xi,linex0,linedi),
00130   lineiterations(lineiterations_)
00131 { 
00132 }
00133 
00134 template < typename EXP, typename LNM >
00135 exploreline<EXP,LNM>::exploreline
00136 (
00137   FN fn_,
00138   uintc N_, 
00139   uintc lineiterations_,
00140   T const h0step, 
00141   uintc indexmax_
00142 )
00143   : EXP(fn_,N_,h0step,indexmax_), 
00144   xi0(new T[N_]), yi0(new T[N_]), //hi0(new T[N_]), 
00145   linex0(new T[N_]), linedi(new T[N_]),
00146   linet0(-1.0), linet1(1.0), 
00147   linemin(N_,EXP::fn_,EXP::xi,linex0,linedi),
00148   lineiterations(lineiterations_)
00149 { 
00150 }
00151 
00152 template < typename EXP, typename LNM >
00153 exploreline<EXP,LNM>::~exploreline()
00154 {
00155   delete[] xi0;
00156   delete[] yi0;
00157   //delete[] hi0;
00158   delete[] linex0;
00159   delete[] linedi;
00160 }
00161 
00162 
00163 /*
00164 Note yi0 can be eliminated. After setting the line,
00165  xi0[] = xi[] ...
00166 Because a move is made at the end.  
00167  This algorithm has been very problamatic and perhaps
00168  I am going to have to wait for a better understanding.
00169 */
00170 
00171 template < typename EXP, typename LNM >
00172 void exploreline<EXP,LNM>::operator ++ ()
00173 {
00174   // Preserve xi
00175   for (uint i=0; i<EXP::N; ++i)
00176     yi0[i] = EXP::xi[i];
00177   T fval0 = EXP::fmin;
00178 
00179 //cout << "xi0: " << printvecfunc(xi0,EXP::N) << endl;
00180 //cout << "xi:  " << printvecfunc(EXP::xi,EXP::N) << endl;
00181   
00182 
00183   // Set the new direction and starting point.
00184   for (uint i=0; i<EXP::N; ++i)
00185   {
00186     linex0[i] = EXP::xi[i];
00187     linedi[i] = EXP::xi[i] - xi0[i];
00188   }
00189 
00190 //cout << "linex0: " << printvecfunc(linex0,EXP::N) << endl;
00191 //cout << "linedi: " << printvecfunc(linedi,EXP::N) << endl;
00192 //cout << SHOW(EXP::fmin) << endl;
00193 
00194   // Minimize on the line.
00195   linemin.reset(linet0,linet1);
00196   for (uint i=0; i<lineiterations; ++i)
00197   {
00198     ++linemin;
00199 
00200 //linemin.printstate();
00201 //cout << SHOW(EXP::fmin) << endl;
00202   }
00203 
00204   T fval = linemin.minimumrealize();
00205 //cout << SHOW(fval) << endl;
00206   if (fval < fval0)
00207   {
00208     EXP::fmin = fval;
00209 //cout << "***" << endl;
00210   }
00211   else
00212   {
00213     cout << "error:  line minimization failed." << endl;
00214     cout << "  backtracking and defaulting to explore." << endl;
00215     for (uint i=0; i<EXP::N; ++i)
00216       EXP::xi[i] = yi0[i];
00217     EXP::fmin = fval0;
00218 
00219     //EXP::move();
00220   }
00221   EXP::move();
00222 
00223   for (uint i=0; i<EXP::N; ++i)
00224     xi0[i] = yi0[i];
00225 
00226 
00227 
00228 //cout << "xi=" << printvecfunc(EXP::xi,EXP::N) << endl;
00229 
00230 }
00231 
00232 #endif
00233 
00234 

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