Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

Chromosome.h

Go to the documentation of this file.
00001 
00010 /*
00011  * GALib v2.0- A simple C++ library for Genetic Algorithms
00012  * Arthur van Dam <dam@math.uu.nl>
00013  * $Id: Chromosome.h 441 2007-11-15 16:55:50Z adam $
00014  */
00015 #ifndef __GAL_CHROMOSOME_H
00016 #define __GAL_CHROMOSOME_H
00017 
00018 #include <iostream>
00019 #include <string>
00020 #include <list>
00021 #include <time.h>
00022 #include <stdlib.h>
00023 #include "Util.h"
00024 
00025 namespace gal {
00026 
00037     template <class T> class Chromosome {
00038     private:
00039         int _length;
00040 
00041     protected:
00042         std::list<char> _bits; 
00044     private:
00045         bool _isElite;
00046         static char _getRandomBit()
00047         {
00048             return (char)(random() > RANDOM_MAX/2);
00049         }
00050 
00051     /* 
00052      * Constructors
00053      */
00054     public:
00058         Chromosome() : _isElite(false) {}
00059 
00065         Chromosome(int length) : _isElite(false)
00066         {
00067             _length = length;
00068             for (int i = 0; i < length; ++i) {
00069                 _bits.push_back(_getRandomBit());
00070             }
00071         }
00072 
00073     /* 
00074      * Accessing variables
00075      */
00076     public:
00080         int length()
00081         {
00082             return _length;
00083         }
00084 
00089         bool isElite()
00090         {
00091             return _isElite;
00092         }
00093 
00100         void setElite(bool isElite = true)
00101         {
00102             _isElite = isElite;
00103         }
00104 
00108         void setLength(int length)
00109         {
00110             _length=length;
00111         }
00112 
00117         virtual void setBits(char *c)
00118         {
00119             std::list<char>::iterator it;
00120             int i;
00121             
00122             i = 0;
00123             for (it = _bits.begin(); it != _bits.end(); it++) {
00124                 *it = c[i]-'0';
00125                 i++;
00126             }
00127         }
00128 
00129     /*
00130      * Representation
00131      */
00132     public:
00140         virtual std::list<char> const getInterpretedBitstring()
00141         {
00142             return _bits;
00143         }
00144 
00149         virtual T const getValue() {}
00150 
00155         virtual std::string const getValueText() {}
00156 
00160         virtual std::string const getBitstringText()
00161         {
00162             return bitstos(_bits);
00163         }
00164 
00169         virtual std::string const getInterpretedBitstringText()
00170         {
00171             return bitstos(getInterpretedBitstring());
00172         }
00173 
00174 
00175     /* 
00176      * Genetic manipulation
00177      */
00178     public:
00184         virtual void mutate(double prob)
00185         {
00186 
00187             /*** Perform mutation on the bitstring of this chromosome ***/
00188         }
00189 
00202         virtual void crossover(int pos, Chromosome<T>* g)
00203         {
00204 
00205             /*** The idea of this crossover implementation is as follows:
00206                  this current Chromosome object is asked to `crossover'
00207                  himself with another Chromosome g.
00208                  It needs to cut its own bitstring and that of g into two
00209                  pieces, and exchange the tail parts of the two bitstrings.
00210                  Since g is a pointer to the other Chromosome, you're able
00211                  to modify the bitstring contents of that Chromosome.
00212 
00213                  You could create a loop over both bitstrings, and copy
00214                  the bits to the correct new bitstring.
00215                  A faster, but more technical approach uses the splice
00216                  function of the standard C++ vector class. See the library
00217                  docs for details.
00218             ***/
00219 
00220             std::list<char> gbits_tmp; // fill the bits for g in this temp. variable.
00221 
00222             /*** Start a loop to exchange all necessary bits ***/
00223         }
00224 
00230         virtual void crossover(Chromosome<T>* g)
00231         {
00232             int pos;
00233             pos = random_int(_length);
00234             crossover(pos, g);
00235         }
00236 
00237     };
00238 }
00239 #endif /* __GAL_CHROMOSOME_H */

Generated on Tue Oct 20 12:59:20 2009 for LabSci GALib optimisation by genetic algorithms, student version by  doxygen 1.3.9.1