ga/src/mutations/gaussianfloat.cpp

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 #include "mutations/gaussianfloat.h"
00021 #include "core/genotype.h"
00022 #include "randomgenerator.h"
00023 #include "genotypes/doublegenotype.h"
00024 #include "configurationparameters.h"
00025 
00026 namespace farsa {
00027 
00028 GaussianFloat::GaussianFloat()
00029     : Mutation() {
00030     varg = 0.1;
00031 }
00032 
00033 GaussianFloat::~GaussianFloat() {
00034     //--- nothing to do
00035 }
00036 
00037 void GaussianFloat::mutate( Genotype* gen ) {
00038     DoubleGenotype* rgen = dynamic_cast<DoubleGenotype*>( gen );
00039     Q_ASSERT_X( rgen != 0,
00040                 "GaussianFloat::mutate",
00041                 "In order to use GaussianFloat mutation the Genotypes must be a subclass of DoubleGenotype" );
00042     double min = rgen->minValue();
00043     double max = rgen->maxValue();
00044     for( unsigned int i=0; i<rgen->numGenes(); i++ ) {
00045         if ( globalRNG->getBool( mutationRate( rgen->geneToBitIndex(i) ) ) ) {
00046             double newvalue = globalRNG->getGaussian( varg, rgen->at(i) );
00047             if ( newvalue > max ) newvalue = max;
00048             if ( newvalue < min ) newvalue = min;
00049             rgen->set( i, newvalue );
00050         }
00051     }
00052 }
00053 
00054 void GaussianFloat::configure( ConfigurationParameters& params, QString prefix ) {
00055     Mutation::configure( params, prefix );
00056     double nvarg = params.getValue( prefix + QString( "variance" ) ).toDouble();
00057     if ( nvarg <= 0 ) {
00058         qWarning() << "GaussianFloat configuration: variance parameter must be positive";
00059     } else {
00060         varg = nvarg;
00061     }
00062 }
00063 
00064 void GaussianFloat::describe( QString type ) {
00065     Mutation::describe( type );
00066     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" );
00067     d.describeReal( "variance" ).limits( 0, 1 ).def( 0.2 ).help( "The variance of the Gaussian distribution" );
00068 }
00069 
00070 void GaussianFloat::save( ConfigurationParameters& params, QString prefix ) {
00071     Mutation::save( params, prefix );
00072     params.createParameter( prefix, QString("variance"), QString("%1").arg(varg) );
00073 }
00074 
00075 } // end namespace farsa