DeJongFunction.cpp

Go to the documentation of this file.
00001 /******************************************************************************/
00002 /*                                                                            */
00003 /*   D E   J O N G   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 "DeJongFunction.h"
00019 
00020 namespace Purple
00021 {
00022 
00023 // GENERAL CONSTRUCTOR
00024 
00025 /// General constructor. It creates a De Jong'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 DeJongFunction::DeJongFunction(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 DeJongFunction::~DeJongFunction(void)
00053 {
00054 
00055 }
00056 
00057 
00058 // METHODS
00059 
00060 // double getEvaluation(Vector<double>) method
00061 
00062 /// This method returns the De Jong's function evaluation for a given argument.
00063 ///
00064 /// @param argument: Objective function argument.
00065 
00066 double DeJongFunction::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: DeJongFunction 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    for(int i = 0; i < numberOfVariables; i++)
00084    {
00085       evaluation += pow(argument[i], 2);
00086    }
00087 
00088    return(evaluation);
00089 }
00090 
00091 
00092 // Vector<double> getGradient(Vector<double>) method
00093 
00094 /// This method returns the De Jong's analytical gradient Vector 
00095 /// for a given argument.
00096 ///
00097 /// @param argument: Point at which the gradient is to be computed.
00098 
00099 Vector<double> DeJongFunction::getGradient(Vector<double> argument)
00100 {
00101    Vector<double> gradient(numberOfVariables, 0.0);
00102 
00103    for(int i = 0; i < numberOfVariables; i++)
00104    {
00105       gradient[i] = 2.0*argument[i];
00106    }
00107 
00108    return(gradient);
00109 }
00110 
00111 
00112 // Matrix<double> getHessian(Vector<double>) method
00113 
00114 /// This method returns the De Jong's analytical Hessian Matrix 
00115 /// for a given argument.
00116 ///
00117 /// @param argument: Point at which the Hessian is to be computed.
00118 
00119 Matrix<double> DeJongFunction::getHessian(Vector<double> argument)
00120 {
00121    Matrix<double> hessian(numberOfVariables, numberOfVariables, 0.0);
00122 
00123    for(int i = 0; i < numberOfVariables; i++)
00124    {
00125       for(int j = 0; j < numberOfVariables; j++)
00126       {
00127          if(i == j)
00128          {
00129             hessian[i][j] = 2.0;
00130          }
00131          else
00132          {
00133             hessian[i][j] = 0.0;
00134          }
00135       }
00136    }
00137 
00138    return(hessian);
00139 }
00140 
00141 
00142 // Matrix<double> getInverseHessian(Vector<double>) method
00143 
00144 /// This method returns the De Jong's analytical inverse Hessian Matrix 
00145 /// for a given argument.
00146 ///
00147 /// @param argument: Point at which the inverse Hessian is to be computed.
00148 
00149 Matrix<double> DeJongFunction::getInverseHessian(Vector<double> argument)
00150 {
00151    Matrix<double> inverseHessian(numberOfVariables, numberOfVariables, 0.0);
00152 
00153    for(int i = 0; i < numberOfVariables; i++)
00154    {
00155       for(int j = 0; j < numberOfVariables; j++)
00156       {
00157          if(i == j)
00158          {
00159             inverseHessian[i][j] = 0.5;
00160          }
00161          else
00162          {
00163             inverseHessian[i][j] = 0.0;
00164          }
00165       }
00166    }
00167 
00168    return(inverseHessian);
00169 }
00170 
00171 }
00172 
00173 
00174 // Purple: An Open Source Numerical Optimization C++ Library.
00175 // Copyright (C) 2006 Roberto Lopez
00176 //
00177 // This library is free software; you can redistribute it and/or
00178 // modify it under the terms of the GNU Lesser General Public
00179 // License as published by the Free Software Foundation; either
00180 // version 2.1 of the License, or any later version.
00181 //
00182 // This library is distributed in the hope that it will be useful,
00183 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00184 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00185 // Lesser General Public License for more details.
00186 
00187 // You should have received a copy of the GNU Lesser General Public
00188 // License along with this library; if not, write to the Free Software
00189 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

Generated on Wed Jun 21 13:10:37 2006 for Purple by  doxygen 1.4.7