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