00001
00010
00011
00012
00013
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
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
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
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
00177
00178 public:
00184 virtual void mutate(double prob)
00185 {
00186
00187
00188 }
00189
00202 virtual void crossover(int pos, Chromosome<T>* g)
00203 {
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220 std::list<char> gbits_tmp;
00221
00222
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