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

genes.h

Go to the documentation of this file.
00001 
00025 #ifndef __GENES_H__
00026 #define __GENES_H__
00027 
00028 #include <magic/mobject.h>
00029 #include <magic/mdatastream.h>
00030 #include "nhp/genetics.h"
00031 
00032 // Is it a bool? Is it a double? No! It's the SuperGene!
00033 
00035 //                                                                           //
00036 //                            ----                                           //
00037 //                           |      ___    _    ___                          //
00038 //                           | --- /   ) |/ \  /   )                         //
00039 //                           |   \ |---  |   | |---                          //
00040 //                           |___/  \__  |   |  \__                          //
00041 //                                                                           //
00043 
00054 class Gene : public Genstruct {
00055     decl_dynamic (Gene);
00056   public:
00057 
00060                     Gene        () {FORBIDDEN}
00061                     Gene        (const GeneticID& n, double mut=1);
00062                     Gene        (const Gene& o) : Genstruct (o) {shallowCopy (o);}
00063 
00065     void                        setMutability   (double val) {mutability=val;}
00066 
00068     double                      getMutability   () const {return mutability;}
00069 
00070     // Implementations
00071         
00073     virtual void                addPrivateGenes (Gentainer& g, const StringMap& params) {;}
00074 
00076     virtual void                copy            (const Genstruct& other);
00077 
00079     virtual const Genstruct*    getGene         (const GeneticID& nam) const {
00080         return (id==nam)? this : (const Gene*) NULL;
00081     }
00082     virtual void                check           () const {Genstruct::check ();}
00083     
00084   protected:
00089     double  mutability;
00090 
00091     virtual int                 calc_len    () const {return 1;}
00092     void                        shallowCopy (const Gene& o) {mutability=o.mutability;}
00093 
00094   private:
00095     Gene& operator= (const Gene& orig) {FORBIDDEN; return *this;}
00096 };
00097 
00098 // The Gene is your friend!
00099 
00102 #define DefGene(gclass) \
00103 class gclass : public Gene { \
00104     decl_dynamic (gclass);\
00105   public:\
00106                         gclass              (const GeneticID& name=NULL) : Gene (name) {;}\
00107                         gclass              (const gclass& o) : Gene (o) {;}\
00108     void                init                () {;}\
00109     bool                pointMutate         (const MutationRate& k) {return false;}\
00110     void                copy                (const Genstruct& o) {Gene::copy(o);}\
00111     Genstruct*          replicate           () const {return new gclass (id);}\
00112     bool                execute             (const GeneticMsg& msg) const;\
00113     void                addPrivateGenes     (Gentainer& g, const StringMap& params);\
00114 };
00115 
00116 
00117 
00119 //                                                                           //
00120 //           ----  o                        ----                             //
00121 //           |   )     _    ___            |      ___    _    ___            //
00122 //           |---  | |/ \   ___| |/\ \   | | --- /   ) |/ \  /   )           //
00123 //           |   ) | |   | (   | |    \  | |   \ |---  |   | |---            //
00124 //           |___  | |   |  \__| |     \_/ |___/  \__  |   |  \__            //
00125 //                                    \_/                                    //
00127 
00130 class BinaryGene : public Gene {
00131     decl_dynamic (BinaryGene);
00132   public:
00133 
00136                             BinaryGene  () {FORBIDDEN}
00137 
00148                             BinaryGene  (const GeneticID& id,
00149                                          double mut=1.0, double initP=0.5);
00150 
00151                             BinaryGene  (const BinaryGene& o) : Gene (o) {shallowCopy (o);}
00152 
00153     // Access operators
00154     
00156     BinaryGene&             set         (bool value) {mValue = value; return *this;}
00157 
00159     bool                    getvalue    () const {return mValue;}
00160 
00164     void                    setInitP    (double initP) {mInitP=initP;}
00165 
00166     // Implementations
00167 
00169     virtual void            init        ();
00170 
00180     virtual bool            pointMutate (const MutationRate& k);
00184     virtual double          equality    (const Genstruct& other) const {
00185         return (mValue == static_cast<const BinaryGene&> (other).mValue)? 1.0 : 0.0;
00186     }
00188     virtual void            copy        (const Genstruct& other);
00190     virtual Genstruct*      replicate   () const {return new BinaryGene (*this);}
00192     virtual void            print       (TextOStream& out) const;
00194     virtual DataOStream&    operator>>	(DataOStream& out) const;
00195 
00196   private:
00197     void                    shallowCopy (const BinaryGene& o) {
00198         mValue=o.mValue;
00199         mInitP=o.mInitP;
00200     }
00201     BinaryGene& operator= (const BinaryGene& orig) {FORBIDDEN; return *this;}
00202 
00203     bool    mValue;
00204     double  mInitP;
00205 };
00206 
00207 // A dead binary is the best binary
00208 
00209 
00210 
00212 //                                                                          //
00213 //       _               ----- |                 ----                       //
00214 //      / \    _         |     |       ___   |  |      ___    _    ___      //
00215 //     /   \ |/ \  \   | |---  |  __   ___| -+- | --- /   ) |/ \  /   )     //
00216 //     |---| |   |  \  | |     | /  \ (   |  |  |   \ |---  |   | |---      //
00217 //     |   | |   |   \_/ |     | \__/  \__|   \ |___/  \__  |   |  \__      //
00218 //                  \_/                                                     //
00220 
00234 class AnyFloatGene : public Gene {
00235     decl_dynamic (AnyFloatGene);
00236   public:
00239                     AnyFloatGene    () {FORBIDDEN}
00240     
00251                     AnyFloatGene    (const GeneticID& id, double min, double max, double m=1.0);
00252                     AnyFloatGene    (const AnyFloatGene& o) : Gene (o) {
00253                         shallowCopy (o);
00254                     }
00255 
00263     virtual double  getvalue    () const {MUST_OVERLOAD; return 0.0;}
00264     
00265     // Implementations
00266 
00268     virtual void    copy        (const Genstruct& other);
00269 
00273     virtual void    check       () const;
00274     
00275   protected:
00277     double          mMin;
00278 
00280     double          mMax;
00281     
00282   private:
00284     AnyFloatGene&   operator= (const AnyFloatGene& orig) {FORBIDDEN; return *this;}
00285     
00286     void            shallowCopy (const AnyFloatGene& o);
00287 
00288 };
00289 
00290 
00291 
00293 //                                                                          //
00294 //               ----- |                 ----                               //
00295 //               |     |       ___   |  |      ___    _    ___              //
00296 //               |---  |  __   ___| -+- | --- /   ) |/ \  /   )             //
00297 //               |     | /  \ (   |  |  |   \ |---  |   | |---              //
00298 //               |     | \__/  \__|   \ |___/  \__  |   |  \__              //
00299 //                                                                          //
00301 
00302 // External
00303 class FloatMutator;
00304 
00311 class FloatGene : public AnyFloatGene {
00312   public:
00313 
00316                             FloatGene   () {FORBIDDEN}
00317 
00327                             FloatGene   (const GeneticID& id, double min, double max,
00328                                          double m=1.0, double value=-999);
00329                             FloatGene   (const FloatGene& o) : AnyFloatGene (o) {
00330                                 shallowCopy (o);
00331                             }
00332 
00334     FloatGene&              set         (double val) {value = val; return *this;}
00335     
00340     virtual double          getvalue    () const {return value;}
00341     
00342 
00349     FloatGene&              setCircular (bool c) {mCircular=c; return *this;}
00350 
00357     FloatGene&              setMutator  (FloatMutator* m) {mMutator=m; return *this;}
00358 
00359     // Implementations
00360 
00364     virtual void            init        ();
00365 
00381     virtual bool            pointMutate (const MutationRate& k);
00382 
00390     virtual double          equality    (const Genstruct& other) const;
00391     virtual void            copy        (const Genstruct& other);
00392     virtual Genstruct*      replicate   () const {return new FloatGene (*this);}
00393     virtual void            print       (TextOStream& out) const;
00394     virtual void            check       () const;
00395     
00396   protected:
00397 
00399     double          value;
00400 
00402     double          mVariance;
00403 
00405     bool            mCircular;
00406 
00408     FloatMutator*   mMutator;
00409 
00410   private:
00411     void                    shallowCopy (const FloatGene& o);
00412     FloatGene&              operator=   (const FloatGene& orig) {FORBIDDEN; return *this;}
00413     decl_dynamic (FloatGene);
00414 };
00415 
00416 // Gee, genes don't sink; they really do double
00417 
00418 
00419 
00421 //                                                                          //
00422 //        ----  o     ----- |                 ----                          //
00423 //        |   )    |  |     |       ___   |  |      ___    _    ___         //
00424 //        |---  | -+- |---  |  __   ___| -+- | --- /   ) |/ \  /   )        //
00425 //        |   ) |  |  |     | /  \ (   |  |  |   \ |---  |   | |---         //
00426 //        |___  |   \ |     | \__/  \__|   \ |___/  \__  |   |  \__         //
00427 //                                                                          //
00429 
00438 class BitFloatGene : public AnyFloatGene {
00439   public:
00440     
00443                             BitFloatGene    () {FORBIDDEN}
00444 
00457                             BitFloatGene    (const GeneticID& id, double min, double max,
00458                                              int bits, const StringMap& params, double m=1.0);
00459                             BitFloatGene    (const BitFloatGene& o) : AnyFloatGene (o) {
00460                                 shallowCopy (o);
00461                             }
00462 
00463     virtual double          getvalue        () const;
00464     BitFloatGene*           setGrayCoding   (bool isg=true) {mGrayCoded=isg; return NULL;}
00465     
00466     // Implementations
00467         
00469     virtual void            init        () {mBits.init();}
00470     
00474     virtual bool            pointMutate (const MutationRate& k) {return mBits.pointMutate (k);}
00475 
00482     virtual double          equality    (const Genstruct& o) const {mBits.equality(o); return 0.0;}
00483     virtual void            copy        (const Genstruct& other);
00484     virtual Genstruct*      replicate   () const {return new BitFloatGene (*this);}
00485     virtual void            print       (TextOStream& out) const;
00486     virtual void            recombine   (const Genstruct& a, const Genstruct& b);
00487     virtual void            check       () const;
00488 
00489   protected:
00492     Gentainer       mBits;
00493 
00499     int             mBitCount;
00500 
00502     bool            mGrayCoded;
00503     
00504     virtual int             calc_len    () const {return ((const Genstruct&)mBits).calc_len();}
00505     
00506   private:
00507     void                    shallowCopy (const BitFloatGene& o);
00508     BitFloatGene&           operator=   (const BitFloatGene& orig) {FORBIDDEN; return *this;}
00509     decl_dynamic (BitFloatGene);
00510 };
00511 
00512 
00513 
00515 //                                                                           //
00516 //            _               ---            ----                            //
00517 //           / \    _          |    _    |  |      ___    _    ___           //
00518 //          /   \ |/ \  \   |  |  |/ \  -+- | --- /   ) |/ \  /   )          //
00519 //          |---| |   |  \  |  |  |   |  |  |   \ |---  |   | |---           //
00520 //          |   | |   |   \_/ _|_ |   |   \ |___/  \__  |   |  \__           //
00521 //                       \_/                                                 //
00523 
00536 class AnyIntGene : public Gene {
00537     decl_dynamic (AnyIntGene);
00538   public:
00539 
00542                     AnyIntGene  () {FORBIDDEN}
00543     
00552                     AnyIntGene  (const GeneticID& id, int min, int max, double m=1.0);
00553     
00554                     AnyIntGene  (const AnyIntGene& o) : Gene (o) {
00555                         shallowCopy (o);
00556                     }
00557 
00563     virtual int     getvalue    () const {MUST_OVERLOAD; return 0;}
00564     
00565     // Implementations
00566 
00568     virtual void    copy        (const Genstruct& other);
00569     
00573     virtual void    check       () const {ASSERT(mMin<mMax); Gene::check ();}
00574     
00575   protected:
00577     int             mMin;
00578 
00580     int             mMax;
00581 
00582     void            shallowCopy (const AnyIntGene& o);
00583 };
00584 
00585 
00587 //                                                                           //
00588 //                    ---            ----                                    //
00589 //                     |    _    |  |      ___    _    ___                   //
00590 //                     |  |/ \  -+- | --- /   ) |/ \  /   )                  //
00591 //                     |  |   |  |  |   \ |---  |   | |---                   //
00592 //                    _|_ |   |   \ |___/  \__  |   |  \__                   //
00593 //                                                                           //
00595 
00601 class IntGene : public AnyIntGene {
00602     decl_dynamic (IntGene);
00603     int     mValue;
00604   public:
00605 
00608                             IntGene     () {FORBIDDEN}
00609     
00610                             IntGene     (const GeneticID& id, int min, int max,
00611                                          double mut=1,int value=-9999);
00612                             IntGene     (const IntGene& o) : AnyIntGene (o) {shallowCopy (o);}
00613 
00615     virtual IntGene&        set         (int val) {mValue = val; return *this;}
00616 
00621     virtual int             getvalue    () const;
00622 
00623     // Implementations
00624 
00628     virtual void            init        ();
00629 
00636     virtual bool            pointMutate (const MutationRate& k);
00637 
00642     virtual double          equality    (const Genstruct& other) const;
00643     virtual void            copy        (const Genstruct& other);
00644     virtual Genstruct*      replicate   () const {return new IntGene (*this);}
00645     virtual void            print       (TextOStream& out) const;
00646     virtual void            check       () const;
00647 
00648   private:
00649     void                    shallowCopy (const IntGene& o);
00650     IntGene&                operator=   (const IntGene& orig) {FORBIDDEN; return *this;}
00651 };
00652 
00653 // IntGene? .. hmm .. Wouldn't InGen be better?
00654 
00655 
00657 //                                                                           //
00658 //             ----  o     ---            ----                               //
00659 //             |   )    |   |    _    |  |      ___    _    ___              //
00660 //             |---  | -+-  |  |/ \  -+- | --- /   ) |/ \  /   )             //
00661 //             |   ) |  |   |  |   |  |  |   \ |---  |   | |---              //
00662 //             |___  |   \ _|_ |   |   \ |___/  \__  |   |  \__              //
00663 //                                                                           //
00665 
00675 class BitIntGene : public AnyIntGene {
00676   public:
00679                             BitIntGene  () {FORBIDDEN}
00680 
00691                             BitIntGene  (const GeneticID& id, int min, int max,
00692                                              int bits, const StringMap& params, double m=1.0);
00693                             BitIntGene  (const BitIntGene& o) : AnyIntGene (o) {
00694                                 shallowCopy (o);
00695                             }
00696 
00699     virtual int             getvalue        () const;
00700 
00710     BitIntGene*             setGrayCoding   (bool gray=true) {mGrayCoded=gray; return this;}
00711     
00712     // Implementations
00713 
00715     virtual void            init        () {mBits.init();}
00716 
00720     virtual bool            pointMutate (const MutationRate& k) {return mBits.pointMutate (k);}
00721 
00727     virtual double          equality    (const Genstruct& o) const {return mBits.equality(o);}
00728     virtual void            copy        (const Genstruct& other);
00729     virtual Genstruct*      replicate   () const {return new BitIntGene (*this);}
00730     virtual void            print       (TextOStream& out) const;
00731     virtual void            recombine   (const Genstruct& a, const Genstruct& b);
00732     virtual void            check       () const;
00733 
00734   protected:
00735 
00737     Gentainer   mBits;
00738 
00743     int         mBitCount;
00744 
00746     bool        mGrayCoded;
00747     
00748     virtual int             calc_len    () const {return ((const Genstruct&)mBits).calc_len();}
00749     
00750   private:
00751     void                    shallowCopy (const BitIntGene& o);
00752     BitIntGene&             operator= (const BitIntGene& orig) {FORBIDDEN; return *this;}
00753     decl_dynamic (BitIntGene);
00754 };
00755 
00756 
00757 
00759 //                                                                           //
00760 //              ---                      ----                                //
00761 //               |    _    |   ___      |      ___    _    ___               //
00762 //               |  |/ \  -+- /   ) |/\ | --- /   ) |/ \  /   )              //
00763 //               |  |   |  |  |---  |   |   \ |---  |   | |---               //
00764 //              _|_ |   |   \  \__  |   |___/  \__  |   |  \__               //
00765 //                                                                           //
00767 
00771 class InterGene : public Gene {
00772     decl_dynamic (InterGene);
00773     GeneticID   targetGene;
00774   public:
00780                             InterGene   (const GeneticID& name=NULL, const GeneticID& trg=NULL)
00781                                     : Gene (name), targetGene (trg) {;}
00782                             InterGene   (const InterGene& o) : Gene (o) {shallowCopy (o);}
00783 
00784     // Implementations
00785 
00789     virtual void            init        () {;}
00790     virtual Genstruct*      replicate   () const {return new InterGene (*this);}
00791     virtual void            copy        (const Genstruct& other);
00792     virtual bool            execute     (const GeneticMsg& msg) const;
00793     virtual void            print       (TextOStream& out) const;
00794     virtual bool            pointMutate (const MutationRate& k) {return false;}
00795 
00796   private:
00797     void                    shallowCopy (const InterGene& o) {targetGene=o.targetGene;}
00798     InterGene&              operator=   (const InterGene& orig) {FORBIDDEN; return *this;}
00799 };
00800 
00801 
00802 
00803 // I don't want any f*cking genes in my food.
00804 
00805 #endif
00806 

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