void GA::Organism::randomize() { for (int i = 0; i < __LOCUS_COUNT__; i++) { Traits[i].randomize(); } calculate_fitness(); }
void Simulation::compare_fitness() { read_back_buffer(currentBuffer); const unsigned long long currentFitness = calculate_fitness(); if(currentFitness <= bestFitness) { bestFitness = currentFitness; copy_current_to_best_triangles(); // Toggle double buffered fbo renderTextureActiveA = !renderTextureActiveA; glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, renderTextureActiveA ? renderTextureIdA : renderTextureIdB, 0); #if DEBUG const unsigned long long currentTotalFitness = calculate_total_fitness(currentBuffer); LOGI("Found new best fitness %ul (%ul of %ul) !!! verts=%i", bestFitness, currentTotalFitness, targetTotalFitness, currentVertexCount); #endif } else { copy_best_to_current_triangles(); } }
void create_generations(int fitness,float lamda,int num_of_parents,int number_of_groups_wanted,int row_swapping, int min_gene_R1R2,int max_gene_R1R2,int freq, int min_count,int max_count,int generations_wanted, int generation_change,int pop_size_change,double mutation_rate, int robustness, int robust_changes,int robust_last_bit, gsl_rng *r, FILE *r1Output, FILE *r2Output,FILE *matrixOutput, FILE *countsOutput,FILE *fitnessOutput, FILE *discreteOutput,FILE *robustOutput){ int i,k,l,p,f; population *robust_population; group* temp_robust_group,*temp_normal_group; int temp; for(i=0;i<generations_wanted;i++){ if(i==0){ generation_array[i%2]=create_population(number_of_groups_wanted,min_gene_R1R2,max_gene_R1R2,min_count,max_count,1); } else{ if (i%2==0) temp=2; else temp=1; if(fitness){ generation_array[i%2]=create_gen_population_fit(temp,num_of_parents,row_swapping,min_count,max_count,mutation_rate,r); } else{ generation_array[i%2]=create_gen_population_no_fit(temp,num_of_parents,row_swapping,min_count,max_count,mutation_rate,r); } } if(i==generation_change){ if(curr_num_of_groups*persons_per_group>pop_size_change){ delete_groups(curr_num_of_groups-(pop_size_change/persons_per_group),i%2); } else{ insert_groups((pop_size_change/persons_per_group)-curr_num_of_groups,lamda,i%2,num_of_parents,fitness,row_swapping,min_count,max_count,mutation_rate,r); } } /* ROBUSTNESS */ if(i%freq==0 && robustness) { robust_population = create_population(curr_num_of_groups,min_gene_R1R2,max_gene_R1R2,min_count,max_count,0); temp_robust_group=robust_population->groups_list; temp_normal_group=generation_array[i%2]->groups_list; for(k=0;k<curr_num_of_groups;k++){ /*sarwse olo ton pli8ismo kai deep copy*/ /*deep copy atomou*/ for(l=0;l<persons_per_group;l++){ temp_robust_group->person_in_group[l]=deep_copy_person_robust(temp_robust_group->person_in_group[l],temp_normal_group->person_in_group[l]); } if(temp_robust_group->next!=NULL&&temp_normal_group->next!=NULL){ temp_robust_group=temp_robust_group->next; temp_normal_group=temp_normal_group->next; } } for(k=0;k<curr_num_of_groups-1;k++){ temp_robust_group=temp_robust_group->prev; } robust_population->groups_list=temp_robust_group; check_robustness(robustOutput,robust_population,robust_changes,robust_last_bit,1,r); /*freeing memory from robustness*/ for(k=0;k<curr_num_of_groups;k++){ for(l=0;l<persons_per_group;l++){ free(temp_robust_group->person_in_group[l]); } if(temp_robust_group->next!=NULL){ temp_robust_group=temp_robust_group->next; } } free(robust_population); } /*END OF ROBUSTNESS*/ /*FREEING MEMORY*/ if(i!=0){ for(p=0;p<curr_num_of_groups;p++){ for(f=0;f<persons_per_group;f++){ free(generation_array[temp-1]->groups_list->person_in_group[f]); } if(generation_array[temp-1]->groups_list->next!=NULL){ generation_array[temp-1]->groups_list=generation_array[temp-1]->groups_list->next; free(generation_array[temp-1]->groups_list->prev); } } free(generation_array[temp-1]->groups_list); free(generation_array[temp-1]); } mature_generation(generation_array[i%2],1); calculate_fitness(i%2,lamda); if(i%freq==0){ printf("Generation %d Simulated. \n",i); extract_R1R2_generation(r1Output, r2Output,i%2); extract_gene_dependancies_matrix_generation(matrixOutput, i%2); extract_gene_counts_generation(countsOutput, i%2); extract_discrete_generation(discreteOutput,i%2); extract_fitness_generation(fitnessOutput,i%2); } } }
void reproduction(int source_colony, struct pop society, int *ptr_N, const gsl_rng *rand){ int k,j; double *fitness; double pop_fitness; unsigned int N_new; unsigned int *tmp; int picked_father; int counter; int **ptr_offspring_matrix_del; /*this is the matrix into which the genotypes of the offspring will be stored*/ int **ptr_offspring_matrix_env; ptr_offspring_matrix_del=make_matrix(society.size_max, LOCI_DEL); ptr_offspring_matrix_env=make_matrix(society.size_max, LOCI_ENV); fitness = malloc(society.size_max*sizeof(double)); tmp = malloc(society.size_max*sizeof(double)); if(fitness==NULL){ fprintf(stderr, "ERROR: memory allocation did not work\n"); fflush(stderr); abort(); } if(fitness==NULL){ fprintf(stderr, "ERROR: memory allocation did not work\n"); fflush(stderr); abort(); } for(k=(*ptr_N);k<society.size_max;k++){ fitness[k]=0; } pop_fitness=0; /*calculate the fitness of individuals*/ pop_fitness=calculate_fitness(ptr_N, fitness, society); /*determine the number of individuals in the next generation*/ if(source_colony == 0){ N_new = gsl_ran_poisson(rand, pop_fitness); } else{ if(source_colony == 1){ N_new = *ptr_N; } else{ fprintf(stderr, "ERROR: neither colony nor source\n"); fflush(stderr); abort(); } } /*find the mothers: tmp[k] gives the number of offspring of individual k*/ gsl_ran_multinomial(rand, society.size_max, N_new, fitness, tmp); /*now we pick a father for each offspring and determine the genotype of the offspring*/ counter=0; picked_father=0; for(k=0;k<*ptr_N;k++){ for(j=0;j<tmp[k];j++){ picked_father = pick_father(rand, ptr_N, fitness, k); form_offspring(rand, k, picked_father, counter, society.ptr_matrix_del, society.ptr_matrix_env, ptr_offspring_matrix_del, ptr_offspring_matrix_env); counter=counter+1; } } /*the population size gets updated*/ *ptr_N=N_new; if(N_new > society.size_max){ fprintf(stderr, "ERROR: population overgrew its limits! \n"); fflush(stderr); abort(); } /*the population matrix gets updated by the offspring matrix*/ /*for(j=0;j<society.size_max;j++){ for(k=0;k<LOCI_DEL;k++){ society.ptr_matrix_del[j][k]=ptr_offspring_matrix_del[j][k]; } for(k=0;k<LOCI_ENV;k++){ society.ptr_matrix_env[j][k]=ptr_offspring_matrix_env[j][k]; } }*/ /*finally, we mutate all the entries in the population matrix*/ for(j=0;j<*ptr_N;j++){ for(k=0;k<LOCI_DEL;k++){ society.ptr_matrix_del[j][k] = mutate(rand, ptr_offspring_matrix_del[j][k]); } for(k=0;k<LOCI_ENV;k++){ society.ptr_matrix_env[j][k] = mutate(rand, ptr_offspring_matrix_env[j][k]); } } for(j=*ptr_N;j<society.size_max;j++){ for(k=0;k<LOCI_DEL;k++){ society.ptr_matrix_del[j][k] = 0; } for(k=0;k<LOCI_ENV;k++){ society.ptr_matrix_env[j][k] = 0; } } free(ptr_offspring_matrix_del); free(ptr_offspring_matrix_env); }
//////////////////////// // ORGANISMS SUB CLASS //////////////////////// GA::Organism::Organism() { randomize(); calculate_fitness(); }