primitives.cc

来自「标准的GP源代码,由Andy Singleton维护」· CC 代码 · 共 1,578 行 · 第 1/3 页

CC
1,578
字号
			retval save_inner_loop_value = inner_loop_value;			inner_loop_value = 0;#else			retval save_loop_value[loop_max_depth+2];			memcpy(save_loop_value,loop_value,sizeof(loop_value));			memset(loop_value,0,sizeof(loop_value));#endif /*ORIGINAL_LOOP*/			evalnode* save_ip = IP;//			if(target==NULL)				result = CHROMEP->evalAll ( tree );#ifdef DEBUG1			cout<<"evalutes to "<<flush;#endif //DEBUG1//			else {//				IP = target;//				result = EVAL;//			}			IP = save_ip;#ifdef ORIGINAL_LOOP			inner_loop_value = save_inner_loop_value;#else			memcpy(loop_value,save_loop_value,sizeof(loop_value));#endif /*ORIGINAL_LOOP*/#ifdef ADF_CACHE#ifdef VAR_TIMINGS			if ( (cache!=NULL) &&			    ((Primitive_error & loop_too_long_error) == 0)){				if(numarg==0) cache->add(Aux1,result,					       ThisTimmer->timmer()-start_time,					       iterate_count-start_loop);				else          cache->add(Arg1,result,					       ThisTimmer->timmer()-start_time,					       iterate_count-start_loop);}#else			if (cache!=NULL)				if(numarg==0) cache->add(Aux1,result);				else          cache->add(Arg1,result);#endif#endif	        }#ifdef DEBUG1                cout<<result<<endl;#endif //DEBUG1#ifdef BYREF		if(update_arg1) {//cout<<"calleval updating "<<last_var_used<<" to "<<result<<endl;		switch(last_var_used)			{			case Vnone:				break;			case Vaux1:				Aux1 = result;				break;			case Vaux2:				Aux2 = result;				break;			case Vaux3:				Aux3 = result;				break;			default:				if ( last_var_used > 0 &&				     last_var_used < store_limit)				    {				    store[last_var_used]       = result;				    store_used[last_var_used] |= store_written;				    }				break;			}//end switch			}//end update_arg1#endif //BYREF	      }//	if (target == NULL) calls [ tree ]--;	calls [ tree ]--;	Arg1   = save_arg1;	Arg2   = save_arg2;#ifdef BYREF	last_var_used = save_var_used;#endif //BYREF	return result;}//end calleval#ifdef MINC_ADFint static_check_loop = 0; //detect loopsint static_check(Chrome* chrome, int tree, int& values)// Perform static analysis of new chromsome. Just changed in tree// Crude but should demonstrate if the idea is worth pursuing{const int good = 0;const int recurse_penalty = 100;const int poor_function_penalty = 100;//const int memory_penalty = 20;//store_written - not used directly by static_checkstatic_check_loop++;if(static_check_loop > 1000){   cout<<"static_check looping! ("<<static_check_loop<<")"<<endl;    static_check_loop = 0;    chrome->write(PRETTY_NONE,cout); cout<<endl;    return good;               //stop it looping}const int num_checks = 2*(Max + 1);//cout<<"static_check (ADF1) "<<static_check_loop<<flush; //debugretval ans [num_checks];BOOL follow    = FALSE;BOOL new_value = FALSE;int  k         = -1;values         = 0; //assume the worstfor (int i = 0; (k<Max) && (i<num_checks); i++)       {//start call adf1 loop	 int input;	 if (!follow && new_value && (ans[i-1] < 0 || ans[i-1] > Max) )         {  input = ans[i-1];	    follow = TRUE;         }	 else         {  input  = ++k;	    follow = FALSE;         }	 //initialise everything to be consistent with queue::fitness	 Primitive_error = 0;	 Memory_errors = 0;	 Aux1 = 0;	 Aux2 = 0;	 Aux3 = 0;	 memset(store,0,sizeof(store));?	 Arg1 = input;	 Arg2 = 0;	 memset(calls,0,sizeof(calls));	 calls [ tree ] = 1; //should be in evalAll, but avoid chrome.cxx	 chrome->SetupEval();	 ans [i] = chrome->evalAll ( tree );//if(recurse_error)cout<<" RECURSE_ERROR";//if(memory_error) cout<<" MEMORY_ERROR";//cout<<" "<<Arg1<<"=>"<<ans[i]<<" "<<flush;//cout<<" "<<last_var_used<<" "<<arg1_used<<" "<<arg1_side_effect<<" ";	 if ((Primitive_error|recurse_error)!=0)  return recurse_penalty;         new_value = TRUE;	 for (int j = 0; j < i; j++)	 {		 if (ans[i] == ans[j]) new_value = FALSE;	 }	 if (new_value) values++;	if (ans[i] == i) return poor_function_penalty; //identity        };//end run adf1 to test it loopif (values <= 1)	return poor_function_penalty; //ie its a constantelse  {//chrome->write(PRETTY_NONE,cout); cout<<endl; //debug        static_check_loop = 0;	return good;  }}//end static_check#endif //MINC_ADF#endif //ADFOPDEF(IfeqEval)	//Ifeq(condition1,condition2,dothis,dothat){	retval rval;        rval=EVAL;	if (rval==EVAL)	{#ifdef TIMINGS		ThisTimmer->opt_time(IP);#endif		rval=EVAL;		IP++;           // Jump the third expression		TRAVERSE();		IP--;           // And back up one (since EVAL increments)	}	else {		IP++;		TRAVERSE();    // Jump the second expression		IP--;                                   // Back up for EVAL#ifdef TIMINGS		ThisTimmer->opt_time(IP);#endif		rval=EVAL;	}	return rval;}OPDEF(IfltEval)	//Iflt(condition1,condition2,dothis,dothat){	retval rval;        rval=EVAL;	if (rval<EVAL)	{#ifdef TIMINGS		ThisTimmer->opt_time(IP);#endif		rval=EVAL;		IP++;           // Jump the third expression		TRAVERSE();		IP--;           // And back up one (since EVAL increments)	}	else {		IP++;		TRAVERSE();    // Jump the second expression		IP--;                                   // Back up for EVAL#ifdef TIMINGS		ThisTimmer->opt_time(IP);#endif		rval=EVAL;	}	return rval;}//end IfltEvalretval loop(int step){retval rval = 0;if (loop_depth>=loop_max_depth) {#ifdef DEBUG	cout<<"Loop too nested "<<IP<<flush;#endif	Primitive_error |= loop_too_nested_error;	IP++;	if(step==0) { //skip two sub trees		TRAVERSE();		TRAVERSE();	}	else {        //skip three sub trees		TRAVERSE();		TRAVERSE();		TRAVERSE();	}	IP--;#ifdef DEBUG	cout<<" skipped to "<<IP<<endl;#endif}else {	evalnode* end_ip = NULL;#ifdef ORIGINAL_LOOP	retval save_inner_loop_value = inner_loop_value;#endif /*ORIGINAL_LOOP*/	if(step==0) {		evalnode* start_ip = IP;		retval dummy1;#ifndef ORIGINAL_LOOP		memmove(&loop_value[1],&loop_value[0],loop_depth*sizeof(retval));#endif		loop_depth++;		inner_loop_value = 0;		while((0!=(dummy1 = EVAL))&&(iterate_count<=max_iterates)) {#ifdef TIMINGS			ThisTimmer->opt_time(IP);#endif			retval dummy2 = EVAL;			end_ip = IP;			inner_loop_value++;			iterate_count++;			IP = start_ip;		}	}	else {  		int overlap;		retval new_inner_loop_value;		if(step==3) {			new_inner_loop_value = 1;			overlap = int(EVAL);			if (overlap <= 0) overlap = 1;		}		else {			new_inner_loop_value = EVAL;		}		retval limit=EVAL;		evalnode* start_ip = IP;#ifndef ORIGINAL_LOOP		memmove(&loop_value[1],&loop_value[0],loop_depth*sizeof(retval));#endif		loop_depth++;		inner_loop_value = new_inner_loop_value;		switch (step) {		case -1 : for(; (inner_loop_value >= limit)        &&			           (iterate_count <= max_iterates) ;			     iterate_count++,IP = start_ip) {#ifdef TIMINGS			          ThisTimmer->opt_time(IP);#endif			          retval dummy = EVAL;			          end_ip = IP;			          inner_loop_value--;		         }		         break;		case  1 : for(; (inner_loop_value <= limit)        && 			           (iterate_count <= max_iterates) ;			     iterate_count++,IP = start_ip) {#ifdef TIMINGS			          ThisTimmer->opt_time(IP);#endif			          retval dummy = EVAL;			          end_ip = IP;			          inner_loop_value++;			  }			  break;                case  2 : {BOOL continue_loop = TRUE;                           for(; (inner_loop_value <= limit)        &&                                     (iterate_count <= max_iterates) &&                                           continue_loop            ;                             iterate_count++,IP = start_ip) {#ifdef TIMINGS			          ThisTimmer->opt_time(IP);#endif                                  retval dummy = EVAL;                                  continue_loop = (dummy != 0);			          if(continue_loop) inner_loop_value++;			          end_ip = IP;			  }}			  break;		case 3 : { /* ForMinSumEval */			rval = inner_loop_value;			int limit2 = ((limit < 1) ||				      (limit <= max_iterates-iterate_count) )?			               int(limit): max_iterates-iterate_count;			if(limit2 < overlap) break; //nopoint running loop 			                            //answer must be one//			retval answers[limit2+1];   //incase of rounding error			retval* answers = new retval [limit2+1];			for(; (inner_loop_value <= limit)        && 			         (iterate_count <= max_iterates) ;			     iterate_count++,IP = start_ip) {#ifdef TIMINGS			          ThisTimmer->opt_time(IP);#endif			          answers[int(inner_loop_value) - 1] = EVAL;			          end_ip = IP;			          inner_loop_value++;			  }			retval min_sum = FLT_MAX;			for(int i=1; i<=(inner_loop_value-overlap); i++)  {				retval sum = 0;				for(int j = i; j < i+overlap; j++)					sum += answers[j-1];				if(sum < min_sum) {					min_sum = sum;					rval = retval(i);				}			      			  }			delete[] answers;		      }			  break;		case  4 : /* ForSumEval */			  rval = 0;			  for(; (inner_loop_value <= limit)        && 			           (iterate_count <= max_iterates) ;			     iterate_count++,IP = start_ip) {#ifdef TIMINGS			          ThisTimmer->opt_time(IP);#endif			          rval += EVAL;			          end_ip = IP;			          inner_loop_value++;			  }			  break;		default : assert (0==1);		     }//end case	}	if(end_ip != NULL)		IP = end_ip;	else {		IP++;		TRAVERSE();    // Jump the loop body		IP--;          // Back up for EVAL	}	if (iterate_count>=max_iterates) 		Primitive_error |= loop_too_long_error;	if(step <= 2)		rval = inner_loop_value;#ifdef ORIGINAL_LOOP	inner_loop_value = save_inner_loop_value;#else	memmove(&loop_value[0],&loop_value[1],loop_depth*sizeof(retval));#endif	loop_depth--;}return rval;}//end loopBOOL sub_nested_loop(Chrome* chrome, const BOOL in_a_loop, 		                     const BOOL check_i0_only = FALSE){typedef gp* gp_ptr;	chrome->ip++;	gp* probl = gp_ptr(chrome->probl);	int funcnum = FUNCNUM(chrome->expr[chrome->ip]);	int args=chrome->funclist[funcnum]->argnum;	if(!in_a_loop && (funcnum==probl->i0_funcnum))        return TRUE;	if((funcnum==probl->for_funcnum) ||	   (funcnum==probl->down_funcnum)||	   (funcnum==probl->forwhile_funcnum)) {		if(in_a_loop&&!check_i0_only)                 return TRUE;		for(int i = 0; i<(args-1) ;i++) {			if(sub_nested_loop(chrome,in_a_loop,check_i0_only))				                              return TRUE;		}		return (sub_nested_loop(chrome,TRUE,check_i0_only));	}        else if (funcnum==probl->while_funcnum) { 		if(in_a_loop&&!check_i0_only)                 return TRUE;		for(int i = 0; i<args ;i++) {			if(sub_nested_loop(chrome,TRUE,check_i0_only))				                              return TRUE;		}        } else {		for(int i = 0;i<args;i++) {			if(sub_nested_loop(chrome,in_a_loop,check_i0_only))				                              return TRUE;	}}	return FALSE;}//end sub_nested_loopBOOL nested_tree_or_i0(Chrome* chrome, int tree){	int save_ip = chrome->ip;	chrome->ip = chrome->tree_starts[tree] -1;	BOOL a = sub_nested_loop(chrome,FALSE);	chrome->ip = save_ip;	return a;}//end nested_tree_or_i0BOOL i0_not_in_loop(Chrome* chrome, int tree){	int save_ip = chrome->ip;	chrome->ip = chrome->tree_starts[tree] -1;	BOOL a = sub_nested_loop(chrome,FALSE,TRUE);	chrome->ip = save_ip;	return a;}//end nested_tree_or_i0OPDEF(ForEval)	//For(start,to,dowork){return loop(1);}//end OPDEF(ForEval) OPDEF(DownEval)	//Down(start,to,dowork){return loop(-1);}//end OPDEF(ForEval) OPDEF(WhileEval) //while(condition<>0,dowork){return loop(0);}//end OPDEF(ForEval) OPDEF(ForWhileEval)	//forwhile(start,to,condition){return loop(2); //For(start, io<=to && condition<>0)}//end OPDEF(ForWhileEval) OPDEF(ForMinSumEval)	//for(overlap,start=1,to,dowork){return loop(3);}//end OPDEF(ForMinSumEval) OPDEF(ForSumEval)	//for(start,to,sum+=dowork){return loop(4);}//end OPDEF(ForSumEval) OPDEF(I0Eval)  {        return inner_loop_value;}#ifndef ORIGINAL_LOOPOPDEF(I1Eval)  {        return loop_value[1];}OPDEF(I2Eval)  {        return loop_value[2];}OPDEF(I3Eval)  {        return loop_value[3];}OPDEF(I4Eval)  {        return loop_value[4];}#endif /*ORIGINAL_LOOP*/// // // // // // // // // // // // // // // // // // // // // // // // BOOL sub_tree_contains(Chrome* chrome, int target){	chrome->ip++;	int funcnum = FUNCNUM(chrome->expr[chrome->ip]);        if(funcnum==target)                                   return TRUE;	int args=chrome->funclist[funcnum]->argnum;	for(int i = 0;i<args;i++) {		if(sub_tree_contains(chrome,target))          return TRUE;	}	return FALSE;}//end sub_tree_containsBOOL tree_contains(Chrome* chrome, int tree, int target){	int save_ip = chrome->ip;	chrome->ip = chrome->tree_starts[tree] -1;

⌨️ 快捷键说明

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