Changeset 168 for cpp/frams/genetics/f9/oper_f9.cpp
- Timestamp:
- 03/11/14 14:45:29 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/genetics/f9/oper_f9.cpp
r145 r168 9 9 10 10 #define FIELDSTRUCT GenoOper_f9 11 static ParamEntry GENOf9param_tab[] =11 static ParamEntry GENOf9param_tab[] = 12 12 { 13 { "Genetics: f9",1,1,},14 { "f9_mut",0,0,"Mutation probability","f 0 1 0.1",FIELD(mut_prob),"How many genes should be mutated during single mutation (1=all genes, 0.1=ten percent)",},15 { 0,},13 { "Genetics: f9", 1, 1, }, 14 { "f9_mut", 0, 0, "Mutation probability", "f 0 1 0.1", FIELD(mut_prob), "How many genes should be mutated during single mutation (1=all genes, 0.1=ten percent)", }, 15 { 0, }, 16 16 }; 17 17 #undef FIELDSTRUCT … … 23 23 par.select(this); 24 24 par.setDefault(); 25 supported_format ='9';25 supported_format = '9'; 26 26 } 27 27 … … 29 29 { 30 30 if (!gene[0]) return 1; //empty is not valid 31 bool ok =true;31 bool ok = true; 32 32 int i; 33 for (i=0;i<strlen(gene);i++) if (!strchr(turtle_commands_f9,gene[i])) {ok=false; break;}34 return ok ? GENOPER_OK : i +1;33 for (i = 0; i < strlen(gene); i++) if (!strchr(turtle_commands_f9, gene[i])) { ok = false; break; } 34 return ok ? GENOPER_OK : i + 1; 35 35 } 36 36 … … 39 39 { 40 40 SString validated; //new genotype (everything except turtle_commands_f9 is skipped) 41 for (int i=0;i<strlen(gene);i++)42 if (strchr(turtle_commands_f9, gene[i])) validated+=gene[i]; //validated contains only turtle_commands_f941 for (int i = 0; i < strlen(gene); i++) 42 if (strchr(turtle_commands_f9, gene[i])) validated += gene[i]; //validated contains only turtle_commands_f9 43 43 free(gene); 44 gene =strdup(validated); //reallocate44 gene = strdup(validated); //reallocate 45 45 return GENOPER_OK; 46 46 } 47 47 48 48 ///Very simple mutation 49 int GenoOper_f9::mutate(char *&gene, float &chg,int &method)49 int GenoOper_f9::mutate(char *&gene, float &chg, int &method) 50 50 { 51 method =0;52 int changes =0,len=strlen(gene);53 int symbols =strlen(turtle_commands_f9);51 method = 0; 52 int changes = 0, len = strlen(gene); 53 int symbols = strlen(turtle_commands_f9); 54 54 55 for (int i=0;i<len;i++)55 for (int i = 0; i < len; i++) 56 56 { 57 if (rnd01 <mut_prob) //normalize prob with the length of the genotype57 if (rnd01 < mut_prob) //normalize prob with the length of the genotype 58 58 { 59 char oldgene =gene[i];60 gene[i] =turtle_commands_f9[randomN(symbols)];61 if (gene[i] !=oldgene) changes++;59 char oldgene = gene[i]; 60 gene[i] = turtle_commands_f9[randomN(symbols)]; 61 if (gene[i] != oldgene) changes++; 62 62 } 63 63 } 64 64 65 if (rnd01 <mut_prob) //add or delete a random char65 if (rnd01 < mut_prob) //add or delete a random char 66 66 { 67 67 SString newgeno(gene); 68 if (randomN(2) ==0) //add68 if (randomN(2) == 0) //add 69 69 { 70 int symbols =strlen(turtle_commands_f9);71 int p =randomN(len+1); //random location70 int symbols = strlen(turtle_commands_f9); 71 int p = randomN(len + 1); //random location 72 72 //printf("before add: %s\n",(const char*)newgeno); 73 newgeno =newgeno.substr(0,p)+SString(turtle_commands_f9+randomN(symbols),1)+newgeno.substr(p);73 newgeno = newgeno.substr(0, p) + SString(turtle_commands_f9 + randomN(symbols), 1) + newgeno.substr(p); 74 74 //printf("after add: %s\n",(const char*)newgeno); 75 75 changes++; 76 } else if (len>1) //delete 76 } 77 else if (len > 1) //delete 77 78 { 78 int p =randomN(len); //random location79 int p = randomN(len); //random location 79 80 //printf("before delete: %s\n",(const char*)newgeno); 80 newgeno =newgeno.substr(0,p)+newgeno.substr(p+1);81 newgeno = newgeno.substr(0, p) + newgeno.substr(p + 1); 81 82 //printf("after delete: %s\n",(const char*)newgeno); 82 83 changes++; 83 84 } 84 85 free(gene); 85 gene =strdup(newgeno); //reallocate86 gene = strdup(newgeno); //reallocate 86 87 } 87 88 88 chg =(float)changes/len;89 return changes >0?GENOPER_OK:GENOPER_OPFAIL; //no changes => OPFAIL so that genman will call mutate again89 chg = (float)changes / len; 90 return changes > 0 ? GENOPER_OK : GENOPER_OPFAIL; //no changes => OPFAIL so that GenMan will call mutate again 90 91 } 91 92 92 93 ///A simple one-point crossover 93 int GenoOper_f9::crossOver(char *&g1, char *&g2,float& chg1,float& chg2)94 int GenoOper_f9::crossOver(char *&g1, char *&g2, float& chg1, float& chg2) 94 95 { 95 int len1 =strlen(g1),len2=strlen(g2);96 int p1 =randomN(len1); //random cut point for first genotype97 int p2 =randomN(len2); //random cut point for second genotype98 char *child1 =(char*)malloc(p1+len2-p2+1);99 char *child2 =(char*)malloc(p2+len1-p1+1);100 strncpy(child1, g1,p1); strcpy(child1+p1,g2+p2);101 strncpy(child2, g2,p2); strcpy(child2+p2,g1+p1);102 free(g1); g1 =child1;103 free(g2); g2 =child2;104 chg1 =(float)p1/strlen(child1);105 chg2 =(float)p2/strlen(child2);96 int len1 = strlen(g1), len2 = strlen(g2); 97 int p1 = randomN(len1); //random cut point for first genotype 98 int p2 = randomN(len2); //random cut point for second genotype 99 char *child1 = (char*)malloc(p1 + len2 - p2 + 1); 100 char *child2 = (char*)malloc(p2 + len1 - p1 + 1); 101 strncpy(child1, g1, p1); strcpy(child1 + p1, g2 + p2); 102 strncpy(child2, g2, p2); strcpy(child2 + p2, g1 + p1); 103 free(g1); g1 = child1; 104 free(g2); g2 = child2; 105 chg1 = (float)p1 / strlen(child1); 106 chg2 = (float)p2 / strlen(child2); 106 107 return GENOPER_OK; 107 108 } … … 110 111 unsigned long GenoOper_f9::style(const char *g, int pos) 111 112 { 112 char ch =g[pos];113 unsigned long style =GENSTYLE_CS(0,GENSTYLE_INVALID); //default, should be changed below114 char *ptr =strchr((char*)turtle_commands_f9,ch);113 char ch = g[pos]; 114 unsigned long style = GENSTYLE_CS(0, GENSTYLE_INVALID); //default, should be changed below 115 char *ptr = strchr((char*)turtle_commands_f9, ch); 115 116 if (ptr) 116 117 { 117 int pos =ptr-turtle_commands_f9;118 int axis =pos/2;119 style =GENSTYLE_RGBS(axis==0?200:0,axis==1?200:0,axis==2?200:0,GENSTYLE_NONE);118 int pos = ptr - turtle_commands_f9; 119 int axis = pos / 2; 120 style = GENSTYLE_RGBS(axis == 0 ? 200 : 0, axis == 1 ? 200 : 0, axis == 2 ? 200 : 0, GENSTYLE_NONE); 120 121 } 121 122 return style;
Note: See TracChangeset
for help on using the changeset viewer.