randombit.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/randombit.h"
21 #include "core/genotype.h"
22 #include "randomgenerator.h"
23 
24 namespace farsa {
25 
27  : Mutation() {
28 }
29 
31  //--- nothing to do
32 }
33 
35  Genotype& genref = *gen;
36  for( unsigned int i=0; i<genref.size(); i++ ) {
37  if ( globalRNG->getBool( mutationRate( i ) ) ) {
38  if ( globalRNG->getBool( 0.5 ) ) {
39  genref.set( i );
40  } else {
41  genref.unset( i );
42  }
43  }
44  }
45 }
46 
47 void RandomBit::describe( QString type ) {
48  Mutation::describe( type );
49  addTypeDescription( type, "Mutation operator", "Replace a bit with a random one with the probability specified by mutation_rate parameter" );
50 }
51 
52 } // end namespace farsa