int main() { int gen = 0; char go = 1; srand(time(NULL)); /* 1. Choose the initial ppopulation */ initial_population(); /* 2. Evaluate the fitness of each individual in the population */ fitness(); /* 3. Repeat */ while((gen++ < MAX_GENS) && go) { /* printf("Find the best fittest\n");*/ /* 1. Select the best-ranking individuals to reproduce */ find_the_best_individuals(); /* Select the reproduce no, dynamicly (Ver. 2) */ reproduce(); /* 2. Breed new generation through crossover and mutation (genetic operations) and give birth to offspring */ /* 3. Evaluate the individual fitnesses of the offspring */ /* 4. Replace worst ranked part of the population with offspring */ fitness(); find_the_best_individuals(); if (population[0].fitness < OK_SNIT) go = 0; } /* 4. Until <terminating condition> */ print_population(); printf("No of gen: %d\n",gen); return 0; }
/* Print metapopulation content */ void print_metapopulation(struct metapopulation *in, bool showGen){ int i, k, K=get_npop(in); struct population *curPop; struct pathogen * curpat; /* display general info */ printf("\nnb of populations: %d", K); printf("\ntotal nb susceptible: %d", get_total_nsus(in)); printf("\ntotal nb infected: %d", get_total_ninf(in)); printf("\ntotal nb recovered: %d", get_total_nrec(in)); printf("\ntotal population size: %d\n", get_total_nsus(in)+get_total_ninf(in)+get_total_nrec(in)); /* display populations */ for(k=0;k<K;k++){ curPop = get_populations(in)[k]; printf("\npopulation %d", k); print_population(curPop); if(showGen){ for(i=0;i<get_maxnpat(in);i++){ curpat = get_pathogens(in)[i]; if(!isNULL_pathogen(curpat) && get_popid(curpat)==k) print_pathogen(curpat); } printf("\n"); } } }
stResult<TCity> * main_genetic(mySlimTree* SlimTree, TCity * queryObject, stDistance range) { time_t begin, end; begin = time(NULL); srand(time(NULL)); Population * pop = new_population(); Population * aux_pop = new_empty_population(); infeasible(SlimTree, pop); set_genotype(pop); evaluate_fitness(pop, SlimTree, queryObject, range); #if DEBUG print_population(pop ,"Pop", number_of_gerations); #endif while( (number_of_gerations < MAX_NUMBER_OF_GERATIONS) && (delta_fitness > 0.0001) ) { number_of_gerations++; #if DEBUG cout << "Number of Gerations: "<< number_of_gerations << endl; #endif selection_by_tournament(pop, aux_pop); #if DEBUG cout << "Torneio" << endl; #endif crossover_uniform(aux_pop); #if DEBUG cout << "Crossover Uniforme" << endl; #endif mutation(aux_pop); #if DEBUG cout << "Mutação" << endl; #endif set_phenotype(aux_pop); #if DEBUG cout << "Fenotipo" << endl; #endif infeasible(SlimTree, aux_pop); #if DEBUG cout << "Infactibilidade" << endl; #endif set_genotype(aux_pop); #if DEBUG cout << "Fenotipo" << endl; #endif copy_population(aux_pop, pop); #if DEBUG cout << "Copiar aux pop" << endl; #endif evaluate_fitness(pop, SlimTree, queryObject, range); #if DEBUG cout << "Fitness" << endl; #endif old_media_of_fitness = media_of_fitness; media_of_fitness = get_media_of_population(pop); delta_fitness = fabs(media_of_fitness - old_media_of_fitness); } end = time(NULL); #if DEBUG print_population(pop, "PopulatioN", number_of_gerations); print_statistic(pop, begin, end); #endif }
int main() { extern counter; /* test create_population */ population* pop; population_fitness* pop_fit; int x; counter = 0; pop = create_population( 0, &create_individual ); if( pop == 0 ) { printf( "population not created at line: %d\n", __LINE__ ); } else { print_population( pop, 0 ); delete_population( pop ); pop = 0; } counter = 0; pop = create_population( 1, &create_individual ); if( pop == 0 ) { printf( "population not created at line: %d\n", __LINE__ ); } else { print_population( pop, 0 ); delete_population( pop ); pop = 0; } counter = 0; pop = create_population( 10, &create_individual ); if( pop == 0 ) { printf( "population not created at line: %d\n", __LINE__ ); } else { print_population( pop, 0 ); print_population_compact( pop ); delete_population( pop ); pop = 0; } counter = 0; printf( "create_population large test: " ); fflush( stdout ); pop = create_population( INT_MAX / 64, &create_individual ); if( pop == 0 ) { printf( "population not created at line: %d\n", __LINE__ ); } else { delete_population( pop ); pop = 0; } printf( "complete\n" ); /* memory leak test */ printf( "create_population memory test: " ); #ifdef MEMLEAK fflush( stdout ); for( x = 0; x < INT_MAX; x++ ) { counter = 0; pop = create_population( 10, &create_individual ); if( pop == 0 ) { eprintf( "population not created at line: %d\n", __LINE__ ); } else { delete_population( pop ); pop = 0; } } printf( "complete\n" ); # else printf( "skipped\n" ); #endif /* test create_empty_population */ pop = create_empty_population( -1 ); if( pop == 0 ) { printf( "population not created at line: %d\n", __LINE__ ); } else { print_population( pop, 0 ); delete_population( pop ); pop = 0; } pop = create_empty_population( 0 ); if( pop == 0 ) { printf( "population not created at line: %d\n", __LINE__ ); } else { print_population( pop, 0 ); delete_population( pop ); pop = 0; } pop = create_empty_population( 1 ); if( pop == 0 ) { printf( "population not created at line: %d\n", __LINE__ ); } else { print_population( pop, 0 ); delete_population( pop ); pop = 0; } pop = create_empty_population( 10 ); if( pop == 0 ) { printf( "population not created at line: %d\n", __LINE__ ); } else { print_population( pop, 0 ); print_population_compact( pop ); delete_population( pop ); pop = 0; } printf( "create_empty_population large test: " ); fflush( stdout ); pop = create_empty_population( INT_MAX / 64 ); if( pop == 0 ) { printf( "population not created at line: %d\n", __LINE__ ); } else { delete_population( pop ); pop = 0; } printf( "complete\n" ); /* memory leak test */ printf( "create_population memory test: " ); #ifdef MEMLEAK fflush( stdout ); for( x = 0; x < INT_MAX; x++ ) { counter = 0; pop = create_empty_population( 10 ); if( pop == 0 ) { eprintf( "population not created at line: %d\n", __LINE__ ); } else { delete_population( pop ); pop = 0; } } printf( "complete\n" ); #else printf( "skipped\n" ); #endif /* test evaluate_population */ counter = 0; pop = create_empty_population( 1 ); if( pop == 0 ) { printf( "population not created at line: %d\n", __LINE__ ); } else { pop_fit = evaluate_population( pop, &evaluate_individual ); if( pop_fit == 0 ) { printf( "population fitness not created at line: %d\n", __LINE__ ); } else { print_population_fitness( pop_fit, 0 ); print_population_fitness_compact( pop_fit ); delete_population_fitness( pop_fit ); pop_fit = 0; } delete_population( pop ); pop = 0; } pop = create_population( 1, &create_individual ); if( pop == 0 ) { printf( "population not created at line: %d\n", __LINE__ ); } else { pop_fit = evaluate_population( pop, &evaluate_individual ); if( pop_fit == 0 ) { printf( "population fitness not created at line: %d\n", __LINE__ ); } else { print_population_fitness( pop_fit, 0 ); print_population_fitness_compact( pop_fit ); delete_population_fitness( pop_fit ); pop_fit = 0; } delete_population( pop ); pop = 0; } pop = create_population( 10, &create_individual ); if( pop == 0 ) { printf( "population not created at line: %d\n", __LINE__ ); } else { pop_fit = evaluate_population( pop, &evaluate_individual ); if( pop_fit == 0 ) { printf( "population fitness not created at line: %d\n", __LINE__ ); } else { print_population_fitness( pop_fit, 0 ); print_population_fitness_compact( pop_fit ); delete_population_fitness( pop_fit ); pop_fit = 0; } delete_population( pop ); pop = 0; } printf( "evaluate_population large test: " ); fflush( stdout ); pop = create_population( INT_MAX / 64, &create_individual ); if( pop == 0 ) { printf( "population not created at line: %d\n", __LINE__ ); } else { pop_fit = evaluate_population( pop, &evaluate_individual ); if( pop_fit == 0 ) { printf( "population_fitness not created at line: %d\n", __LINE__ ); } else { delete_population_fitness( pop_fit ); pop_fit = 0; } delete_population( pop ); pop = 0; } printf( "complete\n" ); /* memory leak test */ printf( "evaluate_population memory test: " ); #ifdef MEMLEAK fflush( stdout ); counter = 0; pop = create_population( 10, &create_individual ); if( pop == 0 ) { eprintf( "population not created at line: %d\n", __LINE__ ); } else { for( x = 0; x < INT_MAX; x++ ) { pop_fit = evaluate_population( pop, &evaluate_individual ); delete_population_fitness( pop_fit ); pop_fit = 0; } delete_population( pop ); pop = 0; } printf( "complete\n" ); #else printf( "skipped\n" ); #endif /* test sort_population */ counter = 0; pop = create_population( 100, &create_individual ); pop_fit = evaluate_population( pop, &evaluate_individual ); sort_population( pop_fit ); delete_population_fitness( pop_fit ); pop_fit = 0; delete_population( pop ); pop = 0; /* test create_next_generation */ counter = 0; pop = create_population( 100, &create_individual ); pop_fit = evaluate_population( pop, &evaluate_individual ); counter = 0; population* pop_next = create_next_generation( 10, 10, 80, pop, pop_fit, &compare_individuals_fitness ); delete_population_fitness( pop_fit ); pop_fit = 0; delete_population( pop ); pop = 0; delete_population( pop_next ); pop_next = 0; /* test create_population_fitness */ // pop_fit = create_population_fitness( 100 ); /* test delete_population_fitness */ // delete_population_fitness( pop_fit ); /* former tests */ /* srand( time_seed() ); population* pop; population_fitness* pop_fit; population_fitness* pop_fit_sorted; pop = create_population( 3, &create_individual ); print_population( pop, 0 ); pop_fit = evaluate_population( pop, &evaluate_individual ); print_population_fitness( pop_fit, 0 ); delete_population_fitness( pop_fit ); delete_population( pop ); pop = create_population( 4, &create_individual ); individual* ic; individual* im; ic = copy_individual( pop->individuals[0] ); im = mutate_individual( pop->individuals[0], 2 ); delete_individual( pop->individuals[1] ); pop->individuals[1] = ic; delete_individual( pop->individuals[2] ); pop->individuals[2] = im; evaluate_population( pop, &evaluate_individual ); printf( "i1: original, i2: copy, i3 mutant (degree 2)\n" ); print_population( pop, 0 ); delete_population( pop ); pop = create_population( 3, &create_individual ); individual* off; off = mate_individuals( pop->individuals[0], pop->individuals[1], 1 ); delete_individual( pop->individuals[2] ); pop->individuals[2] = off; evaluate_population( pop, &evaluate_individual ); printf( "i1: parent1, i2: parent2, i3 offspring\n" ); print_population( pop, 0 ); delete_population( pop ); */ /* pop = create_population( 100, &create_individual ); pop_fit = evaluate_population( pop, &evaluate_individual ); print_population_fitness( pop_fit, 0 ); pop_fit_sorted = sort_population( pop_fit ); printf( "Original Population Fitness\n" ); print_population_fitness( pop_fit, 0 ); printf( "Sorted Population Fitness\n" ); print_population_fitness( pop_fit_sorted, 0 ); */ }