genotype.h
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 #ifndef GENOTYPE_H
21 #define GENOTYPE_H
22 
23 #include "gaconfig.h"
24 #include "parametersettable.h"
25 #include <QString>
26 #include <QVector>
27 
28 namespace farsa {
29 
38 class FARSA_GA_API Genotype : public ParameterSettableWithConfigureFunction {
39 public:
43  Genotype( unsigned int size = 0 );
45  virtual ~Genotype();
47  Genotype( QString, bool compressed=false );
49  Genotype( const Genotype& genotype );
55  virtual Genotype& operator=( const Genotype& genotype );
57  void assign( const Genotype* genotype );
59  virtual Genotype* clone() const;
61  unsigned int size() const;
65  void setFitness( double value );
69  double fitness() const;
76  void setObjective( int i, double value );
80  double objective( int i ) const;
84  int numOfObjectives() const;
93  bool dominatedBy( const Genotype* genotype );
103  void setRank( double rank );
105  double rank() const;
107  bool bit( unsigned int i ) const;
109  void set( unsigned int i );
111  void unset( unsigned int i );
113  void toggle( unsigned int i );
115  QString notes() const;
117  void setNotes( QString notes );
119  int hammingDistance( const Genotype* );
121  virtual void randomize();
123  QString toString() const;
125  void fromString( QString );
127  QString toCompressedString() const;
131  bool fromCompressedString( QString str );
144  unsigned int extractUInt( unsigned int startPos, unsigned int stopPos );
151  void insertUInt( unsigned int value, unsigned int startPos, unsigned int stopPos );
152 
157  virtual unsigned int geneToBitIndex( unsigned int gene ) const;
162  virtual unsigned int bitToGeneIndex( unsigned int bit ) const;
163 
170  virtual void configure( ConfigurationParameters& params, QString prefix );
177  virtual void save( ConfigurationParameters& params, QString prefix );
179  static void describe( QString type );
180 
182  static bool rankGreaterThanComparator( const Genotype *g1, const Genotype *g2 ) {
183  return (g1->rank() > g2->rank());
184  };
187  public:
191  bool operator()( const Genotype* g1, const Genotype* g2 ) {
192  return g1->objective( objectiveToCompare ) > g2->objective( objectiveToCompare );
193  };
194  int objectiveToCompare;
195  };
196 
197 protected:
199  unsigned char* data;
201  unsigned int sizev;
203  unsigned int allocated;
205  QVector<double> fitnessv;
207  double rankv;
209  QString notesv;
211  void copyDataFrom( Genotype* source );
212 
218  void resize( unsigned int newsize );
219 };
220 
221 } // end namespace farsa
222 
223 #endif