📄 gpkernel.c
字号:
#endif gpop.number_in_culled_pop =0; gpop.best_of_run_num = 0; codep=0; for (i=0;i<NUM_RPBS+MAX_NUM_ADFS;i++) { bsize = GetBranchSize(i); gpop.brstart[i] = codep; gpop.brend[i] = codep+bsize-1; gpop.brsize[i] = bsize; gpop.brweight[i] = GetBranchWeight(i); codep +=bsize; } CreateIndividual(&gpop.best_so_far); CreateIndividual(&(gpop.pop_eog.ind)); CreateIndividual(&(gpop.pop_eog.spec_ind));}void CreateInitialPopulation(int max_new_tree_depth)/*;*/ /*funcdef - dgpc - CreateInitialPopulation*/{ int i,j; int min_depth; int grow, fcycle, size; int temp2; int k; Branch * br; Individual * indtemp; indtemp = &(gpop.tempind); i=0;j=0; min_depth=2; grow=0;fcycle = 0;size=0;temp2=0;k=0; InitializeIndividual(&(gpop.best_so_far)); InitializeIndividual(&(gpop.pop_eog.ind)); InitializeIndividual(&(gpop.pop_eog.spec_ind)); for (i=0; i<(gpop.pop_startup_info.num_individuals); i++) { #ifdef __BORLANDC__ fprintf(gpop.out_file,"Random for dude %d is %ld\n",i,GET_RAND()); if (i%100 == 0) printf("c%d ",i); #endif CreateIndividual(indtemp); InitializeIndividual(indtemp); for (j=0;j<NUM_RPBS+NUM_INITIAL_ADFS;j++) { if (j < NUM_RPBS) br = &(indtemp->rpbs[j]); else br = &(indtemp->adfs[j-NUM_RPBS]); #ifdef _ICC if (br == NULL) __asm{seterr;}; #endif for (k=0;k<TOTAL_NUMBER_OF_FUNCTIONS;k++) { if (((br->function_vector[k]) < -1) || ((br->function_vector[k]) > MAX_NUM_ARGS)) { #ifdef _ICC __asm{seterr;}; #else fprintf(stderr,"Function vector is hosed in create_pop "); fprintf(stderr,"branch# %d, function# %d, supposed arity %d\n", j,k,br->function_vector[k]); #endif } } if ((gpop.pop_startup_info.growth_method) == FULL) { size = max_new_tree_depth; grow = 1; } else if ((gpop.pop_startup_info.growth_method) == GROWTH) { size = max_new_tree_depth; grow = 0; } else { /*RAMPED*/ size = (min_depth + (RandomInt(1+max_new_tree_depth-min_depth))); /*size= (min_depth + (i % (max_new_tree_depth - min_depth)));*/ /*if (max_new_tree_depth != min_depth) if (!(i % (max_new_tree_depth - min_depth))) fcycle = (!fcycle);*/ grow = RandomInt(2); /*(fcycle);*/ } if (j < NUM_RPBS) { #if (USE_USER_GENERATION_RPBS) UserCreateRandomTree(&(indtemp->rpbs[j]), 0, size, 1, grow,&gpop); /* SetConstraints(&gpop); temp2 = CreateRandomTree(&(indtemp->rpbs[j]), 0, size, 1, grow,ROOT); */ #else #if (USE_CONSTRAINED_STRUCTURE) temp2 = CreateRandomCSSTree(&(indtemp->rpbs[j]),0,size,1,grow); #else temp2 = CreateRandomTree(&(indtemp->rpbs[j]), 0, size, 1, grow); #endif #endif } else { #if (USE_USER_GENERATION_ADFS) temp2 = UserCreateRandomTree((&(indtemp->adfs[(j-NUM_RPBS)])), 0, size, 1, grow,&gpop); #else #if (USE_CONSTRAINED_STRUCTURE) temp2 = CreateRandomCSSTree(&(indtemp->adfs[(j-NUM_RPBS)]), 0,size,1,grow); #else temp2 = /* CreateRandomTree((&(indtemp->adfs[(j-NUM_RPBS)])), 0, size, 1, grow); */ CreateRandomTree((&(indtemp->adfs[(j-NUM_RPBS)])), 0, size, 1, grow); #endif #endif } } indtemp->s_fitness = (float) UNDEFINED; #if (PRINT_INITIAL_POPULATION)/* #ifndef _ICC*/ PrintIndividual(indtemp,gpop.out_file);/* #endif*/ #endif CompressIndividual(indtemp, &(gpop.members[i])); } /* replace the first members of population with those to be read from a file, if filename is not null */ if ((gpop.pop_startup_info.num_primed_individuals) > 0) { /* CopyIndividual(&(gpop.pop_startup_info.primed_individual),&(gpop.members[0])); */ CompressIndividual(&(gpop.pop_startup_info.primed_individual),&(gpop.members[0])); } temp2=temp2;}/* Formerly individ.c file */int FindGoodTree(int choice_type)/*;*/ /*funcdef - dgpc - FindGoodTree */{ int i; int tournament_indices[GOOD_TOURNAMENT_SIZE]; int best_index; for (i=0;i<GOOD_TOURNAMENT_SIZE;i++) tournament_indices[i] = RandomInt((gpop.pop_startup_info.num_individuals)); best_index = tournament_indices[0]; for (i = 1; i < GOOD_TOURNAMENT_SIZE; i++) { if (choice_type == 0) { if (gpop.members[tournament_indices[i]].s_fitness < gpop.members[best_index].s_fitness) best_index = tournament_indices[i]; } else { #if (SEDUCTION == ON) if (ProbBeautyDecision(&gpop, &(gpop.members[tournament_indices[i]]), &(gpop.members[best_index]))) best_index = tournament_indices[i]; #endif } }/* if (choice_type == 1) printf("type %d best ind fit:%lf, hits:%d, beauty:%lf, avf fit:%f\n", choice_type, gpop.members[best_index].s_fitness, gpop.members[best_index].hits, gpop.members[best_index].beauty, gpop.pop_eog.average_fitness);*/ return(best_index);}int gpreproSelectRandomIndividual(Population *pop)/*;*/ /*funcdef*/{ return(RandomInt((*pop).pop_startup_info.num_individuals));}int FindTreeFromCulledPop(int sel_method)/*;*/ /*funcdef - dgpc - FindTreeFromCulledPop*/{ int i; int tournament_indices[GOOD_TOURNAMENT_SIZE]; int best_index; if (gpop.number_in_culled_pop <=0 ) { sprintf(g_str,"Error, no inds in the culled pop\n"); gpi_SendError(g_str); exit(1); } for (i=0;i<_min(GOOD_TOURNAMENT_SIZE, gpop.number_in_culled_pop);i++) tournament_indices[i] = RandomInt(gpop.number_in_culled_pop); best_index = tournament_indices[0]; for (i = 1; i < _min(GOOD_TOURNAMENT_SIZE, gpop.number_in_culled_pop); i++) { if (sel_method ==0) { if (gpop.members[gpop.culled_population[tournament_indices[i]]].s_fitness < gpop.members[gpop.culled_population[best_index]].s_fitness) best_index = tournament_indices[i]; } else { #if (SEDUCTION == ON) if (ProbBeautyDecision(&gpop, &(gpop.members[gpop.culled_population[tournament_indices[i]]]), &(gpop.members[gpop.culled_population[best_index]]))) best_index = tournament_indices[i]; #endif } } return(gpop.culled_population[best_index]);}int FindBadTree()/*;*/ /*funcdef - dgpc - FindBadTree */{ int i; int tournament_indices[BAD_TOURNAMENT_SIZE]; int worst_index; for (i=0;i<BAD_TOURNAMENT_SIZE;i++) tournament_indices[i] = RandomInt((gpop.pop_startup_info.num_individuals)); worst_index = tournament_indices[0]; for (i = 1; i < BAD_TOURNAMENT_SIZE; i++) if (gpop.members[tournament_indices[i]].s_fitness > gpop.members[worst_index].s_fitness) worst_index = tournament_indices[i]; return(worst_index);}/* Formerly repro.c file *//*Branch * GetRightBranch(Individual * dude, int bnum){ if (bnum < NUM_RPBS) return( &(dude->rpbs[bnum]) ); else return( &(dude->adfs[bnum - NUM_RPBS]) );}*/int ChoosePoint(Branch * br, /*funcdef - dgpc - ChoosePoint*/ int CurTreeSize, /*funcdef - dgpc - ChoosePoint*/ int choice)/*;*/ /*funcdef - dgpc - ChoosePoint*/{/* if choice == 0, must choose a point with 0 arity if choice == 1, must choose a point with non-zero arity if choice == 2, can choose any point*/ int rnum; int done =0; int counter =0; while (!done) { counter++; if (CurTreeSize < 2) choice = 2; rnum = RandomInt(CurTreeSize); /* if (rnum > 0 && _function_is_constant(br->tree[rnum-1].opcode)) done=0; else */ switch (choice) { case 0: if (_function_arity(br->tree[rnum].opcode) == 0) done = 1; break; case 1: if (_function_arity(br->tree[rnum].opcode) != 0) done =1; break; case 2: done =1; break; default: done=1; break; } } return(rnum);}int ValidSubtreeGivenFVector(Branch *br, int index, int * fvector)/*;*/ /*funcdef -- gpkernel.c */{int correct_flag = 1;int temp;temp = doValidSubtreeGivenFVector(br,index,fvector,&correct_flag); if (correct_flag) return(1); else return(0);}int doValidSubtreeGivenFVector(Branch *br, /*funcdef - dgpc - doValidSubtreeGivenFVector*/ int index, /*funcdef - dgpc - doValidSubtreeGivenFVector*/ int * fvector, int * pcorrect)/*;*/ /*funcdef - dgpc - doValidSubtreeGivenFVector*/{ int i,num; int tvector[TOTAL_NUMBER_OF_FUNCTIONS]; if (br->tree[index].opcode == EMPTY) { sprintf(g_str,"Error, an unused node is being accessed at %d\n in doValidSubtreeGivenFVector\n", index); gpi_SendError(g_str); exit(1); } if (index > br->num_nodes) { sprintf(g_str, "Oops!! Ran past the end of a tree, in doValidSubtreeGivenFVector!!\n"); gpi_SendError(g_str); exit(1); } else { /* BASE CASE 1: This node doesn't fit -- set pcorrect to 0 and return (must check in previous calls) BASE CASE 2: This node is a terminal -- return INDUCTION CASE: CopyFvector for each arg, call recursively, return */ num = _fv_map(br->tree[index].opcode); if (fvector[num] < 0) { *pcorrect = 0; return(index); } if (_function_arity(num) == 0) return(index); if (_function_is_constant(br->tree[index].opcode)) return(index); for (i=0;((i< _function_arity(num)) && (*pcorrect)); i++) { CopyFVector(fvector,tvector); #if (USE_CONSTRAINED_STRUCTURE) ConstrainedSyntaxFilters(tvector, br, num, i, &gpop); #endif index = doValidSubtreeGivenFVector(br,index+1,tvector,pcorrect); } } return(index);}int MakeFunctionVector(Branch *br, /*funcdef - dgpc - MakeFunctionVector*/ int index, /*funcdef - dgpc - MakeFunctionVector*/ int * fvector)/*;*/ /*funcdef - dgpc - MakeFunctionVector*//*Assumes that the fvector is filled initially with not flags (-1)*//*returns the node_index at the end of the subtree...*/{ int i; for (i=0;i<TOTAL_NUMBER_OF_FUNCTIONS;i++) fvector[i]= -1; return(doMakeFunctionVector(br,index,fvector));}/*Assumes that the fvector is filled initially with not flags (-1)*//*returns the node_index at the end of the subtree...*/int doMakeFunctionVector(Branch *br, /*funcdef - dgpc - doMakeFunctionVector*/ int index, /*funcdef - dgpc - doMakeFunctionVector*/ int * fvector)/*;*/ /*funcdef - dgpc - doMakeFunctionVector*/{ int i,num; if (br->tree[index].opcode == EMPTY) { sprintf(g_str,"Error, an unused node is being accessed at %d\n in doMakeFunctionVector\n", index); gpi_SendError(g_str);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -