home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / genetk / genetic.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-27  |  6.6 KB  |  174 lines

  1. //------------------------< GENETIC.H >------------------------------------//
  2.  
  3.  
  4.  
  5. #define MAX_ELLEMENTS      1000
  6.  
  7. #include<stdlib.h>
  8. #include<time.h>
  9. #include<iostream.h>
  10. #include<iomanip.h>
  11. #include<conio.h>
  12. #include<math.h>
  13. #include<fstream.h>
  14. #include<bios.h>
  15.  
  16.  
  17. #define OK                                          0
  18.  
  19.  
  20.  
  21.  
  22. extern ofstream outfile;
  23.  
  24.  
  25.  
  26.  
  27. class ellement
  28.    {
  29.    unsigned char   *byte;
  30.    double          fitness;
  31.    double          offspring;
  32.    unsigned int    id;
  33.    unsigned long   age;
  34.    unsigned char   status;
  35.    public:
  36.            ellement(unsigned int gene_size,unsigned int gene_number);
  37.            ~ellement();
  38.    void            print(unsigned int gene_size, unsigned int gene_number);
  39.    double          get_fitness() {return fitness;};
  40.    unsigned long   get_age() {return age;};
  41.    void            set_offspring(double o) {offspring=o;};
  42.    double          get_offspring() {return offspring;};
  43.    double          get_offspring_floor() {return floor(offspring);};
  44.    double          get_offspring_frac() {return offspring-floor(offspring);};
  45.    unsigned char   get_byte(unsigned int i) {return byte[i];};
  46.    void            set_byte(unsigned int i,unsigned char c) {byte[i]=c;};
  47.    void            crossover(ellement *parrent1,ellement *parrent2,unsigned int gene_size, unsigned int gene_number);
  48.    void            mutate_in_place(unsigned char byte_change,long gene_ratio,long byte_ratio,unsigned int gene_size, unsigned int gene_number);
  49.    int             mutate(long gene_ratio,long bits_to_change,unsigned int gene_size, unsigned int gene_number,long byte_change);
  50.    double          set_score(double(*score_func)(unsigned char *)){fitness=(*score_func)(byte);age++;return fitness;};
  51.    double          get_score(){return fitness;};
  52.    unsigned char   *get_byte_ptr(){return byte;};
  53.    void            print_string(unsigned int gene_size, unsigned int gene_number);
  54.    void            randomize_ellement(unsigned int gene_size, unsigned int gene_number);
  55.    void            print_longs(unsigned int gene_number);
  56.    unsigned int    get_id(){return id;};
  57.    void            copy(ellement *from, int number_of_bytes);
  58.    void            save_to_file(ofstream *fs,int number_of_bytes);
  59.    void            load_from_file(ifstream *fs,int number_of_bytes);
  60.    void            set_status_bit(int bit_number);
  61.    void            reset_status_bit(int bit_number);
  62.    int             get_status_bit(int bit_number);
  63.    void            clr_status_byte(){status='\0';};
  64.    };
  65.  
  66.  
  67. typedef ellement *ellement_ptr;
  68.  
  69.  
  70.  
  71. class evolve_parms
  72.    {
  73.    public:
  74.    long top_breeder_ratio ;
  75.    long mortality_ratio   ;
  76.    long use_mutate1       ;
  77.    long byte_change1      ;
  78.    long mutate_section1   ;
  79.    long pop_ratio1        ;
  80.    long gene_ratio1       ;
  81.    long byte_ratio1       ;
  82.    long use_mutate2       ;
  83.    long byte_change2      ;
  84.    long mutate_section2   ;
  85.    long pop_ratio2        ;
  86.    long gene_ratio2       ;
  87.    long byte_ratio2       ;
  88.    long use_mutate3       ;
  89.    long byte_change3      ;
  90.    long mutate_section3   ;
  91.    long pop_ratio3        ;
  92.    long gene_ratio3       ;
  93.    long byte_ratio3       ;
  94.    long new_pop_ratio     ;
  95.    long mutate_area       ;
  96.    long use_adjust_mutation ;
  97.    long adjust_m_parm1    ;
  98.    long adjust_m_parm2    ;
  99.    long adjust_m_parm3    ;
  100.    long breeding_age      ;
  101.    long select_by_rank    ;
  102.    void fix_evolve_parms();
  103.    int  read_file(char *filename);
  104.    };
  105.  
  106.  
  107.  
  108.  
  109.  
  110. class population
  111.    {
  112.    ellement       *e_ptr[2][MAX_ELLEMENTS];
  113.    int current;
  114.    unsigned int   number_of_ellements;
  115.    unsigned int   unique_scores;
  116.    unsigned int   unique_genes;
  117.    unsigned int   bytes_per_gene;
  118.    unsigned int   genes_per_ellement;
  119.    long           generation_count;
  120.    double         highest_score,lowest_score;
  121.    double         average_score;
  122.    double         total_score;
  123.    double         (*score_func)(unsigned char *);
  124.    void           (*print_gene_func)(unsigned char *,double,ofstream *);
  125.    void           (*init_round_func)();
  126.    void           (*pre_init_func)();
  127.    public:
  128.  
  129.           population(unsigned int population_size,unsigned int gene_size,unsigned int gene_number,double (*score_function)(unsigned char *),void (*print_gene_function)(unsigned char *,double score,ofstream *ofs),void (*init_round_function)(),void (*pre_init_function)() );
  130.    void           print(unsigned int e_number,ofstream *ofs=NULL);
  131.    void           sort_by_fitness(double *ptrbestfitness,unsigned long *ptrbestage);
  132.    void           crossover(long free_ratio,long breeding_age,long top_breeders);
  133.    void           print();
  134.    void           mutate_in_place(unsigned char byte_change,double mutate_section,long pop_ratio,long gene_ratio,long byte_ratio);
  135.    void           mutate(long mutate_area,long pop_ratio,long gene_ratio,long bits_to_change,long byte_change);
  136.    double         set_score(unsigned int i){return (*e_ptr[current][i]).set_score(score_func);};
  137.    double         get_score(unsigned int i){return (*e_ptr[current][i]).get_score();};
  138.    void           set_score();
  139.    unsigned char  *get_byte_ptr(int i){return (*e_ptr[current][i]).get_byte_ptr();};
  140.    unsigned long  get_age(int i) {return (*e_ptr[current][i]).get_age();};
  141.    time_t         evolve_till_criteria(int display_on,double criteria,evolve_parms ep);
  142.    double         evolve_for_time(int display_on,time_t seconds,evolve_parms ep);
  143.    double         evolve(int display_on,int display_pop_stats,evolve_parms ep);
  144.    ellement_ptr   select_ellement_ptr(long selection_area);
  145.    void           get_migration_from(long selection_area,unsigned int number_to_migrate, population sourse_pop);
  146.    void           print_best(){(*print_gene_func)((*e_ptr[current][0]).get_byte_ptr(),(*e_ptr[current][0]).get_score(),NULL);};
  147.    void           propogate(long nupop_select,int select_by_rank=1);
  148.    double         get_fitness(int i){return (*e_ptr[current][i]).get_fitness();};
  149.    double         standard_deviation();
  150.    double         variance();
  151.    void           randomize_pop(long keep_ratio);
  152.    void           copy_ellement(population *source,int from,int to);
  153.    ellement       *get_ellement(int i) {return e_ptr[current][i];};
  154.    void           print_top(int n,ofstream *ofs=NULL);
  155.    void           save_to_file(char *filename,int start,int end);
  156.    void           load_from_file(char *filename,int start,int end);
  157.    void           set_status_bit(int e,int bit_number);
  158.    void           reset_status_bit(int e,int bit_number);
  159.    int            get_status_bit(int e,int bit_number);
  160.    void           clr_status_byte(int e);
  161.    int            comp_genes(int i1,int i2);
  162.    };
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173. //
  174.