00001 #ifndef GLORGANISMVECTOR_H_
00002 #define GLORGANISMVECTOR_H_
00003
00004 #include <limits.h>
00005 #include <vector>
00006
00007 #include <NLstl_utils.h>
00008
00009 #include "GLBaseOrganism.h"
00010 #include "GLConstants.h"
00011 #include "GLInterfaceExchangeableGenes.h"
00012 #include "GLInterfaceMoveGene.h"
00013 #include "GLInterfaceShuffleGenes.h"
00014 #include "GLInterfaceSwapSection.h"
00015 #include "GLInterfaceSwapTails.h"
00016
00017 using namespace std;
00018 #pragma pack(push,1)
00019
00023 template <class T>
00024 class GLOrganismVector : public GLBaseOrganism,
00025 public GLInterfaceExchangeableGenes,
00026 public GLInterfaceMoveGene,
00027 public GLInterfaceShuffleGenes,
00028 public GLInterfaceSwapSection,
00029 public GLInterfaceSwapTails
00030 {
00031 public:
00043 GLOrganismVector(unsigned init_size,
00044 GLConstants::TSizeLimits size_property =
00045 GLConstants::FIXED,
00046 unsigned max_size = UINT_MAX);
00057 GLOrganismVector(const vector<T>& chromosome,
00058 GLConstants::TSizeLimits size_property =
00059 GLConstants::FIXED,
00060 unsigned max_size = UINT_MAX);
00061
00066 GLOrganismVector(const GLOrganismVector<T>& source);
00067
00071 virtual ~GLOrganismVector();
00072
00078 const T& operator[](int i) const
00079 { return getGene(i); }
00080
00086 T& operator[](int i)
00087 { return getGene(i); }
00088
00097 virtual void assertCompatibility(const GLBaseOrganism* source,
00098 const string& message) const;
00099
00105 bool checkBounds(const T& gene)
00106 {return (m_property_bounds == GLConstants::UNBOUNDED_GENES) ||
00107 ((m_gene_max >= gene) && (m_gene_min <= gene)); }
00108
00114 virtual bool checkIndex(int index) const
00115 {return (index >= 0) && (index < getSize()); };
00116
00124 virtual void copyGeneticData(const GLBaseOrganism *source);
00125
00131 virtual void exchangeGenes(int first, int second);
00132
00138 const vector<T>& getChromosome() const
00139 { return m_storage; }
00140
00146 T& getGene(int index);
00147
00153 const T& getGene(int index) const;
00154
00162 pair<const T, const T> getGeneBounds() const
00163 { return pair<const T, const T>(m_gene_min, m_gene_max); }
00164
00169 GLConstants::TSizeLimits getPropertySize() const
00170 {return m_property_size;}
00171
00176 GLConstants::TGeneBounds getPropertyBounds() const
00177 { return m_property_bounds; }
00178
00183 virtual int getSize() const;
00184
00193 virtual bool isEqual(const GLBaseOrganism *organism,
00194 bool soft = true) const;
00195
00204 virtual bool isLess(const GLBaseOrganism *organism,
00205 bool soft = true) const;
00206
00217 virtual GLBaseOrganism* makeClone(bool soft = true) const;
00218
00224 virtual void moveGene(int old_position, int new_position);
00225
00229 void removeGeneBounds()
00230 { m_property_bounds = GLConstants::UNBOUNDED_GENES; }
00231
00238 void setChromosome(const vector<T>& chromosome);
00239
00244 void setPropertySize(GLConstants::TSizeLimits size_limits)
00245 { m_property_size = size_limits; }
00246
00253 void setGene(int index, const T& gene);
00254
00263 bool setGeneBounds(const T& gene_min,
00264 const T& gene_max,
00265 bool correct_organism = true);
00266
00273 virtual void shuffleGenes(const vector<int>& gene_numbers);
00274
00291 virtual void swapSections(GLBaseOrganism* anotherOrganism,
00292 int leftPos, int rightPos);
00293
00299 virtual void swapTails(GLBaseOrganism* anotherOrganism, int i);
00300
00305 virtual ostream& toStream(ostream& out) const;
00306
00312 virtual ostream& toStreamDebug(ostream& out) const;
00313
00314 private:
00320 virtual void setSizeData(unsigned init_size,
00321 unsigned max_size);
00322 private:
00323 T m_gene_max;
00324 T m_gene_min;
00325
00326 GLConstants::TGeneBounds m_property_bounds;
00327
00328
00329 GLConstants::TSizeLimits m_property_size;
00330 unsigned m_max_size;
00331 protected:
00333 vector<T> m_storage;
00334 };
00335 #pragma pack(pop)
00336 #endif