📄 chrome.cxx
字号:
int l=SubLen(0); if (l>2) { rval=rnd(l); while (funclist[FUNCNUM(expr[rval])]->argnum <1) rval=rnd(l); } else rval=0;#endif return rval;} // Mutate the current Chromevoid Chrome::Mutate() // mutate nodes with rate r{//replace about pMuteRate/1024 of nodes in program with a function//that takes the same number of arguments int end,i,args; int rate=params->params[pMuteRate];#ifdef MULTREEfor (int t = 0; t < NUM_TREES; t++ ) { ip = tree_starts[t]; Traverse(); end=ip; ip = tree_starts[t];#else ip=0; Traverse(); end=ip; ip=0;#endif while (ip<end) { if (rnd(1024) < rate) { args=ARGNUM(); do { //find a function with same number of arguments#ifdef MULTREE i=probl->funcbag[t]->roulette();#else i=probl->funcbag[0]->roulette();#endif } while (funclist[i]->argnum!=args); if (funclist[i]->varnum == 0) {SETNODE(expr[ip],i,0);} //WBL bug fix 001 else {SETNODE(expr[ip],i,rnd(funclist[i]->varnum));} } ip++; }#ifdef MULTREE }//endfor NUM_TREES#endif#ifdef TRACE mum.xpoint = -2;#endif}void Chrome::MutateC() // jiggle constants with a random walk {#ifdef MULTREE assert (2==0); // havent implemted this yet#endif int i,end,newconst; int oldconst; int radius=8; ip= 0; Traverse(); end=ip; ip=0; while (ip<end) { if (FUNCNUM(expr[ip])==0) { oldconst=VARNUM(expr[ip]); newconst=oldconst; for (i=0;i<radius;i++) newconst+=rnd(2); newconst-=radius/2; newconst=min(CONSTARRAYSIZE-1,max(newconst,0)); if ((constlist[oldconst]>100 && constlist[newconst]<0) || (constlist[oldconst]<-100 && constlist[newconst]>0)) // overrun? newconst=oldconst; SETNODE(expr[ip],0,newconst); } ip++; }}void Chrome::MutateShrink() // shrink by promoting a subtree{#ifdef MULTREE assert (3==0); // havent implemted this yet#endif int tree,treelen,subtree,subtreelen,l; node *temp; int argnum; argnum=0; l=SubLen(0); if (l>argnum+1) { // node required temp = new node[params->params[pMaxExpr]]; tree=rnd(l); while (funclist[FUNCNUM(expr[tree])]->argnum == 0) tree=rnd(l); // now pick a subtree treelen=SubLen(tree); subtree=tree+rnd(treelen-1)+1; subtreelen=SubLen(subtree); memcpy(temp,expr,l*sizeof(node)); // save the old expr memcpy(expr+tree,temp+subtree,subtreelen*sizeof(node)); // add the subtree memcpy(expr+tree+subtreelen,temp+tree+treelen,sizeof(node)*(l-(tree+treelen))); // add the rest delete[] temp; }}void Chrome::MutateSubTree() // Replace a sub tree{ int tree,treelen,subtreelen; int t; int depth = params->params[pInitExpr];#ifdef MULTREE int len=tree_starts[NUM_TREES-1] + SubLen(tree_starts[NUM_TREES-1]);#else int len=SubLen(0);#endif node* temp = new node[len]; memcpy(temp,expr,len*sizeof(node));#ifdef MULTREE t = rnd(NUM_TREES); //choose tree#endif#ifdef MULTREE tree=tree_starts[t]+rnd(SubLen(tree_starts[t]));#else tree=rnd(len);#endif treelen=SubLen(tree); if(params->params[pMaxDepth] != 0) {#ifdef MULTREE const int tree_depth = Depth(tree_starts[tree],tree);#else const int tree_depth = Depth(0,tree);#endif if(depth+tree_depth > params->params[pMaxDepth]) depth = params->params[pMaxDepth] - tree_depth; } int chnglen; do { // DO "ramped half and half from 2 to depth" // go to some depth less than max, full or half full ip = tree;#ifdef MULTREE SubInit(probl->funcbag[t],1,rnd(depth-1)+1,rnd(2), TRUE, t);#else SubInit(probl->funcbag[0],1,rnd(depth-1)+1,rnd(2), TRUE);#endif subtreelen=SubLen(tree); //length of new tree chnglen = subtreelen-treelen; --depth; //if tree too big try creating it again but smaller } while (((len+chnglen) > params->params[pMaxExpr]) && (depth > 1)); if(len+chnglen>params->params[pMaxExpr]) { //may trip this? cout<<"MutateSubTree too big: len+chnglen="; cout<<len<<"+"<<chnglen<<">"<<params->params[pMaxExpr]; cout<<endl; memcpy(expr,temp,len*sizeof(node)); //restore original chnglen = 0; } else { memcpy(expr+tree+subtreelen,temp+tree+treelen, (len-(tree+treelen))*sizeof(node)); // add the rest } delete[] temp;#ifdef MULTREE for(int i=t+1;i<NUM_TREES;i++) tree_starts[i] += chnglen;#endif#ifdef TRACE mum.xpoint = tree;#endif//}while (probl->static_check(this,t)>rnd(100)); implemnt sometime?}//end MutateSubTree#ifdef MULTREEvoid Chrome::write(int pretty,ostream& ofile, int last_tree)//WBL{for (int t = 0; t <= last_tree; t++ ) { ip = tree_starts[t]-1; int depth = 0; ofile<<endl; probl->WriteTreeName(t,ofile); ofile<<"\t="; SubWrite(ofile,pretty, depth); // write the expression }}#endif#ifdef TRACEvoid Chrome::write_trace(ostream& fout) //WBL{ fout << " Created " << birth; fout << " fitness "; nfitness->write (fout); fout << " len " << tree_starts[NUM_TREES-1] + SubLen(tree_starts[NUM_TREES-1]); fout << " children " << num_children; if ( mum.birth >= 0 ) { fout << "\tmum=" << mum.birth; fout << "x" << mum.xpoint; } if ( dad.birth >= 0 ) { fout << ",dad=" << dad.birth; fout << "x" << dad.xpoint << ","; }#ifdef MULTREE if ( mum.birth >= 0 && mum.xtree >= 0 ) probl->WriteTreeName(mum.xtree);#endif}//end Chrome::write_trace()#endif //TRACEBOOL iscomment(int c,istream& istr) { if(c=='%') { do{istr.get();} while((istr.peek()!=EOF)&&(istr.peek()!='\n')); return TRUE; } else return FALSE;}#define EATSPACE c=istr.peek();while(isspace(c)||c==':'||iscomment(c,istr)) {istr.get();c=istr.peek();}#ifndef debug#define GETTOK tp=0;c=istr.peek();while(c!=EOF && !isspace(c) && c!=':' && c!='(' && c!=')' &&tp<79) {scratch[tp++]=istr.get(); c=istr.peek();} scratch[tp]='\0'#else#define GETTOK tp=0;c=istr.peek();while(c!=EOF && !isspace(c) &&c!=':' && c!='(' && c!=')' &&tp<79) {scratch[tp++]=istr.get(); c=istr.peek();} scratch[tp]='\0';cout<<" `"<<scratch<<"'"<<flush;//debug#endif //debugint Chrome::Load(istream& istr,int issource)// Load an expression from a stream// issource should always be TRUE{ int x,tempop, tempidx; int rval=LOAD_OK; node* buf; //Get a new clean FitnessValue object delete nfitness; nfitness = probl->GetFitnessObj(this); birth = 0; if (!issource) { for(x=0;x<params->params[pMaxExpr];x++) { istr >> tempop; expr[x].op =(unsigned) tempop;// istr >> tempidx;// expr[x].idx=(unsigned) tempidx; } } else // load a Source file { ip=0; buf=new node[params->params[pMaxExpr]];#ifdef MULTREE char c; int tp; rval = LOAD_OK; for (int t = 0; t < NUM_TREES && rval == LOAD_OK; t++ ){ tree_starts[t] = ip; EATSPACE; GETTOK; int temp = probl->TreeNameMatch(t,scratch);#ifdef dbugcout <<"tree "<<scratch<<((temp==0)? " name ok" : "BAD_TREE_NAME")<<endl;#endif //debug if (temp != 0) rval=BAD_TREE_NAME; else{ EATSPACE; GETTOK; if (scratch[0] != '=') rval=BAD_TREE_FORMAT; else{ rval=SubLoad(istr,buf,t); }}#ifdef debug cout<<endl;#endif }//end for each tree#else rval=SubLoad(istr,buf);#ifdef debug cout<<endl;#endif#endif //MULTREE if (rval==LOAD_OK) { delete[] expr; expr=buf;#ifdef GEN_MEM rval = nfitness->Load(istr);#endif } else delete[] buf; }#ifdef MULTREE for (int t = 0; t < NUM_TREES && rval == LOAD_OK; t++ ){ ip = tree_starts[t]; int valid = probl->static_check(this,t); if (valid>0) cout<<"static_check reports "<<valid <<" on tree "<<t<<endl; }#endif return rval;}#ifdef MULTREEint Chrome::SubLoad(istream& istr,node* buf, int tree)#elseint Chrome::SubLoad(istream& istr,node* buf)#endif// load a sub-expression from source format// Return LOAD_OK or an error value// text expression is in istr// nodes go into buf{ int rval = LOAD_OK; char c;// char token[80]; int tp; int func; int args; int vnum; char* s; scratch[0]='\0'; EATSPACE; if (istr.peek()==')' || istr.peek()==EOF) { rval=LOAD_TOOFEW; // missing expression body } else if (ip>=params->params[pMaxExpr]) { rval=LOAD_TOOLONG; } else if (istr.peek()=='(') // Get an expression and the close paren { istr >> c;#ifdef debugcout<<c<<flush;#endif //debug#ifdef MULTREE rval = SubLoad(istr,buf,tree);#else rval = SubLoad(istr,buf);#endif if (rval==LOAD_OK) { EATSPACE; istr >> c; if (c!=')') {#ifdef debugcout<<"LOAD_TOOMANY `"<<c<<"'"<<flush;#endif //debug rval=LOAD_TOOMANY;}#ifdef debug else cout<<c<<flush;#endif //debug } } else // Get an expression. Return if you hit a close paren. { GETTOK; if (strlen(scratch)==0) rval=LOAD_TOOFEW; else {//DIRTY FRIG - I DONT USE CONSTANTS (EXCEPT 0 and 1)// if (isdigit(scratch[0]) || scratch[0]=='-') // it's a number. Function 0// {// func=atoi(scratch);// if (func>=0-CONSTARRAYSIZE/2 && func<CONSTARRAYSIZE/2)// {// SETNODE(buf[ip],0,func+CONSTARRAYSIZE/2);// ip++;// rval=LOAD_OK;// }// else rval=LOAD_BADCONST;// }// else // look up a function name { vnum=0; if (strchr(scratch,'_')!=NULL) { // parse it to take off the variable number? // This is fancy footwork for functions with operands // Except for NUMBER (function 0) these functions are // written as <name>_operand s=strrchr(scratch,'_'); if (strchr("0123456789",s[1])) // it is an underscore followed by numbers { vnum=atoi(s+1); if (vnum>=0 && vnum <= CONSTARRAYSIZE) { s[0]='\0'; // found a valid function with variable number appended. Keep function name. } else vnum=0; } }#ifdef MULTREE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -