00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __VALUE_GENOME_H
00025 #define __VALUE_GENOME_H
00026
00027 #include "Genome.h"
00028 #include <valarray>
00029
00034 namespace Teem
00035 {
00038 class ValueGenome: public Genome
00039 {
00040 public:
00042 virtual double getDouble(double min, double max) = 0;
00044 virtual float getFloat(float min, float max) = 0;
00046 virtual int getSignedInt(unsigned precision, int min, int max) = 0;
00048 virtual unsigned getUnsignedInt(unsigned precision, unsigned min, unsigned max) = 0;
00049
00051 virtual void resetReading(void) = 0;
00052 };
00053
00056 class BitGenome: public ValueGenome
00057 {
00058 protected:
00060 std::valarray<unsigned> bits;
00062 size_t length;
00063
00065 Ishtar::Variable<double> mutationProbability;
00067 Ishtar::Variable<unsigned> defaultPrecision;
00068
00070 size_t readPos;
00071
00072 protected:
00074 bool checkBoundWord(size_t pos);
00075
00076 public:
00078 BitGenome(Ishtar::InputStream *stream);
00080 BitGenome(size_t size);
00081
00083 bool getBit(size_t pos);
00085 bool operator[](size_t pos) { return getBit(pos); }
00087 void setBit(size_t pos, bool bit);
00089 void seek(size_t pos);
00090
00092 size_t getLength() { return length; }
00093
00095 size_t getWordCount() { return bits.size(); }
00097 void setWord(size_t pos, unsigned val) { if (checkBoundWord(pos)) bits[pos] = val; }
00099 unsigned getWord(size_t pos) { if (checkBoundWord(pos)) return bits[pos]; }
00100
00102 unsigned getUnsignedIntRaw(unsigned precision)
00103 {
00104
00105 unsigned int val = 0;
00106 for (unsigned i=0; i<precision; i++)
00107 val = getBit(readPos++) ? (val + (1<<i)) : val;
00108 return val;
00109 }
00110
00112 template<typename T>
00113 T getValue(unsigned bitCount)
00114 {
00115 if (bitCount <= 32)
00116 {
00117 unsigned int val = 0;
00118 for (unsigned i=0; i<bitCount; i++)
00119 val = getBit(readPos++) ? (val + (1<<i)) : val;
00120 return (static_cast<T>(val) / static_cast<T>((1<<bitCount)-1));
00121 }
00122 else
00123 {
00124 std::cerr << "BitGenome::getValue<T> : deserializing value bigger than 32 bits not supported" << std::endl;
00125 return 0;
00126 }
00127 }
00128
00130 template<typename T>
00131 T getValueRange(unsigned bitCount, T min, T max)
00132 {
00133 return min + (getValue<T>(bitCount)*(max - min));
00134 }
00135
00137 virtual double getDouble(double min, double max)
00138 {
00139 return getValueRange<double>(defaultPrecision, min, max);
00140 }
00141
00143 virtual float getFloat(float min, float max)
00144 {
00145 return getValueRange<float>(defaultPrecision, min, max);
00146 }
00147
00149 virtual int getSignedInt(unsigned precision, int min, int max)
00150 {
00151 return static_cast<unsigned>(getValueRange<double>(precision, static_cast<double>(min), static_cast<double>(max)));
00152 }
00153
00155 virtual unsigned getUnsignedInt(unsigned precision, unsigned min, unsigned max)
00156 {
00157 return static_cast<unsigned>(getValueRange<double>(precision, static_cast<double>(min), static_cast<double>(max)));
00158 }
00159
00160 virtual void resetReading(void);
00161
00163 void setDefaultPrecision(unsigned p) { defaultPrecision = p; }
00164
00165 virtual void randomize(void);
00166 virtual void mutate(void);
00167 virtual void cross(const Genome *other);
00168 virtual Genome *clone(void);
00169
00170 virtual void save(Ishtar::OutputStream *stream);
00171 };
00172 }
00173
00174 #endif