/* SGPC: Simple Genetic Programming in C (c) 1993 by Walter Alden Tackett and Aviram Carmi This code and documentation is copyrighted and is not in the public domain. All rights reserved. - This notice may not be removed or altered. - You may not try to make money by distributing the package or by using the process that the code creates. - You may not distribute modified versions without clearly documenting your changes and notifying the principal author. - The origin of this software must not be misrepresented, either by explicit claim or by omission. Since few users ever read sources, credits must appear in the documentation. - Altered versions must be plainly marked as such, and must not be misrepresented as being the original software. Since few users ever read sources, credits must appear in the documentation. - The authors are not responsible for the consequences of use of this software, no matter how awful, even if they arise from flaws in it. If you make changes to the code, or have suggestions for changes, let us know! (gpc@ipld01.hac.com) */ #ifndef lint static char generations_c_rcsid[]="$Id: generations.c,v 2.12 1993/04/15 09:10:10 gpc-avc Exp gpc-avc $"; #endif /* * * $Log: generations.c,v $ * Revision 2.12 1993/04/15 09:10:10 gpc-avc * Added bsd_qsort() * * */ #include #include #include #include #include "gpc.h" #ifdef ANSI_FUNC VOID generations( int numpops, int numgens, int start_gen, pop_struct *pop ) #else VOID generations(numpops,numgens,start_gen,pop) int numpops; int numgens; int start_gen; pop_struct *pop; #endif { int g, p, i, j; float valperf; for (g=start_gen; g 0.0) add_parsimony_to_fitness(pop,p); normalize_fitness_of_population(pop,p); sort_population_by_fitness(pop,p); pop[p].best_of_gen_fitness = pop[p].standardized_fitness[pop[p].fitness_sort_index[0]]; pop[p].best_of_generation = copy_tree(pop[p].population[pop[p].fitness_sort_index[0]]); #if REP_ON_GEN == 1 report_on_generation(g,pop,p); #endif #if ((DEBUG == 1)||(DBSS == 1)) dump_population(pop,p); #endif valperf = validate_fitness_of_tree(numpops, numgens, pop, p, pop[p].population[pop[p].fitness_sort_index[0]]); #if REP_ON_GEN == 1 printf("\nValidation Fitness= %f\n", valperf); #endif if (!g) { pop[p].best_of_run_fitness = valperf; pop[p].best_of_run = copy_tree(pop[p].population[pop[p].fitness_sort_index[0]]); pop[p].best_of_run_gen = 0; } else if (valperf < pop[p].best_of_run_fitness) { pop[p].best_of_run_fitness = valperf; free((char *)pop[p].best_of_run); pop[p].best_of_run = copy_tree(pop[p].best_of_generation); pop[p].best_of_run_gen = g; } free_tree(pop[p].best_of_generation); } if (CHECKPOINT_FREQUENCY) { if (g && !(g % CHECKPOINT_FREQUENCY)) { checkpoint(numpops, numgens, pop, g); } } if (terminate_early(numpops,numgens,pop)) break; } /* checkpoint the last generation, if it was not just saved */ if (CHECKPOINT_FREQUENCY) { if ((numgens-1) % CHECKPOINT_FREQUENCY) { checkpoint(numpops,numgens,pop,numgens-1); } } } #ifdef ANSI_FUNC VOID dump_population( pop_struct *pop, int p ) #else VOID dump_population(pop,p) pop_struct *pop; int p; #endif { int i, index; for (i=0; i pop[global_p].normalized_fitness[*i]) { return 1; } else if (pop[global_p].normalized_fitness[*j] < pop[global_p].normalized_fitness[*i]) { return -1; } else return 0; } #ifdef ANSI_FUNC VOID sort_population_by_fitness( pop_struct *pop, int p ) #else VOID sort_population_by_fitness(pop,p) pop_struct *pop; int p; #endif { int i; global_p = p; /* kludge for fitness_compare */ for (i=0; i