list.cc
来自「标准的GP源代码,由Andy Singleton维护」· CC 代码 · 共 1,002 行 · 第 1/3 页
CC
1,002 行
ARG1_funcnum(0), ARG2_funcnum(0), FUNC_funcnum(0), print_funcnum(0){//cout<<"gp::gp"<<endl;//debug ProbVersion = list_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)); AddF3(Makenull,Insert,Delete, new Function(2,0,100,Prog2Eval,"PROG2"));// AddF1(Locate_adf, new IflteFunc(100));// AddF1(Locate_adf, new Function(4,0,100,IfeqEval,"Ifeq")); 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,BotEval,"bot"));// AddFall(new Function(0,0,100,TopEval,"top")); AddF5(Insert,Delete,End,First,adf1, new Function(0,0,100,Aux1Eval,"aux1")); AddF8(Retrieve,Next,Previous,adf1, Insert_adf,Delete_adf,Locate_adf,Printlist_adf, new Function(0,0,100,Arg1Eval,"arg1")); arg1_funcnum=funccount-1;// AddF1(adf2,new Function(0,0,100,Arg2Eval,"arg2"));// arg2_funcnum=funccount-1; AddF5(Insert,Locate,Delete,Delete_adf,Locate_adf, new Function(0,0,100,ARG1Eval,"ARG1")); ARG1_funcnum=funccount-1; AddF1(Insert, new Function(0,0,100,ARG2Eval,"ARG2")); ARG2_funcnum=funccount-1;// AddF2(Insert,Delete,new Function(0,0,100,inc_Aux1Eval,"Inc_Aux1"));// AddF2(Insert,Delete,new Function(0,0,100,dec_Aux1Eval,"Dec_Aux1")); AddF3(Retrieve,Locate_adf,Printlist_adf, new Function(1,0,100,readEval,"read")); AddF2(Makenull,Insert, new Function(2,0,100,writeEval,"write")); AddF1(Makenull,new Function(1,0,100,set_Aux1Eval,"Set_Aux1"));// AddF4(Insert,Locate,Delete,Printlist,// new Function(3,0,100,ForEval,"for"));// for_funcnum=funccount-1;// AddF4(Insert,Locate,Delete,Printlist,// new Function(3,0,100,DownEval,"down"));// down_funcnum=funccount-1;// AddF4(Insert,Locate,Delete,Printlist,// new Function(2,0,100,WhileEval,"while"));// while_funcnum=funccount-1; AddF1(adf1, new Function(3,0,100,ForWhileEval,"forwhile")); forwhile_funcnum=funccount-1; AddF1(adf1, new Function(0,0,100,I0Eval,"i0")); i0_funcnum=funccount-1; AddF1(Printlist_adf, new Function(1,0,100,printEval,"print")); print_funcnum=funccount-1; AddF1(Insert, new Function(1,0,100,Adf1_InsertEval,"adf1")); Adf1_Insert_funcnum=funccount-1; AddF1(Delete, new Function(1,0,100,Adf1_DeleteEval,"adf1")); Adf1_Delete_funcnum=funccount-1; AddF1(Locate, new Function(1,0,100,Adf1_LocateEval,"adf1")); Adf1_Locate_funcnum=funccount-1; AddF1(Printlist, new Function(1,0,100,Adf1_PrintlistEval,"adf1")); Adf1_Printlist_funcnum=funccount-1; AddF2(Insert_adf,Delete_adf, new Function(2,0,100,swapEval,"swap"));// new Function(2,0,100,Adf2Eval,"adf2")); AddF1(adf1, new Function(1,0,100,FUNCEval,"FUNC")); FUNC_funcnum=funccount-1;// AddF2(Locate,Printlist,// new Function(tree_arg_num[Retrieve],0,100,RetrieveEval,"Retrieve")); AddF2(Makenull,adf1, new Function(tree_arg_num[End], 0,100,EndEval, "End")); AddF2(Locate,Printlist, new Function(tree_arg_num[First], 0,100,FirstEval, "First")); AddF3(Insert,Delete,Delete_adf, new Function(tree_arg_num[Next], 0,100,NextEval, "Next")); AddF1(Delete, new Function(tree_arg_num[Previous],0,100,PrevEval, "Prev"));}void gp::LoadTests(istream& in){ test* t = new test (this, in);t->write(this);}void gp::WriteTreeName(int tree, ostream& ostr){switch(tree) { case Makenull: ostr<< "makenul"; break; case Retrieve: ostr<< "retriev"; break; case Insert: ostr<< "insert"; break; case Delete: ostr<< "delete"; break; case End: ostr<< "end"; break; case First: ostr<< "first"; break; case Next: ostr<< "next"; break; case Previous: ostr<< "prev"; break; case Locate: ostr<< "locate"; break; case Printlist:ostr<< "prtlist"; break; case adf1: ostr<< "adf1"; break;// case adf2: ostr<< "adf2"; break; case Insert_adf: ostr<< "ins_adf"; break; case Delete_adf: ostr<< "del_adf"; break; case Locate_adf: ostr<< "loc_adf"; break; case Printlist_adf:ostr<< "prt_adf"; break; default: ostr<< "ERROR322"; break; }}//end queue::WriteTreeName()BOOL gp::TreeNameMatch(int tree, char* s){switch(tree) { case Makenull: return strcmp("makenul", s); case Retrieve: return strcmp("retriev", s); case Insert: return strcmp("insert", s); case Delete: return strcmp("delete", s); case End: return strcmp("end", s); case First: return strcmp("first", s); case Next: return strcmp("next", s); case Previous: return strcmp("prev", s); case Locate: return strcmp("locate", s); case Printlist:return strcmp("prtlist", s); case Insert_adf: return strcmp("ins_adf", s); case Delete_adf: return strcmp("del_adf", s); case Locate_adf: return strcmp("loc_adf", s); case Printlist_adf: return strcmp("prt_adf", s); default: return FALSE; }}//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#ifdef DEBUGif (tree==Insert ||tree==Locate||tree==Delete ||tree==Printlist|| tree==Retrieve||tree==Next ||tree==Previous||tree==adf1 || tree==Insert_adf||tree==Delete_adf||tree==Locate_adf||tree==Printlist_adf){chrome->write(PRETTY_NONE,cout,tree); cout<<endl;}#endifint answer = 0; //ie okif (tree==adf1) { BOOL a = nested_tree_or_i0(chrome,tree); answer = a? 100 : 0;#ifdef DEBUG cout<<" nested_tree_or_i0 = "<<(a? "TRUE" : "FALSE")<<endl;#endif if (answer==0) { BOOL a = tree_contains(chrome,tree,i0_funcnum); answer = a? 0 : 100;#ifdef DEBUG cout<<" tree_contains (i0) = "<<(a? "TRUE" : "FALSE")<<endl;#endif }}//check loopif ((answer==0) && (tree==Retrieve ||tree==Next ||tree==Previous|| tree==adf1 ||tree==Insert_adf || tree==Delete_adf||tree==Locate_adf||tree==Printlist_adf)) { BOOL a = tree_contains(chrome,tree,arg1_funcnum); answer = a? 0 : 100;#ifdef DEBUG cout<<" tree_contains (arg1) = "<<(a? "TRUE" : "FALSE")<<endl;#endif}//check for arg1//if ((answer==0) && (tree==Insert||tree==Locate||tree==Delete)) {// BOOL a = tree_contains(chrome,tree,ARG1_funcnum);// answer = a? 0 : 100;//#ifdef DEBUG// cout<<" tree_contains (ARG1) = "<<(a? "TRUE" : "FALSE")<<endl;//#endif//}//check for ARG1//if ((answer==0) && (tree==adf2)) {// BOOL a = tree_contains(chrome,tree,arg2_funcnum);// answer = a? 0 : 100;//#ifdef DEBUG// cout<<" tree_contains (arg2) = "<<(a? "TRUE" : "FALSE")<<endl;//#endif//}//check for arg2//if ((answer==0) && (tree==Insert)) {// BOOL a = tree_contains(chrome,tree,ARG2_funcnum);// answer = a? 0 : 100;//#ifdef DEBUG// cout<<" tree_contains (ARG2) = "<<(a? "TRUE" : "FALSE")<<endl;//#endif//}//check for ARG2if ((answer==0) && (tree==adf1)) { BOOL a = tree_contains(chrome,tree,FUNC_funcnum); answer = a? 0 : 100;#ifdef DEBUG cout<<" tree_contains (FUNC) = "<<(a? "TRUE" : "FALSE")<<endl;#endif}//check for ARG2if ((answer==0) && (tree==Printlist_adf)) { BOOL a = tree_contains(chrome,tree,print_funcnum); answer = a? 0 : 100;#ifdef DEBUG cout<<" tree_contains (print) = "<<(a? "TRUE" : "FALSE")<<endl;#endif}//check for printif(answer==0) { static_check_loop_check = 0; return 0; //says its ok}else { static_check_loop_check++;// if(static_check_loop_check>=10) { if(static_check_loop_check>=1000) { cout<<"static_check_looping on tree "<<tree; static_check_loop_check = 0; chrome->write(PRETTY_NONE,cout,tree); cout<<endl; return 0; //says its ok to abort overlong loop } else return answer; }}//end queue::static_checkvoid gp::write_stats(ostream& out){ cout<<"End "; end_cache.write(); cout<<"First"; first_cache.write(); cout<<"Next "; next_cache.write(); cout<<"Prev "; prev_cache.write();}retval random_value(int& seed) //seed must be > 0{return retval(F_tan_spread*tan(float(intrnd(seed) % 3141)/1000.0));}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;memset(hits,0,sizeof(hits));//memset(chain,0,sizeof(chain));mem_used = 0;#ifdef TRACE_RUN#ifndef STORE_FITmemset( ok,0,sizeof(ok));memset(nak,0,sizeof(nak));#endif#endif#ifdef STORE_FITmemset(test_data,0,sizeof(test_data));#endif}//inlined WBL 20-2-95//void test::update_score(Chrome* chrome, int first_test, int last_test, // scoretype score)//{//ptrmyfitnessvalue fitptr = ptrmyfitnessvalue (chrome->nfitness);////for(int t = first_test; t<last_test; t++) {// if((tests[t].op<=last_op)&&(tests[t].op>=first_op)) {// fitptr->hits[tests[t].op] += score; }//}//}//end test::update_scorevoid test::update_passed(BOOL passed[], int first_test, int last_test) const{for(int t = first_test; t<last_test; t++) { passed[t] = TRUE; }}//end test::update_passedvoid test::set_max_fitness() const{//we never stopmax_fitness = float (max_passes[num_phases-1])*(1.0+FLT_EPSILON); //check required for update_population_fitness(). Do it here because dont want//it to abort the program run possibly after several hours or runningassert (num_ellite_pareto<=last_op+1); //since we dont change rank info}#ifdef STORE_FITvoid test::update_seq_score(Chrome* chrome, int s, //test_sequence int seqpasses, int tests) const{const ptrmyfitnessvalue fitptr = ptrmyfitnessvalue (chrome->nfitness);fitptr->test_data[s].mem_used = cells_written();fitptr->test_data[s].memory_errors = (Memory_errors > 255)? 255: Memory_errors;fitptr->test_data[s].Ntests_run = tests;#ifdef TIMINGStotal_cpu += ThisTimmer->timmer(); //for statistical purposes onlyfitptr->test_data[s].cpu_time = (ThisTimmer->timmer() < myfitnessvalue::per_test::cpu_time_max)? ThisTimmer->timmer() : myfitnessvalue::per_test::cpu_time_max;#endif#ifdef TRACE_RUNfitptr->test_data[s].trace.trace_pack(ThisTrace);#endiffitptr->test_data[s].passes = seqpasses;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?