Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

genetics.h

Go to the documentation of this file.
00001 
00025 /*
00026   Neljänneksi tulee Tuulen kirja. Tämä kirja ei ole yhteydessä minun
00027   Ichi-kouluuni vaan muihin strategian kouluihin. Tuulella tarkoitan
00028   strategian vanhoja perinteitä, tämän päivän perinteitä ja
00029   perhetraditioita. Siten voi selkeästi selittää maailman
00030   strategiat. Tämä on perinne. On vaikeaa tuntea itseäsi jos et tunne
00031   muita. Kaikilla Teillä on sivupolkuja. Jos opiskelet Tietä
00032   päivittäin ja henkesi joutuu harhaan, voit luulla noudattavasi hyvää
00033   Tietä vaikka objektiivisesti se ei ole oikea Tie. Jos seuraat oikeaa
00034   Tietä mutta harhaannut hieman, tulee siitä myöhemmin suuri
00035   poikkeama. Sinun täytyy tajuta tämä. Muut strategiat ovat tulleet
00036   otettaviksi pelkkänä miekkailuna, eikä se ole väärin että näin
00037   olisi. Minun strategiani hyöty, vaikkakin se sisältää miekkailun,
00038   sijaitsee erillisessä periaatteessa. Olen selittänyt mitä
00039   tavallisesti tarkoitetaan strategialla muissa kouluissa Perinteen
00040   (Tuulen) kirjassa.  */
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 // Externals
00052 class MagiC::DataOStream;
00053 class MagiC::OStream;
00054 class Individual;
00055 class Gene;
00056 
00057 // Internals
00058 class Genstruct;
00059 class Gentainer;
00060 
00061 enum printflags {PRINT_CLASSNAMES=0, PRINT_NAMES, PRINT_HIDDEN};
00062 
00064 //                                                                           //
00065 //            ----                       o       |   |                       //
00066 //           |      ___    _    ___   |     ___  |\ /|  ____                 //
00067 //           | --- /   ) |/ \  /   ) -+- | |   \ | V | (      ___            //
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 //       |   |                     o            ----                        //
00141 //       |\ /|        |   ___   |           _   |   )  ___   |   ___        //
00142 //       | V | |   | -+-  ___| -+- |  __  |/ \  |---   ___| -+- /   )       //
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     //FloatMutator* mFloatMutator;
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                             // Do copying quickly
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     // Virtual functions
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     // The world has become so virtual that it's a wonder we any more
00359     // know what is real. For example, this class is abstract.
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     // Implementations
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 //                ----                       o                               //
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     // Implementations
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 // I CALL AND I CALL!
00507 // But in vain -
00508 // no genstruct hears,
00509 // no genstruct cares.
00510 //
00511 // So vast is the genome,
00512 // so full of abstract gentainers
00513 // that my messages are lost,
00514 // in unoverridden virtual methods.
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     // Implementations
00557 
00559     virtual void                init        ();
00561     //virtual void              recombine   (const Genstruct& a, const Genstruct& b);
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 // |   |                      |      o     |  ----                              //
00582 // |\ /|        _         --  |            | |      ___    _               ___  //
00583 // | V |  __  |/ \   __  |  ) |  __  |  ---| | --- /   ) |/ \   __  |/|/| /   ) //
00584 // | | | /  \ |   | /  \ |--  | /  \ | (   | |   \ |---  |   | /  \ | | | |---  //
00585 // |   | \__/ |   | \__/ |    | \__/ |  ---| |___/  \__  |   | \__/ | | |  \__  //
00586 //                                                                              //
00588 
00591 class MonoploidGenome : public Genome {
00592   public:
00593 
00594                 MonoploidGenome     ();
00595 };
00596 
00597 
00598 
00600 //                                                                          //
00601 //      ___   o      |      o     |  ----                                   //
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     // Implementations
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 // -----                        ----                       o                 //
00642 //   |        ___    _    ____ |      ___    _    |   ___      _    ___      //
00643 //   |   |/\  ___| |/ \  (     | --- /   ) |/ \  -+-  ___| | |/ \  /   ) |/\ //
00644 //   |   |   (   | |   |  \__  |   \ |---  |   |  |  (   | | |   | |---  |   //
00645 //   |   |    \__| |   | ____) |___/  \__  |   |   \  \__| | |   |  \__  |   //
00646 //                                                                           //
00648 
00651 /*
00652 class TransGentainer {
00653   public:
00654   private:
00655     Gentainer*  gentainer;
00656 };
00657 */
00658 
00659 // And Void became to exist, together with the Light,
00660 // The Mother Ocean was born, together with her Brother,
00661 // filled with the primal froths of the holy Union,
00662 // their little children, the eternal existance.
00663 
00664 
00665 #endif
00666 

Generated on Thu Feb 10 20:12:00 2005 for NeHeP by doxygen1.2.18