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

📄 calc.cc

📁 标准的GP源代码,由Andy Singleton维护
💻 CC
📖 第 1 页 / 共 2 页
字号:
// calc.cc   File to demonstrate evolution of calclator, build with GPQUICK-2.1// W. Langdon cs.ucl.ac.uk 24 Sep 1995 $Revision: 1.20 $//                     based upon brack.cc Revision: 1.27//#define DEBUG //Modifications (in reverse order)//WBL 18 Apr 97 Easter release. Use KARP2 symbol to switch between //              stack primitives and indexed memory versions of the problem.//              Other reported versions can be restore by undoing comments//              rename div as divide//WBL 17 Dec 95 Go back to two adfs and restore SUBR and DIVR//WBL 14 Dec 95 Try KARP2 again with three adfs rather than 2//WBL 10 Dec 95 Now try KARP2 again (all prims all trees)//WBL  5 Dec 95 Restore to near r1.3 (cf changes to gp.test.rpn--r1.11)//              PROG2, 1, aux, set_aux all trees. (Dont restore ifs or max//WBL  5 Dec 95 Restore near r1.6 (but no SUBR DIVR)//WBL  4 Dec 95 BUGFIX ensure max_fitness is set correctly (given intermediate//              scores in gp.test//WBL  2 Dec 95 Restore normal SUB and DIV (for RPN2) and the other //              "unneccessary primitives (except Max, Makenull and Empty)//              Use more detailed scoring scheme in gp.test//WBL 30 Nov 95 Revert to r1.3 (integer score type, fvalue==hits[0])//WBL 30 Nov 95 Remove unnecessary primtives, //              Use argument reversed arithemtic operators  //WBL 28 Nov 95 Convert to floating point scoretype//requires MULTREE to be defined#include <assert.h>#include "pch.h"#include "chrome.h"#include "primitiv.h"#ifdef TRACE_RUN#include "trace.h"#endif#ifdef TIMINGS#include "time.h"#endif#include "Primitives.h"#include "gp.h"#include "test.h"#ifdef KARP2#include "stack_prim.h"#endif#ifdef KARP2#ifdef RPN#define version "RPN 2. $Revision: 1.20 $"#else#define version "calc2. $Revision: 1.20 $"#endif#else#ifdef RPN#define version "RPN. $Revision: 1.20 $"#else#define version "calc. $Revision: 1.20 $"#endif#endifextern char scratch[100];#ifdef FASTEVAL	extern evalnode* IpGlobal;	extern Chrome* ChromeGlobal;#endifextern Pop*  ThisPop;extern test* ThisTest;#ifdef RPNconst int tree_arg_num[last_op+1] = {1,0,0,0,0};const BOOL tree_return_value[last_op+1] = {FALSE,  TRUE, TRUE, TRUE, TRUE};#elseconst int tree_arg_num[last_op+1] = {0,1,0,0,0,0,0,0};const BOOL tree_return_value[last_op+1] = {TRUE,  FALSE, FALSE, FALSE,                                           FALSE, FALSE, FALSE, FALSE};#endifint num_sol_found = 0;//used to report solution to each test phase foundBOOL sol_found = FALSE;//used to report 1st total solution found                       //Once found there are no thresholds on mem or cpu usageBOOL current_phase_reported = FALSE;//Used by gen_gens                       //should be in dumpfile but ignore this for time being //**************************************************************************void write_problem_params(){cout<<store_limit<<"["<<Array_bot<<".."<<Array_top<<"] ";cout<<mem_penalty_bot<<" "<<cpu_penalty_bot<<endl;}// Define your FitnessValue classvoid myfitnessvalue::write(ostream& fout){	{char buf[80]; sprintf(buf,"%4d",int(fvalue));	 fout << buf;}#ifdef PARETO        scoretype total = 0;	fout << " {";	for (int i=first_op; i <= last_op; i++)		{ total += hits[i];		  char buf[80]; sprintf(buf,"%4d",hits[i]);		  fout << buf << ",";	        }	fout << "[";	for (i=0; i < NUM_OTHERS; i++)		{ char buf[80]; sprintf(buf,"%4d",hits[i+last_op+1]);		  fout << buf;		  if (i < NUM_OTHERS-1 ) fout << ",";	        }	fout << "]}= " << total;#endif	fout << " mem " << int(mem_used);	fout <<","<<Memory_errors();#ifdef MINC_ADF	fout << " adf1ver " << adf1;#endif#ifdef TIMINGS	fout << " cpu " << cpu_time;#endif	fout << " tests " << Ntests_run;	fout << " seed " << test_seed;#ifdef TRACE_RUN	fout << " (";	int notrun[NUM_TREES];	missed(this, notrun);	for (i=0; i < NUM_TREES; i++)		{ char buf[80]; 		  sprintf(buf,"%4d", ok(i)); fout << buf;		  {if(Tree_OK(this,i))			   fout<<"K"; else fout<<"-";}		  sprintf(buf,"%-4d",nak(i)); fout << buf;		  sprintf(buf,"%-4d",notrun[i]); fout << buf;		  	        }	fout << ")";#endif}//end myfitnessvalue::writevoid cache_init(){}void cache_stats_init(){}void cache_stats(){}//**************************************************************************///////////////////////// PROBLEM definition#ifndef KARP2OPDEF(Adf1Eval)     {return calleval (0, adf1);}OPDEF(Adf2Eval)     {return calleval (1, adf2);}OPDEF(Adf3Eval)     {return calleval (2, adf3);}#endif /*KARP2*/OPDEF(ifmatchEval){//if matching brackets dothis else dothat	retval r1 = EVAL;	switch(r1){	case  5: case 13: case 31: case 43: //opening bracket#ifdef TIMINGS		ThisTimmer->opt_time(IP);#endif		retval r2 = EVAL;		if((r1== 5&&r2== 71)||                   (r1==13&&r2==103)||                   (r1==31&&r2==137)||                   (r1==43&&r2==167)   ) { //matching closing bracket#ifdef TIMINGS		ThisTimmer->opt_time(IP);#endif			retval r3 = EVAL;			IP++;           // Jump the expression			TRAVERSE();			IP--;           // back up one (since EVAL increments)			return r3;		}		else {			IP++;           // Jump the expression			TRAVERSE();			IP--;           // back up one (since EVAL increments)#ifdef TIMINGS		ThisTimmer->opt_time(IP);#endif			return EVAL;		}	default:		IP++;           // Jump the 2nd arg		TRAVERSE();		IP--;           // back up one (since EVAL increments)		IP++;           // Jump the 3rd arg		TRAVERSE();		IP--;           // back up one (since EVAL increments)#ifdef TIMINGS		ThisTimmer->opt_time(IP);#endif		return EVAL;	}}#ifdef KARP2OPDEF(ifemptyEval){//if stack is empty dothis else dothat	retval r1 = emptyEval();	if(r1==0){#ifdef TIMINGS		ThisTimmer->opt_time(IP);#endif			retval r3 = EVAL;			IP++;           // Jump the expression			TRAVERSE();			IP--;           // back up one (since EVAL increments)			return r3;		}		else {			IP++;           // Jump the expression			TRAVERSE();			IP--;           // back up one (since EVAL increments)#ifdef TIMINGS		ThisTimmer->opt_time(IP);#endif			return EVAL;		}}#endif /*KARP2*/OPDEF(ifopenEval){//if open bracket dothis else dothat	retval r1 = EVAL;	switch(r1){	case  5: case 13: case 31: case 43: //opening bracket#ifdef TIMINGS		ThisTimmer->opt_time(IP);#endif		retval r3 = EVAL;		IP++;           // Jump the expression		TRAVERSE();		IP--;           // back up one (since EVAL increments)		return r3;	default:		IP++;           // Jump the 2nd arg		TRAVERSE();		IP--;           // back up one (since EVAL increments)#ifdef TIMINGS		ThisTimmer->opt_time(IP);#endif		return EVAL;	}}OPDEF(AEval)    {return   5;}OPDEF(A_Eval)   {return  71;}OPDEF(BEval)    {return  13;}OPDEF(B_Eval)   {return 103;}OPDEF(CEval)    {return  31;}OPDEF(C_Eval)   {return 137;}OPDEF(DEval)    {return  43;}OPDEF(D_Eval)   {return 167;}int Adf1_Insert_funcnum = -1;int Adf1_Delete_funcnum = -1;int Adf1_Locate_funcnum = -1;int Adf1_Printlist_funcnum = -1;int ifeq_funcnum = -1;int iflte_funcnum = -1;int ifopen_funcnum = -1;int ifmatch_funcnum = -1;int ifempty_funcnum = -1;gp::gp():i0_funcnum(0),         for_funcnum(0),	 down_funcnum(0),	 while_funcnum(0),	 forwhile_funcnum(0), 	 arg1_funcnum(0),	 arg2_funcnum(0),	 ARG1_funcnum(0),	 ARG2_funcnum(0),	 FUNC_funcnum(0), 	 print_funcnum(0){//cout<<"gp::gp"<<endl;//debug	ProbVersion = version;	assert ( NUM_TREES >= NUM_OPERATIONS );	AddF(new ConstFunc(0));    // REQUIRED as function 0	// Add some standard primitives	AddFall(new AddFunc(100));	AddFall(new SubFunc(100));//	AddFall(new SubRFunc(100)); r1.19	AddFall(new MulFunc(100));	AddFall(new DivFunc(100));//	AddFall(new DivRFunc(100)); r1.19	AddFall(new Function(2,0,100,Prog2Eval,"PROG2"));//	AddFall(new IflteFunc(100));//	iflte_funcnum=funccount-1;//	AddFall(new Function(4,0,100,IfeqEval,"Ifeq"));//	ifeq_funcnum=funccount-1;	AddFall(new Function(0,0,100,zeroEval,"0"));	AddFall(new Function(0,0,100,oneEval,"1"));//	AddFall(new Function(0,0,100,MaxEval,"max"));	AddFall(new Function(0,0,100,Aux1Eval,"aux1"));	AddFall(new Function(1,0,100,set_Aux1Eval,"Set_Aux1"));#ifdef KARP2	AddF1(num,new Function(0,0,100,Arg1Eval,"arg1"));        arg1_funcnum=funccount-1;	new stack_prim(100); //delete when prog aborts//	AddFall(new Function(0,0,100,makenullEval,"Makenull"));//	AddFall(new Function(0,0,100,emptyEval,   "Empty"));	AddFall(new Function(0,0,100,topEval,     "Top"));	AddFall(new Function(0,0,100,popEval,     "Pop"));	AddFall(new Function(1,0,100,pushEval,    "Push"));#else//	AddF3(num,adf2,adf3, new Function(0,0,100,Arg1Eval,"arg1"));	AddF2(num,adf2, new Function(0,0,100,Arg1Eval,"arg1"));        arg1_funcnum=funccount-1;//	AddF1(adf3, new Function(0,0,100,Arg2Eval,"arg2"));	AddFall(new Function(1,0,100,readEval,"read"));	AddFall(new Function(2,0,100,writeEval,"write"));	AddFall(new Function(0,0,100,inc_Aux1Eval,"inc_aux1"));	AddFall(new Function(0,0,100,dec_Aux1Eval,"dec_aux1"));//	AddF2(adf1,adf2, new Function(1,0,100,readEval,"read"));//	AddF2(adf1,adf2, new Function(2,0,100,writeEval,"write"));//	AddF2(adf1,adf2, new Function(0,0,100,inc_Aux1Eval,"inc_aux1"));//	AddF2(adf1,adf2, new Function(0,0,100,dec_Aux1Eval,"dec_aux1"));	AddF5(num,plus,minus,times,divide, new Function(0,0,100,Adf1Eval,"adf1"));	AddF5(num,plus,minus,times,divide, new Function(1,0,100,Adf2Eval,"adf2"));//	AddF5(num,plus,minus,times,divide, new Function(2,0,100,Adf3Eval,"adf3"));#endif

⌨️ 快捷键说明

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