Changeset 145 for cpp/frams/genetics/fF
- Timestamp:
- 02/26/14 20:21:22 (11 years ago)
- Location:
- cpp/frams/genetics/fF
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/genetics/fF/fF_genotype.h
r140 r145 8 8 #include <stdio.h> 9 9 #include "common/stl-util.h" 10 10 #include <frams/param/param.h> 11 11 12 12 struct fF_growth_params 13 13 { 14 14 int number_of_chambers; 15 double scalex, scaley, scalez; //scaling factors16 double translation; //translation factor, TF range [−1; 1]15 double scalex, scaley, scalez; 16 double translation; 17 17 double angle1, angle2; 18 18 19 static ParamEntry paramtab[]; 20 Param param; 21 19 22 fF_growth_params() 23 :param(paramtab, this) 20 24 { 21 25 reset(); … … 24 28 void reset() 25 29 { 26 number_of_chambers = 1; 27 scalex = scaley = scalez = 1; 28 translation = 1; 29 angle1 = angle2 = 0; 30 param.setDefault(); 30 31 } 31 32 32 33 bool load(const char* serialized) 33 34 { 34 return sscanf(serialized, "%d %lf %lf %lf %lf %lf %lf", &number_of_chambers, &scalex, &scaley, &scalez, &translation, &angle1, &angle2) == 7; 35 SString s = serialized; 36 int p = 0; //position in string 37 return ((param.load2(s, p)==7) && (p==s.len())); 35 38 } 36 39 37 40 string save() 38 41 { 39 return ssprintf("%d %f %f %f %f %f %f", number_of_chambers, scalex, scaley, scalez, translation, angle1, angle2); 42 SString tmp; 43 param.save2(tmp, NULL/*object containing default values for comparison*/, false/*add CR*/, false/*force field names*/); 44 return string((const char*)tmp); 40 45 } 41 46 }; -
cpp/frams/genetics/fF/oper_fF.cpp
r140 r145 4 4 5 5 #include "oper_fF.h" 6 #include <frams/genetics/f9/conv_f9.h>6 #include "fF_genotype.h" 7 7 #include <common/nonstd.h> //randomN, rnd01 8 8 9 9 10 //THIS FILE NEEDS UPDATE. OLD, UNRELATED SOURCES BELOW (COPIED FROM f9).11 12 10 #define FIELDSTRUCT GenoOper_fF 13 static ParamEntry GENOf 9param_tab[]=11 static ParamEntry GENOfFparam_tab[] = 14 12 { 15 { "Genetics: fF",1,1,},16 { "fF_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)",},17 { 0,},13 { "Genetics: fF", 1, 1, }, 14 { "fF_xover", 0, 0, "Averaging crossover proportion", "f 0 0.5 0.5", FIELD(xover_proportion), "0.5 => children are averaged parents.\n0.2 => children are only 20% different from parents.\n0 => each child is identical to one parent (no crossover).", }, 15 { 0, }, 18 16 }; 19 17 #undef FIELDSTRUCT … … 22 20 GenoOper_fF::GenoOper_fF() 23 21 { 24 par.setParamTab(GENOf 9param_tab);22 par.setParamTab(GENOfFparam_tab); 25 23 par.select(this); 26 24 par.setDefault(); 27 supported_format ='F';25 supported_format = 'F'; 28 26 } 29 27 30 28 int GenoOper_fF::checkValidity(const char* gene) 31 29 { 32 return GENOPER_OK; 33 if (!gene[0]) return 1; //empty is not valid 34 bool ok=true; 35 int i; 36 for(i=0;i<strlen(gene);i++) if (!strchr(turtle_commands_f9,gene[i])) {ok=false; break;} 37 return ok ? GENOPER_OK : i+1; 30 fF_growth_params par; 31 return par.load(gene) ? GENOPER_OK : 1; 38 32 } 39 33 40 ///Remove all invalid letters from the genotype41 34 int GenoOper_fF::validate(char *&gene) 42 35 { 43 SString validated; //new genotype (everything except turtle_commands_f9 is skipped)44 for(int i=0;i<strlen(gene);i++)45 if (strchr(turtle_commands_f9,gene[i])) validated+=gene[i]; //validated contains only turtle_commands_f936 fF_growth_params par; //is initialized with default values 37 par.load(gene); //loads as much as possible, other fields remain with default values 38 string validated = par.save(); 46 39 free(gene); 47 gene =strdup(validated); //reallocate40 gene = strdup(validated.c_str()); //reallocate 48 41 return GENOPER_OK; 49 42 } 50 43 51 // /Very simple mutation52 int GenoOper_fF::mutate(char *&gene, float &chg,int &method)44 //Creep-mutate one property 45 int GenoOper_fF::mutate(char *&gene, float &chg, int &method) 53 46 { 54 method=0; 55 int changes=0,len=strlen(gene); 56 int symbols=strlen(turtle_commands_f9); 57 58 for(int i=0;i<len;i++) 47 method = 0; 48 fF_growth_params par; 49 par.load(gene); 50 int which = randomN(par.param.getPropCount()); 51 bool mutated_ok = GenoOperators::mutatePropertyNaive(par.param, which); 52 if (mutated_ok) 59 53 { 60 if (rnd01<mut_prob) //normalize prob with the length of the genotype 61 { 62 char oldgene=gene[i]; 63 gene[i]=turtle_commands_f9[randomN(symbols)]; 64 if (gene[i]!=oldgene) changes++; 65 } 54 string saved = par.save(); 55 free(gene); 56 gene = strdup(saved.c_str()); //reallocate 57 chg = 1.0f / par.param.getPropCount(); 58 return GENOPER_OK; 66 59 } 67 68 if (rnd01<mut_prob) //add or delete a random char 60 else 69 61 { 70 SString newgeno(gene); 71 if (randomN(2)==0) //add 72 { 73 int symbols=strlen(turtle_commands_f9); 74 int p=randomN(len+1); //random location 75 //printf("before add: %s\n",(const char*)newgeno); 76 newgeno=newgeno.substr(0,p)+SString(turtle_commands_f9+randomN(symbols),1)+newgeno.substr(p); 77 //printf("after add: %s\n",(const char*)newgeno); 78 changes++; 79 } else if (len>1) //delete 80 { 81 int p=randomN(len); //random location 82 //printf("before delete: %s\n",(const char*)newgeno); 83 newgeno=newgeno.substr(0,p)+newgeno.substr(p+1); 84 //printf("after delete: %s\n",(const char*)newgeno); 85 changes++; 86 } 87 free(gene); 88 gene=strdup(newgeno); //reallocate 62 chg = 0; 63 return GENOPER_OPFAIL; 89 64 } 90 91 chg=(float)changes/len;92 return changes>0?GENOPER_OK:GENOPER_OPFAIL; //no changes => OPFAIL so that genman will call mutate again93 65 } 94 66 95 ///A simple one-pointcrossover96 int GenoOper_fF::crossOver(char *&g1, char *&g2,float& chg1,float& chg2)67 ///Averaging crossover 68 int GenoOper_fF::crossOver(char *&g1, char *&g2, float& chg1, float& chg2) 97 69 { 98 int len1=strlen(g1),len2=strlen(g2); 99 int p1=randomN(len1); //random cut point for first genotype 100 int p2=randomN(len2); //random cut point for second genotype 101 char *child1=(char*)malloc(p1+len2-p2+1); 102 char *child2=(char*)malloc(p2+len1-p1+1); 103 strncpy(child1,g1,p1); strcpy(child1+p1,g2+p2); 104 strncpy(child2,g2,p2); strcpy(child2+p2,g1+p1); 105 free(g1); g1=child1; 106 free(g2); g2=child2; 107 chg1=(float)p1/strlen(child1); 108 chg2=(float)p2/strlen(child2); 70 //g1 = strdup("1,0.5,0.5,0.5,0.5,1,1"); //testing... 71 //g2 = strdup("4,1, 1, 1, 1, 2,2"); //testing... 72 //xover_proportion = 0.1; //testing... 73 fF_growth_params par1; 74 par1.load(g1); 75 fF_growth_params par2; 76 par2.load(g2); 77 chg1 = xover_proportion; 78 chg2 = 1 - xover_proportion; 79 for (int i = 0; i < par1.param.getPropCount(); i++) 80 GenoOperators::linearMix(par1.param, i, par2.param, i, xover_proportion); 81 string saved = par1.save(); 82 free(g1); 83 g1 = strdup(saved.c_str()); //reallocate 84 saved = par2.save(); 85 free(g2); 86 g2 = strdup(saved.c_str()); //reallocate 109 87 return GENOPER_OK; 110 88 } … … 113 91 unsigned long GenoOper_fF::style(const char *g, int pos) 114 92 { 115 char ch=g[pos]; 116 unsigned long style=GENSTYLE_CS(0,GENSTYLE_INVALID); //default, should be changed below 117 char *ptr=strchr((char*)turtle_commands_f9,ch); 118 if (ptr) 119 { 120 int pos=ptr-turtle_commands_f9; 121 int axis=pos/2; 122 style=GENSTYLE_RGBS(axis==0?200:0,axis==1?200:0,axis==2?200:0,GENSTYLE_NONE); 123 } 93 char ch = g[pos]; 94 unsigned long style = GENSTYLE_CS(0, GENSTYLE_INVALID); //default, should be changed below 95 if (strchr("-.e 0123456789", ch) != NULL) 96 style = GENSTYLE_CS(GENCOLOR_NUMBER, GENSTYLE_NONE); 97 else if (ch == ',') 98 style = GENSTYLE_RGBS(0, 0, 0, GENSTYLE_BOLD); 124 99 return style; 125 100 } -
cpp/frams/genetics/fF/oper_fF.h
r140 r145 18 18 int crossOver(char *&g1, char *&g2, float& chg1, float& chg2); 19 19 unsigned long style(const char *g, int pos); 20 const char* getSimplest() { return " 5 0.9 0.8 0.7 0.7 0.30.7"; }20 const char* getSimplest() { return "6,0.8,0.85,0.9,0.6,0.3,0.7"; } 21 21 22 double mut_prob; //mutation probability22 double xover_proportion; 23 23 }; 24 24
Note: See TracChangeset
for help on using the changeset viewer.