home *** CD-ROM | disk | FTP | other *** search
- //------------------------< GENETIC.H >------------------------------------//
-
-
-
- #define MAX_ELLEMENTS 1000
-
- #include<stdlib.h>
- #include<time.h>
- #include<iostream.h>
- #include<iomanip.h>
- #include<conio.h>
- #include<math.h>
- #include<fstream.h>
- #include<bios.h>
-
-
- #define OK 0
-
-
-
-
- extern ofstream outfile;
-
-
-
-
- class ellement
- {
- unsigned char *byte;
- double fitness;
- double offspring;
- unsigned int id;
- unsigned long age;
- unsigned char status;
- public:
- ellement(unsigned int gene_size,unsigned int gene_number);
- ~ellement();
- void print(unsigned int gene_size, unsigned int gene_number);
- double get_fitness() {return fitness;};
- unsigned long get_age() {return age;};
- void set_offspring(double o) {offspring=o;};
- double get_offspring() {return offspring;};
- double get_offspring_floor() {return floor(offspring);};
- double get_offspring_frac() {return offspring-floor(offspring);};
- unsigned char get_byte(unsigned int i) {return byte[i];};
- void set_byte(unsigned int i,unsigned char c) {byte[i]=c;};
- void crossover(ellement *parrent1,ellement *parrent2,unsigned int gene_size, unsigned int gene_number);
- void mutate_in_place(unsigned char byte_change,long gene_ratio,long byte_ratio,unsigned int gene_size, unsigned int gene_number);
- int mutate(long gene_ratio,long bits_to_change,unsigned int gene_size, unsigned int gene_number,long byte_change);
- double set_score(double(*score_func)(unsigned char *)){fitness=(*score_func)(byte);age++;return fitness;};
- double get_score(){return fitness;};
- unsigned char *get_byte_ptr(){return byte;};
- void print_string(unsigned int gene_size, unsigned int gene_number);
- void randomize_ellement(unsigned int gene_size, unsigned int gene_number);
- void print_longs(unsigned int gene_number);
- unsigned int get_id(){return id;};
- void copy(ellement *from, int number_of_bytes);
- void save_to_file(ofstream *fs,int number_of_bytes);
- void load_from_file(ifstream *fs,int number_of_bytes);
- void set_status_bit(int bit_number);
- void reset_status_bit(int bit_number);
- int get_status_bit(int bit_number);
- void clr_status_byte(){status='\0';};
- };
-
-
- typedef ellement *ellement_ptr;
-
-
-
- class evolve_parms
- {
- public:
- long top_breeder_ratio ;
- long mortality_ratio ;
- long use_mutate1 ;
- long byte_change1 ;
- long mutate_section1 ;
- long pop_ratio1 ;
- long gene_ratio1 ;
- long byte_ratio1 ;
- long use_mutate2 ;
- long byte_change2 ;
- long mutate_section2 ;
- long pop_ratio2 ;
- long gene_ratio2 ;
- long byte_ratio2 ;
- long use_mutate3 ;
- long byte_change3 ;
- long mutate_section3 ;
- long pop_ratio3 ;
- long gene_ratio3 ;
- long byte_ratio3 ;
- long new_pop_ratio ;
- long mutate_area ;
- long use_adjust_mutation ;
- long adjust_m_parm1 ;
- long adjust_m_parm2 ;
- long adjust_m_parm3 ;
- long breeding_age ;
- long select_by_rank ;
- void fix_evolve_parms();
- int read_file(char *filename);
- };
-
-
-
-
-
- class population
- {
- ellement *e_ptr[2][MAX_ELLEMENTS];
- int current;
- unsigned int number_of_ellements;
- unsigned int unique_scores;
- unsigned int unique_genes;
- unsigned int bytes_per_gene;
- unsigned int genes_per_ellement;
- long generation_count;
- double highest_score,lowest_score;
- double average_score;
- double total_score;
- double (*score_func)(unsigned char *);
- void (*print_gene_func)(unsigned char *,double,ofstream *);
- void (*init_round_func)();
- void (*pre_init_func)();
- public:
-
- 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)() );
- void print(unsigned int e_number,ofstream *ofs=NULL);
- void sort_by_fitness(double *ptrbestfitness,unsigned long *ptrbestage);
- void crossover(long free_ratio,long breeding_age,long top_breeders);
- void print();
- void mutate_in_place(unsigned char byte_change,double mutate_section,long pop_ratio,long gene_ratio,long byte_ratio);
- void mutate(long mutate_area,long pop_ratio,long gene_ratio,long bits_to_change,long byte_change);
- double set_score(unsigned int i){return (*e_ptr[current][i]).set_score(score_func);};
- double get_score(unsigned int i){return (*e_ptr[current][i]).get_score();};
- void set_score();
- unsigned char *get_byte_ptr(int i){return (*e_ptr[current][i]).get_byte_ptr();};
- unsigned long get_age(int i) {return (*e_ptr[current][i]).get_age();};
- time_t evolve_till_criteria(int display_on,double criteria,evolve_parms ep);
- double evolve_for_time(int display_on,time_t seconds,evolve_parms ep);
- double evolve(int display_on,int display_pop_stats,evolve_parms ep);
- ellement_ptr select_ellement_ptr(long selection_area);
- void get_migration_from(long selection_area,unsigned int number_to_migrate, population sourse_pop);
- void print_best(){(*print_gene_func)((*e_ptr[current][0]).get_byte_ptr(),(*e_ptr[current][0]).get_score(),NULL);};
- void propogate(long nupop_select,int select_by_rank=1);
- double get_fitness(int i){return (*e_ptr[current][i]).get_fitness();};
- double standard_deviation();
- double variance();
- void randomize_pop(long keep_ratio);
- void copy_ellement(population *source,int from,int to);
- ellement *get_ellement(int i) {return e_ptr[current][i];};
- void print_top(int n,ofstream *ofs=NULL);
- void save_to_file(char *filename,int start,int end);
- void load_from_file(char *filename,int start,int end);
- void set_status_bit(int e,int bit_number);
- void reset_status_bit(int e,int bit_number);
- int get_status_bit(int e,int bit_number);
- void clr_status_byte(int e);
- int comp_genes(int i1,int i2);
- };
-
-
-
-
-
-
-
-
-
-
- //
-