Main Page | Modules | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ValueGenome.h

Go to the documentation of this file.
00001 /*
00002 Teem - an open source evolutionary software framework
00003 http://lis.epfl.ch/resources/Teem
00004 Stephane Magnenat <stephane.magnenat@epfl.ch>,
00005 Antoine Beyeler <antoine.beyeler@epfl.ch>,
00006 and other contributors. See AUTHORS for details
00007 Laboratory of Intelligent Systems, EPFL, Lausanne
00008 
00009 This program is free software; you can redistribute it and/or modify
00010 it under the terms of the GNU General Public License as published by
00011 the Free Software Foundation; either version 2 of the License, or
00012 (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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

Generated on Mon Oct 24 17:38:25 2005 for Teem by  doxygen 1.4.2