primitives.cc

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

CC
1,578
字号
// Primitives.cc   New primitives not in primitives.cxx// W. Langdon cs.ucl.ac.uk 23 January 1995//                     based upon queue2.cc 20 Dec 1994 Revision: 1.57//                     and primitiv.cxx from Andy Singleton// $Revision: 1.47 $//#define DEBUG1 true//Modifications (in reverse order)//WBL 15 Apr 97  Add ORIGINAL_LOOP for simple inner_loop_value behaviour//WBL 12 Apr 97  Ensure LIST_LIB does not get (address1==0) bugfix//WBL  9 Apr 97  Ensure calleval arg2 != -2 since code is nasty frig which//               may not work on Alpha//WBL 27 May 96  BUGFIX ensure i0 is saved on entering loop not at start of//               forxxx primitive. ie limits use outer scope not inner//WBL  5 May 96  Add R0..R9//WBL 17 Apr 96  Add IfltEval//WBL  7 Apr 96  BUGFIX ensure loop summing EVAL answers includes last value//WBL  4 Mar 96  Add ForSumEval and change loop to accomodate it//               Add ForMinSumEval//               Add i1 to i4//WBL 26 Oct 95  Ensure modEval uses int in calculations rather than retval//               Add intEval, setEval//WBL 15 Oct 95  Add calling opt_time in ifeqeval//WBL 28 Sep 95  Add NegoneEval//WBL 22 Sep 95  Replace list.h by prob.h//WBL  2 Sep 95  BUGFIX Add support for VAR_TIMINGS//WBL  1 Sep 95  BUGFIX Ensure inner_loop_value is always zero outside loops//WBL 30 Aug 95  Support nested loops, add i0_not_in_loop//WBL 29 Aug 95  Protect swapEval from itself overwritting contents of//               buffer word store[0]. //WBL 23 Jun 95  Allow partial matching on string length in print_match//WBL 13 Jun 95  Make comptaible with gcc version 2.6.3//WBL 12 May 95  Add incrementing Memory_errors on memory addressing error//WBL  1 May 95  Add TRACE_RUN//WBL 26 Apr 95  BUGFIX print_match says complete string match if all //               components are there but they are in the wrong order, fixit//WBL 23 Apr 95  Allow aux1 as cache input if no arguments to function//WBL 22 Apr 95  Ensure ADF_CACHE still works, Move def to Primitives.h//WBL 22 Apr 95  BUGFIX. Correct value returned by forwhile loop on EVAL=0//WBL 20 Apr 95  Add TIMINGS//WBL 18 Apr 95  BUGFIX. Correct location of IP on exiting forwhile loop //WBL 18 Apr 95  Add swapEval//WBL  8 Apr 95  Make arg2_address int rather than bool//WBL 31 Mar 95  Add ARG1Eval and ARG2EVAL, AddF5 etc//WBL 31 Mar 95  Add forwhileEval, AddF7, FUNCEval//               add update_arg1 to calleval//WBL 11 Mar 95  Add tree_contains and sub_tree_contains//WBL 21 Feb 95  Performance changes: inline get_address. Make loop remember//               where end of loop is.//WBL 19 Feb 95  Add nested_loop//               Allow loops withing end point calculations of from and down//WBL 16 Feb 95  Allow partial matching on print buffer//WBL  2 Feb 95  In while loop, Start I0 from 0 rather than 1//WBL 23 Jan 95  New file#include <assert.h>#include "pch.h"#include "chrome.h"#include "prob.h"#ifdef TIMINGS#include "time.h"#endif#ifdef TRACE_RUN#include "trace.h"#endif#include "Primitives.h"#include "gp.h"#ifdef STACK_PRIM#include "stack_prim.h"#endifextern char scratch[100];#ifdef FASTEVAL	extern evalnode* IpGlobal;	extern Chrome* ChromeGlobal;#endif#define Max               10retval Arg1, Arg2;retval ARG1, ARG2;retval Aux1, Aux2, Aux3, store[store_limit];int store_used[store_limit];#ifdef BYREF#define Vnone (-4)#define Vaux3 (-3)#define Vaux2 (-2)#define Vaux1 (-1)//else store[x]int last_var_used = Vnone;#endif //BYREFint Primitive_error;int Memory_errors;#ifdef ADFint calls [ NUM_TREES]; //used to prevent recursive calls#endif //ADF#ifdef ORIGINAL_LOOPretval inner_loop_value;#else /*ORIGINAL_LOOP*/retval loop_value[loop_max_depth+2];#if(loop_max_depth==1)retval inner_loop_value;#endif#endif /*ORIGINAL_LOOP*/int iterate_count;int loop_depth;#ifdef GEN_MEMvoid myfitnessvalue::load_genetic_memory() {	memcpy(genetic_memory,	       &store[1+(-Array_bot)],	       GENETIC_MEM_SIZE*sizeof(retval));}void myfitnessvalue::read_genetic_memory() {	memcpy(&store[1+(-Array_bot)],	       genetic_memory,	       GENETIC_MEM_SIZE*sizeof(retval));}#endifint get_address(){int address = int((EVAL)+(-Array_bot)+1);if ( address <= 0 || address >= store_limit )  {	Primitive_error |= memory_error;	Memory_errors++;	store[0] = 0;        //Prevent using buffer area (depends on write)	return 0;             //safe buffer area  }else  {	return address;  }}//end get_address//**************************************************************************#ifdef ADF_CACHEvoid retval_cache::stats_init(){	calls = 0;	hits  = 0;	magic_hits = 0;	min   = MAXINT;	max   = -MAXINT;}//retval_cache adf1_cache(1000);#endif //ADF_CACHE//**************************************************************************/////////////////// Problem specific functions and terminals// Use the OPDEF,EVAL,IP and CHROMEP defines// and the code will recompile for different eval methods// Problem Specific Terminals// gcc refuses to accept these in primitiv So stuff em here!OPDEF(Prog2Eval) {EVAL; return EVAL;}//OPDEF(Prog3Eval) {EVAL;EVAL; return EVAL;}OPDEF(Qrog2Eval) {        retval first_eval=EVAL;#ifdef BYREF        int    save_last_var_used = last_var_used;#endif //BYREF	EVAL;#ifdef BYREF	last_var_used = save_last_var_used;#endif //BYREF	return first_eval;}//OPDEF(Qrog3Eval) {retval first_eval=EVAL;EVAL;EVAL; return first_eval;}OPDEF(zeroEval) {return 0;}OPDEF(oneEval) {return 1;}OPDEF(NegoneEval) {return -1;}OPDEF(twoEval) {return 2;}OPDEF(MaxEval) {return Max;}OPDEF(BotEval) {return Array_bot;}OPDEF(TopEval) {return Array_top;}OPDEF(R0Eval) {return RandomValue[0];}OPDEF(R1Eval) {return RandomValue[1];}OPDEF(R2Eval) {return RandomValue[2];}OPDEF(R3Eval) {return RandomValue[3];}OPDEF(R4Eval) {return RandomValue[4];}OPDEF(R5Eval) {return RandomValue[5];}OPDEF(R6Eval) {return RandomValue[6];}OPDEF(R7Eval) {return RandomValue[7];}OPDEF(R8Eval) {return RandomValue[8];}OPDEF(R9Eval) {return RandomValue[9];}OPDEF(modEval) { //see workbook III 10 June 1994                 //    workbook IV  3 August 1994       int a = int(EVAL);       int b = abs(int(EVAL));       if (b!=0) return retval((a % b + b) % b);       else      return retval(a);}OPDEF(intEval) {return int(EVAL);}OPDEF(Arg1Eval) {return Arg1;}OPDEF(Arg2Eval) {return Arg2;}OPDEF(ARG1Eval) {return ARG1;}OPDEF(ARG2Eval) {return ARG2;}OPDEF(Aux1Eval) {#ifdef BYREF        last_var_used=Vaux1;#endif //BYREF        return Aux1;}OPDEF(Aux2Eval) {#ifdef BYREF        last_var_used=Vaux2;#endif //BYREF        return Aux2;}OPDEF(Aux3Eval) {#ifdef BYREF        last_var_used=Vaux3;#endif //BYREF        return Aux3;}OPDEF(inc_Aux1Eval)  {#ifdef BYREF        last_var_used=Vaux1; #endif //BYREF        return ++Aux1;}OPDEF(Minc_Aux1Eval) {#ifdef BYREF        last_var_used=Vaux1; #endif //BYREF        return Aux1 = ((int(++Aux1))%Max+Max)%Max;}OPDEF(dec_Aux1Eval)  {#ifdef BYREF        last_var_used=Vaux1;#endif //BYREF        return --Aux1;}OPDEF(inc_Aux2Eval)  {#ifdef BYREF        last_var_used=Vaux2; #endif //BYREF        return ++Aux2;}OPDEF(Minc_Aux2Eval) {#ifdef BYREF        last_var_used=Vaux2;#endif //BYREF        return Aux2 = ((int(++Aux2))%Max+Max)%Max;}OPDEF(dec_Aux2Eval)  {#ifdef BYREF        last_var_used=Vaux2;#endif //BYREF        return --Aux2;}OPDEF(inc_Aux3Eval)  {#ifdef BYREF        last_var_used=Vaux3;#endif //BYREF        return ++Aux3;}OPDEF(Minc_Aux3Eval) {#ifdef BYREF        last_var_used=Vaux3;#endif //BYREF        return Aux3 = ((int(++Aux3))%Max+Max)%Max;}OPDEF(dec_Aux3Eval)  {#ifdef BYREF        last_var_used=Vaux3;#endif //BYREF        return --Aux3;}retval readdata(int addr){int address = addr+(-Array_bot)+1;if ( address <= 0 || address >= store_limit )  {	Primitive_error |= memory_error;	Memory_errors++;	return 0;  }else {       if (store_used[address]==0)               store_used[address] = store_read_unassigned;#ifdef BYREF       last_var_used = address;#endif //BYREF       return store[address];}}//end readdataOPDEF(readEval) {       int address = get_address();       if (store_used[address]==0)               store_used[address] = store_read_unassigned;#ifdef BYREF       last_var_used = address;#endif //BYREF       return store[address];}OPDEF(writeEval) {       int address = get_address();        retval old = store[address];       store[address] = EVAL;       store_used[address] = store_used[address] | store_written;#ifdef BYREF       last_var_used = address;#endif //BYREF       return old;                // copy what Tesler does}OPDEF(setEval) {       int address = get_address();        store[address] = EVAL;       store_used[address] = store_used[address] | store_written;#ifdef BYREF       last_var_used = address;#endif //BYREF       return store[address];}OPDEF(swapEval) {       int address1 = get_address();       if (store_used[address1]==0)               store_used[address1] = store_read_unassigned;       int address2 = get_address();       if (store_used[address2]==0)               store_used[address2] = store_read_unassigned;       store_used[address1] |= store_written;       store_used[address2] |= store_written;#ifdef LIST_LIB       retval temp = store[address1];#else       retval temp = (address1==0)? 0:store[address1];#endif       store[address1] = store[address2];       store[address2] = temp;#ifdef BYREF       last_var_used = address2;#endif //BYREF       return 1;}void blockset(const int address, const int size, const retval data[]){	if(address<=0 || (address+size-1) >= store_limit ) return;	for(int i = address; i<=(address+size-1); i++) {		store_used[i] |= store_written;}#ifdef BYREF	last_var_used = address+size-1;#endif //BYREF	memcpy(&store[address],data,size*sizeof(retval));	return;}//end blockset//OPDEF(write_Aux1Eval) {//       retval old = Aux1;//       Aux1 = EVAL;//       return old;                // copy what write (ie Tesler) does//}//OPDEF(write_Aux2Eval) {//       retval old = Aux2;//       Aux2 = EVAL;//       return old;                // copy what write (ie Tesler) does//}OPDEF(set_Aux1Eval) {       Aux1 = EVAL;#ifdef BYREF       last_var_used = Vaux1;#endif //BYREF       return Aux1;                // Nb simplier than Tesler}OPDEF(set_Aux2Eval) {       Aux2 = EVAL;#ifdef BYREF       last_var_used = Vaux2;#endif //BYREF       return Aux2;                // Nb simplier than Tesler}OPDEF(set_Aux3Eval) {       Aux3 = EVAL;#ifdef BYREF       last_var_used = Vaux3;#endif //BYREF       return Aux3;                // Nb simplier than Tesler}#ifdef ADF#ifdef ADF_CACHEretval calleval (int numarg, int tree,                  BOOL update_arg1, int arg2_address,                 retval_cache* cache)#else#ifdef BYREFretval calleval (int numarg, int tree,                  BOOL update_arg1/* = FALSE*/, int arg2_address /* = -1*///		 ,evalnode* target = NULL		 )#elseretval calleval (int numarg, int tree )#endif //BYREF#endif //ADF_CACHE{#ifdef TIMINGS	ThisTimmer->base_time(tree);#endif#ifdef TRACE_RUN	ThisTrace->trace(tree);#endif	retval result;	retval save_arg1 = Arg1;	retval save_arg2 = Arg2;#ifdef BYREF	int save_var_used = last_var_used;#endif //BYREF	retval t1 = 0;	retval t2 = 0;#ifdef BYREF	int var_used = Vnone;#endif //BYREF	if ( numarg > 0 )		{ #ifdef BYREF                  last_var_used = Vnone; #endif //BYREF		  t1 = EVAL;#ifdef BYREF		  var_used = last_var_used;#endif //BYREF	        }	if ( numarg > 1 ) {#ifdef ADF_CACHE #define ADF_OR_BYREF true#endif#ifdef BYREF#define ADF_OR_BYREF true#endif#ifdef ADF_OR_BYREF		if     (arg2_address == -1) 			t2 = EVAL;		else if(arg2_address == -2) {assert(0==-2);		//t2 = IP;			IP++;           // Jump the expression			TRAVERSE();			IP--;           // back up one (since EVAL increments)		}		else			t2 = arg2_address;#else		t2 = EVAL;#endif	}	assert ( numarg <= 2 ); 	Arg1 = t1;	Arg2 = t2;//	if ( (target == NULL) && (calls [ tree ]++ > 1) )	if ( calls [ tree ]++ > 1 )	      { Primitive_error |= recurse_error;	        result = 0;	      }	else  { #ifdef DEBUG1		cout<<"Tree ("<<tree<<") ";		if(numarg==1) cout<<"Arg1="<<Arg1<<" "<<flush;		if(numarg==0) cout<<"Aux1="<<Aux1<<" "<<flush;#endif //DEBUG1#ifdef ADF_CACHE		if (cache==NULL || #ifdef VAR_TIMINGS		    (numarg==1 &&cache->held(Arg1,&result,ThisTimmer)==FALSE)||		    (numarg==0 &&cache->held(Aux1,&result,ThisTimmer)==FALSE))#else		    (numarg==1 && cache->held(Arg1,&result)==FALSE) ||		    (numarg==0 && cache->held(Aux1,&result)==FALSE)   )#endif#endif		{#ifdef ADF_CACHE#ifdef VAR_TIMINGS			run_time::timetyp start_time = ThisTimmer->timmer();			int start_loop = iterate_count;#endif#endif#ifdef ORIGINAL_LOOP

⌨️ 快捷键说明

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