📄 calc.cc
字号:
}void gp::LoadTests(istream& in){ test* t = new test (this, in);t->write(this);}void gp::WriteTreeName(int tree, ostream& ostr){switch(tree) {#ifndef RPN case ans: ostr<< "ans"; break;#endif case num: ostr<< "num"; break; case plus: ostr<< "plus"; break; case minus: ostr<< "minus"; break; case times: ostr<< "times"; break; case divide: ostr<< "div"; break;#ifndef RPN case open: ostr<< "open"; break; case close: ostr<< "close"; break;#endif#ifndef KARP2 case adf1: ostr<< "adf1"; break; case adf2: ostr<< "adf2"; break; case adf3: ostr<< "adf3"; break;#endif default: ostr<< "ERROR322"; break; }}//end queue::WriteTreeName()BOOL gp::TreeNameMatch(int tree, char* s){switch(tree) {#ifndef RPN case ans: return strcmp("ans", s);#endif case num: return strcmp("num", s); case plus: return strcmp("plus", s); case minus: return strcmp("minus", s); case times: return strcmp("times", s); case divide: return strcmp("div", s);#ifndef RPN case open: return strcmp("open", s); case close: return strcmp("close", s);#endif#ifndef KARP2 case adf1: return strcmp("adf1", s); case adf2: return strcmp("adf2", s); case adf3: return strcmp("adf3", s);#endif 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}//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 = FLT_MAX;//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[last_op+1] = ((sol_found==FALSE) && ((fitptr->cpu_time/fitptr->Ntests_run)<=cpu_penalty_bot))? 0 : -(fitptr->cpu_time/fitptr->Ntests_run);#endif /*TIMINGS*/#ifdef PARETO//chrome->nfitness->fvalue = float(fitptr->hits[0]);chrome->nfitness->fvalue = float(passes);#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[last_op+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 + -