📄 test.cc
字号:
cout <<"\nmax_passes " << max_passes[p] << "\tMax scores";scoretype total = 0;for(i = 0; i<NUM_OPERATIONS; i++) { total += Op_Max[p][i]; cout<< " " << Op_Max[p][i];}cout<< "\t= " << total;}cout<<endl;set_max_fitness();} //end test::testtest::~test(){for(int i=1;i<=test_size;i++) { if((tests[i].op==scorep)&&(tests[i].print!=0)) delete[] tests[i].print;}delete []tests;}//end ~testvoid test::write_test(int i, Problem* probl, ostream& fout) const{ switch(tests[i].op) { case scoreq: fout<<"scoreq\t"; write_data(tests[i].arg1,fout); write_data(tests[i].arg2,fout); break; case scoreq_: fout<<"scoreq_\t"; write_data(tests[i].arg1,fout); write_data(tests[i].arg2,fout); break; case scorneq_: fout<<"scorneq_\t"; write_data(tests[i].arg1,fout); write_data(tests[i].arg2,fout); break; case hurdle: fout<<"hurdle\t"; write_data(tests[i].arg1,fout); break; case scorep: { fout<<"scorep\t"<<tests[i].arg1; retval* p = tests[i].print; for(int j = 0; j<tests[i].arg1; j++,p++) { write_data(*p, fout); } break;} case scorneq: { fout<<"scorneq\t"<<tests[i].arg1; retval* p = tests[i].print; for(int j = 0; j<tests[i].arg1; j++,p++) { write_data(*p, fout); } break;} case break_test: fout<<"break"; break; case start_test: fout<<"start\t"<<tests[i].arg1; break; case end_test: fout<<"end_test"; break; default: probl->WriteTreeName(tests[i].op,fout); if(tree_return_value[tests[i].op]) fout<<"\tr"<<tests[i].res; if(tree_arg_num[tests[i].op]>=1) write_data(tests[i].arg1,fout); if(tree_arg_num[tests[i].op]>=2) write_data(tests[i].arg2,fout); break; }}void test::write(Problem* probl, ostream& fout) const{cout <<"GPtests "<<test_size<<" % max_passes "<<max_passes<<endl;cout <<"rand_test "<<rand_test_min_<<" "<<rand_test_max_fails_;cout <<"stop_on "<<stop_mask<< " %";#ifdef ADFif(( stop_mask & recurse_error) !=0) cout<<" recurse";#endifif(( stop_mask & memory_error) !=0) cout<<" memory";if(( stop_mask & loop_too_nested_error)!=0) cout<<" loop_nest";if(( stop_mask & loop_too_long_error) !=0) cout<<" loop_long";if(( stop_mask & print_overrun_error) !=0) cout<<" print";if(( stop_mask & stop_on_error) !=0) cout<<" stop";cout<<endl;for (int i = 1; i<=test_size;i++) {write_test(i,probl,fout); fout<<endl;}fout<<"FINISHED_TESTS\n";for(int p = 0; p<num_phases; p++) { cout<<"Test sequence "<<phase_end_seq << " is the last in phase "<<p<<endl;for(i = 0; i<NUM_OPERATIONS; i++)for(int s = 0; s<=phase_end_seq[p]; s++) if(Op_Ok[i][s] != 0 ) { fout<< "Op_Ok " << i << " " << s << " " << int(Op_Ok[i][s]) << " " << Op_Max[p][i] << endl; }}//exit(0);//debug}// end test::writeint test::num_tests_run() const{return tests_run;}#ifdef TEST_STATSextern Pop* ThisPop;inline float test::sequence_fitness(int s, int last_test) const{ int p = tot_seq_max_passes[s]; if(last_test>tot_seq_max_passed_test[s]) p++; float r = 1.0; for(;p>1;p--) r *= 10.0; if(ThisPop==NULL || ThisPop->initeval) return r; if((last_test==0 || last_test>=tot_seq_max_passed_test[s]) && tot_seq_passes[s] < ThisPop->params->params[pPopSize]) { return r * float(ThisPop->params->params[pPopSize]) / float(tot_seq_passes[s]+1); } else return r;}inline void test::sequence_stats(int s, int passes){ if(passes>tot_seq_max_passes[s]) { tot_seq_max_passes[s] = passes;// tot_seq_passes[s] = 1; }// else if(passes>0 && passes>=tot_seq_max_passes[s]) {// tot_seq_passes[s]++;// } tot_seq_run[s]++;}//end test::sequence_statsinline void test::sequence_passed_stats(int s, int last_test_passed) const{ if(last_test_passed>tot_seq_max_passed_test[s]) { tot_seq_max_passed_test[s] = last_test_passed; tot_seq_passes[s] = 1; } else if(last_test_passed==tot_seq_max_passed_test[s]) { tot_seq_passes[s]++; }// tot_seq_run[s]++;}//end test::sequence_stats#endifvoid 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_stats////////////////////////////////////////////////////////////#ifdef STORE_FITinline void clear_seq_score(Chrome* chrome, int test_sequence){const ptrmyfitnessvalue fitptr = ptrmyfitnessvalue(chrome->nfitness);memset(&(fitptr->test_data[test_sequence]), 0, sizeof(fitptr->test_data[test_sequence]));for(int i = ThisTest->sequence_start[test_sequence]; i< ThisTest->sequence_start[test_sequence+1]; i++) fitptr->score[ThisTest->break_index[i]] = 0; //nb score[0] is not used}//end clear_scoreinline BOOL need_to_run(Chrome* chrome, int test_sequence){if( (chrome->params->params[pStoreFit] & 1) == 0 ) return TRUE;const ptrmyfitnessvalue fitptr = ptrmyfitnessvalue(chrome->nfitness);if((fitptr->test_phase >= 0) && (fitptr->test_phase != current_phase)) { //debug cout<<"test_phase="<<int(fitptr->test_phase)<<endl; return (test_sequence > ThisTest->phase_end_seq[fitptr->test_phase]) ? TRUE : FALSE; //only catchup required?}const int xtree = chrome->mum.xtree;if(xtree<0) return TRUE; //-1 => either created by copy or initially created //if copy fitness wont be called, so run testsreturn fitptr->test_data[test_sequence].trace.used(xtree);}//end need_to_run#endif /*STORE_FIT*/#ifdef STORE_FITunsigned char pack_float(float data) { if (data >= 1.0) return 255; if (data <= 0.0) return 0; int answer = int(data*254.0) + 1; if (answer >= 255) return 254; if (answer <= 0 ) return 1; return answer;}float unpack_float(unsigned char data) {return (data==0)? 0.0 : (data==255)? 1.0 : data/254.0;}//inlined WBL 21-2-95inline void test::update_score(Chrome* chrome, int test_sequence, int first_test, int last_test, scoretype score, BOOL passed) const{ptrmyfitnessvalue fitptr = ptrmyfitnessvalue (chrome->nfitness);const int offset = last_test - sequence_start[test_sequence]; if(passed && //and passed all previous times for this sequence fitptr->test_data[test_sequence].last_oktest_offset == fitptr->test_data[test_sequence].last_test_offset ) fitptr->test_data[test_sequence].last_oktest_offset = offset;fitptr->test_data[test_sequence].last_test_offset = offset;fitptr->score[break_index[last_test]] = pack_float(score); }//end test::update_score#else /*STORE_FIT*/#ifdef PARETO//inlined WBL 21-2-95inline void test::update_score(Chrome* chrome, int test_sequence, int first_test, int last_test, scoretype score, BOOL passed) const{//General code commented out to speedup GPkarp 15/10/95//General code restored for GPcalc2 11/12/95BOOL op_run[NUM_OPERATIONS];memset(op_run,0,sizeof(op_run));if(passed) {#ifdef TEST_STATS sequence_passed_stats(test_sequence,last_test);#endif /*TEST_STATS*/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) && (!op_run[tests[t].op]) ) { op_run[tests[t].op] = TRUE; //increase score only once fitptr->hits[tests[t].op] += score; }// ptrmyfitnessvalue (chrome->nfitness)->hits[0] +=+ score;// *// sequence_fitness(test_sequence, last_test);}}}//end test::update_score#else /*PARETO*/inline void test::update_score(Chrome* chrome, int test_sequence, int first_test, int last_test, scoretype score, BOOL passed) const{//chrome->nfitness->fvalue += float(score);chrome->nfitness->fvalue += float(score) =*= sequence_fitness(test_sequence);};//end test::update_score#endif#endif#ifdef STORE_FITvoid test::not_run_seq(void* p, int test_seq, int answer[NUM_OPERATIONS]) const{const ptrmyfitnessvalue f1 = ptrmyfitnessvalue(p);int i = sequence_start[test_seq];if(i!=0) {//count tests which where not runfor(int t = i + f1->test_data[test_seq].last_test_offset + 1 ; tests[t].op != end_test; t++) { if( (tests[t].op <= last_op) && (tests[t].op >= first_op) ) answer[tests[t].op]++;};//endfor};//endif}//end not_run_seqvoid test::not_run(void* fitptr, int answer[NUM_OPERATIONS] ) const{memset(answer,0,sizeof(int)*NUM_OPERATIONS);for(int s=0; s<=phase_end_seq[current_phase];s++) not_run_seq(fitptr, s, answer);}//end not_runvoid test::write_not_run(void* fitptr, int test_seq, ostream& os = cout) const{int ops[NUM_OPERATIONS];memset(ops,0,sizeof(ops));not_run_seq(fitptr, test_seq, ops);os<<"not run "; //cf trace_run<<for(int t=0;t<NUM_OPERATIONS;t++) { char buf[80]; sprintf(buf,"%4d",ops[t]); os << buf;}os<<endl;}//end write_not_run#endif /*STORE_FIT*/void test::sub_sequence(int sub_test_start, int sub_test_top, scoretype score, scoretype hits[] ) const{BOOL op_run[NUM_OPERATIONS];memset(op_run,0,sizeof(op_run));for(int t = sub_test_start; t<=sub_test_top; t++) { if( (tests[t].op <= last_op) && (tests[t].op >= first_op) && (!op_run[tests[t].op]) ) { op_run[tests[t].op] = TRUE; //increase score only hits[tests[t].op] += score; } };//endfor}//end sub_sequencevoid test::ok_sub_sequences(int start_test, int last_test, scoretype hits[]) const{scoretype sub_seq_score = 1;int sub_test_start = start_test;for(int j=start_test; j<=last_test;j++){ if(tests[j].op==scorep) { sub_seq_score *= print_match_max_score(tests[j].arg1); } if(tests[j].op==scoreq_ || tests[j].op==scorneq_) { sub_seq_score += scoretype(tests[j].arg2) - 1; } if(tests[j].op==break_test) { sub_sequence(sub_test_start, j, #ifdef STORE_FIT unpack_float(pack_float(sub_seq_score)),#else /*STORE_FIT*/ sub_seq_score,#endif /*STORE_FIT*/ hits); sub_seq_score = 1; sub_test_start = j + 1; }}//endfor}//end ok_sub_sequences#ifdef STORE_FITBOOL test::Operation_OK(void* f, int operation) const{if(operation < 0 || operation >= NUM_OPERATIONS) return FALSE;const ptrmyfitnessvalue fitptr = ptrmyfitnessvalue (f);for(int t=0; t<=phase_end_seq[current_phase]; t++) { if( (Op_Ok[operation][t] != 0) && (Op_Ok[operation][t] <= fitptr->test_data[t].last_oktest_offset) ) return TRUE; }return FALSE;}//end test::Operation_OKvoid test::unpack_hits(Chrome* chrome) const{ptrmyfitnessvalue fitptr = ptrmyfitnessvalue (chrome->nfitness);memset(fitptr->hits,0,sizeof(fitptr->hits));for(int t=0;t<=phase_end_seq[current_phase];t++) { int i = sequence_start[t]; int sub_test_start = i; for(int j=i; j<sequence_start[t+1];j++){ if(tests[j].op == break_test) { sub_sequence(sub_test_start, j, unpack_float(fitptr->score[break_index[j]]), fitptr->hits); sub_test_start = j + 1; } }//endfor j}//endfor each test_sequence //see if any operation has maximum score. If so override calcfor(int i=0;i<NUM_OPERATIONS;i++) { if(Operation_OK(fitptr,i)) fitptr->hits[i]=Op_Max[current_phase][i];}}//end test::unpack_hits#endifinline int test::run_tests(Chrome* chrome, int sequence, int first_test) const{#include "test.run.cc"}//end run_testfloat gp::fitness(Chrome* chrome) // fitness function.{#include "test.fitness.cc"}//end gp::fitness////////////////////////////// Now do again with display onint test::set_passed(BOOL passed[], Chrome* chrome, int first_test) const{#define PREPASS#include "test.run.cc"#undef PREPASS}//end set_passedint test::display_tests(Chrome* chrome, int sequence, int first_test) const{//BOOL passed[ThisTest->test_size];//memset(passed,0,sizeof(passed));BOOL* passed = new BOOL [ThisTest->test_size];memset(passed,0,ThisTest->test_size*sizeof(BOOL));set_passed(passed,chrome,first_test);::clear_stats(); //pretend prog is being run for the first time#define DISPLAY#include "test.run.cc"#undef DISPLAYdelete[] passed;}//end display_testsfloat display_run(Chrome* chrome){#define DISPLAY#include "test.fitness.cc"#undef DISPLAY}//end display_run
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -