00001 /******************************************************************************/ 00002 /* */ 00003 /* P L A N E - C Y L I N D E R 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 #include <iostream> 00014 #include <fstream> 00015 #include <math.h> 00016 00017 #include "PlaneCylinder.h" 00018 00019 namespace Purple 00020 { 00021 00022 // GENERAL CONSTRUCTOR 00023 00024 /// General constructor. It creates a plane-cylinder objective function object. 00025 /// It also initializes all the rest of class members to their default values: 00026 /// 00027 /// <ul> 00028 /// <li> Number of variables = 2. 00029 /// <li> Lower bound = -5.12,...,-5.12. 00030 /// <li> Upper bound = 5.12,...,5.12. 00031 /// <li> Penalty = 100. 00032 /// </ul> 00033 00034 PlaneCylinder::PlaneCylinder(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 penalty = 100.0; 00047 } 00048 00049 00050 // DESTRUCTOR 00051 00052 /// Destructor. 00053 00054 PlaneCylinder::~PlaneCylinder(void) 00055 { 00056 00057 } 00058 00059 00060 // METHODS 00061 00062 // double getPenalty(void) method 00063 00064 /// This method returns the penalty term ratio to be used in the plane-cylinder 00065 /// problem 00066 /// 00067 /// @see getError(Vector<double>) 00068 /// @see getEvaluation(Vector<double>) 00069 00070 double PlaneCylinder::getPenalty(void) 00071 { 00072 return(penalty); 00073 } 00074 00075 00076 /// This method sets a new penalty term ratio to be used in the plane-cylinder 00077 /// problem 00078 /// 00079 /// @param newPenalty: New penalty term ratio. 00080 /// 00081 /// @see getError(Vector<double>) 00082 /// @see getEvaluation(Vector<double>) 00083 00084 void PlaneCylinder::setPenalty(double newPenalty) 00085 { 00086 penalty = newPenalty; 00087 } 00088 00089 00090 // double getError(Vector<double>) method 00091 00092 /// This method returns the error made in the constraint by a given argument. 00093 /// 00094 /// @param argument: argument. 00095 /// 00096 /// @see getEvaluation(Vector<double>) 00097 00098 double PlaneCylinder::getError(Vector<double> argument) 00099 { 00100 double error = 0.0; 00101 00102 double x = argument[0]; 00103 double y = argument[1]; 00104 00105 if(pow(x,2) + pow(y,2) <= 1.0) 00106 { 00107 error = 0.0; 00108 } 00109 else 00110 { 00111 error = pow(x,2) + pow(y,2) - 1.0; 00112 } 00113 00114 return(error); 00115 } 00116 00117 00118 // double getEvaluation(void) method 00119 00120 /// This method returns the plane-cylinder function evaluation for a given 00121 /// argument. 00122 /// 00123 /// @param argument: Objective function argument. 00124 00125 double PlaneCylinder::getEvaluation(Vector<double> argument) 00126 { 00127 double evaluation = 0.0; 00128 00129 int size = argument.getSize(); 00130 00131 if(size != numberOfVariables) 00132 { 00133 std::cout << std::endl 00134 << "Error: PlaneCylinder class. " 00135 << "double getEvaluation(Vector<double>) method." << std::endl 00136 << "Size of argument must be equal to number of variables." << std::endl 00137 << std::endl; 00138 00139 exit(1); 00140 } 00141 00142 double x = argument[0]; 00143 double y = argument[1]; 00144 00145 double error = getError(argument); 00146 00147 evaluation = x + y + penalty*pow(error, 2); 00148 00149 return(evaluation); 00150 } 00151 00152 00153 // void print(void) method 00154 00155 /// This method prints to the screen the error made in the constraint by 00156 /// a given argument during the optimization process. 00157 /// 00158 /// @param argument: argument. 00159 /// 00160 /// @see getError(Vector<double>) 00161 00162 void PlaneCylinder::print(Vector<double> argument) 00163 { 00164 double error = getError(argument); 00165 00166 std::cout << "Error: " << error << std::endl; 00167 } 00168 00169 } 00170 00171 00172 // Purple: An Open Source Numerical Optimization C++ Library. 00173 // Copyright (C) 2006 Roberto Lopez 00174 // 00175 // This library is free software; you can redistribute it and/or 00176 // modify it under the terms of the GNU Lesser General Public 00177 // License as published by the Free Software Foundation; either 00178 // version 2.1 of the License, or any later version. 00179 // 00180 // This library is distributed in the hope that it will be useful, 00181 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00182 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00183 // Lesser General Public License for more details. 00184 00185 // You should have received a copy of the GNU Lesser General Public 00186 // License along with this library; if not, write to the Free Software 00187 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA