00001
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #ifndef __GENETICS_H__
00043 #define __GENETICS_H__
00044
00045 #include <magic/mobject.h>
00046 #include <magic/mstring.h>
00047 #include <magic/mmap.h>
00048
00049 using namespace MagiC;
00050
00051
00052 class MagiC::DataOStream;
00053 class MagiC::OStream;
00054 class Individual;
00055 class Gene;
00056
00057
00058 class Genstruct;
00059 class Gentainer;
00060
00061 enum printflags {PRINT_CLASSNAMES=0, PRINT_NAMES, PRINT_HIDDEN};
00062
00064
00065
00066
00067
00068
00069
00070
00072
00073 typedef String GeneticID;
00074
00077 class GeneticMsg {
00078 public:
00079
00089 GeneticMsg (const GeneticID& rcvr, Individual& ind) : receiver (rcvr), host (ind) {
00090 previous = NULL;
00091 }
00092
00106 virtual void pushCaller (Genstruct* clr) {previous = clr;}
00107
00109 Genstruct* getCaller () const {return previous;}
00110
00112 const GeneticID& receiver;
00113
00120 Individual& host;
00121
00122 private:
00126 Genstruct* previous;
00127
00128 GeneticMsg (const GeneticMsg& orig) :
00129 receiver (*(GeneticID*)NULL), host (*(Individual*)NULL) {FORBIDDEN}
00130
00131 GeneticMsg& operator= (const GeneticMsg& orig) {FORBIDDEN; return *this;}
00132 };
00133
00138
00139
00140
00141
00142
00143
00144
00145
00147
00151 class MutationRate {
00152 double mBinaryRate;
00153 double mIntRate;
00154 double mFloatRate;
00155 double mFloatVariance;
00156 bool mOneBitMutation;
00157 bool mAutoAdaptation;
00158
00159 public:
00160 MutationRate () {
00161 mBinaryRate=mIntRate=mFloatRate=mFloatVariance=0.0;
00162 mOneBitMutation=false;
00163 mAutoAdaptation=false;
00164 }
00165
00166 MutationRate (const MutationRate& o) {
00167
00168 memcpy (this, &o, sizeof (MutationRate));
00169 }
00170
00174 MutationRate (const MutationRate& o, const MutationRate& m) {
00175 mBinaryRate = o.mBinaryRate * m.mBinaryRate;
00176 mIntRate = o.mIntRate * m.mIntRate;
00177 mFloatRate = o.mFloatRate * m.mFloatRate;
00178 mFloatVariance = o.mFloatVariance * m.mFloatVariance;
00179 mOneBitMutation = o.mOneBitMutation || m.mOneBitMutation;
00180 mAutoAdaptation = o.mAutoAdaptation || m.mAutoAdaptation;
00181 }
00182
00185 MutationRate (const Gentainer& g);
00186
00188 static void addGenesTo (Gentainer& g, const StringMap& params);
00189
00191 double binaryRate () const {return mBinaryRate;}
00193 double intRate () const {return mIntRate;}
00195 double doubleRate () const {return mFloatRate;}
00197 double doubleVariance () const {return mFloatVariance;}
00198
00200 void binaryRate (double x) {mBinaryRate=x;}
00202 void intRate (double x) {mIntRate=x;}
00204 void doubleRate (double x) {mFloatRate=x;}
00206 void doubleVariance (double x) {mFloatVariance=x;}
00207
00210 void oneBitMutation (bool mt) {mOneBitMutation = mt;}
00211
00215 bool oneBitMutation () const {return mOneBitMutation;}
00216
00220 void autoAdaptation (bool aa) {mAutoAdaptation=aa;}
00221
00223 bool autoAdaptation () const {return mAutoAdaptation;}
00224
00225 private:
00226 MutationRate& operator= (const MutationRate& orig) {FORBIDDEN; return *this;}
00227 };
00228
00229
00230
00232
00233
00234
00235
00236
00237
00238
00240
00241 EXCEPTIONCLASS (invalid_gene_name);
00242
00246 class Genstruct : public Object {
00247 decl_dynamic (Genstruct);
00248 public:
00249
00253 Genstruct (const GeneticID& name=NULL);
00254 Genstruct (const Genstruct& other);
00255
00256
00261 const Genstruct& operator[] (const GeneticID& name) const {
00262 return *getGene (name);
00263 }
00264
00268 Genstruct& operator[] (const GeneticID& name) {
00269 return const_cast<Genstruct&>(const_cast<const Genstruct*>(this)->operator[] (name));
00270 }
00271
00273 const GeneticID& getID () const {return id;}
00274
00278 int length () const;
00279
00281
00282
00285 virtual void init () {MUST_OVERLOAD}
00286
00293 virtual const Genstruct* getGene (const GeneticID& name) const {MUST_OVERLOAD; return NULL;}
00294
00300 virtual bool execute (const GeneticMsg& msg) const {return false;}
00301
00306 virtual void addPrivateGenes (Gentainer& g) {MUST_OVERLOAD}
00313 virtual void addPrivateGenes (Gentainer& g, const StringMap& params) {MUST_OVERLOAD}
00314
00322 virtual bool pointMutate (const MutationRate& r) {MUST_OVERLOAD; return false;}
00323
00328 virtual void recombine (const Genstruct& a, const Genstruct& b) {copy(a);}
00329
00340 virtual double equality (const Genstruct& other) const {MUST_OVERLOAD; return 0.0;}
00341
00345 virtual Genstruct* replicate () const {MUST_OVERLOAD; return NULL;}
00346
00351 virtual void copy (const Genstruct& other) {MUST_OVERLOAD}
00352
00356 virtual void print (TextOStream& out) const;
00357
00358
00359
00360
00364 virtual DataOStream& operator>> (DataOStream& out) const;
00365
00369 Genstruct& hide (bool h=true) {mHidden=h; return *this;}
00370
00372 bool isHidden () const {return mHidden;}
00373
00376 virtual int calc_len () const {MUST_OVERLOAD; return 0;}
00377
00378
00379
00381 virtual void check () const;
00382
00383 protected:
00384
00387 void copyGenstr (const Genstruct& other) {
00388 id = other.id;
00389 size = other.size;
00390 mHidden = other.mHidden;
00391 }
00392
00396 GeneticID id;
00397
00398 private:
00402 mutable int size;
00403
00406 bool mHidden;
00407
00408 Genstruct operator= (const Genstruct& orig) {FORBIDDEN; return *this;}
00409
00411 friend class Gentainer;
00412 };
00413
00414
00415
00417
00418
00419
00420
00421
00422
00423
00425
00430 class Gentainer : public Genstruct {
00431 decl_dynamic (Gentainer);
00432 public:
00433
00434 Gentainer (const GeneticID& name = NULL);
00435
00436 Gentainer (const Gentainer& orig);
00437
00439 void add (Genstruct* newstruct);
00440
00444 const Genstruct& operator[] (const GeneticID& name) const {
00445 return *getGene (name);
00446 }
00452 Genstruct& operator[] (const GeneticID& name) {
00453 return const_cast<Genstruct&>(const_cast<const Gentainer*>(this)->operator[](name));
00454 }
00455
00457 const Genstruct& operator[] (int i) const {return substructs[i];}
00459 Genstruct& operator[] (int i) {return substructs[i];}
00460
00462 int size () const {return substructs.size();}
00463
00468 void selfadjust (bool val=true) {self_adjust=val;}
00469
00471 void recombRate (double rate) {mRecombRate=rate;}
00472
00473
00474
00475 virtual void init ();
00476 virtual void addPrivateGenes (Gentainer& g, const StringMap& params);
00477 virtual const Genstruct* getGene (const GeneticID& name) const;
00478 virtual bool pointMutate (const MutationRate& k);
00479 virtual void recombine (const Genstruct& a, const Genstruct& b);
00480 virtual double equality (const Genstruct& other) const;
00481 virtual Genstruct* replicate () const;
00482 virtual void copy (const Genstruct& other);
00483 virtual void print (TextOStream& out) const;
00484 virtual bool execute (const GeneticMsg& msg) const;
00485
00486 virtual DataOStream& operator>> (DataOStream& out) const;
00487 virtual void check () const;
00488
00489 protected:
00490 virtual int calc_len () const;
00491
00493 Array<Genstruct> substructs;
00494
00496 bool self_adjust;
00497
00499 double mRecombRate;
00500
00501 private:
00502 Gentainer& operator= (const Gentainer& orig) {FORBIDDEN; return *this;}
00503 };
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00519
00520
00521
00522
00523
00524
00525
00527
00533 class Genome : public Gentainer {
00534 decl_dynamic (Genome);
00535 protected:
00539 int kings;
00540
00541 public:
00542
00543 Genome ();
00544 Genome (const Genome& other);
00545
00549 void addking () {kings++;}
00550
00554 int getkings () const {return kings;}
00555
00556
00557
00559 virtual void init ();
00561
00563 virtual void print (TextOStream& out) const;
00565 virtual void addPrivateGenes (Gentainer& g, const StringMap& pars);
00566
00568 virtual DataOStream& operator>> (DataOStream& out) const;
00570 virtual void check () const;
00571
00572 private:
00573 Genstruct& operator= (const Genstruct& orig) {FORBIDDEN; return *this;}
00574 };
00575
00576
00577
00578
00580
00581
00582
00583
00584
00585
00586
00588
00591 class MonoploidGenome : public Genome {
00592 public:
00593
00594 MonoploidGenome ();
00595 };
00596
00597
00598
00600
00601
00602
00603
00604
00605
00606
00608
00611 class DiploidGenome : public Genome {
00612 MonoploidGenome genome_a, genome_b;
00613
00614 public:
00615
00616 DiploidGenome ();
00617
00618
00619
00620 virtual void init ();
00621 virtual void addPrivateGenes (Gentainer& g, const StringMap& params);
00622 virtual int length () const;
00623 virtual const Gene& operator[] (const char* name) const throw (invalid_gene_name);
00624 virtual bool pointMutate (const MutationRate& k);
00625 virtual void recombine (const Genstruct& a, const Genstruct& b);
00626 virtual double equality (const Genstruct& other) const;
00627 virtual Genstruct* replicate () const;
00628 virtual void copy (const Genstruct& other);
00629 virtual void print (TextOStream& out) const;
00630 protected:
00631 virtual int calc_len () const;
00632
00633 private:
00634 DiploidGenome& operator= (const DiploidGenome& orig) {FORBIDDEN; return *this;}
00635 };
00636
00637
00638
00640
00641
00642
00643
00644
00645
00646
00648
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665 #endif
00666