RosenbrockFunction.cpp

Go to the documentation of this file.
00001 /******************************************************************************/
00002 /*                                                                            */
00003 /*   R O S E N B R O C K   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 "RosenbrockFunction.h"
00019 
00020 namespace Purple
00021 {
00022 
00023 // GENERAL CONSTRUCTOR
00024 
00025 /// General constructor. It creates a Rosenbrock'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 = -2.048,...,-2.048.
00031 /// <li> Upper bound = 2.048,...,2.048.
00032 /// </ul> 
00033 
00034 RosenbrockFunction::RosenbrockFunction(void) : ObjectiveFunction()
00035 {
00036    numberOfVariables = 2;
00037 
00038    Vector<double> newLowerBound(numberOfVariables, -2.048);
00039 
00040    lowerBound = newLowerBound;
00041 
00042    Vector<double> newUpperBound(numberOfVariables, 2.048);
00043    
00044    upperBound = newUpperBound;
00045 }
00046 
00047 
00048 // DESTRUCTOR
00049 
00050 /// Destructor.
00051 
00052 RosenbrockFunction::~RosenbrockFunction(void)
00053 {
00054 
00055 }
00056 
00057 
00058 // METHODS
00059 
00060 // double getEvaluation(Vector<double>) method
00061 
00062 /// This method returns the Rosenbrock's function evaluation for a given argument.
00063 ///
00064 /// @param argument: Objective function argument.
00065 
00066 double RosenbrockFunction::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: RosenbrockFunction 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       std::cout << "Size of argument: " << size << std::endl
00081                 << "Number of variables: " << numberOfVariables <<std::endl;
00082 
00083       exit(1);
00084    }
00085 
00086    // Get evaluation
00087 
00088    for(int i = 0; i < numberOfVariables-1; i++)
00089    {
00090       evaluation += 100.0*pow(argument[i+1] - pow(argument[i],2), 2)
00091       + pow(1.0 - argument[i], 2);
00092    }
00093 
00094    return(evaluation);
00095 }
00096 
00097 }
00098 
00099 
00100 // Purple: An Open Source Numerical Optimization C++ Library.
00101 // Copyright (C) 2006 Roberto Lopez
00102 //
00103 // This library is free software; you can redistribute it and/or
00104 // modify it under the terms of the GNU Lesser General Public
00105 // License as published by the Free Software Foundation; either
00106 // version 2.1 of the License, or any later version.
00107 //
00108 // This library is distributed in the hope that it will be useful,
00109 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00110 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00111 // Lesser General Public License for more details.
00112 
00113 // You should have received a copy of the GNU Lesser General Public
00114 // License along with this library; if not, write to the Free Software
00115 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

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