Changeset 168 for cpp/frams/genetics/f9
- Timestamp:
- 03/11/14 14:45:29 (11 years ago)
- Location:
- cpp/frams/genetics/f9
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/genetics/f9/conv_f9.cpp
r157 r168 13 13 { 14 14 name = "Turtle3D-ortho encoding"; 15 in_format 15 in_format = '9'; 16 16 out_format = '0'; 17 17 mapsupport = 0; //would be easy and nice to add! … … 19 19 20 20 21 const char* turtle_commands_f9 = "LRDUBF";21 const char* turtle_commands_f9 = "LRDUBF"; 22 22 23 23 //const char* turtle_commandsX_f9="-+0000"; … … 31 31 Model m; 32 32 m.open(); 33 int recently_added =addSegment(m,vertices,current,0xDead);34 for (int i=0;i<in.len();i++)33 int recently_added = addSegment(m, vertices, current, 0xDead); 34 for (int i = 0; i < in.len(); i++) 35 35 { 36 char command =in[i];37 char *ptr =strchr((char*)turtle_commands_f9,command);36 char command = in[i]; 37 char *ptr = strchr((char*)turtle_commands_f9, command); 38 38 if (ptr) 39 39 { 40 int delta[] ={0,0,0};41 int pos =ptr-turtle_commands_f9;42 int axis =pos/2;43 int dir =pos%2;44 (*(delta +axis))+=dir*2-1; //+1 or -1 in the given axis40 int delta[] = { 0, 0, 0 }; 41 int pos = ptr - turtle_commands_f9; 42 int axis = pos / 2; 43 int dir = pos % 2; 44 (*(delta + axis)) += dir * 2 - 1; //+1 or -1 in the given axis 45 45 current.add(delta); 46 recently_added =addSegment(m,vertices,current,recently_added);46 recently_added = addSegment(m, vertices, current, recently_added); 47 47 } 48 48 } … … 57 57 } 58 58 59 int GenoConv_f90::addSegment(Model &m, vector<XYZ_LOC> &vertices,const XYZ_LOC &new_vertex,int recently_added)59 int GenoConv_f90::addSegment(Model &m, vector<XYZ_LOC> &vertices, const XYZ_LOC &new_vertex, int recently_added) 60 60 { 61 if (vertices.size() <1) //empty model?61 if (vertices.size() < 1) //empty model? 62 62 { 63 return addNewVertex(m,vertices,new_vertex); 64 } else 63 return addNewVertex(m, vertices, new_vertex); 64 } 65 else 65 66 { 66 int vertex_here =findVertexAt(vertices,new_vertex);67 if (vertex_here <0) //need to create a new Part67 int vertex_here = findVertexAt(vertices, new_vertex); 68 if (vertex_here < 0) //need to create a new Part 68 69 { 69 vertex_here =addNewVertex(m,vertices,new_vertex);70 vertex_here = addNewVertex(m, vertices, new_vertex); 70 71 } //else there already exists a Part in new_vertex; new Joint may or may not be needed 71 Part *p1 =m.getPart(recently_added);72 Part *p2 =m.getPart(vertex_here);73 if (m.findJoint(p1, p2)<0 && m.findJoint(p2,p1)<0) //new Joint needed? should always be true if we just created a new Part (vertex_here was <0)72 Part *p1 = m.getPart(recently_added); 73 Part *p2 = m.getPart(vertex_here); 74 if (m.findJoint(p1, p2) < 0 && m.findJoint(p2, p1) < 0) //new Joint needed? should always be true if we just created a new Part (vertex_here was <0) 74 75 { 75 m.addNewJoint(p1, p2);76 m.addNewJoint(p1, p2); 76 77 } 77 78 return vertex_here; … … 79 80 } 80 81 81 int GenoConv_f90::findVertexAt(vector<XYZ_LOC> &vertices, const XYZ_LOC &vertex)82 int GenoConv_f90::findVertexAt(vector<XYZ_LOC> &vertices, const XYZ_LOC &vertex) 82 83 { 83 for (int i=0;i<vertices.size();i++)84 for (int i = 0; i < vertices.size(); i++) 84 85 if (vertices[i].same_coordinates(vertex)) return i; 85 86 return -1; … … 87 88 88 89 89 int GenoConv_f90::addNewVertex(Model &m, vector<XYZ_LOC> &vertices,const XYZ_LOC &new_vertex)90 int GenoConv_f90::addNewVertex(Model &m, vector<XYZ_LOC> &vertices, const XYZ_LOC &new_vertex) 90 91 { 91 Part *p =new Part;92 p->p.x =new_vertex.x;93 p->p.y =new_vertex.y;94 p->p.z =new_vertex.z;92 Part *p = new Part; 93 p->p.x = new_vertex.x; 94 p->p.y = new_vertex.y; 95 p->p.z = new_vertex.z; 95 96 m.addPart(p); 96 97 97 98 vertices.push_back(new_vertex); 98 return vertices.size() -1;99 return vertices.size() - 1; 99 100 } 100 101 101 double mix(int *colortab, int maxind,double ind)102 double mix(int *colortab, int maxind, double ind) 102 103 { 103 int indpre =(int)ind;104 int indpost =indpre+1;105 if (indpost >maxind) indpost=maxind;106 int v1 =colortab[indpre];107 int v2 =colortab[indpost];108 double d1 =ind-indpre;109 double d2 =indpost-ind;110 double v =indpre==indpost?v1:d2*v1+d1*v2; //d1+d2==1104 int indpre = (int)ind; 105 int indpost = indpre + 1; 106 if (indpost > maxind) indpost = maxind; 107 int v1 = colortab[indpre]; 108 int v2 = colortab[indpost]; 109 double d1 = ind - indpre; 110 double d2 = indpost - ind; 111 double v = indpre == indpost ? v1 : d2*v1 + d1*v2; //d1+d2==1 111 112 return v; 112 113 } … … 115 116 { 116 117 //a rainbow on Joints: from the first one red, through middle green, to blue or violet - last 117 static int r[] ={1,1,0,0,0,1};118 static int g[] ={0,1,1,1,0,0};119 static int b[] ={0,0,0,1,1,1};120 int maxind =ARRAY_LENGTH(r)-1;118 static int r[] = { 1, 1, 0, 0, 0, 1 }; 119 static int g[] = { 0, 1, 1, 1, 0, 0 }; 120 static int b[] = { 0, 0, 0, 1, 1, 1 }; 121 int maxind = ARRAY_LENGTH(r) - 1; 121 122 122 int joints_count =m.getJointCount();123 for (int i=0;i<joints_count;i++)123 int joints_count = m.getJointCount(); 124 for (int i = 0; i < joints_count; i++) 124 125 { 125 Joint *j =m.getJoint(i);126 double x =joints_count<2?0:(double)i/(joints_count-1); //0..1, postion in the rainbow127 double ind =x*maxind;128 j->vcolor.x =mix(r,maxind,ind);129 j->vcolor.y =mix(g,maxind,ind);130 j->vcolor.z =mix(b,maxind,ind);126 Joint *j = m.getJoint(i); 127 double x = joints_count < 2 ? 0 : (double)i / (joints_count - 1); //0..1, postion in the rainbow 128 double ind = x*maxind; 129 j->vcolor.x = mix(r, maxind, ind); 130 j->vcolor.y = mix(g, maxind, ind); 131 j->vcolor.z = mix(b, maxind, ind); 131 132 } 132 133 133 int parts_count =m.getPartCount();134 int parts_count = m.getPartCount(); 134 135 SList jlist; 135 for (int i=0;i<parts_count;i++)136 for (int i = 0; i<parts_count; i++) 136 137 { 137 Part *p =m.getPart(i);138 Part *p = m.getPart(i); 138 139 jlist.clear(); 139 int count =m.findJoints(jlist,p);140 Pt3D averagecolor(0, 0,0); //Parts will get averaged colors from all attached Joints141 FOREACH(Joint*, j,jlist)142 averagecolor +=j->vcolor;143 if (count>5) count=5; //avoid too fat...144 p->vsize=0.3+count/15.0; //the more Joints is attached to a Part, the fatter it is145 p->v color=averagecolor/count;140 int count = m.findJoints(jlist, p); 141 Pt3D averagecolor(0, 0, 0); //Parts will get averaged colors from all attached Joints 142 FOREACH(Joint*, j, jlist) 143 averagecolor += j->vcolor; 144 p->vcolor = averagecolor / count; 145 if (count>5) count = 5; //avoid too fat... 146 p->vsize = 0.3 + count / 15.0; //the more Joints is attached to a Part, the fatter it is 146 147 } 147 148 } … … 149 150 void GenoConv_f90::perturbPartLocations(Model &m) //deterministic "body noise", see APPLY_DETERMINISTIC_BODY_NOISE 150 151 { 151 for (int i=0;i<m.getPartCount();i++)152 for (int i = 0; i < m.getPartCount(); i++) 152 153 { 153 Part *p =m.getPart(i);154 Part *p = m.getPart(i); 154 155 Pt3D noise( 155 ((i +1) %10)-4.5,156 ((3 *i+5)%10)-4.5,157 ((7 *i+2)%10)-4.5156 ((i + 1) % 10) - 4.5, 157 ((3 * i + 5) % 10) - 4.5, 158 ((7 * i + 2) % 10) - 4.5 158 159 ); //-4.5 .. 4.5 in each axis 159 p->p +=noise/1000;160 p->p += noise / 1000; 160 161 } 161 162 } -
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.