list.cc
来自「标准的GP源代码,由Andy Singleton维护」· CC 代码 · 共 1,002 行 · 第 1/3 页
CC
1,002 行
}//end test::update_seq_score#endif /*STORE_FIT*/int myfitnessvalue::Memory_errors() const {int count = 0;for(int s=0; s<NUM_TEST_SEQUENCES;s++) { count += test_data[s].memory_errors;}return count;}void 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{cout << "Removing memory and cpu penalty thresholds " << endl;for(UINT j=0;j<ThisPop->popsize;j++) { const ptrmyfitnessvalue fitptr = ptrmyfitnessvalue(ThisPop->pop[j]->nfitness); fitptr->hits[last_op+NUM_OTHERS] = -float(fitptr->mem_used);#ifdef TIMINGS fitptr->hits[last_op+1 ] = -float(fitptr->cpu_time) / float(fitptr->Ntests_run);#endif}//scan whole population}//end update_population_fitness()void reinitialise_population()//At end of first test phase. Regenerate all delete/locate and their adfs//This is required because initial population will have lost almost all//it variation in these unused trees.//Calculate the new fitness of the whole population{time_t t;time (&t); cout << "Reinitialising population " << ctime(&t) << flush;BOOL do_trees[NUM_TREES];memset(do_trees,0,sizeof(do_trees));do_trees[Delete] = TRUE;do_trees[Delete_adf] = TRUE;do_trees[Locate] = TRUE;do_trees[Locate_adf] = TRUE;ThisPop->reinitialise(do_trees);time (&t); cout << " done " << ctime(&t) << endl;}//end reinitialise_population()int 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*/int cw = cells_written();if(fitptr->mem_used < cw) fitptr->mem_used = cw;fitptr->Ntests_run = tests;#ifdef TIMINGSfitptr->cpu_time = ThisTimmer->timmer();#endif /*TIMINGS*/#endif /*STORE_FIT*/tests_apparently_run += fitptr->Ntests_run;chrome->nfitness->fvalue = float(passes);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(); mincpu_found = fitptr->cpu_time; } 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; } if ((fitptr->cpu_time/1000) < (mincpu_found/1000)) { mincpu_found = fitptr->cpu_time; display_solution = TRUE; } }//endelse if(current_phase >= (NUM_TEST_PHASES-1) ) { sol_found = TRUE; update_population_fitness(); //remove cpu and memory thresholds }};//endif passes all tests ie a solutionfitptr->hits[last_op+NUM_OTHERS] = ((fitptr->mem_used <= mem_penalty_bot) && (!sol_found) ) ? 0 : -fitptr->mem_used;#ifdef TIMINGS{scoretype temp = float (fitptr->cpu_time)/ float(fitptr->Ntests_run);fitptr->hits[last_op+1 ] = (( temp <= cpu_penalty_bot) && (!sol_found) ) ? 0 : -temp;}#endif//ptrmyfitnessvalue(chrome->nfitness)->update_rank(); now done in InsertMember cf ORIGINAL_RANKif(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(){ //Should we move on to next test phase? if((current_phase < (ThisTest->num_phases-1) ) && //FIX sometime. This changes functionality so wait until testing in /27*-jun-95//has been completed// current_phase_reported && (num_sol_found >= 1000 )) { //allow solution to spread num_sol_found = 0; current_phase++; current_phase_reported = FALSE; reinitialise_population(); } //Report 1st time we complete each test phase if( (num_sol_found > 0) && (!current_phase_reported) ) { current_phase_reported = TRUE; return current_phase + 1; } else return 0;}//end_gensBOOL IsLoop(int funcnum) {return (funcnum==ThisProblem->forwhile_funcnum);}int IsBranch(int funcnum) {return 0; //no branch instructions in list function set}int Chrome::ChooseCrossTree(Chrome* second_parent) {//choose a promising tree to crossover between this and second_parent//Select trees in this where there is a high ratio of possible failures to//probable oks. Exclude cases where tree is never run or is always ok.//ignores second_parent int tree = rnd(NUM_TREES);int ok_trials = 0;float best_ratio = -FLT_MAX;const ptrmyfitnessvalue f1 = ptrmyfitnessvalue(this->nfitness);int unknown[NUM_TREES];missed(f1, unknown);#ifdef DEBUGint debug[100];int d=0;#endiffor(int trials = 0; (ok_trials<3) && (trials<100) && (best_ratio<FLT_MAX); trials++) { int t = rnd(NUM_TREES);#ifdef DEBUG debug[d++] = t;#endif if( !Tree_OK(f1,t) ) { //dont cross those that work const int f1_nak = f1->nak(t); const int f1_ok = f1->ok(t); if( (f1_ok != 0) || (f1_nak != 0)){//only change trees that are used if( (f1_nak!= 0) || (unknown[t]!= 0)){//only change when may hav failed const float ratio = ((f1_ok < f1_nak) || (f1_ok + unknown[t]) == 0)? FLT_MAX : float(f1_nak+unknown[t])/float(f1_ok+unknown[t]); if( ratio > best_ratio ) { best_ratio = ratio; tree = t; } ok_trials++; } }//endif tree used }//endif !Tree_OK}//end for#ifdef DEBUGif(tree==Previous) { cout<<"ChoosePrevious "; for(int i = 0; i < d; i++) cout<<debug[i]<<" "; f1->write(cout); cout<<" "<<ThisPop->num_crosses[Previous]<<endl;}#endifreturn tree;} //end Chrome::ChooseCrossTree#ifdef MAXTREEDEPTHvoid gp::ProbLPStats(int f[][NUM_TREES][MAXTREEDEPTH+1], int pcount[pcount_size]){// See if each tree that can use adf1 does so.for ( int d = 0; d<=MAXTREEDEPTH; d++) {pcount[0] += (( f[Adf1_Insert_funcnum][Insert][d] != 0 ) && ( f[Adf1_Delete_funcnum][Delete][d] != 0 ) && ( f[Adf1_Locate_funcnum][Locate][d] != 0 ) && ( f[Adf1_Printlist_funcnum][Printlist][d] != 0 )) ? 1: 0;// See if ARG1 is used by delete or its adfpcount[1] += (( f[ARG1_funcnum][Delete][d] != 0 ) || ( f[ARG1_funcnum][Delete_adf][d] != 0 ) ) ? 1: 0;// See if ARG1 is used by locate or its adfpcount[2] += (( f[ARG1_funcnum][Locate][d] != 0 ) || ( f[ARG1_funcnum][Locate_adf][d] != 0 ) ) ? 1: 0;}//endfor all depthspcount[3] += 1;//not used}//end ProbLPStats#elsevoid gp::ProbLPStats(int f[][NUM_TREES], int pcount[pcount_size] ) {// See if each tree that can use adf1 does so.pcount[0] += (( f[Adf1_Insert_funcnum][Insert] != 0 ) && ( f[Adf1_Delete_funcnum][Delete] != 0 ) && ( f[Adf1_Locate_funcnum][Locate] != 0 ) && ( f[Adf1_Printlist_funcnum][Printlist] != 0 )) ? 1: 0;// See if ARG1 is used by delete or its adfpcount[1] += (( f[ARG1_funcnum][Delete] != 0 ) || ( f[ARG1_funcnum][Delete_adf] != 0 ) ) ? 1: 0;// See if ARG1 is used by locate or its adfpcount[2] += (( f[ARG1_funcnum][Locate] != 0 ) || ( f[ARG1_funcnum][Locate_adf] != 0 ) ) ? 1: 0;pcount[3] += 1;//not used}//end ProbLPStats#endif /*MAXTREEDEPTH*/int gp::ProbLTStat(int tree, int start, int length, node* expr ){// Look for FUNC(I0) combinationsif(tree != adf1) return -1; //ignorefor(int ip=start; ip < (start+length); ) { if(expr[ip++].op==FUNC_funcnum){ int args = 1; while (args > 0) { if(expr[ip].op==i0_funcnum) return 1; args=args+ARGNUM()-1; ip++; } }}return 0;}//end ProbLTStat
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?