mutation.h
1 /********************************************************************************
2  * FARSA Genetic Algorithm Library *
3  * Copyright (C) 2007-2008 Gianluca Massera <emmegian@yahoo.it> *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the Free Software *
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
18  ********************************************************************************/
19 
20 #ifndef MUTATION_H
21 #define MUTATION_H
22 
23 #include "gaconfig.h"
24 #include "parametersettable.h"
25 #include <QMutex>
26 
27 namespace farsa {
28 
29 class Genotype;
30 class Genome;
31 class GeneticAlgo;
32 
47 class FARSA_GA_API Mutation : public ParameterSettableWithConfigureFunction {
48 public:
50  Mutation();
51 
53  virtual ~Mutation();
54 
58  virtual void mutate( Genotype* ) = 0;
59 
71  void setMutationRate( double rate, int start = 0, int length = -1);
72 
77  void setMutationRate( double initialRate, double finalRate, double variation, int start = 0, int length = -1);
78 
86  double mutationRate( int bit );
87 
94  double initialMutationRate( int bit );
95 
102  double finalMutationRate( int bit );
103 
110  double variationMutationRate( int bit );
111 
113  void setGenome( const Genome* g ) {
114  genomev = g;
115  }
116 
118  const Genome* genome() {
119  return genomev;
120  }
121 
123  void setGA( GeneticAlgo* g ) {
124  ga = g;
125  }
126 
129  return ga;
130  }
131 
138  virtual void configure( ConfigurationParameters& params, QString prefix );
139 
146  virtual void save( ConfigurationParameters& params, QString prefix );
148  static void describe( QString type );
149 
150 protected:
152  const Genome* genomev;
153 
156 
157 private:
158  // Here we have all mutation rates stuffs, to force even child classes
159  // to use functions, which will keep everything in a consistent state
160 
162  void updateMutationRates();
163 
165  struct MutationRate {
167  MutationRate() :
168  initial(0.0),
169  final(0.0),
170  variation(0.0),
171  mutaRate(0.0)
172  {
173  }
174 
176  void rateForGeneration(unsigned int gen);
177 
179  double initial;
180 
182  double final;
183 
185  double variation;
186 
188  double mutaRate;
189  };
190 
195  const MutationRate& getMutationRateForBit( int bit ) const;
196 
198  QMap<int, MutationRate> mutaRates;
199 
201  QMutex mutaRatesMutex;
202 
204  unsigned int lastGenMutaRatesChange;
205 };
206 
207 } // end namespace farsa
208 
209 #endif