📄 brack.cc
字号:
// 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;// AddFall(new Function(3,0,100,ForWhileEval,"forwhile"));// forwhile_funcnum=funccount-1;// AddFall(new Function(0,0,100,I0Eval,"i0"));// i0_funcnum=funccount-1;#else AddFall(new Function(0,0,100,Aux1Eval,"aux1")); AddF1(rpb,new Function(0,0,100,ARG1Eval,"ARG1")); AddF1(adf2,new Function(0,0,100,Arg1Eval,"arg1")); arg1_funcnum=funccount-1; AddFall(new Function(1,0,100,readEval,"read")); AddFall(new Function(2,0,100,writeEval,"write")); AddF1(rpb,new Function(1,0,100,set_Aux1Eval,"Set_Aux1")); AddFall(new Function(0,0,100,inc_Aux1Eval,"inc_aux1")); AddFall(new Function(0,0,100,dec_Aux1Eval,"dec_aux1")); AddF1(rpb, new Function(0,0,100,Adf1Eval,"adf1")); AddF1(rpb, new Function(1,0,100,Adf2Eval,"adf2")); AddF2(rpb,adf1, new Function(0,0,100,Adf3Eval,"adf3"));#endif}void gp::LoadTests(istream& in){ test* t = new test (this, in);t->write(this);}void gp::WriteTreeName(int tree, ostream& ostr){switch(tree) { case rpb: ostr<< "rpb"; break; case adf1: ostr<< "adf1"; break; case adf2: ostr<< "adf2"; break; case adf3: ostr<< "adf3"; break; default: ostr<< "ERROR322"; break; }}//end queue::WriteTreeName()BOOL gp::TreeNameMatch(int tree, char* s){switch(tree) { case rpb: return strcmp("rpb", s); case adf1: return strcmp("adf1", s); case adf2: return strcmp("adf2", s); case adf3: return strcmp("adf3", 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 DEBUGchrome->write(PRETTY_NONE,cout,tree); cout<<endl;#endifint answer = 0; //ie ok BOOL a = i0_not_in_loop(chrome,tree); answer = a? 100 : 0;#ifdef DEBUG cout<<" i0_not_in_loop = "<<(a? "TRUE" : "FALSE")<<endl;#endifif ((answer==0) && (tree==rpb || tree==adf2) ) { 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 arg1if(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){}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 PARETOmemset(hits,0,sizeof(hits));//memset(chain,0,sizeof(chain));#endifmem_used = 0;Memory_Errors = 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}void 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 stop//max_fitness = float (max_passes[num_phases-1])*(1.0+FLT_EPSILON); max_fitness = Op_Max [num_phases-1][0]*(1.0+FLT_EPSILON); assert(NUM_OPERATIONS==1);//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}#ifndef STORE_FITvoid test::update_seq_score(Chrome* chrome, int s, //test_sequence int seqpasses) const{const ptrmyfitnessvalue fitptr = ptrmyfitnessvalue (chrome->nfitness);fitptr->Memory_Errors += Memory_errors;//Ntests_run set by argument of final_score//mem_used set by final_score calling cells_written}//end test::update_seq_score#else /*STORE_FIT*/void 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;}//end test::update_seq_scoreint myfitnessvalue::Memory_errors() const {int count = 0;for(int s=0; s<NUM_TEST_SEQUENCES;s++) { count += test_data[s].memory_errors;}return count;}#endif /*STORE_FIT*/#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 minmem_found; //lowest memory used by any solution so farint minmerr_found;//lowest memory errors by any solution so farint mincpu_found; //lowest cpu used by any solution reported so far#ifdef STORE_FITfloat test::final_score(Chrome* chrome) const#elsefloat test::final_score(Chrome* chrome, int passes, int tests) const#endif{const ptrmyfitnessvalue fitptr = ptrmyfitnessvalue (chrome->nfitness);#ifdef STORE_FITfitptr->mem_used = 0;fitptr->Ntests_run = 0;fitptr->cpu_time = 0;int passes = 0;//memset(fitptr->hits,0,sizeof(fitptr->hits));//memset(fitptr->ok, 0,sizeof(fitptr->ok));//memset(fitptr->nak, 0,sizeof(fitptr->nak));ThisTest->unpack_hits(chrome);for(int s=0; s<NUM_TEST_SEQUENCES;s++) {if(fitptr->mem_used < fitptr->test_data[s].mem_used) fitptr->mem_used = fitptr->test_data[s].mem_used;fitptr->Ntests_run += fitptr->test_data[s].Ntests_run;#ifdef TIMINGSfitptr->cpu_time += fitptr->test_data[s].cpu_time;#endif//#ifdef TRACE_RUN//for(int t=0; t<NUM_TREES;t++) {// fitptr->ok[t] += fitptr->test_data[s].trace.unpack_ok(t); //add overflow// fitptr->nak[t] += fitptr->test_data[s].trace.unpack_nak(t); //checks!//}//#endifpasses += fitptr->test_data[s].passes;}//endfor each test_sequence#else /*STORE_FIT*/fitptr->mem_used = cells_written();fitptr->Ntests_run = tests;#ifdef TIMINGSfitptr->cpu_time = ThisTimmer->timmer();fitptr->hits[1] = ((sol_found==FALSE) && ((fitptr->cpu_time/fitptr->Ntests_run)<=cpu_penalty_bot))? 0 : -(fitptr->cpu_time/fitptr->Ntests_run);#endif /*TIMINGS*/#ifdef PARETOchrome->nfitness->fvalue = float(fitptr->hits[0]);#endif#endif /*STORE_FIT*/tests_apparently_run += fitptr->Ntests_run;BOOL display_solution = FALSE;if(passes==max_passes[current_phase]) { num_sol_found++; if(num_sol_found==1) { display_solution = TRUE; minmem_found = fitptr->mem_used; minmerr_found = fitptr->Memory_errors();#ifdef TIMINGS mincpu_found = fitptr->cpu_time;#endif } else { if (fitptr->mem_used < minmem_found) { minmem_found = fitptr->mem_used; display_solution = TRUE; } int memerrs = fitptr->Memory_errors(); if (memerrs < minmerr_found) { minmerr_found = memerrs; display_solution = TRUE; }#ifdef TIMINGS if ((fitptr->cpu_time/1000) < (mincpu_found/1000)) { mincpu_found = fitptr->cpu_time; display_solution = TRUE; }#endif /*TIMINGS*/ }//endelse if((current_phase >= (num_phases-1)) && (!sol_found)) { sol_found = TRUE; #ifdef TIMINGS fitptr->hits[1] = -(fitptr->cpu_time/fitptr->Ntests_run);#endif update_population_fitness(); //remove cpu and memory thresholds }};//endif passes all tests ie a solutionptrmyfitnessvalue(chrome->nfitness)->update_rank();if(display_solution && (ThisPop != NULL)) { cout << "\n\nafter " << ThisPop->gencount+1 << endl; fitptr->write(); chrome->write(PRETTY_NONE,cout); cout<<endl;}return chrome->nfitness->fvalue;}//end test::final_scoreint 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{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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -