00001 #ifndef PLAYA_OBJECTIVE_BASE_H 00002 #define PLAYA_OBJECTIVE_BASE_H 00003 00004 #include "PlayaDefs.hpp" 00005 #include "PlayaVectorDecl.hpp" 00006 #include "PlayaObjectWithVerbosity.hpp" 00007 00008 #ifndef HAVE_TEUCHOS_EXPLICIT_INSTANTIATION 00009 #include "PlayaVectorImpl.hpp" 00010 #endif 00011 00012 00013 /** \example Rosenbrock.cpp 00014 * An example of an objective function 00015 */ 00016 00017 00018 namespace Playa 00019 { 00020 /** 00021 * Base class for differentiable objective functions. 00022 * @author Paul Boggs and Kevin Long 00023 * 00024 */ 00025 class ObjectiveBase : public ObjectWithVerbosity, 00026 public Describable 00027 { 00028 public: 00029 /** */ 00030 ObjectiveBase(int verb=0) : ObjectWithVerbosity(verb), contextString_() {;} 00031 00032 /** virtual dtor */ 00033 virtual ~ObjectiveBase(){;} 00034 00035 /** evaluate objective function and gradient */ 00036 virtual void evalGrad(const Vector<double>& x, double& f, 00037 Vector<double>& grad) const = 0; 00038 00039 /** evaluate objective function without gradient. */ 00040 virtual void eval(const Vector<double>& x, double& f) const = 0 ; 00041 00042 /** return an initial guess for the design vector */ 00043 virtual Vector<double> getInit() const = 0; 00044 00045 /** return an initial approximation to the scale for the 00046 * inverse of the Hessian */ 00047 virtual double getInvHScale() const {return 1.0;} 00048 00049 /** User-overrideable hook for any callbacks to be done at the 00050 * end of each iteration. Default is a no-op. */ 00051 virtual void iterationCallback(const Vector<double>& x, int iter) const {;} 00052 00053 /** User-overrideable hook for any callbacks to be done at the 00054 * end of an optimization loop. Default is a no-op. */ 00055 virtual void finalCallback(const Vector<double>& x) const {;} 00056 00057 /** 00058 * Set a string describing the context in which the function is being called. 00059 * This is intended to aid in reading diagnostic output. 00060 */ 00061 void setContextString(const string& str) const {contextString_ = str;} 00062 00063 /** 00064 * Set a string describing the context in which the function is being called. 00065 * This is intended to aid in reading diagnostic output. 00066 */ 00067 const string& contextString() const {return contextString_;} 00068 00069 /** 00070 * Return the number of evaluations 00071 */ 00072 virtual int numFuncEvals() const {return -1;} 00073 00074 /** 00075 * 00076 */ 00077 virtual string description() const {return "ObjectiveBase";} 00078 00079 /** Debugging utility to check the gradient 00080 * by comparing to a finite difference 00081 * gradient calculation. This is will be expensive. */ 00082 bool fdCheck(const Vector<double>& x, double tol, int verbosity=0) const ; 00083 00084 protected: 00085 /** */ 00086 virtual double fdStep() const {return 1.0e-4;} 00087 00088 00089 private: 00090 mutable string contextString_; 00091 }; 00092 00093 } 00094 00095 #endif