ga/include/core/genotype.h

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 #ifndef GENOTYPE_H
00021 #define GENOTYPE_H
00022 
00023 #include "gaconfig.h"
00024 #include "parametersettable.h"
00025 #include <QString>
00026 #include <QVector>
00027 
00028 namespace farsa {
00029 
00038 class FARSA_GA_API Genotype : public ParameterSettableWithConfigureFunction {
00039 public:
00043     Genotype( unsigned int size = 0 );
00045     virtual ~Genotype();
00047     Genotype( QString, bool compressed=false );
00049     Genotype( const Genotype& genotype );
00055     virtual Genotype& operator=( const Genotype& genotype );
00057     void assign( const Genotype* genotype );
00059     virtual Genotype* clone() const;
00061     unsigned int size() const;
00065     void setFitness( double value );
00069     double fitness() const;
00076     void setObjective( int i, double value );
00080     double objective( int i ) const;
00084     int numOfObjectives() const;
00093     bool dominatedBy( const Genotype* genotype );
00103     void setRank( double rank );
00105     double rank() const;
00107     bool bit( unsigned int i ) const;
00109     void set( unsigned int i );
00111     void unset( unsigned int i );
00113     void toggle( unsigned int i );
00115     QString notes() const;
00117     void setNotes( QString notes );
00119     int hammingDistance( const Genotype* );
00121     virtual void randomize();
00123     QString toString() const;
00125     void fromString( QString );
00127     QString toCompressedString() const;
00131     bool fromCompressedString( QString str );
00144     unsigned int extractUInt( unsigned int startPos, unsigned int stopPos );
00151     void insertUInt( unsigned int value, unsigned int startPos, unsigned int stopPos );
00152 
00157     virtual unsigned int geneToBitIndex( unsigned int gene ) const;
00162     virtual unsigned int bitToGeneIndex( unsigned int bit ) const;
00163 
00170     virtual void configure( ConfigurationParameters& params, QString prefix );
00177     virtual void save( ConfigurationParameters& params, QString prefix );
00179     static void describe( QString type );
00180     
00182     static bool rankGreaterThanComparator( const Genotype *g1, const Genotype *g2 ) {
00183         return (g1->rank() > g2->rank());
00184     };
00186     class nObjectiveGreaterThanComparator {
00187     public:
00191         bool operator()( const Genotype* g1, const Genotype* g2 ) {
00192             return g1->objective( objectiveToCompare ) > g2->objective( objectiveToCompare );
00193         };
00194         int objectiveToCompare; 
00195     };
00196 
00197 protected:
00199     unsigned char* data;
00201     unsigned int sizev;
00203     unsigned int allocated;
00205     QVector<double> fitnessv;
00207     double rankv;
00209     QString notesv;
00211     void copyDataFrom( Genotype* source );
00212 
00218     void resize( unsigned int newsize );
00219 };
00220 
00221 } // end namespace farsa
00222 
00223 #endif