00001 /******************************************************************************/ 00002 /* */ 00003 /* R A S T R I G I N F U N C T I O N C L A S S */ 00004 /* */ 00005 /* Roberto Lopez */ 00006 /* International Center for Numerical Methods in Engineering (CIMNE) */ 00007 /* Technical University of Catalonia (UPC) */ 00008 /* Barcelona, Spain */ 00009 /* E-mail: rlopez@cimne.upc.edu */ 00010 /* */ 00011 /******************************************************************************/ 00012 00013 00014 #include <iostream> 00015 #include <fstream> 00016 #include <math.h> 00017 00018 #include "RastriginFunction.h" 00019 00020 namespace Purple 00021 { 00022 00023 // GENERAL CONSTRUCTOR 00024 00025 /// General constructor. It creates a Rastrigin's objective function object. 00026 /// It also initializes all the rest of class members to their default values: 00027 /// 00028 /// <ul> 00029 /// <li> Number of variables = 2. 00030 /// <li> Lower bound = -5.12,...,-5.12. 00031 /// <li> Upper bound = 5.12,...,5.12. 00032 /// </ul> 00033 00034 RastriginFunction::RastriginFunction(void) : ObjectiveFunction() 00035 { 00036 numberOfVariables = 2; 00037 00038 Vector<double> newLowerBound(numberOfVariables, -5.12); 00039 00040 lowerBound = newLowerBound; 00041 00042 Vector<double> newUpperBound(numberOfVariables, 5.12); 00043 00044 upperBound = newUpperBound; 00045 } 00046 00047 00048 // DESTRUCTOR 00049 00050 /// Destructor. 00051 00052 RastriginFunction::~RastriginFunction(void) 00053 { 00054 00055 } 00056 00057 00058 // METHODS 00059 00060 // double getEvaluation(Vector<double>) method 00061 00062 /// This method returns the Rastrigin's function evaluation for a given argument. 00063 /// 00064 /// @param argument: Objective function argument. 00065 00066 double RastriginFunction::getEvaluation(Vector<double> argument) 00067 { 00068 double evaluation = 0.0; 00069 00070 int size = argument.getSize(); 00071 00072 if(size != numberOfVariables) 00073 { 00074 std::cout << std::endl 00075 << "Error: RastriginFunction class. " 00076 << "double getEvaluation(Vector<double>) method." << std::endl 00077 << "Size of argument must be equal to number of variables." << std::endl 00078 << std::endl; 00079 00080 exit(1); 00081 } 00082 00083 const double pi = 3.1415927; 00084 00085 evaluation = 10.0*numberOfVariables; 00086 00087 for(int i = 0; i < numberOfVariables; i++) 00088 { 00089 evaluation += pow(argument[i], 2) - 10.0*cos(2.0*pi*argument[i]); 00090 } 00091 00092 return(evaluation); 00093 } 00094 00095 00096 // Vector<double> getGradient(Vector<double>) method 00097 00098 /// This method returns the De Jong's analytical gradient Vector 00099 /// for a given argument. 00100 /// 00101 /// @param argument: Point at which the gradient is to be computed. 00102 00103 Vector<double> RastriginFunction::getGradient(Vector<double> argument) 00104 { 00105 Vector<double> gradient(numberOfVariables); 00106 00107 const double pi = 3.1415927; 00108 00109 for(int i = 0; i < numberOfVariables; i++) 00110 { 00111 gradient[i] = 2.0*argument[i] + 10.0*sin(2.0*pi*argument[i])*2.0*pi; 00112 } 00113 00114 return(gradient); 00115 } 00116 00117 00118 // Matrix<double> getHessian(Vector<double>) method 00119 00120 /// This method returns the Rosenbrock's analytical Hessian Matrix 00121 /// for a given argument. 00122 /// 00123 /// @param argument: Point at which the Hessian is to be computed. 00124 00125 Matrix<double> RastriginFunction::getHessian(Vector<double> argument) 00126 { 00127 Matrix<double> hessian(numberOfVariables, numberOfVariables, 0.0); 00128 00129 const double pi = 3.1415927; 00130 00131 for(int i = 0; i < numberOfVariables; i++) 00132 { 00133 for(int j = 0; j < numberOfVariables; j++) 00134 { 00135 if(i == j) 00136 { 00137 hessian[i][j] = 2.0 + 10.0*cos(2.0*pi*argument[i])*4.0*pow(pi,2); 00138 } 00139 else 00140 { 00141 hessian[i][j] = 0.0; 00142 } 00143 } 00144 } 00145 00146 return(hessian); 00147 } 00148 00149 00150 // Matrix<double> getInverseHessian(Vector<double>) method 00151 00152 /// This method returns the Rastrigin's analytical inverse Hessian Matrix 00153 /// for a given argument. 00154 /// 00155 /// @param argument: Point at which the inverse Hessian is to be computed. 00156 00157 Matrix<double> RastriginFunction::getInverseHessian(Vector<double> argument) 00158 { 00159 Matrix<double> inverseHessian(numberOfVariables, numberOfVariables, 0.0); 00160 00161 const double pi = 3.1415927; 00162 00163 for(int i = 0; i < numberOfVariables; i++) 00164 { 00165 for(int j = 0; j < numberOfVariables; j++) 00166 { 00167 if(i == j) 00168 { 00169 inverseHessian[i][j] 00170 = 1.0/(2.0 + 10.0*cos(2.0*pi*argument[i])*4.0*pow(pi,2)); 00171 } 00172 else 00173 { 00174 inverseHessian[i][j] = 0.0; 00175 } 00176 } 00177 } 00178 00179 return(inverseHessian); 00180 } 00181 00182 } 00183 00184 00185 // Purple: An Open Source Numerical Optimization C++ Library. 00186 // Copyright (C) 2006 Roberto Lopez 00187 // 00188 // This library is free software; you can redistribute it and/or 00189 // modify it under the terms of the GNU Lesser General Public 00190 // License as published by the Free Software Foundation; either 00191 // version 2.1 of the License, or any later version. 00192 // 00193 // This library is distributed in the hope that it will be useful, 00194 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00195 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00196 // Lesser General Public License for more details. 00197 00198 // You should have received a copy of the GNU Lesser General Public 00199 // License along with this library; if not, write to the Free Software 00200 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA