📄 gpi_stub.c
字号:
/*======================================================================+| PGPC: Parallel Genetic Programming in C || (c) 1995 Genetic Algorithm Technology Corp. all rights reserved || written by David Andre |+======================================================================*//*======================================================================+| FILE: gpi_stub.c || DESCRIPTION: Handles communication between the i/o code and the gp || kernel for the uniprocessor stub version. || || REVISIONS: || Jan 24, 1995: Works as of today, no known bugs. |+======================================================================*//* gpi_stub *//* This is the pentium version of the gpi file -- printing to screen andfile rather than dealing with any messaging....*/#include <string.h>#include <stdio.h>#include <fcntl.h>/*#include <sys/io.h>*/#include <sys/stat.h>#include <stdlib.h>#include <math.h>#include <time.h>#include <ctype.h>#include "gp.h"#include "proto.h"#include "gpi_stub.h"#include "gpshared.h"#include "pentstub.h"char g_str[256];extern Population gpop;void gpi_PopCreation(Population * pop)/*;*/ /*funcdef*/{ gpinitPopCreation(pop);}void gpi_EvaluateGeneration(int gen,Population *popptr)/*;*/ /*funcdef*/{int i;int j;int ne;float fit_sum,var_sum;Individual *dude;dude = &(popptr->tempind);fit_sum=0.0;popptr->pop_eog.ps_count = 0;#if (HISTOGRAMS) for (i=0;i<NUM_BUCKETS;i++) { (*popptr).pop_eog.histogram.histogram[i] = 0; }#endif /*Add code to check heartbeat level....*/ ne=0; #if (GENERATION_VARIED_FITNESS) CreateFitnessCases(popptr); #endif for (i=0;(i<(popptr->pop_startup_info.num_individuals)) && (!gpentTerminationQuery(gen)); i++) { #if FIT_CASES_CHANGE_BY_GEN if (1) #else if (popptr->members[i].s_fitness == (float)UNDEFINED) #endif {/* printf("%d ",i);*/ ne++; #if (!LOW_GEN_OUTPUT) if (i% NUM_PER_DOT == 0) printf("."); #endif UnCompressIndividual(&(popptr->members[i]),dude); popptr->pop_eog.ps_count += EvalFitnessOfDude(dude,popptr); popptr->members[i].s_fitness = dude->s_fitness; popptr->members[i].hits = dude->hits; popptr->members[i].beauty = dude->beauty; if (popptr->members[i].s_fitness < 0) { sprintf(g_str,"Error, oops, fitness less than 0 out of eval fit of dude\n"); gpi_SendError(g_str); exit(1); }/* if (popptr->members[i].s_fitness == popptr->best_fitness) printf(" ==, f=%f,b=%f\n",dude->s_fitness,dude->beauty);*/ #if (SEDUCTION) if (popptr->members[i].s_fitness < popptr->best_fitness || ((popptr->members[i].s_fitness == popptr->best_fitness) && dude->beauty < popptr->best_so_far.beauty)) #else #if (HILL_CLIMBING) if (popptr->members[i].s_fitness <= popptr->best_fitness) #else if (popptr->members[i].s_fitness < popptr->best_fitness) #endif #endif {/* printf("Wf=%f,b=%f\n",dude->s_fitness,dude->beauty);*/ #if (!LOW_GEN_OUTPUT) popptr->new_best =1; #else if (popptr->members[i].s_fitness < popptr->best_fitness) popptr->new_best =1; #endif CopyIndividual(dude, &(popptr->best_so_far)); popptr->best_fitness = popptr->best_so_far.s_fitness; popptr->best_dude_index = i; popptr->best_of_run_num =gen; for (j=0;j<NUM_EXTRA_SLOTS;j++) { popptr->best_extra.extra_ints[j] = popptr->temp_extra.extra_ints[j]; popptr->best_extra.extra_floats[j] = popptr->temp_extra.extra_floats[j]; #if (USER_EXTRA_BEST_INFO) UserExtraInfoCopy(j,&(popptr->temp_extra.user_extra_info[j]), &(popptr->best_extra.user_extra_info[j])); #endif } } #if (HILL_CLIMBING) else { CopyIndividual(&(popptr->best_so_far),dude); CompressIndividual(dude,&(popptr->members[i])); } #endif } if (i==0) popptr->pop_eog.worst_fitness = popptr->members[i].s_fitness; else if (popptr->members[i].s_fitness > popptr->pop_eog.worst_fitness) popptr->pop_eog.worst_fitness = popptr->members[i].s_fitness; fit_sum+= popptr->members[i].s_fitness;/* printf("%.4f ",popptr->members[i].s_fitness); */#if (HISTOGRAMS) ((*popptr).pop_eog.histogram.histogram[popptr->members[i].hits])++;#endif } popptr->pop_eog.average_fitness = (fit_sum / (float)popptr->pop_startup_info.num_individuals); if (gen==0) popptr->pop_eog.delta_avf = 0.0; else popptr->pop_eog.delta_avf = popptr->last_avf - popptr->pop_eog.average_fitness; popptr->last_avf = popptr->pop_eog.average_fitness; var_sum=0.0; for (i=0;(i<(popptr->pop_startup_info.num_individuals));i++) { var_sum +=((popptr->members[i].s_fitness - popptr->pop_eog.average_fitness)* (popptr->members[i].s_fitness - popptr->pop_eog.average_fitness)); } popptr->pop_eog.variance_of_fitness = (var_sum / (float) popptr->pop_startup_info.num_individuals); /*printf("ne%d ",ne);*/}void gpi_ReproducePopulation(int counter)/*;*/ /*funcdef*/{ counter=counter; gpreproReproducePopulation(counter);}void gpi_DoEndOfGenMessage(int counter, Population * pop)/*;*/ /*funcdef*/{ struct tm *now; time_t clck; int i,done; float old_fitness; static float old_out_fit; static int old_out_hits; int old_hits; static GTYPE gbest = (GTYPE) 999999999999.9; time(&clck); now = localtime (&clck); #if (LOW_GEN_OUTPUT) if (gpstatGetBestFitness() < gbest || pop->new_best) { #endif fprintf(stderr, "\n%s %d G:%d BF:%.4f H:%d psC:%d ", ((*pop).problem), ((*pop).pop_startup_info.run_num), counter, gpstatGetBestFitness(), (*pop).best_so_far.hits, pop->pop_eog.ps_count); fprintf(stderr,"wf:%.3f avf:%.3f varf:%.4f df:%.4f", pop->pop_eog.worst_fitness, pop->pop_eog.average_fitness, pop->pop_eog.variance_of_fitness, pop->pop_eog.delta_avf); #if (LOW_GEN_OUTPUT) } #endif #if (!DONT_PRINT_EXTRA_TO_SCREEN) PrintProblemSpecificInfo(stderr,pop); #endif fflush(stderr); #if (LOW_GEN_OUTPUT) if (gpstatGetBestFitness() < gbest || pop->new_best) { #endif fprintf((*pop).out_file, "%s %d G:%d BF:%.4f H:%d psC:%d ",((*pop).problem), ((*pop).pop_startup_info.run_num), counter, gpstatGetBestFitness(), (*pop).best_so_far.hits, pop->pop_eog.ps_count); fprintf(pop->out_file,"wf:%.3f avf:%.3f varf:%.4f df:%.4f %s", pop->pop_eog.worst_fitness, pop->pop_eog.average_fitness, pop->pop_eog.variance_of_fitness, pop->pop_eog.delta_avf, asctime(now)); fflush((*pop).out_file); #if (LOW_GEN_OUTPUT) } #endif #if (ALWAYS_PRINT_PROB_SPECIFIC_INFO) PrintProblemSpecificInfo((*pop).out_file,pop); #endif fflush((*pop).out_file); if ((gpstatGetBestFitness() < gbest) || pop->new_best) { #if SPICE_PROBLEM PrintProblemSpecificInfoForBest(gpop.out_file,&gpop); #endif #ifdef NUM_OUT_SAMPLE_FIT_CASES #if (NUM_OUT_SAMPLE_FIT_CASES > 0) old_fitness = pop->best_so_far.s_fitness; old_hits = pop->best_so_far.hits; fprintf((*pop).out_file,"OUT_OF_SAMPLE: "); printf("OUT_OF_SAMPLE: "); EvalOutSampleFitnessOfDude(&(pop->best_so_far),pop); printf("fit: %f, hits %d\n",pop->best_so_far.s_fitness,pop->best_so_far.hits); fflush(stdout); fprintf((*pop).out_file,"fit: %f, hits %d\n",pop->best_so_far.s_fitness,pop->best_so_far.hits); fflush(pop->out_file); old_out_hits = pop->best_so_far.hits; old_out_fit = pop->best_so_far.s_fitness; pop->best_so_far.s_fitness = old_fitness; pop->best_so_far.hits = old_hits; #endif #endif #if (!ALWAYS_PRINT_PROB_SPECIFIC_INFO) PrintProblemSpecificInfo((*pop).out_file,pop); #endif PrintIndividual(&(pop->best_so_far),(*pop).out_file);/* PrintProblemSpecificIndividual(&(pop->best_so_far),pop);*/ fflush((*pop).out_file); gbest = gpstatGetBestFitness(); } pop->new_best=0; #if (!LOW_GEN_OUTPUT) #ifdef NUM_OUT_SAMPLE_FIT_CASES #if (NUM_OUT_SAMPLE_FIT_CASES > 0) printf("OUT fit %f hits %d\n",old_out_fit,old_out_hits); #else printf("Best Fitness So FAR: %f, hits %d\n",gpop.best_fitness,gpop.best_so_far.hits); #endif #endif #endif fflush((*pop).out_file);done=done;i=i;}void gpi_DoEurekaMessage(int counter, Population * pop,time_t start_time)/*;*/ /*funcdef*/{time_t end_time;double elapsed_time;FILE *fp; time(&end_time); elapsed_time = 1234.56; /*difftime(end_time,start_time);*/ if ((*pop).best_so_far.s_fitness < RUN_END_CRITERION) { fprintf(stderr,"A solution has been found\n"); fprintf(stderr,"on gen %d, best_fitness %.5f, hits %d\n",counter, pop->best_so_far.s_fitness,pop->best_so_far.hits); fprintf((*pop).out_file,"A solution has been found\n"); fprintf((*pop).out_file,"on gen %d, best_fitness %.5f, hits %d\n",counter, pop->best_so_far.s_fitness,pop->best_so_far.hits); PrintProblemSpecificInfo((*pop).out_file,pop); PrintIndividual(&(pop->best_so_far),(*pop).out_file); sprintf(g_str,"%s/P%6.6d.out", ((*pop).problem), ((*pop).pop_startup_info.perpetual_number)); fprintf((*pop).out_file, "RSTAT SUCCESS: %d %d %d %s\n", counter+1, (counter+1) * (pop->pop_startup_info.num_individuals), (int)elapsed_time, g_str); fp = fopen("archive.out","a"); fprintf(fp, "RSTAT SUCCESS: %d %d %d %s\n", counter+1, (counter+1) * (pop->pop_startup_info.num_individuals), (int)elapsed_time, g_str); fclose(fp); } else { fprintf(stderr,"The run has reached the maximum number of gens\n"); fprintf(stderr,"No solution was found\n"); fprintf((*pop).out_file,"The run has reached the maximum number of gens\n"); fprintf((*pop).out_file,"No solution was found\n"); sprintf(g_str,"%s/P%6.6d.out", ((*pop).problem), ((*pop).pop_startup_info.perpetual_number)); fprintf((*pop).out_file, "RSTAT FAILURE: %d %d %d %s\n", counter+1, (counter+1) * (pop->pop_startup_info.num_individuals), (int)elapsed_time, g_str); fp = fopen("archive.out","a"); fprintf(fp, "RSTAT FAILURE: %d %d %d %s\n", counter+1, (counter+1) * (pop->pop_startup_info.num_individuals), (int)elapsed_time, g_str); fclose(fp); }}void gpi_CondSendTrace(char *str){ gpi_SendTrace(str);}void gpi_SendTrace(char * str)/*;*/ /*funcdef*/{ fprintf(stderr,"TRACE: %s\n",str); fprintf(gpop.out_file,"TRACE: %s\n",str);}void gpi_SendError(char *str)/*;*/ /*funcdef*/{ fprintf(stderr,"ERROR: %s\n",str); fflush(stderr); if (gpop.out_file != NULL) fprintf(gpop.out_file,"ERROR: %s\n",str); fflush(gpop.out_file);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -