MLPACK  1.0.4
test_functions.hpp
Go to the documentation of this file.
00001 
00028 #ifndef __MLPACK_CORE_OPTIMIZERS_LBFGS_TEST_FUNCTIONS_HPP
00029 #define __MLPACK_CORE_OPTIMIZERS_LBFGS_TEST_FUNCTIONS_HPP
00030 
00031 #include <mlpack/core.hpp>
00032 
00033 // To fulfill the template policy class 'FunctionType', we must implement
00034 // the following:
00035 //
00036 //   FunctionType(); // constructor
00037 //   void Gradient(const arma::mat& coordinates, arma::mat& gradient);
00038 //   double Evaluate(const arma::mat& coordinates);
00039 //   const arma::mat& GetInitialPoint();
00040 //
00041 // Note that we are using an arma::mat instead of the more intuitive and
00042 // expected arma::vec.  This is because L-BFGS will also optimize matrices.
00043 // However, remember that an arma::vec is simply an (n x 1) arma::mat.  You can
00044 // use either internally but the L-BFGS method requires arma::mat& to be passed
00045 // (C++ does not allow implicit reference casting to subclasses).
00046 
00047 namespace mlpack {
00048 namespace optimization {
00049 namespace test {
00050 
00063 class RosenbrockFunction
00064 {
00065  public:
00066   RosenbrockFunction(); // initialize initial point
00067 
00068   double Evaluate(const arma::mat& coordinates);
00069   void Gradient(const arma::mat& coordinates, arma::mat& gradient);
00070 
00071   const arma::mat& GetInitialPoint() const;
00072 
00073  private:
00074   arma::mat initialPoint;
00075 };
00076 
00093 class WoodFunction
00094 {
00095  public:
00096   WoodFunction(); // initialize initial point
00097 
00098   double Evaluate(const arma::mat& coordinates);
00099   void Gradient(const arma::mat& coordinates, arma::mat& gradient);
00100 
00101   const arma::mat& GetInitialPoint() const;
00102 
00103  private:
00104   arma::mat initialPoint;
00105 };
00106 
00123 class GeneralizedRosenbrockFunction
00124 {
00125  public:
00126   /***
00127    * Set the dimensionality of the extended Rosenbrock function.
00128    *
00129    * @param n Number of dimensions for the function.
00130    */
00131   GeneralizedRosenbrockFunction(int n);
00132 
00133   double Evaluate(const arma::mat& coordinates) const;
00134   void Gradient(const arma::mat& coordinates, arma::mat& gradient) const;
00135 
00136   size_t NumFunctions() const { return n - 1; }
00137   double Evaluate(const arma::mat& coordinates, const size_t i) const;
00138   void Gradient(const arma::mat& coordinates,
00139                 const size_t i,
00140                 arma::mat& gradient) const;
00141 
00142   const arma::mat& GetInitialPoint() const;
00143 
00144  private:
00145   arma::mat initialPoint;
00146   int n; // Dimensionality
00147 };
00148 
00154 class RosenbrockWoodFunction
00155 {
00156  public:
00157   RosenbrockWoodFunction(); // initialize initial point
00158 
00159   double Evaluate(const arma::mat& coordinates);
00160   void Gradient(const arma::mat& coordinates, arma::mat& gradient);
00161 
00162   const arma::mat& GetInitialPoint() const;
00163 
00164  private:
00165   arma::mat initialPoint;
00166   GeneralizedRosenbrockFunction rf;
00167   WoodFunction wf;
00168 };
00169 
00170 }; // namespace test
00171 }; // namespace optimization
00172 }; // namespace mlpack
00173 
00174 #endif // __MLPACK_CORE_OPTIMIZERS_LBFGS_TEST_FUNCTIONS_HPP