00001 /******************************************************************************/ 00002 /* */ 00003 /* E V O L U T I O N A R Y A L G O R I T H M C L A S S H E A D E R */ 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 #ifndef __EVOLUTIONARYALGORITHM_H__ 00015 #define __EVOLUTIONARYALGORITHM_H__ 00016 00017 00018 #include "OptimizationAlgorithm.h" 00019 #include "../ObjectiveFunction/ObjectiveFunction.h" 00020 00021 namespace Purple 00022 { 00023 00024 /// This concrete class represents an evolutionary optimization algorithm 00025 /// for an objective function. 00026 /// 00027 /// @see ObjectiveFunction. 00028 /// @see OptimizationAlgorithm. 00029 00030 class EvolutionaryAlgorithm : public OptimizationAlgorithm 00031 { 00032 00033 public: 00034 00035 // ENUMERATIONS 00036 00037 /// Enumeration of the available optimization operators for fitness assignment. 00038 00039 enum FitnessAssignmentMethod{LinearRanking, NonLinearRanking}; 00040 00041 /// Enumeration of the available optimization operators for selection. 00042 00043 enum SelectionMethod{RouletteWheel, StochasticUniversalSampling}; 00044 00045 /// Enumeration of the available optimization operators for recombination. 00046 00047 enum RecombinationMethod{Line, Intermediate}; 00048 00049 /// Enumeration of the available optimization operators for mutation. 00050 00051 enum MutationMethod{Normal, Uniform}; 00052 00053 private: 00054 00055 // FIELDS 00056 00057 // Population stuff 00058 00059 /// Number of individuals in the population. 00060 00061 int populationSize; 00062 00063 /// Population matrix. 00064 00065 Matrix<double> population; 00066 00067 /// Function evaluation of population. 00068 00069 Vector<double> evaluation; 00070 00071 /// Fitness of population. 00072 00073 Vector<double> fitness; 00074 00075 /// Selected individuals in population. 00076 00077 Vector<bool> selection; 00078 00079 // Optimization parameters 00080 00081 /// Selective pressure. 00082 /// Linear ranking allows values for the selective pressure between 1 and 2. 00083 /// Non linear ranking allows values for the selective pressure 00084 /// between 1 and [population size] - 2. 00085 00086 double selectivePressure; 00087 00088 /// Recombination size. 00089 /// The recombination size value must be equal or greater than 0. 00090 00091 double recombinationSize; 00092 00093 /// Mutation rate. 00094 /// The mutation rate value must be between 0 and 1. 00095 00096 double mutationRate; 00097 00098 /// Mutation range. 00099 /// The mutation range value must be 0 or a positive number. 00100 00101 double mutationRange; 00102 00103 // Stopping criteria 00104 00105 /// Maximum number of generations. 00106 00107 int maximumNumberOfGenerations; 00108 00109 /// Number of generations between showing progress. 00110 00111 int showPeriod; 00112 00113 /// Mean evaluation optimization history. 00114 00115 Vector<double> meanEvaluationHistory; 00116 00117 /// Standard deviation of evaluation optimization history. 00118 00119 Vector<double> standardDeviationEvaluationHistory; 00120 00121 /// Best evaluation ever optimization history. 00122 00123 Vector<double> bestEvaluationHistory; 00124 00125 00126 /// Fitness assignment optimization operators enumeration. 00127 00128 FitnessAssignmentMethod fitnessAssignmentMethod; 00129 00130 /// Selection optimization operators enumeration. 00131 00132 SelectionMethod selectionMethod; 00133 00134 /// Recombination optimization operators enumeration. 00135 00136 RecombinationMethod recombinationMethod; 00137 00138 /// Mutation optimization operators enumeration. 00139 00140 MutationMethod mutationMethod; 00141 00142 public: 00143 00144 // GENERAL CONSTRUCTOR 00145 00146 EvolutionaryAlgorithm(ObjectiveFunction*); 00147 00148 00149 // DEFAULT CONSTRUCTOR 00150 00151 EvolutionaryAlgorithm(void); 00152 00153 00154 // DESTRUCTOR 00155 00156 virtual ~EvolutionaryAlgorithm(void); 00157 00158 00159 // METHODS 00160 00161 // Get methods 00162 00163 int getPopulationSize(void); 00164 00165 Matrix<double> getPopulation(void); 00166 00167 Vector<double> getEvaluation(void); 00168 Vector<double> getFitness(void); 00169 Vector<bool> getSelection(void); 00170 00171 double getSelectivePressure(void); 00172 double getRecombinationSize(void); 00173 double getMutationRate(void); 00174 double getMutationRange(void); 00175 00176 int getMaximumNumberOfGenerations(void); 00177 int getShowPeriod(void); 00178 00179 Vector<double> getMeanEvaluationHistory(void); 00180 Vector<double> getStandardDeviationEvaluationHistory(void); 00181 Vector<double> getBestEvaluationHistory(void); 00182 00183 FitnessAssignmentMethod getFitnessAssignmentMethod(void); 00184 SelectionMethod getSelectionMethod(void); 00185 RecombinationMethod getRecombinationMethod(void); 00186 MutationMethod getMutationMethod(void); 00187 00188 // Set methods 00189 00190 void setPopulationSize(int); 00191 00192 void setPopulation(Matrix<double>); 00193 00194 void setEvaluation(Vector<double>); 00195 void setFitness(Vector<double>); 00196 void setSelection(Vector<bool>); 00197 00198 void setSelectivePressure(double); 00199 void setRecombinationSize(double); 00200 00201 void setMutationRate(double); 00202 void setMutationRange(double); 00203 00204 void setFitnessAssignmentMethod(FitnessAssignmentMethod); 00205 void setSelectionMethod(SelectionMethod); 00206 void setRecombinationMethod(RecombinationMethod); 00207 void setMutationMethod(MutationMethod); 00208 00209 void setMaximumNumberOfGenerations(int); 00210 void setShowPeriod(int); 00211 00212 // Population methods 00213 00214 Vector<double> getIndividual(int); 00215 void setIndividual(int, Vector<double>); 00216 00217 void initPopulationAtRandom(void); 00218 void initPopulationAtRandom(double, double); 00219 void initPopulationAtRandom(Vector<double>, Vector<double>); 00220 void initPopulationAtRandom(Matrix<double>); 00221 00222 // Population evaluation methods 00223 00224 void evaluatePopulation(void); 00225 00226 // Fitness assignment methods 00227 00228 void performLinearRankingFitnessAssignment(void); 00229 void performNonLinearRankingFitnessAssignment(void); 00230 00231 // Selection methods 00232 00233 void performRouletteWheelSelection(void); 00234 void performStochasticUniversalSamplingSelection(void); 00235 00236 00237 // Recombination methods 00238 00239 void performIntermediateRecombination(void); 00240 void performLineRecombination(void); 00241 00242 // Mutation methods 00243 00244 void performNormalMutation(void); 00245 void performUniformMutation(void); 00246 00247 // Optimization methods 00248 00249 Vector<double> getMinimalArgument(void); 00250 00251 // Utility methods 00252 00253 void print(void); 00254 00255 void load(char*); 00256 void save(char*); 00257 00258 void saveOptimizationHistory(char*); 00259 }; 00260 00261 } 00262 00263 #endif 00264 00265 00266 // Purple: An Open Source Numerical Optimization C++ Library. 00267 // Copyright (C) 2005 Roberto Lopez 00268 // 00269 // This library is free software; you can redistribute it and/or 00270 // modify it under the terms of the GNU Lesser General Public 00271 // License as published by the Free Software Foundation; either 00272 // version 2.1 of the License, or any later version. 00273 // 00274 // This library is distributed in the hope that it will be useful, 00275 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00276 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00277 // Lesser General Public License for more details. 00278 // 00279 // You should have received a copy of the GNU Lesser General Public 00280 // License along with this library; if not, write to the Free Software 00281 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA