max.cc

来自「标准的GP源代码,由Andy Singleton维护」· CC 代码 · 共 298 行

CC
298
字号
// max.cc   File to demonstrate max problem, build with GPQUICK-2.1// W.B.Langdon cs.bham.ac.uk 13 Dec 1996 $Revision: 1.8 $//                     based upon cacl.cc Revision: 1.19//#define DEBUG //Modifications (in reverse order)//WBL 19 Apr 97 For Easter release//WBL 13 Dec 96 New file #include <assert.h>#include "pch.h"#include "chrome.h"#include "primitiv.h"#include "prob.h"#ifdef TRACE_RUN#include "trace.h"#endif#ifdef TIMINGS#include "time.h"#endif//#include "Primitives.h"#include "gp.h"#include "test.h"#define Version "MAX. $Revision: 1.8 $"extern char scratch[100];#ifdef FASTEVAL	extern evalnode* IpGlobal;	extern Chrome* ChromeGlobal;#endifofstream temp("");ofstream& crossfile = temp;const int tree_arg_num[last_op+1] = {0};const BOOL tree_return_value[last_op+1] = {TRUE};int 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 //**************************************************************************float display_run(Chrome* chrome) {  cout << "\nExecuting "<< chrome << flush;  chrome->write(PRETTY_NONE); cout<<endl;  chrome->SetupEval();  chrome->nfitness->fvalue = chrome->evalAll ( 0 );  cout << "\nReturns " << chrome->nfitness->fvalue << endl;  return chrome->nfitness->fvalue;}int current_phase = 0; int tests_run = 0;int tests_apparently_run = 0;int total_cpu = 0;test::test(Problem* probl, istream& istr) {}void test::write_stats(ostream& out) const{out<<"Ran " << tests_run << " trees. Should have run " << tests_apparently_run   <<" ratio "<< float(tests_apparently_run)/float(tests_run)   <<" Instructions " << total_cpu << endl;//#ifdef TEST_STATS//out<<"Seq no\tRun\tMax Passes\tPasses\ttest no\tSequence Fitness\n";//for(int s=0; s<NUM_TEST_SEQUENCES; s++) if(tot_seq_run[s]!=0) {//	out<<s<<"\t"<<tot_seq_run[s]//              <<"\t"<<tot_seq_max_passes[s]//              <<"\t\t"<<tot_seq_passes[s]//              <<"\t"<<tot_seq_max_passed_test[s]//	      <<"\t"<<sequence_fitness(s)<<endl;//}//#endif}//end write_statsfloat gp::fitness(Chrome* chrome)  // fitness function.{  tests_run++;  tests_apparently_run++;  chrome->nfitness->fvalue = chrome->evalAll ( 0 );  return chrome->nfitness->fvalue;}//end gp::fitnessvoid write_problem_params(){//cout<<store_limit<<"["<<Array_bot<<".."<<Array_top<<"] ";//cout<<mem_penalty_bot<<" "  cout << "max fitness "<<max_fitness;#ifdef TIMINGS  cout<<" "<<cpu_penalty_bot<<;#endif  cout<<endl;}// Define your FitnessValue classvoid myfitnessvalue::write(ostream& fout){	fout << fvalue;#ifdef MINC_ADF	fout << " adf1ver " << adf1;#endif#ifdef TIMINGS	fout << " cpu " << cpu_time;#endif#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 definitionOPDEF(halfEval) {return 0.5;}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 MulFunc(100));	AddFall(new Function(0,0,100,halfEval,"0.5"));}void gp::LoadTests(istream& in){ }void gp::WriteTreeName(int tree, ostream& ostr){ostr<< "ans";}//end queue::WriteTreeName()BOOL gp::TreeNameMatch(int tree, char* s){return 0; //any string will do}//end queue::TreeNameMatch()int static_check_loop_check = 0;int gp::static_check(Chrome* chrome, int tree)// Perform static analysis of new chromsome. Just changed in tree{return 0; //Uncomment to disable static_check//ie ok}//end queue::static_checkvoid gp::write_stats(ostream& out){}retval random_value(int& seed) //seed must be > 0{//return retval(F_tan_spread*tan(float(intrnd(seed) % 3141)/1000.0));return retval(intrnd(seed)); //default -- not used}void myfitnessvalue::clear(){//NB clearing whole of myfitnessclass cause gcc produced code to crash//In theory dont need to do this most of the time since on copy or//crossover will soon be overwritten with data from parent//cout<<"Sizeof myfitnessvalue "<<sizeof(*this)<<endl;}#ifdef PARETOvoid update_population_fitness()//now that a solution has been found new rules apply for fitness calculation//scan whole of population and update each fitness value in line with new//rules. Ie remove cpu and memory penalty thresholds{if(ThisPop==NULL) return;cout << "Removing cpu penalty threshold " << endl;for(UINT j=0;(j<ThisPop->nexteval-1)&&(j<ThisPop->popsize);j++) {	const ptrmyfitnessvalue fitptr = 		                 ptrmyfitnessvalue(ThisPop->pop[j]->nfitness);//No mem_used penalty//	fitptr->hits[last_op+NUM_OTHERS] = -scoretype(fitptr->mem_used);#ifdef TIMINGS	if((fitptr!=NULL)&&(fitptr->Ntests_run != 0)) {	fitptr->hits[last_op+1] = -(fitptr->cpu_time/fitptr->Ntests_run);        }//end if have determined a fitness value before#endif}//end scan whole population}//end update_population_fitness()#endifint mincpu_found; //lowest cpu used by any solution reported so farint end_gens(){	//we never move on to next test phase	return 0;}//end_gensBOOL IsLoop(int funcnum) {return (funcnum==ThisProblem->forwhile_funcnum);}int IsBranch(int funcnum) {	if(funcnum==ifmatch_funcnum)		return 3;	else if(funcnum==ifopen_funcnum  ||		funcnum==ifempty_funcnum ||		funcnum==ifeq_funcnum    ||		funcnum==iflte_funcnum     )		return 2;	else		return 0;}int Chrome::ChooseCrossTree(Chrome* second_parent) {//not actually in usereturn rnd(NUM_TREES);} //end Chrome::ChooseCrossTree#ifdef MAXTREEDEPTHvoid gp::ProbLPStats(int f[][NUM_TREES][MAXTREEDEPTH+1], int pcount[pcount_size] ) #elsevoid gp::ProbLPStats(int f[][NUM_TREES], int pcount[pcount_size] ) #endif /*MAXTREEDEPTH*/{pcount[0] += 1;//not usedpcount[1] += 1;//not usedpcount[2] += 1;//not usedpcount[3] += 1;//not used}//end ProbLPStatsint gp::ProbLTStat(int tree, int start, int length, node* expr ){return -1; //ignore}//end ProbLTStat

⌨️ 快捷键说明

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