realgenotype.cpp
1 /********************************************************************************
2  * FARSA Genetic Algorithm Library *
3  * Copyright (C) 2007-2009 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 "genotypes/realgenotype.h"
21 #include "randomgenerator.h"
22 #include <cmath>
23 #include "configurationparameters.h"
24 
25 namespace farsa {
26 
27 RealGenotype::RealGenotype( unsigned int numGenes, float min, float max )
28  : DoubleGenotype( numGenes, min, max, 32 ) {
29  genes = (float*)( data );
30 }
31 
33  //--- nothing to do
34 }
35 
37  : DoubleGenotype( genotype ) {
38  genes = (float*)( data );
39 }
40 
42  RealGenotype* genoreal = (RealGenotype*)(&genotype);
43  copyDataFrom( genoreal );
44  this->numgenes = genoreal->numgenes;
45  this->minv = genoreal->minv;
46  this->maxv = genoreal->maxv;
47  genes = (float*)( data );
48  return *this;
49 }
50 
51 void RealGenotype::configure( ConfigurationParameters& params, QString prefix ) {
52  numgenes = params.getValue( prefix + QString( "ngenes" ) ).toInt();
53  Q_ASSERT_X( numgenes > 0,
54  "RealGenotype::configure",
55  "The ngenes must be present in the config file and its value must be greater than zero" );
56  minv = params.getValue( prefix + QString( "minValue" ) ).toDouble();
57  maxv = params.getValue( prefix + QString( "maxValue" ) ).toDouble();
58  Q_ASSERT_X( minv < maxv,
59  "RealGenotype::configure",
60  "The minValue and maxValue must be different!! Check you config file" );
61  resize( numgenes*32 );
62  QString zipdata = params.getValue( prefix + QString( "data" ) );
63  if ( !zipdata.isNull() ) {
64  fromCompressedString( zipdata );
65  }
66  genes = (float*)( data );
67  QStringList valuesList = params.getValue( prefix + QString( "fitness" ) )
68  .split( QRegExp("\\s+"), QString::SkipEmptyParts );
69  fitnessv.resize(0);
70  foreach( QString avalue, valuesList ) {
71  fitnessv << avalue.toDouble();
72  }
73  rankv = params.getValue( prefix + QString( "rank" ) ).toDouble();
74  notesv = params.getValue( prefix + QString( "notes" ) );
75 }
76 
77 void RealGenotype::save( ConfigurationParameters& params, QString prefix ) {
78  params.createParameter( prefix, QString("type"), "RealGenotype" );
79  params.createParameter( prefix, QString("ngenes"), QString("%1").arg( numgenes ) );
80  params.createParameter( prefix, QString("minValue"), QString("%1").arg( minv ) );
81  params.createParameter( prefix, QString("maxValue"), QString("%1").arg( maxv ) );
82  QString fitstring;
83  foreach( double avalue, fitnessv ) {
84  fitstring.append( QString("%1 ").arg(avalue) );
85  }
86  params.createParameter( prefix, QString("fitness"), fitstring );
87  params.createParameter( prefix, QString("data"), toCompressedString() );
88  params.createParameter( prefix, QString("rank"), QString("%1").arg(rankv) );
89  params.createParameter( prefix, QString("notes"), notesv );
90 }
91 
92 void RealGenotype::describe( QString type ) {
93  Descriptor d = addTypeDescription( type, "RealGenotype encode a vector of real double values, each double correspond to a gene" );
94  d.describeInt( "ngenes" ).limits( 1, INT_MAX ).def( 8 ).props( IsMandatory ).help( "The number of real values represented by the Genotype" );
95  d.describeReal( "minValue" ).def( -5.0 ).help( "The minimum value representable" );
96  d.describeReal( "maxValue" ).def( +5.0 ).help( "The maximum value representable" );
97  d.describeReal( "fitness" ).props( IsList ).help( "The fitness of the Genotype", "The fitness of a Genotype support multi objective fitness; if you specify a vector of values they are considered different objectives of the fitness" );
98  d.describeString( "data" ).help( "The bits composing the Genotype stored in a compressed string" );
99  d.describeReal( "rank" ).def( 0 ).help( "The rank indicate who is more fitted that others and how much; the values are dependent on the kind of GeneticAlgo used" );
100 }
101 
102 double RealGenotype::at( unsigned int i ) {
103  Q_ASSERT_X( (i+1)*32 <= size(),
104  "RealGenotype::at",
105  "The value requested is beyond the dimension of this Genotype" );
106  return (double) ( genes[i] );
107 }
108 
109 void RealGenotype::set( unsigned int i, double value ) {
110  Q_ASSERT_X( (i+1)*32 <= size(),
111  "RealGenotype::set",
112  "The value to be set is beyond the dimension of this Genotype" );
113  genes[i] = (float) ( value );
114  return;
115 }
116 
118  for( unsigned int i=0; i<numgenes; i++ ) {
119  genes[i] = (float)( globalRNG->getDouble( minv, maxv ) );
120  }
121 }
122 
124  return ( new RealGenotype( *this ) );
125 }
126 
127 } // end namespace farsa