📄 gpkernel.c
字号:
/*======================================================================+| PGPC: Parallel Genetic Programming in C || (c) 1995 Genetic Algorithm Technology Corp. all rights reserved || written by David Andre |+======================================================================*//*======================================================================+| FILE: gpkernel.c || DESCRIPTION: Is the main GP kernel. Runs on the meshnode breeder || process, and is the location of most major functions. || || REVISIONS: || Jan 24, 1995: Works as of today, no known bugs. || Jan 26, 1995: Added N Plus 2 Repro functionality || Feb 23, 1995: Changed crossoverop to call choose point..(2-all) for || choosing point in female parent || Mar 14, 1995: Adding function-only crossover, || Mar 14, 1995: Adding constrained syntactic generation || Mar 17, 1995: UNI-KERNEL VERSION STARTS HERE! |+======================================================================*/#include <string.h>#include <stdio.h>#ifdef __BORLANDC__#include <fcntl.h>#include <io.h>#include <sys\stat.h>#else#ifdef _ICC#include <iocntrl.h>#include <process.h>#include <channel.h>#include <semaphor.h>#endif#endif#include <stdlib.h>#include <math.h>#include <time.h>#include "gp.h"#ifdef _ICC#include "mncommon.h"#include "mnproto.h"#include "gpi.h"#else#include "gpi_stub.h"#include "pentstub.h"#endif#include "proto.h"#include "newops.h"#include <ctype.h>#define PRINT_CROSSOVER_INDS OFF/*----------------------------------------------------------------------*//* GLOBALS *//* I think that these only need to be globals on the meshnode *//* processes. *//*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/Population gpop;static char g_str[256];int g_male_selection_method;/*int g_index_ptr;*/int gpstatGetRunNum()/*;*/ /*funcdef*/{ return(gpop.pop_startup_info.run_num);}float gpstatGetBestFitness()/*;*/ /*funcdef*/{ return(gpop.best_fitness);}int gpstatGetBestSoFarHits()/*;*/ /*funcdef*/{ return(gpop.best_so_far.hits);}int gpstatGetBestIndex()/*;*/ /*funcdef*/{ return(gpop.best_dude_index);}Individual * gpstatGetBestDude()/*;*/ /*funcdef*/{ return(&(gpop.best_so_far));}Population * gpinitGetPopptr()/*;*/ /*funcdef*/{ return(&gpop);}StartupInfo * gpinitGetParamptr()/*;*/ /*funcdef*/{ return(&(gpop.pop_startup_info));} int FullCompare(Individual * ind1, Individual * ind2){ int i,j,k; int len1,len2; if (ind1->current_number_of_adfs != ind2->current_number_of_adfs) return(0); for (i=0;i<NUM_RPBS;i++) { len1 = ind1->rpbs[i].tree[0].jump; len2 = ind2->rpbs[i].tree[0].jump; if (len1 != len2) return(0); for (j=0;j<len1;j++) { if (ind1->rpbs[i].tree[j].opcode != ind2->rpbs[i].tree[j].opcode) return(0); } } for (i=0;i<ind1->current_number_of_adfs;i++) { len1 = ind1->adfs[i].tree[0].jump; len2 = ind2->adfs[i].tree[0].jump; if (len1 != len2) return(0); for (j=0;j<len1;j++) { if (ind1->adfs[i].tree[j].opcode != ind2->adfs[i].tree[j].opcode) return(0); } } return(1);}int QuickCompare(CompInd * c1, CompInd * c2){/*Returns a 1 if the two are quickly the same*/ int i; for (i=0;i<10;i++) { if (c1->code[i] != c2->code[i]) return(0); } return(1);}void init_pop_check(Population * pop){ Individual * pind1; Individual * pind2; int cind1,cind2; int i,j,k; int index,branch;#ifndef _ICC pind1 = &(pop->parent1); pind2 = &(pop->parent2); CreateIndividual(pind1); CreateIndividual(pind2); for (cind1=0;cind1<pop->pop_startup_info.num_individuals;cind1++) { for (cind2=cind1+1;cind2 < pop->pop_startup_info.num_individuals;cind2++) { if (QuickCompare(&(pop->members[cind1]),&(pop->members[cind2]))) { /*printf("f");*/ UnCompressIndividual(&(pop->members[cind1]),pind1); UnCompressIndividual(&(pop->members[cind2]),pind2); if (FullCompare(pind1,pind2) && pind1->rpbs[0].tree[0].jump > 10) { printf("S"); fprintf(pop->out_file,"\n\nPERROR:: TWO INDS ARE THE SAME!!!!!\n"); fprintf(pop->out_file,"PERROR:: They are %d and %d\n",cind1,cind2); PrintIndividual(pind1,pop->out_file); PrintIndividual(pind2,pop->out_file); fprintf(pop->out_file,"PERROR:: END OF PERROR\n\n"); } } } }#endif}void gpinitPopCreation(Population * pop)/*;*/ /*funcdef*/{ int i; for (i=0;i<MAXNUMFUNCTIONS;i++) (*pop).constants[i] = (*pop).pop_startup_info.random_constant_table[i]; SetSeed(pop,(*pop).pop_startup_info.seed); (*pop).num_constants = MAXNUMFORARANDOMCONSTANT +1; #ifndef __BORLANDC__ MakeFunctionTable(pop); #endif #if (!(BOSS_NODE_SENDS_FIT_CASES)) CreateFitnessCases (pop); /*This is a valid question -- must have a fitness-case random-number generator or some such, or must decide to allow different measures in different demes*/ #else InitFitnessCases(pop); #endif #if BREEDER_DOES_OUT_SAMPLE CreateBreederOutFitnessCases(); #endif /* Create initial random population */ InitializePopulation(); /*This will happen in the meshnodes*/ CreateInitialPopulation(((*pop).pop_startup_info.max_new_tree_depth)); sprintf(g_str,"\nPopulation Created at time\n"); gpi_SendTrace(g_str);}int gpstatTerminationQuery(int gen)/*;*/ /*funcdef*/{#ifdef USE_USER_TERMINATION#if (USE_USER_TERMINATION ==1) if (UserTermination(gen, gpop.best_fitness,&gpop)) return(1); else return(0);#endif#endif if (/*(gen < ((int)((float) (gpop.pop_startup_info.num_generations) * (float) 1.20))) &&*/ (gpop.best_fitness > RUN_END_CRITERION)) return(0);else return(1);}void gpreproReproducePopulation(int counter)/*;*/ /*funcdef*/{ ReproducePopulation((int)( counter <= LAST_EARLY_GEN),counter); /* PopCommTest(&gpop); */}void PreparePopulationForExportStep()/*;*/ /*funcdef*/{int i; gpop.num_emigrants =0; for (i=0;i<MAX_NUM_EMIGRANTS_PER_GENERATION;i++) { gpop.emigrants[i] = -1; }}void PreparePopulationForImportStep()/*;*/ /*funcdef*/{ gpop.num_immigrants =0;}void UpdateForEndOfRun(Population *spop)/*;*/ /*funcdef - gpkernel - UpdateForEndOfRun*/{CreateIndividual( &((*spop).best_so_far) );CopyIndividual(&(gpop.best_so_far),&((*spop).best_so_far));(*spop).best_of_run_num = gpop.best_of_run_num;(*spop).best_dude_index = gpop.best_dude_index;(*spop).best_fitness = gpop.best_fitness;}int FindBestAndWorst ()/*;*/ /*funcdef - dgpc - FindBestAndWorst*/{ int i, best_index, worst_index; best_index = 0; worst_index = 0; for (i=0; i<(gpop.pop_startup_info.num_individuals); i++) { if (gpop.members[i].s_fitness < gpop.members[best_index].s_fitness) best_index = i; if (gpop.members[i].s_fitness > gpop.members[worst_index].s_fitness) worst_index = i; } return(best_index);}/* Formerly initial.c file *//*For the moment, I assume that all RPBs can have all ADFs*/void InitializeIndividual(Individual * ind)/*;*/ /*funcdef - dgpc - InitializeIndividual */{ int i,j; int temp; temp=0; i=0;j=0; ind->s_fitness = (float) UNDEFINED; ind->hits = 0; ind->beauty = 0; ind->current_number_of_adfs = NUM_INITIAL_ADFS; for (i=0;i<MAX_NUM_ADFS;i++) { ind->adf_arity[i]=MIN_NUM_ADF_ARGS; #if (USE_MULTI_TYPED_ADFS) ind->adf_type[i] = -1; #endif } for (j=0;j<NUM_RPBS;j++) { for (i=0;i<gpop.num_general_functions;i++) { ind->rpbs[j].function_vector[i] = gpop.function_assignment[j][i]; temp = (int) ind->rpbs[j].function_vector[i]; #ifdef _ICC if (temp > MAX_NUM_ARGS || temp < -1) __asm{seterr;}; #endif } } for (j=0;j<MAX_NUM_ADFS;j++) { for (i=0;i<gpop.num_general_functions;i++) { ind->adfs[j].function_vector[i] = gpop.function_assignment[NUM_RPBS+j][i]; temp = (int) ind->adfs[j].function_vector[i]; #ifdef _ICC if (temp > MAX_NUM_ARGS || temp < -1) __asm{seterr;}; #endif } } for (i=0;i<MAX_NUM_NODES_PER_DUDE;i++) { ind->code[i].opcode = -1; ind->code[i].jump = -1; }}void InitializePopulation()/*;*/ /*funcdef - dgpc - InitializePopulation */{ int i,codep,bsize,temp; temp=0; for (i=0;i<NUM_RPBS+MAX_NUM_ADFS;i++) { temp+= GetBranchSize(i); } if (MAX_NUM_NODES_PER_DUDE < temp) /*( (NODES_PER_RPB * NUM_RPBS)+(NODES_PER_ADF*MAX_NUM_ADFS)))*/ { #ifdef _ICC __asm{seterr;}; #else gpi_SendError("YO! MAX_NUM_NODES IS NOT LARGE ENOUGH!!!!!\n"); exit(1); #endif } gpop.pop_size = (gpop.pop_startup_info.num_individuals); gpop.best_fitness = (float) 9999999999999.0; gpop.new_best =0; gpop.members = (CompInd *) calloc((gpop.pop_startup_info.num_individuals), sizeof(CompInd)); gpop.culled_population = (int *) calloc((gpop.pop_startup_info.num_individuals), sizeof(int)); gpop.repro_op_list = (ReproOpInfo *) calloc((gpop.pop_startup_info.num_individuals), sizeof(ReproOpInfo));#ifdef _ICC if (gpop.culled_population == NULL) __asm{seterr;}; if (gpop.members == NULL) __asm{seterr;}; if (gpop.repro_op_list == NULL) __asm{seterr;};#else if (gpop.culled_population == NULL) { fprintf(stderr,"Error in allocating space for gpop.culled_pop\n"); exit(1); } if (gpop.members == NULL) { fprintf(stderr,"Error in allocating space for gpop.members\n"); exit(1); } if (gpop.repro_op_list == NULL) { fprintf(stderr,"Error in allocating space for gpop.repro_op_list\n"); exit(1); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -