ga/include/core/mutation.h

00001 /********************************************************************************
00002  *  FARSA Genetic Algorithm Library                                             *
00003  *  Copyright (C) 2007-2008 Gianluca Massera <emmegian@yahoo.it>                *
00004  *                                                                              *
00005  *  This program is free software; you can redistribute it and/or modify        *
00006  *  it under the terms of the GNU General Public License as published by        *
00007  *  the Free Software Foundation; either version 2 of the License, or           *
00008  *  (at your option) any later version.                                         *
00009  *                                                                              *
00010  *  This program is distributed in the hope that it will be useful,             *
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00013  *  GNU General Public License for more details.                                *
00014  *                                                                              *
00015  *  You should have received a copy of the GNU General Public License           *
00016  *  along with this program; if not, write to the Free Software                 *
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00018  ********************************************************************************/
00019 
00020 #ifndef MUTATION_H
00021 #define MUTATION_H
00022 
00023 #include "gaconfig.h"
00024 #include "parametersettable.h"
00025 #include <QMutex>
00026 
00027 namespace farsa {
00028 
00029 class Genotype;
00030 class Genome;
00031 class GeneticAlgo;
00032 
00047 class FARSA_GA_API Mutation : public ParameterSettableWithConfigureFunction {
00048 public:
00050     Mutation();
00051 
00053     virtual ~Mutation();
00054 
00058     virtual void mutate( Genotype* ) = 0;
00059 
00071     void setMutationRate( double rate, int start = 0, int length = -1);
00072 
00077     void setMutationRate( double initialRate, double finalRate, double variation, int start = 0, int length = -1);
00078 
00086     double mutationRate( int bit );
00087 
00094     double initialMutationRate( int bit );
00095 
00102     double finalMutationRate( int bit );
00103 
00110     double variationMutationRate( int bit );
00111     
00113     void setGenome( const Genome* g ) {
00114         genomev = g;
00115     }
00116 
00118     const Genome* genome() {
00119         return genomev;
00120     }
00121 
00123     void setGA( GeneticAlgo* g ) {
00124         ga = g;
00125     }
00126 
00128     GeneticAlgo* GA() {
00129         return ga;
00130     }
00131 
00138     virtual void configure( ConfigurationParameters& params, QString prefix );
00139 
00146     virtual void save( ConfigurationParameters& params, QString prefix );
00148     static void describe( QString type );
00149 
00150 protected:
00152     const Genome* genomev;
00153 
00155     GeneticAlgo* ga;
00156 
00157 private:
00158     // Here we have all mutation rates stuffs, to force even child classes
00159     // to use functions, which will keep everything in a consistent state
00160 
00162     void updateMutationRates();
00163 
00165     struct MutationRate {
00167         MutationRate() :
00168             initial(0.0),
00169             final(0.0),
00170             variation(0.0),
00171             mutaRate(0.0)
00172         {
00173         }
00174 
00176         void rateForGeneration(unsigned int gen);
00177 
00179         double initial;
00180 
00182         double final;
00183 
00185         double variation;
00186 
00188         double mutaRate;
00189     };
00190 
00195     const MutationRate& getMutationRateForBit( int bit ) const;
00196 
00198     QMap<int, MutationRate> mutaRates;
00199 
00201     QMutex mutaRatesMutex;
00202 
00204     unsigned int lastGenMutaRatesChange;
00205 };
00206 
00207 } // end namespace farsa
00208 
00209 #endif