⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gpkernel.c

📁 遗传规划工具
💻 C
📖 第 1 页 / 共 5 页
字号:
		exit(1);	}	if (index > br->num_nodes)	{			sprintf(g_str,			"Oops!! Ran past the end of a tree, in FillSubtreeFunctionVector!!\n");                gpi_SendError(g_str);		exit(1);	}	else	{		num = _fv_map(br->tree[index].opcode);		fvector[num] = _function_arity(num);                if (_function_is_constant(br->tree[index].opcode))                    return(index);		for (i=0;i< _function_arity(num); i++)		{			index = doMakeFunctionVector(br,index+1,fvector);		}	}	return(index);}int CheckFVectorFit(Branch * br, /*funcdef - dgpc - CheckFVectorFit */                      int * fvector)/*;*/ /*funcdef - dgpc - CheckFVectorFit */{	int fit;	int i;	fit =1;	for (i=0;i<TOTAL_NUMBER_OF_FUNCTIONS;i++)	{		if ( (fvector[i] >=0) && (br->function_vector[i] !=   fvector[i]))			fit=0;	}	return(fit);}int FVectorSubsumeP(int * fvector_large, int * fvector_small){	int fit;	int i=0;;	fit =1;	for (i=0;i<TOTAL_NUMBER_OF_FUNCTIONS;i++)	{/*            fprintf(gpop.out_file,"i:%d, %s, large:%d,  small:%d  \n", i,gpop.func_table[i].print_name,                        fvector_large[i],                        fvector_small[i]);*/		if ( (fvector_small[i] >=0) && (fvector_large[i] !=   fvector_small[i]))			fit=0;	}	return(fit);}void CullPopulation(int * fvector)/*;*/ /*funcdef - dgpc - CullPopulation */{	int i,j;	int match;	Individual * ind;        ind = &(gpop.tempind);	gpop.number_in_culled_pop =0;	for (i=0;i<(gpop.pop_startup_info.num_individuals);i++)	{                UnCompressIndividual(&(gpop.members[i]),ind);                /*ind = &(gpop.members[i]);*/		match=0;		for (j=0;j<(NUM_RPBS + ind->current_number_of_adfs);j++)			if (CheckFVectorFit(GetRightBranch(ind,j),fvector))				match=1;		if (match)			gpop.culled_population[gpop.number_in_culled_pop++]=i;	}		if (gpop.number_in_culled_pop <=0)	{		sprintf(g_str,"OOPS!.  No Individuals in the culled population!!!\n");                gpi_SendError(g_str);		exit(1);	}}	int RandCullPopulation(int * fvector)/*;*/  /*funcdef - dgpc - RandCullPopulation */{	int i,j;	int match,counter;	Individual * ind;        gpop.number_in_culled_pop =0;        counter = 0;        ind = &(gpop.tempind);        while (gpop.number_in_culled_pop < GOOD_TOURNAMENT_SIZE &&               counter < SEARCH_LIMIT_FOR_CROSSOVER)        {            counter++;            match=0;            i = RandomInt((gpop.pop_startup_info.num_individuals));            UnCompressIndividual(&(gpop.members[i]),ind);            /*ind = &(gpop.members[i]);*/            for (j=0;j<(NUM_RPBS + ind->current_number_of_adfs);j++)            {			if (CheckFVectorFit(GetRightBranch(ind,j),fvector))                        {				match=1;                        }            }	    if (match)		gpop.culled_population[gpop.number_in_culled_pop++]=i;        }        if (counter >= SEARCH_LIMIT_FOR_CROSSOVER)            return(0);        else            return(1);}int ChooseWRandomBranch(Individual * ind){    int i,temp;    int weight_list,sum;    Branch * br;        sum=0;	 for (i=0;i<NUM_RPBS+ind->current_number_of_adfs;i++)	 {		  br = GetRightBranch(ind,i);		  sum += br->tree[0].jump;	 }	 temp = RandomInt(sum);	 i=0;	 sum=0;	 while(i < NUM_RPBS+ind->current_number_of_adfs)    {        br = GetRightBranch(ind,i);        sum += br->tree[0].jump;        if (temp < sum)        {            return(i);        }        i++;    }    gpi_SendError("Error in choose weighted by points random branch\n");    return(0);}                                		int ChooseRandomBranch(int current_num_adfs)/*;*/ /*funcdef*/{    int i,temp;    int weight_list,sum;    sum=0;    for (i=0;i<NUM_RPBS+current_num_adfs;i++)        sum+= gpop.brweight[i];    temp = RandomInt(sum);    i=0;    sum=0;    while(i < NUM_RPBS+current_num_adfs)    {        sum += gpop.brweight[i];        if (temp < sum)            return(i);        i++;    }    gpi_SendError("Error in choose random branch\n");    return(0);}int FindGoodSWBranch(Individual * parent,   /*funcdef - dgpc - FindGoodBranch*/                     int *fvector)/*;*/     /*funcdef - dgpc - FindGoodBranch*/{	int i,temp;	int fit_list[NUM_RPBS+MAX_NUM_ADFS];	int num_fit=0;        int sum;        Branch * br;        sum=0;        		for (i=0;i<(NUM_RPBS+parent->current_number_of_adfs);i++)			if (CheckFVectorFit(GetRightBranch(parent,i),fvector))                        {				fit_list[num_fit++] = i;                		br = GetRightBranch(parent,i);		                sum += br->tree[0].jump;                        }	if (num_fit==0)	{		sprintf(g_str, "Crossover female chosen incorrectly, exiting\n");                gpi_SendError(g_str);		exit(1);	}        temp=RandomInt(sum);        sum=0;        i=0;        while (i<num_fit)        {   	    br = GetRightBranch(parent,fit_list[i]);            sum += br->tree[0].jump;            if (temp < sum)    	       return(fit_list[i]);            i++;		  }	return(-1);}int FindGoodBranch(Individual * parent,   /*funcdef - dgpc - FindGoodBranch*/                     int *fvector)/*;*/     /*funcdef - dgpc - FindGoodBranch*/{	int i,temp;	int fit_list[NUM_RPBS+MAX_NUM_ADFS];	int num_fit=0;        int sum;        sum=0;        		for (i=0;i<(NUM_RPBS+parent->current_number_of_adfs);i++)			if (CheckFVectorFit(GetRightBranch(parent,i),fvector))								{				fit_list[num_fit++] = i;										  sum+= gpop.brweight[i];								}	if (num_fit==0)	{		sprintf(g_str, "Crossover female chosen incorrectly, exiting\n");					 gpi_SendError(g_str);		exit(1);	}		  temp=RandomInt(sum);		  sum=0;		  i=0;		  while (i<num_fit)		  {				sum+=gpop.brweight[fit_list[i]];				if (temp < sum)				 return(fit_list[i]);				i++;		  }	return(-1);}void DoCrossoverPrep(int level,                 /*funcdef*/                     ReproOpInfo * repro_op,    /*funcdef*/                     Individual * parent,       /*funcdef*/                     int bnum)/*;*/             /*funcdef*/{	int p1_size, cr1_size;	int crosspt1;	int cross_frag_fvector[TOTAL_NUMBER_OF_FUNCTIONS];	Branch * child_branch, *male_parent_branch;	int parent2,female_bnum;	male_parent_branch = GetRightBranch(parent, bnum);	p1_size = TraverseSubtree(male_parent_branch, 0) +1;	crosspt1 = ChoosePoint(male_parent_branch, p1_size, level);        repro_op->point1 = crosspt1;#if (POINT_TYPING   )	cr1_size = MakeFunctionVector(male_parent_branch, crosspt1,                                            cross_frag_fvector)- crosspt1 +1;        if (!RandCullPopulation(cross_frag_fvector))	   CullPopulation(cross_frag_fvector);        #if (SEDUCTION == ON)    	   parent2 = FindTreeFromCulledPop(!g_male_selection_method);        #else    	   parent2 = FindTreeFromCulledPop(0);        #endif#else	cr1_size = TraverseSubtree(male_parent_branch,crosspt1) -                                                            crosspt1 +1;        #if (SEDUCTION == ON)            parent2 = FindGoodTree(!g_male_selection_method);        #else            parent2 = FindGoodTree(0);        #endif#endif        repro_op->parent2 = parent2;        gpop.members[parent2].usage++;        UnCompressIndividual(&(gpop.members[parent2]),&(gpop.parent2));		#if (POINT_TYPING)  #ifdef SIZE_WEIGHTED_CROSSOVER_BRANCH_SELECTION	 #if (SIZE_WEIGHTED_CROSSOVER_BRANCH_SELECTION)	female_bnum = FindGoodSWBranch(&(gpop.parent2), cross_frag_fvector);	 #else	female_bnum = FindGoodBranch(&(gpop.parent2), cross_frag_fvector);	 #endif  #else	female_bnum = FindGoodBranch(&(gpop.parent2), cross_frag_fvector);  #endif#else        female_bnum = bnum;#endif        repro_op->brnum2 = female_bnum;        repro_op->next_for_parent2 = gpop.members[parent2].op_list;        gpop.members[parent2].op_list = repro_op;}#define DEBUG_GPAP OFFvoid gpap(Branch *br,int pt,ParentInfo * pinfo){    int i=0,j=0,k=0,temp=0;    int unclaimed_children=0;    pinfo->num_parents=0;    unclaimed_children=0;    #if (DEBUG_GPAP)    fprintf(stdout,"\nIN gpap. pt is %d\n",pt);    fflush(stdout);    #endif    for (i= pt-1; i>=0; i--)    {        if ((temp = ((_function_arity(br->tree[i].opcode) - unclaimed_children))) > 0)        {/*IS PARENT*/            pinfo->parents[pinfo->num_parents] = i;            pinfo->branches[pinfo->num_parents++] = unclaimed_children;            #if (DEBUG_GPAP)            fprintf(stdout,"arity %d unc %d parent %d, arg %d\n",_function_arity(br->tree[i].opcode),                                 unclaimed_children,i,unclaimed_children);            #endif            unclaimed_children =0; /*DA -- CHANGE  10-17-95*/        }        else        {/*IS NOT PARENT*/            unclaimed_children = 1 + (unclaimed_children - _function_arity(br->tree[i].opcode));        }    }}void DoCrossoverOp(int level,                       /*funcdef*/                   Individual * child,              /*funcdef*/                   ReproOpInfo * repro_op,          /*funcdef*/                   Individual * parent)/*;*/        /*funcdef*/{	int p2_size =0, cr1_size=0, cr2_size=0,temp=0;	int crosspt1=0, crosspt2=0;	int done=0;        int i=0,j=0,k=0;#if (USE_CONSTRAINED_STRUCTURE)	int cross_frag_fvector[TOTAL_NUMBER_OF_FUNCTIONS];	int fvector[TOTAL_NUMBER_OF_FUNCTIONS];        ParentInfo  pinfo;#endif	Branch * child_branch, *male_parent_branch, *female_parent_branch;	int parent2=0,female_bnum=0;        int ct=0;#if (USE_CONSTRAINED_STRUCTURE)/*        printf("TEST\n");*/        for (i=0;i<TOTAL_NUMBER_OF_FUNCTIONS;i++)        {            cross_frag_fvector[i] = -1;            fvector[i] = -1;        }        for (i=0;i<MAX_NUM_NODES_PER_DUDE/2;i++)        {            pinfo.num_parents = 0;            pinfo.parents[i] = -1;            pinfo.branches[i] = -1;        }#endif	ct=ct;	male_parent_branch = GetRightBranch(parent, repro_op->brnum1);        crosspt1 = repro_op->point1;        #if (PRINT_CROSSOVER_INDS)        fprintf(stdout,"\nIn Crossover, Male Parent: BrNum:%d,pt%d\n",repro_op->brnum1,crosspt1);        PrintIndividual(parent,stdout);        #endif        cr1_size = TraverseSubtree(male_parent_branch,crosspt1) - crosspt1+1;        parent2 = repro_op->parent2;        UnCompressIndividual(&(gpop.members[parent2]),&(gpop.parent2));		CopyIndividual(&(gpop.parent2),child);        female_bnum = repro_op->brnum2;        #if (PRINT_CROSSOVER_INDS)        fprintf(stdout,"In Crossover, Female Parent: BrNum:%d\n",repro_op->brnum2);        PrintIndividual(&(gpop.parent2),stdout);        #endif	female_parent_branch = GetRightBranch(&(gpop.parent2),female_bnum);	child_branch = GetRightBranch(child,female_bnum);	p2_size = TraverseSubtree(female_parent_branch,0) +1;#if (USE_CONSTRAINED_STRUCTURE)#if 0	temp = MakeFunctionVector(male_parent_branch, crosspt1,                    /*This is un-needed now*/                                            cross_frag_fvector)- crosspt1 +1;#endif#endif                			while (!done  && ct < MAX_ATTEMPTS)	{                ct++;             #if (USE_NO_LEAF_CROSSOVER )		crosspt2 = ChoosePoint(female_parent_branch, p2_size, level); /*IN GENERAL SHOULD BE level=2*/             #else		crosspt2 = ChoosePoint(female_parent_branch, p2_size, 2);             #endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -