00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
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 }