NLF.h

00001 #ifndef NLF_h
00002 #define NLF_h
00003 
00004 
00005 /*----------------------------------------------------------------------
00006  Copyright (c) 2001, Sandia Corporation.   Under the terms of Contract 
00007  DE-AC04-94AL85000, there is a non-exclusive license for use of this 
00008  work by or on behalf of the U.S. Government.
00009 
00010  J. C. Meza, Sandia National Laboratories, meza@ca.sandia.gov
00011  ----------------------------------------------------------------------*/
00012 
00013 #include "NLP2.h"
00014 
00015 using std::ostream;
00016 
00017 extern "C" {
00018   double get_cpu_time();
00019   double get_wall_clock_time();
00020 }
00021 
00022 namespace OPTPP {
00023 
00024 typedef void (*INITFCN)(int, NEWMAT::ColumnVector&);
00025 
00026 typedef CompoundConstraint* (*INITCONFCN)(int);
00027 
00028 typedef void (*USERFCN0)(int, const NEWMAT::ColumnVector&, real&, int&);
00029 typedef void (*USERFCN0V)(int, const NEWMAT::ColumnVector&, real&, int&, void*);
00030 
00031 typedef void (*USERFCN1)(int, int, const NEWMAT::ColumnVector&, real&, 
00032                          NEWMAT::ColumnVector&, int&);
00033 typedef void (*USERFCN1V)(int, int, const NEWMAT::ColumnVector&, real&, 
00034                          NEWMAT::ColumnVector&, int&, void*);
00035 
00036 typedef void (*USERFCN2)(int, int, const NEWMAT::ColumnVector&, real&, 
00037                          NEWMAT::ColumnVector&, NEWMAT::SymmetricMatrix&, int&);
00038 typedef void (*USERFCN2V)(int, int, const NEWMAT::ColumnVector&, real&, 
00039                          NEWMAT::ColumnVector&, NEWMAT::SymmetricMatrix&, 
00040                          int&, void*);
00041 
00042 typedef void (*USERFCN2A)(int, int, int, const NEWMAT::ColumnVector&, real&, 
00043                          NEWMAT::ColumnVector&, NEWMAT::Matrix&, int&);
00044 typedef void (*USERFCN2AV)(int, int, int, const NEWMAT::ColumnVector&, real&, 
00045                          NEWMAT::ColumnVector&, NEWMAT::Matrix&, int&, void*);
00046 
00047 typedef void (*USERNLNCON0)(int, const NEWMAT::ColumnVector&, NEWMAT::ColumnVector&, int&);
00048 
00049 typedef void (*USERNLNCON1)(int, int, const NEWMAT::ColumnVector&,
00050   NEWMAT::ColumnVector&, NEWMAT::Matrix&, int&);
00051 
00052 typedef void (*USERNLNCON2)(int, int, const NEWMAT::ColumnVector&, 
00053   NEWMAT::ColumnVector&, NEWMAT::Matrix&, OptppArray<NEWMAT::SymmetricMatrix>&, int&);
00054 
00055 
00056 //
00057 //  Derived from NLP's
00058 //
00059 
00072 class NLF0: public NLP0 {
00073 protected:
00074   USERFCN0 fcn;                 
00075   USERFCN0V fcn_v;              
00076   USERNLNCON0 confcn;           
00077   INITFCN init_fcn;             
00078   INITCONFCN init_confcn;       
00079   bool init_flag;               
00080   void *vptr;                   
00081 
00082   static void f_helper(int n, const NEWMAT::ColumnVector& xc, real& f, 
00083          int& result, void *v) {NLF0 *o = (NLF0*)v; (*o->fcn)(n,xc,f,result);}
00084 
00085 public:
00086   // Constructors
00087   NLF0(): 
00088      NLP0(){;}
00089   NLF0(int ndim): 
00090      NLP0(ndim){;}
00091   NLF0(int ndim, USERFCN0 f): 
00092      NLP0(ndim), fcn(f), fcn_v(f_helper), vptr(this) {;}
00093   NLF0(int ndim, USERFCN0 f, INITFCN i, CompoundConstraint* constraint = 0):
00094      NLP0(ndim, constraint), fcn(f), fcn_v(f_helper), init_fcn(i), 
00095      init_flag(false), vptr(this) {;}
00096   NLF0(int ndim, USERFCN0 f, INITFCN i, INITCONFCN c):
00097      NLP0(ndim), fcn(f), fcn_v(f_helper),init_fcn(i), init_confcn(c), 
00098      init_flag(false), vptr(this) 
00099      {constraint_ = init_confcn(ndim);}
00100   NLF0(int ndim, int nlncons, USERNLNCON0 f, INITFCN i):
00101      NLP0(ndim,nlncons), confcn(f), init_fcn(i), init_flag(false), vptr(this) {;}
00103   NLF0(int ndim, USERFCN0V f, INITFCN i, CompoundConstraint* constraint = 0, void* v = 0):
00104      NLP0(ndim, constraint), fcn(0), fcn_v(f), init_fcn(i), init_flag(false) 
00105      {if (v == 0) vptr = this; else vptr= v ;}
00106   NLF0(int ndim, USERFCN0V f, INITFCN i, void* v): 
00107      NLP0(ndim), fcn(0), fcn_v(f), init_fcn(i), vptr(v) {;}
00108   NLF0(int ndim, USERFCN0V f, INITFCN i, INITCONFCN c, void* v):
00109      NLP0(ndim), fcn(0), fcn_v(f),init_fcn(i), init_confcn(c), 
00110      init_flag(false), vptr(v) 
00111      {constraint_ = init_confcn(ndim);}
00112 
00113   // Destructor
00114   virtual ~NLF0() {;}               
00115 
00117   virtual void reset();                         
00118 
00120   virtual void initFcn();                       
00121 
00123   virtual void eval();                          
00124 
00126   virtual real evalF();                         
00128   virtual real evalF(const NEWMAT::ColumnVector& x);    
00130   virtual NEWMAT::ColumnVector evalCF(const NEWMAT::ColumnVector& x);   
00131 
00132 // Default Gradient function for NLF0 is to do a finite-difference
00134   virtual NEWMAT::ColumnVector evalG();                 
00135 
00137   virtual real evalLagrangian(const NEWMAT::ColumnVector& x, NEWMAT::ColumnVector& mult,
00138                               const NEWMAT::ColumnVector& type) ;
00140   virtual NEWMAT::ColumnVector evalLagrangianGradient(const NEWMAT::ColumnVector& x,
00141                                               const NEWMAT::ColumnVector& mult,
00142                                               const NEWMAT::ColumnVector& type) ;
00143 
00144 private:
00146   virtual NEWMAT::ColumnVector evalG(const NEWMAT::ColumnVector& x);  
00148   virtual NEWMAT::SymmetricMatrix evalH();              
00150   virtual NEWMAT::SymmetricMatrix evalH(NEWMAT::ColumnVector &x);
00152   virtual NEWMAT::Matrix evalCG(const NEWMAT::ColumnVector& x);         
00154   virtual NEWMAT::SymmetricMatrix evalCH(NEWMAT::ColumnVector &x);      
00156   virtual OptppArray<NEWMAT::SymmetricMatrix> evalCH(NEWMAT::ColumnVector &x, int darg);
00157   virtual void evalC(const NEWMAT::ColumnVector& x);    
00158 };
00159 
00171 class NLF1: public NLP1 {
00172 protected:
00173   USERFCN1 fcn;                 
00174   USERFCN1V fcn_v;              
00175   USERNLNCON1 confcn;           
00176   INITFCN init_fcn;             
00177   INITCONFCN init_confcn;       
00178   bool init_flag;               
00179   void *vptr;                   
00180 
00181   static void f_helper(int m, int n, const NEWMAT::ColumnVector& xc, real& f, 
00182          NEWMAT::ColumnVector& g, int& result, void  *v) 
00183          {NLF1 *o = (NLF1*)v; (*o->fcn)(m,n,xc,f,g,result);}
00184   
00185 
00186 public:
00187   // Constructors
00188   NLF1(): 
00189      NLP1(){;}
00190   NLF1(int ndim): 
00191      NLP1(ndim){;}
00192   NLF1(int ndim, USERFCN1 f, INITFCN i, CompoundConstraint* constraint = 0):
00193      NLP1(ndim, constraint), fcn(f), fcn_v(f_helper), init_fcn(i), 
00194      init_flag(false), vptr(this)
00195      {analytic_grad = 1;}
00196   NLF1(int ndim, USERFCN1 f, INITFCN i, INITCONFCN c):
00197      NLP1(ndim), fcn(f), fcn_v(f_helper), init_fcn(i), init_confcn(c), 
00198      init_flag(false), vptr(this)
00199      {analytic_grad = 1; constraint_ = init_confcn(ndim);}
00200   NLF1(int ndim, int nlncons, USERNLNCON1 f, INITFCN i):
00201      NLP1(ndim,nlncons), confcn(f), init_fcn(i), init_flag(false), vptr(this)
00202      {analytic_grad = 1;}
00204   NLF1(int ndim, USERFCN1V f, INITFCN i, CompoundConstraint* constraint = 0, void* v = 0):
00205      NLP1(ndim, constraint), fcn(0), fcn_v(f), init_fcn(i), init_flag(false) 
00206      { analytic_grad = 1; if (v == 0) vptr = this; else vptr= v ;}
00207   NLF1(int ndim, USERFCN1V f, INITFCN i, void* v):
00208      NLP1(ndim), fcn(0), fcn_v(f), init_fcn(i), init_flag(false), vptr(v)
00209      {analytic_grad = 1;}
00210   NLF1(int ndim, USERFCN1V f, INITFCN i, INITCONFCN c, void* v):
00211      NLP1(ndim), fcn(0), fcn_v(f), init_fcn(i), init_confcn(c), 
00212      init_flag(false), vptr(v)
00213      {analytic_grad = 1; constraint_ = init_confcn(ndim);}
00214 
00215   // Destructor
00216   virtual ~NLF1() {;}                     
00217 
00219   virtual void reset();                         
00220 
00222   virtual void initFcn();                       
00223 
00225   virtual void eval();                          
00226 
00228   virtual real evalF();                         
00229 
00231   virtual real evalF(const NEWMAT::ColumnVector& x);    
00232 
00234   virtual NEWMAT::ColumnVector evalG();                 
00235 
00237   virtual NEWMAT::ColumnVector evalG(const NEWMAT::ColumnVector& x);    
00238 
00240   virtual NEWMAT::SymmetricMatrix evalH();              
00241 
00243   virtual NEWMAT::SymmetricMatrix evalH(NEWMAT::ColumnVector& x); 
00244 
00246   virtual real evalLagrangian(const NEWMAT::ColumnVector& x, NEWMAT::ColumnVector& mult,
00247                               const NEWMAT::ColumnVector& type) ;
00248 
00250   virtual NEWMAT::ColumnVector evalLagrangianGradient(const NEWMAT::ColumnVector& x,
00251                                               const NEWMAT::ColumnVector& mult,
00252                                               const NEWMAT::ColumnVector& type) ;
00253 
00255   virtual NEWMAT::ColumnVector evalCF(const NEWMAT::ColumnVector& x);   
00257   virtual NEWMAT::Matrix evalCG(const NEWMAT::ColumnVector& x);         
00259   virtual NEWMAT::SymmetricMatrix evalCH(NEWMAT::ColumnVector &x);      
00260   // Evaluate constraint hessian at x
00261   virtual OptppArray<NEWMAT::SymmetricMatrix> evalCH(NEWMAT::ColumnVector &x, int darg);
00262   virtual void evalC(const NEWMAT::ColumnVector& x);    
00263 };
00264 
00275 class NLF2: public NLP2 {
00276 protected:
00277   USERFCN2 fcn;                 
00278   USERFCN2V fcn_v;              
00279   USERNLNCON1 confcn1;          
00280   USERNLNCON2 confcn2;          
00281   INITFCN init_fcn;             
00282   INITCONFCN init_confcn;       
00283   bool init_flag;               
00284   void *vptr;                   
00285 
00286   static void f_helper(int m, int n, const NEWMAT::ColumnVector& xc, real& f, 
00287         NEWMAT::ColumnVector& g, NEWMAT::SymmetricMatrix& H, int& result, void  *v)
00288   {NLF2 *o = (NLF2*)v; (*o->fcn)(m,n,xc,f,g,H,result);}
00289 
00290 public:
00291   // Constructors
00292   NLF2(): 
00293      NLP2(){;}
00294   NLF2(int ndim): 
00295      NLP2(ndim){;}
00296   NLF2(int ndim, USERFCN2 f, INITFCN i, CompoundConstraint* constraint = 0):
00297      NLP2(ndim, constraint), fcn(f), fcn_v(f_helper), init_fcn(i), 
00298      init_flag(false), vptr(this) {;}
00299   NLF2(int ndim, USERFCN2 f, INITFCN i, INITCONFCN c):
00300      NLP2(ndim), fcn(f), fcn_v(f_helper), init_fcn(i), init_confcn(c), 
00301      init_flag(false), vptr(this)
00302      {constraint_ = init_confcn(ndim);}
00303   NLF2(int ndim, int nlncons, USERNLNCON1 f, INITFCN i):
00304      NLP2(ndim, nlncons), confcn1(f), confcn2(NULL), init_fcn(i), 
00305      init_flag(false), vptr(this) {;}
00306   NLF2(int ndim, int nlncons, USERNLNCON2 f, INITFCN i):
00307      NLP2(ndim, nlncons), confcn1(NULL), confcn2(f), init_fcn(i), 
00308      init_flag(false), vptr(this) {;}
00310   NLF2(int ndim, USERFCN2V f, INITFCN i, CompoundConstraint* constraint = 0, void* v = 0):
00311      NLP2(ndim, constraint), fcn(0), fcn_v(f), init_fcn(i), init_flag(false) 
00312      { if (v == 0) vptr = this; else vptr= v ;}
00313   NLF2(int ndim, USERFCN2V f, INITFCN i, void* v):
00314      NLP2(ndim), fcn(0), fcn_v(f), init_fcn(i), 
00315      init_flag(false), vptr(v) {;}
00316   NLF2(int ndim, USERFCN2V f, INITFCN i, INITCONFCN c, void* v):
00317      NLP2(ndim), fcn(0), fcn_v(f), init_fcn(i), init_confcn(c), 
00318      init_flag(false), vptr(v)
00319      {constraint_ = init_confcn(ndim);}
00320 
00321   // Destructor
00322   virtual ~NLF2() {;}                     
00323 
00324 
00326   virtual void reset();                         
00327 
00329   virtual void initFcn();                               
00330 
00332   virtual void eval();                                  
00333 
00335   virtual real evalF();                                 
00336 
00338   virtual real evalF(const NEWMAT::ColumnVector& x);            
00339 
00341   virtual NEWMAT::ColumnVector evalG();                         
00342 
00344   virtual NEWMAT::ColumnVector evalG(const NEWMAT::ColumnVector& x);    
00345 
00347   virtual NEWMAT::SymmetricMatrix evalH();                      
00348 private:
00350   virtual NEWMAT::SymmetricMatrix evalH(NEWMAT::ColumnVector& x); 
00351 
00353   virtual real evalLagrangian(const NEWMAT::ColumnVector& x, NEWMAT::ColumnVector& mult,
00354                               const NEWMAT::ColumnVector& type) ;
00355 
00357   virtual NEWMAT::ColumnVector evalLagrangianGradient(const NEWMAT::ColumnVector& x,
00358                                               const NEWMAT::ColumnVector& mult,
00359                                               const NEWMAT::ColumnVector& type) ;
00361   NEWMAT::SymmetricMatrix evalLagrangianHessian(NEWMAT::ColumnVector& x,
00362                                         const NEWMAT::ColumnVector& mult,
00363                                         const NEWMAT::ColumnVector& type);
00365   virtual NEWMAT::ColumnVector evalCF(const NEWMAT::ColumnVector& x);   
00366 
00368   virtual NEWMAT::Matrix evalCG(const NEWMAT::ColumnVector& x);         
00369 
00371   virtual NEWMAT::SymmetricMatrix evalCH(NEWMAT::ColumnVector &x);      
00372 
00374   OptppArray<NEWMAT::SymmetricMatrix> evalCH(NEWMAT::ColumnVector &x, int darg);
00375   virtual void evalC(const NEWMAT::ColumnVector& x);    
00376 
00377 };
00378 
00388 class FDNLF1: public NLP1 {
00389 protected:
00390   USERFCN0 fcn;                 
00391   USERFCN0V fcn_v;              
00392   USERNLNCON0 confcn;           
00393   INITFCN init_fcn;             
00394   INITCONFCN init_confcn;       
00395   bool init_flag;               
00396   void *vptr;                   
00397 
00398   static void f_helper(int n, const NEWMAT::ColumnVector& xc, real& f, 
00399          int& result, void *v) {FDNLF1 *o = (FDNLF1*)v; (*o->fcn)(n,xc,f,result);}
00400 
00401 public:
00402   // Constructor
00403   FDNLF1(): 
00404      NLP1(){;}
00405   FDNLF1(int ndim): 
00406      NLP1(ndim){;}
00407   FDNLF1(int ndim, USERFCN0 f, INITFCN i, CompoundConstraint* constraint = 0): 
00408     NLP1(ndim, constraint), fcn(f), fcn_v(f_helper), init_fcn(i), 
00409     init_flag(false), vptr(this)
00410     { analytic_grad = 0;}
00411   FDNLF1(int ndim, USERFCN0 f, INITFCN i, INITCONFCN c): 
00412     NLP1(ndim), fcn(f), fcn_v(f_helper), init_fcn(i), init_confcn(c), 
00413     init_flag(false), vptr(this)
00414     { analytic_grad = 0; constraint_ = init_confcn(ndim);}
00415   FDNLF1(int ndim, int nlncons, USERNLNCON0 f, INITFCN i):
00416     NLP1(ndim, nlncons), confcn(f), init_fcn(i), init_flag(false), vptr(this)
00417     { analytic_grad = 0;}
00419   FDNLF1(int ndim, USERFCN0V f, INITFCN i, CompoundConstraint* constraint = 0, void* v = 0):
00420      NLP1(ndim, constraint), fcn(0), fcn_v(f), init_fcn(i), init_flag(false) 
00421      { analytic_grad = 1; if (v == 0) vptr = this; else vptr= v ;}
00422   FDNLF1(int ndim, USERFCN0V f, INITFCN i, void* v): 
00423     NLP1(ndim), fcn(0), fcn_v(f), init_fcn(i), init_flag(false), vptr(v)
00424     { analytic_grad = 0;}
00425   FDNLF1(int ndim, USERFCN0V f, INITFCN i, INITCONFCN c, void* v): 
00426     NLP1(ndim), fcn(0), fcn_v(f), init_fcn(i), init_confcn(c), 
00427     init_flag(false), vptr(v)
00428     { analytic_grad = 0; constraint_ = init_confcn(ndim);}
00429 
00430   // Destructor
00431   virtual ~FDNLF1() {;}                     
00432 
00434   virtual void reset();                         
00435 
00437   virtual void initFcn();                               
00438 
00440   virtual void eval();                                  
00441 
00443   virtual real evalF();                                 
00444 
00446   virtual real evalF(const NEWMAT::ColumnVector& x);            
00447 
00449   virtual NEWMAT::ColumnVector evalG();                         
00450 
00452   virtual NEWMAT::ColumnVector evalG(const NEWMAT::ColumnVector& x);    
00453 
00455   virtual NEWMAT::SymmetricMatrix evalH();                      
00456 
00459   NEWMAT::SymmetricMatrix FDHessian(NEWMAT::ColumnVector& x);     
00460 
00463   virtual void printState(char *);    
00464 
00467   virtual void fPrintState(ostream *, char *);    
00468 
00469 
00471   virtual real evalLagrangian(const NEWMAT::ColumnVector& x, NEWMAT::ColumnVector& mult,
00472                               const NEWMAT::ColumnVector& type) ;
00473 
00475   virtual NEWMAT::ColumnVector evalLagrangianGradient(const NEWMAT::ColumnVector& x,
00476                                               const NEWMAT::ColumnVector& mult,
00477                                               const NEWMAT::ColumnVector& type) ;
00479   virtual NEWMAT::ColumnVector evalCF(const NEWMAT::ColumnVector& x);   
00480 
00482   virtual NEWMAT::Matrix evalCG(const NEWMAT::ColumnVector& x);         
00483 private:
00485   virtual NEWMAT::SymmetricMatrix evalH(NEWMAT::ColumnVector& x);       
00487   virtual NEWMAT::SymmetricMatrix evalCH(NEWMAT::ColumnVector &x);      
00489   virtual OptppArray<NEWMAT::SymmetricMatrix> evalCH(NEWMAT::ColumnVector &x, int darg);
00490   virtual void evalC(const NEWMAT::ColumnVector& x);    
00491 };
00492 
00493 } // namespace OPTPP
00494 
00495 #endif
Generated on Mon Jan 24 12:04:37 2011 for FASTlib by  doxygen 1.6.3