gaussianfloat.cpp
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 #include "mutations/gaussianfloat.h"
21 #include "core/genotype.h"
22 #include "randomgenerator.h"
23 #include "genotypes/doublegenotype.h"
24 #include "configurationparameters.h"
25 
26 namespace farsa {
27 
29  : Mutation() {
30  varg = 0.1;
31 }
32 
34  //--- nothing to do
35 }
36 
38  DoubleGenotype* rgen = dynamic_cast<DoubleGenotype*>( gen );
39  Q_ASSERT_X( rgen != 0,
40  "GaussianFloat::mutate",
41  "In order to use GaussianFloat mutation the Genotypes must be a subclass of DoubleGenotype" );
42  double min = rgen->minValue();
43  double max = rgen->maxValue();
44  for( unsigned int i=0; i<rgen->numGenes(); i++ ) {
45  if ( globalRNG->getBool( mutationRate( rgen->geneToBitIndex(i) ) ) ) {
46  double newvalue = globalRNG->getGaussian( varg, rgen->at(i) );
47  if ( newvalue > max ) newvalue = max;
48  if ( newvalue < min ) newvalue = min;
49  rgen->set( i, newvalue );
50  }
51  }
52 }
53 
54 void GaussianFloat::configure( ConfigurationParameters& params, QString prefix ) {
55  Mutation::configure( params, prefix );
56  double nvarg = params.getValue( prefix + QString( "variance" ) ).toDouble();
57  if ( nvarg <= 0 ) {
58  qWarning() << "GaussianFloat configuration: variance parameter must be positive";
59  } else {
60  varg = nvarg;
61  }
62 }
63 
64 void GaussianFloat::describe( QString type ) {
65  Mutation::describe( type );
66  Descriptor d = addTypeDescription( type, "Mutate a value accordlying a Gaussian distribution", "The mutated value is extracted stochastically from a Gaussian distribution centered on the current value of the gene with a variance specified by the variance parameter" );
67  d.describeReal( "variance" ).limits( 0, 1 ).def( 0.2 ).help( "The variance of the Gaussian distribution" );
68 }
69 
70 void GaussianFloat::save( ConfigurationParameters& params, QString prefix ) {
71  Mutation::save( params, prefix );
72  params.createParameter( prefix, QString("variance"), QString("%1").arg(varg) );
73 }
74 
75 } // end namespace farsa