📄 stats.cc
字号:
//summary.children.display ("tree child" , fout );//summary.fitness. display ("tree fitness", fout );//summary.nrank. display ("tree nrank" , fout );summary.nfit. display ("tree nfit" , fout );for ( int t = 0; t<NUM_TREES; t++){fout << endl;problem->WriteTreeName(t, fout);fout << "\t";length[t].display ("tree size", fout, NULL, NULL, &length_nfit[t]);length[t].display ("tree size2",fout, NULL, NULL, &length_fit[t]);for ( int op = 1; op < problem->funccount; op++) //surpress NUMBER opcodefor ( int d = 1; d <= MaxDepth; d++ ){ problem->WriteTreeName(t, fout); fout << "\t" << problem->funclist[op]->getname() << "'"; fout << d << "\t"; if ( counts[t][op][d].nfit.Cases() <=0 ) fout << counts[t][op][d].nfit.Cases() << endl; else {// fout << "occurs " << counts[t][op][d].length.Cases() << " times\t"; counts[t][op][d].frequency. display ("frequency ", fout ); problem->WriteTreeName(t, fout); fout << "\t" << problem->funclist[op]->getname() << "'"; fout << d << "\t";//nolonger supported// counts[t][op].length. display ("length ", fout, // &counts[t][op].frequency, &length[t] ); // problem->WriteTreeName(t, fout);// fout << "\t" << problem->funclist[op]->getname() << "\t";// counts[t][op].children.display ("childs ", fout, // &counts[t][op].frequency, &summary.children );// problem->WriteTreeName(t, fout);// fout << "\t" << problem->funclist[op]->getname() << "\t";// counts[t][op].fitness. display ("fitness", fout, // &counts[t][op].frequency, &summary.fitness );// problem->WriteTreeName(t, fout);// fout << "\t" << problem->funclist[op]->getname() << "\t";//// counts[t][op].nrank. display ("nrank ", fout, // &counts[t][op].frequency, &summary.nrank );// problem->WriteTreeName(t, fout);// fout << "\t" << problem->funclist[op]->getname() << "\t"; counts[t][op][d].nfit. display ("nfit ", fout, &counts[t][op][d].frequency, &summary.nfit ); }//end if op code is used in tree};//end for each op code};//end for each treedelete [] nfit;}//end Pop::DisplayDStats//fix latervoid Pop::GatherPStats(int fcount[][NUM_TREES][MAXTREEDEPTH+1], int pcount[pcount_size], int tcount[NUM_TREES] ) const//// Count total no of each opcode in the population. // And record which trees they are in//{assert (problem->funccount<=funccount_max);memset(fcount,0,sizeof(int) * problem->funccount * NUM_TREES*(MAXTREEDEPTH+1));memset(pcount,0,sizeof(int) * pcount_size);memset(tcount,0,sizeof(int) * NUM_TREES);for ( UINT i = 0; i < popsize; i++ ){int f[funccount_max][NUM_TREES][MAXTREEDEPTH+1];memset(f,0,sizeof(f));int len;for ( int t = 0; t<NUM_TREES; t++) { len = pop[i]->SubLen(pop[i]->tree_starts[t]); tcount[t] += problem->ProbLTStat( t,pop[i]->tree_starts[t],len,pop[i]->expr); int tree_stack [EXPRLEN]; tree_stack[0] = -1; int sp = 0; {for ( int n = pop[i]->tree_starts[t]; n < (pop[i]->tree_starts[t] + len); n++ ) { while (tree_stack[sp] == 0) { //remove finished branches tree_stack[--sp] -= 1; } ++sp; tree_stack[sp] = QARGNUM(pop[i]->expr[n].op); if(sp<MAXTREEDEPTH) f[pop[i]->expr[n].op][t][sp]++; else f[pop[i]->expr[n].op][t][MAXTREEDEPTH]++; if(pop[i]->expr[n].op == 0) { cout<<"ERROR zero opcode in "<<flush; cout<<i<<" "<<pop[i]->birth<<" "<<pop[i]->expr<<" "<<flush; pop[i]->write(PRETTY_NONE,cout); cout<<endl; } }};//end for each op in this tree };//end for each treeproblem->ProbLPStats(f, pcount);for(int op = 0; op<problem->funccount; op++) { for ( int t = 0; t<NUM_TREES; t++) for ( int d = 0; d<=MAXTREEDEPTH; d++) fcount[op][t][d] += f[op][t][d];}};//end for whole population}//end Pop::GatherPStatsvoid Pop::DisplayPStats(ostream & fout ) const //WBL// Gather and display frequency statistics by tree/opcode//{const int MaxDepth = (params->params[pMaxDepth]!=0)? params->params[pMaxDepth] : MAXTREEDEPTH;assert(MaxDepth<=MAXTREEDEPTH);assert (problem->funccount<=funccount_max);//int f [problem->funccount][NUM_TREES]; //Total no of each opcode by treeint f [funccount_max][NUM_TREES][MAXTREEDEPTH+1]; //Total no of each opcode by treeint dummy[pcount_size];int probl[NUM_TREES];GatherPStats(f, dummy, probl); for ( int op = 1; op < (problem->funccount+1); op++) //surpress NUMBER opcode{for ( int d = (MaxDepth==0)? 0:1; d <= MaxDepth; d++ ){//?? if(((((op-1)*MaxDepth)+d)%16)==0) fout<<endl; if(op>=problem->funccount) fout << "\tProbl"; else { if(strlen(problem->funclist[op]->getname())+2<8) fout<<"\t"; fout << problem->funclist[op]->getname(); } if(d!=0) fout << "'" << d;}fout << endl;}//end display header line//fout << endl;for ( int t = 0; t<NUM_TREES; t++){problem->WriteTreeName(t, fout);for ( int op = 1; op < (problem->funccount+1); op++) //surpress NUMBER opcode{for ( int d = (MaxDepth==0)? 0:1; d <= MaxDepth; d++ ){//??? if(((((op-1)*MaxDepth)+d)%16)==0) fout<<endl; fout << "\t"; if(op>=problem->funccount) fout << probl[t]; else if(problem->funcbag[t]->find(op)>=0) //opcode allowed in tree fout << f[op][t][d]; else if(f[op][t][d]!=0) fout << "ERROR " << f[op][t][d] << " should be zero!\n";}//end for each tree depthfout << endl;};//end for each op code//fout << endl;};//end for each tree}//end Pop::DisplayPStatsvoid Pop::DisplayLPStats(ostream & fout ) const //WBL{// Display cases of low tree/opcode frequency in the population//const int MaxDepth = (params->params[pMaxDepth]!=0)? params->params[pMaxDepth] : MAXTREEDEPTH;assert(MaxDepth<=MAXTREEDEPTH);//int f [problem->funccount][NUM_TREES]; //Total no of each opcode by treeassert (problem->funccount<=funccount_max);int f [funccount_max][NUM_TREES][MAXTREEDEPTH+1]; //Total no of each opcode by treeint problem_count[pcount_size];int problem_tcount[NUM_TREES];GatherPStats(f, problem_count, problem_tcount);const int threshold = ( (popsize/10) > 9 )? popsize/10 : 9;for ( int t = 0; t<NUM_TREES; t++) {BOOL first = TRUE;for ( int op = 0; op < (problem->funccount+1); op++) { if(op>=problem->funccount) {//NOT an opcode if(problem_tcount[t]>=0) {//interesting result if( problem_tcount[t] <= threshold ) { if(first) { first = FALSE; problem->WriteTreeName(t, fout); fout <<"\t"; } else { fout <<" "; } fout << "Problem"<<";"; fout << problem_tcount[t]; };//endif <= threshold } }//end problem_tcount else if(problem->funcbag[t]->find(op)>=0) {//opcode allowed in tree for ( int d = (MaxDepth==0)? 0:1; d <= MaxDepth; d++ ) if( f[op][t][d] <= threshold ) { if(first) { first = FALSE; problem->WriteTreeName(t, fout); if(d!=0){fout <<"'"<<d;} fout<<"\t"; } else { fout <<" "; } fout << problem->funclist[op]->getname(); if(d!=0){fout << "'"<<d;} fout<<";"; fout << f[op][t][d]; };//endif <= threshold } else for ( int d = (MaxDepth==0)? 0:1; d <= MaxDepth; d++ ) if(f[op][t][d]!=0) { problem->WriteTreeName(t, fout); fout <<"\t"; fout << problem->funclist[op]->getname(); fout << " ERROR " << f[op][t][d] << " ("<<d<<")"; fout << " should be zero!\n"; }};//end for each op codeif(!first) fout << endl;};//end for each treeBOOL first = TRUE;for ( int o = 0; o<pcount_size; o++) { if( problem_count[o] <= threshold ) { if(first) { first = FALSE; fout <<"lowprob\t"; } else { fout <<" "; } fout << char('A'+o) <<";"; fout << problem_count[o]; };//endif <= threshold};//end for each categoryif(!first) fout << endl;}//end Pop::DisplayLPStatsvoid Pop::DisplayCStats(ostream & fout ) const //WBL{int total = 0;for ( int t = 0; t<NUM_TREES; t++) { total += num_crosses[t]; fout<< float(num_crosses[t]*100)/float(gencount-num_crosses_cleared_at) << " ";};//end for each treefout << "= " <<float(total*100)/float(gencount-num_crosses_cleared_at) << endl;}//end Pop::DisplayCStatsclass hashchain;struct data { stats dupl; stats crossover;};class hashtable { hashchain** table; int size; inline int safe_index(int code) const { return (code%size+size)%size; }public: hashtable(int s); ~hashtable(); inline hashchain* chainhead(int code) const { return table[safe_index(code)]; }; inline void setchainhead(int code, hashchain* value) { table[safe_index(code)] = value; }; void include(data* output) const; //friend class hashchain;};class hashchain { int count; int cross; //number with identical code produced by Xover since oldest one inline void init(Chrome* c) { chain = NULL; chrome = c; count = 1; cross = 0; } inline int Count() const { return count; }; inline int Cross() const { return cross; };public: hashchain* chain; Chrome* chrome; //oldest chrome with identical code hashchain(hashtable* t, int code, Chrome* c) { t->setchainhead(code,this); init(c); } hashchain(hashchain* endofchain, Chrome* c) { endofchain->chain = this; init(c); } inline void inc() { count++; }; inline void inc_cross() { cross++; }; void include(data* answer) const { for(hashchain* h = this; h != NULL; h = h->chain) { answer->dupl.include (float( h->Count() )); answer->crossover.include(float( h->Cross() )); } }};hashtable::hashtable(int s){ size = s; table = new hashchain*[size]; memset(table,NULL,sizeof(hashchain*)*size);}hashtable::~hashtable(){ for(int i=0; i<size;i++) { if(table[i]!=NULL) { hashchain* tmp; for(hashchain* h = table[i]->chain; h != NULL; h = tmp ) { tmp = h->chain; delete h; } } } delete[] table;}void hashtable::include(data* answer) const{ for(int i=0; i<size;i++) { if(table[i]!=NULL) table[i]->include(answer); }}int Chrome::hashcode() const{int code = 1;#ifdef MULTREEfor (int t = 0; t < NUM_TREES; t++ ) { int ip = tree_starts[t];#else int ip = 0;#endif int args = 1; while (args > 0) { code = code + 1 + expr[ip].op; args=args+ARGNUM()-1; ip++; } code *= 99; //arbitary value should be ok for NUM_TREES=5???#ifdef MULTREE};//endfor all trees#endifreturn code;}//end Chrome::hashcodeBOOL Chrome::same(Chrome* other) const{#ifdef MULTREEif( memcmp(tree_starts, other->tree_starts, sizeof(tree_starts)) !=0 ) return FALSE;for (int t = 0; t < NUM_TREES; t++ ) { int ip = tree_starts[t];#else int ip = 0;#endif int args = 1; while (args > 0) { if(expr[ip].op != other->expr[ip].op) return FALSE; args=args+ARGNUM()-1; ip++; }#ifdef MULTREE};//endfor all trees#endifreturn TRUE;}//end samevoid Pop::DisplayDupStats(ostream & fout ) const //WBL// Gather and display Duplicate Tree statistics//{int duplicates = 0;hashtable ht(popsize);for (UINT i=0;i<popsize;i++) { int code = pop[i]->hashcode(); hashchain* ptr = ht.chainhead(code); if(ptr == NULL) new hashchain(&ht,code,pop[i]); else { BOOL found = FALSE; hashchain* prev = ptr; while(!found && ptr != NULL) { found = (pop[i]->same(ptr->chrome)); prev = ptr; ptr = ptr->chain; } if(!found) new hashchain(prev,pop[i]); else { duplicates++; prev->inc(); Chrome* pc = prev->chrome; if(pop[i]->birth < pc->birth) {#ifdef TRACE#ifdef MULTREE //Was rev created by crossover? if(pc->mum.birth >=0 && pc->mum.xtree >=0){ prev->inc_cross();// fout<<"\nDuplicate pop "<<i// <<" created by crossover1";// pop[i]->write_trace(fout);// pc->write_trace(fout);// pc->write(PRETTY_NONE,fout); }#endif#endif prev->chrome = pop[i]; // keep oldest }//endif pop[i] created before prev else {#ifdef TRACE#ifdef MULTREE //Was pop[i] created by crossover? if(pop[i]->mum.birth >= 0 && pop[i]->mum.xtree >=0){ prev->inc_cross();// fout<<"\nDuplicate pop "<<i// <<" created by crossover2";// pop[i]->write_trace(fout);// pc->write_trace(fout);// pop[i]->write(PRETTY_NONE,fout); }#endif#endif }//end else pop[i] created after prev } }}//end for all population//fout<<duplicates;data s;ht.include(&s);fout<<"different "<<s.dupl.Cases();fout<<" duplicates "<<duplicates;fout<<" produced by crossover "<<s.crossover.Sum();if(duplicates!=0) fout<<" ratio "<<s.crossover.Sum()/duplicates<<endl;else fout<<" ratio -0\n";fout<<"dupl\t";s.dupl.display("",fout);fout<<"xovr\t";s.crossover.display("",fout);}//end Pop::DisplayDupStats
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -