00001 #ifndef GLBASEORGANISM_H_
00002 #define GLBASEORGANISM_H_
00003 #include <ostream>
00004 #include <set>
00005 #include <utility>
00006 #include <vector>
00007
00008 #include <NLInterfaceOutput.h>
00009
00010 #include "GLBaseEvaluator.h"
00011 #include "GLBaseFitness.h"
00012
00013 using namespace std;
00014
00015
00019 class GLBaseOrganism: public nlutils::NLInterfaceOutput
00020 {
00021 public:
00025 GLBaseOrganism();
00026
00031 GLBaseOrganism(const GLBaseOrganism& source);
00032
00036 virtual ~GLBaseOrganism();
00037
00046 virtual void assertCompatibility(const GLBaseOrganism* source,
00047 const string& message) const;
00048
00056 virtual void copyGeneticData(const GLBaseOrganism *source)
00057 { resetFitness(); };
00058
00067 virtual const GLBaseFitness* evaluate(GLBaseEvaluator *evaluator);
00068
00073 const GLBaseFitness* getFitness() const
00074 { return m_fitness; }
00075
00080 virtual int getSize() const = 0;
00081
00086 int getTag() const
00087 { return m_tag; }
00088
00093 bool invertMemoryLock()
00094 { return m_locked_in_memory = !(m_locked_in_memory); }
00104 virtual bool isEqual(const GLBaseOrganism *organism,
00105 bool soft = true) const = 0;
00106
00113 bool isEvaluated() const
00114 { return m_is_evaluated; }
00115
00125 virtual bool isLess(const GLBaseOrganism *organism,
00126 bool soft = true) const = 0;
00127
00138 bool isLockedInMemory() const
00139 { return m_locked_in_memory; }
00140
00145 void lockInMemory()
00146 { m_locked_in_memory = true; }
00147
00158 virtual GLBaseOrganism* makeClone(bool soft = true) const = 0;
00159
00164 virtual void resetFitness();
00165
00172 virtual void setFitnessValue(const GLBaseFitness* fitness);
00173
00179 void setMemoryLock(bool new_lock)
00180 { m_locked_in_memory = new_lock; }
00181
00187 void setTag(int new_tag)
00188 { m_tag = new_tag; }
00189
00193 void unlockInMemory()
00194 { m_locked_in_memory = false; }
00195
00196 public:
00197
00201 class CompareLess
00202 {
00203 public:
00208 CompareLess(bool soft_flag = true):tm_soft(soft_flag) {}
00213 bool operator()(const GLBaseOrganism* left,
00214 const GLBaseOrganism* right) const
00215 { return left->isLess(right, tm_soft); }
00216 private:
00217 bool tm_soft;
00218 };
00219
00223 class CompareEqual
00224 {
00225 public:
00227 typedef const GLBaseOrganism* first_argument_type;
00229 typedef const GLBaseOrganism* second_argument_type;
00231 typedef bool result_type;
00236 CompareEqual(bool soft_flag = true):tm_soft(soft_flag) {}
00241 result_type operator()(first_argument_type left,
00242 first_argument_type right) const
00243 { return left->isEqual(right, tm_soft); }
00244 private:
00245 bool tm_soft;
00246 };
00247
00252 class CompareFitnessLess
00253 {
00254 public:
00259 bool operator()(const GLBaseOrganism* left,
00260 const GLBaseOrganism* right) const;
00261 };
00262
00266 class Cloner
00267 {
00268 public:
00275 Cloner(bool soft_clone = true): tm_soft_clone(soft_clone){}
00276
00282 GLBaseOrganism* operator()(const GLBaseOrganism* organism)
00283 { return organism->makeClone(tm_soft_clone); }
00284 private:
00285 bool tm_soft_clone;
00286 };
00287
00288 private:
00289 bool m_is_evaluated;
00290
00291 const GLBaseFitness* m_fitness;
00292
00293 int m_tag;
00294
00295
00296 bool m_locked_in_memory;
00297
00298 };
00299
00304 inline bool operator<(const GLBaseOrganism& left,
00305 const GLBaseOrganism& right)
00306 { return left.isLess(&right); }
00307
00312 inline bool operator==(const GLBaseOrganism& left,
00313 const GLBaseOrganism& right)
00314 { return left.isEqual(&right); }
00315
00316
00320 typedef set<GLBaseOrganism*,
00321 GLBaseOrganism::CompareLess>
00322 TGLSetOfOrganisms;
00323
00327 typedef set<const GLBaseOrganism*,
00328 GLBaseOrganism::CompareLess>
00329 TGLSetOfOrganismsConst;
00330
00334 typedef vector<GLBaseOrganism*> TGLVectorOfOrganisms;
00335
00339 typedef vector<const GLBaseOrganism*> TGLVectorOfOrganismsConst;
00340
00341 #endif