ga/src/mutations/randomfloat.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/randomfloat.h"
00021 #include "core/genotype.h"
00022 #include "randomgenerator.h"
00023 #include "genotypes/doublegenotype.h"
00024 
00025 namespace farsa {
00026 
00027 RandomFloat::RandomFloat()
00028     : Mutation() {
00029 }
00030 
00031 RandomFloat::~RandomFloat() {
00032     //--- nothing to do
00033 }
00034 
00035 void RandomFloat::mutate( Genotype* gen ) {
00036     DoubleGenotype* rgen = dynamic_cast<DoubleGenotype*>( gen );
00037     Q_ASSERT_X( rgen != 0,
00038                 "RandomFloat::mutate",
00039                 "In order to use RandomFloat mutation the Genotypes must be a subclass of DoubleGenotype" );
00040     double min = rgen->minValue();
00041     double max = rgen->maxValue();
00042     for( unsigned int i=0; i<rgen->numGenes(); i++ ) {
00043         if ( globalRNG->getBool( mutationRate( rgen->geneToBitIndex(i) ) ) ) {
00044             rgen->set( i, globalRNG->getDouble( min, max ) );
00045         }
00046     }
00047 }
00048 
00049 void RandomFloat::describe( QString type ) {
00050     Mutation::describe( type );
00051     addTypeDescription( type, "Mutation operator", "Replace a real value with a random one with the probability specified by mutation_rate parameter" );
00052 }
00053 
00054 } // end namespace farsa