Opt.h

00001 #ifndef Opt_h
00002 #define Opt_h
00003 
00004 /*---------------------------------------------------------------------
00005  Copyright (c) 2001, Sandia Corporation.  Under the terms of Contract
00006  DE-AC04-94AL85000, there is a non-exclusive license for use of this
00007  work by or on behalf of the U.S. Government.
00008 
00009  J.C. Meza, Sandia National Laboratories, meza@ca.sandia.gov
00010  ----------------------------------------------------------------------*/
00011 
00012 #ifdef HAVE_CONFIG_H
00013 #include "OPT++_config.h"
00014 #endif
00015 
00016 #include <iostream>
00017 #include <fstream>
00018 #ifdef HAVE_STD
00019 #include <cstring>
00020 #else
00021 #include <string.h>
00022 #endif
00023 
00024 #include "globals.h"
00025 
00026 #include "include.h"
00027 #include "newmatap.h"
00028 
00029 #include "NLP.h"
00030 #include "NLF.h"
00031 #include "TOLS.h"
00032 
00033 using std::cerr;
00034 using std::cout;
00035 using std::ifstream;
00036 using std::ostream;
00037 using std::endl;
00038 using std::filebuf;
00039 using std::flush;
00040 
00041 namespace OPTPP {
00042 
00043 //-------------------------------------------------------------------------
00044 // Various Optimization methods and related support routines
00045 //-------------------------------------------------------------------------
00046 
00047 inline void abort_handler(int code)
00048 {
00049   if (code > 1) // code = 2 (Cntl-C signal), 0 (normal), & -1/1 (abnormal)
00050     cout << "Signal Caught!" << endl;
00051  
00052   // Clean up
00053   cout << flush; // flush cout or ofstream redirection
00054   cerr << flush; // flush cerr or ofstream redirection
00055   exit(code);
00056 }
00057 
00058 inline void opt_default_update_model(int k, int dim, NEWMAT::ColumnVector x) {
00059   /*  clog << "opt_default_update_model: " 
00060        << "Iter =    "   << k 
00061        << ", dim  =    " << dim
00062        << ", x(1) =    " << x(1) << "\n"; */
00063 }
00064 
00065 int trustregion(NLP1*, ostream*, NEWMAT::SymmetricMatrix&, NEWMAT::ColumnVector&, 
00066                 NEWMAT::ColumnVector&, real&, real&, real stpmax = 1.e3,
00067                 real stpmin = 1.e-9);
00068 
00069 int trustpds(NLP1*, ostream*, NEWMAT::SymmetricMatrix&, NEWMAT::ColumnVector&, 
00070                 NEWMAT::ColumnVector&, real&, real&, real stpmax = 1.e3,
00071                 real stpmin = 1.e-9, int searchSize = 64);
00072 
00073 int dogleg(NLP1*, ostream*, NEWMAT::SymmetricMatrix&, NEWMAT::ColumnVector&, NEWMAT::ColumnVector&,
00074            NEWMAT::ColumnVector&, real&, real&, real);
00075 
00076 int pdsstep(NLP1*, ostream*, NEWMAT::SymmetricMatrix&, NEWMAT::ColumnVector&, NEWMAT::ColumnVector&, 
00077            NEWMAT::ColumnVector&, real&, real&, real, double&, bool, int);
00078 
00079 int linesearch(NLP1*, ostream*, NEWMAT::ColumnVector&, NEWMAT::ColumnVector&, real *,
00080                real stpmax = 1.e3, real stpmin = 1.e-9,
00081                int itnmax = 5, real ftol = 1.e-4, real xtol = 2.2e-16, 
00082                real gtol = 0.9);
00083 
00084 int backtrack(NLP1*, ostream*, NEWMAT::ColumnVector&, NEWMAT::ColumnVector&, real *,
00085               int itnmax = 5, real ftol = 1.e-4, 
00086               real stpmax = 1.e3, real stpmin = 1.e-9);
00087 
00088 int mcsrch(NLP1*, NEWMAT::ColumnVector&, ostream*, real *,
00089            int itnmax = 5, real ftol = 1.e-4, real xtol = 2.2e-16, 
00090            real gtol = 0.9, real stpmax = 1.e3, 
00091            real stpmin = 1.e-9);
00092 
00093 int mcstep(real *, real *, real *, real *, real *, 
00094            real *, real *, real  , real  , bool *, 
00095            real, real, int *);
00096 
00097 NEWMAT::ReturnMatrix PertChol(NEWMAT::SymmetricMatrix&, NEWMAT::Real, 
00098                               NEWMAT::Real&);
00099 NEWMAT::ReturnMatrix MCholesky(NEWMAT::SymmetricMatrix&);
00100 
00108 class OptimizeClass {
00109 
00110 private:
00111   int x_optout_fd;
00112 
00113 protected:
00115   int  dim;                     
00117   TOLS tol;                     
00119   NEWMAT::ColumnVector sx;              
00121   NEWMAT::ColumnVector sfx;             
00123   NEWMAT::ColumnVector xprev;           
00125   real         fprev;           
00127   NEWMAT::ColumnVector mem_step;        
00129   real         step_length;     
00130   virtual real stepTolNorm() const { return step_length; }
00131 
00133   char method[80];  
00135   char mesg[80];    
00137   int  ret_code;    
00139   int  iter_taken;  
00141   int  fcn_evals;   
00143   int  backtracks;  
00145   bool  debug_;     
00146   int  trace;
00148   double iter_time;
00150   double total_time;
00151 
00153   UPDATEFCN  update_fcn;  
00154 
00155   filebuf file_buffer;
00157   ostream *optout;
00159   int     optout_fd;
00160 
00161 
00165   virtual void defaultReset(int n) 
00166   { 
00167      sfx.ReSize(n);
00168      sx.ReSize(n);
00169      xprev.ReSize(n);
00170      sx    = 1.0; 
00171      sfx   = 1.0; 
00172      xprev = 0.0;
00173      fcn_evals = backtracks = 0; 
00174   }
00175 
00179   virtual void defaultAcceptStep(int, int) 
00180     { if (debug_) *optout << "Optimize: AcceptStep not implemented yet.\n";};
00181 
00185   virtual void defaultUpdateModel(int k, int ndim, NEWMAT::ColumnVector x) 
00186     {update_fcn(k, ndim, x);};
00187 
00191   void copyright()
00192   {
00193     //pdh  did a quick fix here...path should not be relative
00194     //     for now, took out cerr statement and put else block
00195     //     around the rest.  
00196      char str[255];
00197      ifstream in("../../include/abbrev_copyright.h");
00198      if (!in){
00199        //        cerr << "Cannot open input file.\n";
00200      }
00201      else {
00202        while (in) {
00203          in.getline(str,255);
00204          if(in) *optout << str<< endl;
00205        }
00206        in.close();
00207      }
00208   }
00209 
00210 public:
00217   OptimizeClass(): x_optout_fd(-1), dim(0), debug_(0), trace(0) {
00218     optout = new ostream(&file_buffer);
00219     file_buffer.open("OPT_DEFAULT.out", std::ios::out);
00220     if (!file_buffer.is_open() || !optout->good()) {
00221       cout << "OptimizeClass:: Can't open default output file\n";
00222       optout_fd = 0;
00223     }
00224     update_fcn = &opt_default_update_model;
00225     tol.setDefaultTol();
00226   }
00227 
00231   OptimizeClass(int n): x_optout_fd(-1), dim(n), sx(n), sfx(n), xprev(n),
00232     fcn_evals(0), backtracks(0), debug_(0), trace(0)      {
00233     optout = new ostream(&file_buffer);
00234     file_buffer.open("OPT_DEFAULT.out", std::ios::out);
00235     if (!file_buffer.is_open() || !optout->good()) {
00236       cout << "OptimizeClass:: Can't open default output file\n";
00237       optout_fd = 0;
00238     }
00239     update_fcn = &opt_default_update_model;
00240     sx  = 1.0; sfx = 1.0; xprev = 0.0; 
00241     tol.setDefaultTol(); 
00242   }
00243   
00247   OptimizeClass(TOLS t): x_optout_fd(-1), dim(0), tol(t), debug_(0), trace(0){
00248     optout = new ostream(&file_buffer);
00249     file_buffer.open("OPT_DEFAULT.out", std::ios::out);
00250     if (!file_buffer.is_open() || !optout->good()) {
00251       cout << "OptimizeClass:: Can't open default output file\n";
00252       optout_fd = 0;
00253     }
00254     update_fcn = &opt_default_update_model;
00255     sx  = 1.0; sfx = 1.0; xprev = 0.0; 
00256   }
00257   
00262   OptimizeClass(int n, TOLS t): x_optout_fd(-1), dim(n), tol(t), sx(n),sfx(n),
00263       xprev(n), fcn_evals(0), backtracks(0), debug_(0), trace(0){
00264     optout = new ostream(&file_buffer);
00265     file_buffer.open("OPT_DEFAULT.out", std::ios::out);
00266     if (!file_buffer.is_open() || !optout->good()) {
00267       cout << "OptimizeClass:: Can't open default output file\n";
00268       optout_fd = 0;
00269     }
00270       update_fcn = &opt_default_update_model;
00271       sx  = 1.0; sfx = 1.0; xprev = 0.0;
00272     }
00273 
00274   virtual ~OptimizeClass() { cleanup(); if (optout != NULL) delete optout;}
00275   void  cleanup() {optout->flush();};
00276 
00277 // set various properties
00278 
00280   void setMesg(const char *s)   {strcpy(mesg,s);}     
00282   void setMethod(const char *s) {strcpy(method,s);}   
00284   void setMaxStep(real x) {tol.setMaxStep(x);}  
00286   void setMinStep(real x) {tol.setMinStep(x);}  
00288   void setStepTol(real x) {tol.setStepTol(x);}  
00290   void setFcnTol(real x)  {tol.setFTol(x);}     
00292   void setConTol(real x)  {tol.setCTol(x);}     
00294   void setGradTol(real x) {tol.setGTol(x);}     
00296   void setLineSearchTol(real x) {tol.setLSTol(x);} 
00298   void setMaxIter(int k)  {tol.setMaxIter(k);}  
00300   void setMaxBacktrackIter(int k)  {tol.setMaxBacktrackIter(k);} 
00302   void setMaxFeval(int k) {tol.setMaxFeval(k);} 
00304   void setUpdateModel(UPDATEFCN u) {update_fcn = u;}
00305 
00307   void setXScale(NEWMAT::ColumnVector x)  {sx = x;}     
00309   void setFcnScale(NEWMAT::ColumnVector x) {sfx = x;}   
00311   void setFcnScale(NEWMAT::Real x) {sfx = x;}           
00312 
00314   void setReturnCode(int val) {ret_code = val;}         
00315 
00319   int          getDim()      const {return dim;}        
00323   int          getIter()     const {return iter_taken;} 
00327   int          getReturnCode()  const {return ret_code;}        
00331   NEWMAT::ColumnVector getXPrev()    const {return xprev;}
00335   NEWMAT::ColumnVector getXScale()   const {return sx;}
00339   NEWMAT::ColumnVector getFcnScale() const {return sfx;}
00343   ostream*    getOutputFile() {return optout;};
00344 
00345   int setOutputFile(const char *filename, int append) { 
00346 
00347     if (x_optout_fd == -1) {  // Change the default output file
00348       file_buffer.close();
00349       if (append)
00350          file_buffer.open(filename, std::ios::out|std::ios::app);
00351       else
00352          file_buffer.open(filename, std::ios::out);
00353       if (!file_buffer.is_open() || !optout->good()) {
00354         cout << "OptimizeClass::setOutputFile: Can't open " << filename << endl;
00355         optout_fd = 0;
00356       }
00357       else
00358         optout_fd = 1;
00359     }
00360     else {
00361       cout << "OptimizeClass::setOutputFile: File already attached\n";
00362       optout_fd = 1;
00363     }
00364     return optout_fd;
00365   }
00366 
00367   int setOutputFile(int FileDescriptor) { 
00368 
00369     optout_fd = FileDescriptor;
00370     cerr << "setOutputFile(int FileDescriptor) no longer supported.\n"
00371          << "Please use setOutputFile(const char *filename, int append)"
00372          << "or setOutputFile(ostream& fout)."
00373          << endl;
00374     optout_fd = 0;
00375 
00376     return optout_fd;
00377   }
00378 
00379   int setOutputFile(ostream& fout) { 
00380 
00381     optout->rdbuf(fout.rdbuf());
00382     if (!optout->good()) {
00383       cout << "OptimizeClass::setOutputFile: Can't open file." << endl;
00384       optout_fd = 0;
00385     }
00386     else
00387       optout_fd = 1;
00388 
00389     return optout_fd;
00390   }
00391 
00393   void setDebug()         {debug_ = true;}   
00397   bool Debug()            {return debug_;}
00398 
00404   virtual void  acceptStep(int, int )  = 0;
00405   virtual int   checkConvg()           = 0;
00406   virtual NEWMAT::ColumnVector computeSearch(NEWMAT::SymmetricMatrix& ) = 0;
00407   virtual void  optimize()             = 0;
00408   virtual void  reset()                = 0;
00409   virtual void  readOptInput()         = 0;
00410   virtual void  printStatus(char *)    = 0;
00411   virtual void  updateModel(int, int, NEWMAT::ColumnVector)       = 0;
00412 };
00413 
00414 } // namespace OPTPP
00415 
00416 #endif
Generated on Mon Jan 24 12:04:37 2011 for FASTlib by  doxygen 1.6.3