Changeset 759 for cpp/frams/genetics/f1
- Timestamp:
- 03/15/18 22:53:35 (7 years ago)
- Location:
- cpp/frams/genetics/f1
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/genetics/f1/conv_f1.cpp
r743 r759 8 8 #include <frams/util/multirange.h> 9 9 #include <frams/util/multimap.h> 10 #include <frams/genetics/geneprops.h> 10 11 #include <ctype.h> 11 12 #include <assert.h> 12 13 13 14 //#define v1f1COMPATIBLE //as in ancient Framsticks 1.x 14 15 F1Props stdprops = { 1, 0, 1, 0.4, 0.25, 0.25, 0.25, 0.25, 0.0, 1.0, 1.0, 1,16 0.2, 0.5, 0.5, 0.5 };17 15 18 16 class Builder … … 43 41 double model_energy; 44 42 int model_energy_count; 45 void grow(int part1, const char*g, Pt3D k, F1Props c, int branching_part);43 void grow(int part1, const char*g, Pt3D k, GeneProps c, int branching_part); 46 44 void setPartMapping(int p, const char* g); 47 int growJoint(int part1, int part2, Pt3D &angle, F1Props &c, const char *g);48 int growPart( F1Props &c, const char *g);45 int growJoint(int part1, int part2, Pt3D &angle, GeneProps &c, const char *g); 46 int growPart(GeneProps &c, const char *g); 49 47 const char *skipNeuro(const char *z); 50 const char* growNeuro(const char* t, F1Props &c, int&);51 void growConnection(const char* begin, const char* colon, const char* end, F1Props& props);48 const char* growNeuro(const char* t, GeneProps &c, int&); 49 void growConnection(const char* begin, const char* colon, const char* end, GeneProps& props); 52 50 int countBranches(const char*g, SList &out); 53 51 SyntParam* lastNeuroClassParam(); … … 96 94 } 97 95 98 void F1Props::normalizeBiol4()99 {100 double sum = muscle_power + assimilation + stamina + ingestion;101 muscle_power /= sum;102 assimilation /= sum;103 stamina /= sum;104 ingestion /= sum;105 }106 107 96 /** main conversion function - with conversion map support */ 108 97 SString GenoConv_f1::convert(SString &i, MultiMap *map, bool using_checkpoints) … … 111 100 Builder builder(g, map ? 1 : 0); 112 101 builder.model.open(using_checkpoints); 113 builder.grow(-1, g, Pt3D_0, stdprops, -1); // uses Model::addFromString() to create model elements102 builder.grow(-1, g, Pt3D_0, GeneProps::standard_values, -1); // uses Model::addFromString() to create model elements 114 103 if (builder.invalid) return SString(); 115 104 builder.addPendingInputs(); … … 133 122 } 134 123 135 void Builder::grow(int part1, const char*g, Pt3D k, F1Props c, int branching_part)124 void Builder::grow(int part1, const char*g, Pt3D k, GeneProps c, int branching_part) 136 125 { 137 126 int hasmuscles = 0; … … 139 128 while (1) 140 129 { 141 switch (*g) 142 { 143 case 0: return; 144 case ',': case ')': setPartMapping(branching_part, g); return; 145 case 'R': k.x += 0.7853; setPartMapping(part1, g); break; 146 case 'r': k.x -= 0.7853; setPartMapping(part1, g); break; 147 case 'Q': c.twist += (1.58 - c.twist)*0.3; setPartMapping(part1, g); break; 148 case 'q': c.twist += (-1.58 - c.twist)*0.3; setPartMapping(part1, g); break; 149 #ifdef v1f1COMPATIBLE 150 case 'L': c.length += (3.0 - c.length)*0.3; setPartMapping(part1, g); break; 151 #else 152 case 'L': c.length += (2.0 - c.length)*0.3; setPartMapping(part1, g); break; 153 #endif 154 case 'l': c.length += (0.33 - c.length)*0.3; setPartMapping(part1, g); break; 155 case 'A': c.assimilation += (1 - c.assimilation)*0.8; c.normalizeBiol4(); setPartMapping(part1, g); break; 156 case 'a': c.assimilation -= c.assimilation*0.4; c.normalizeBiol4(); setPartMapping(part1, g); break; 157 case 'I': c.ingestion += (1 - c.ingestion)*0.8; c.normalizeBiol4(); setPartMapping(part1, g); break; 158 case 'i': c.ingestion -= c.ingestion*0.4; c.normalizeBiol4(); setPartMapping(part1, g); break; 159 case 'S': c.stamina += (1 - c.stamina)*0.8; c.normalizeBiol4(); setPartMapping(part1, g); break; 160 case 's': c.stamina -= c.stamina*0.4; c.normalizeBiol4(); setPartMapping(part1, g); break; 161 case 'M': c.muscle_power += (1 - c.muscle_power)*0.8; c.normalizeBiol4(); setPartMapping(part1, g); break; 162 case 'm': c.muscle_power -= c.muscle_power*0.4; c.normalizeBiol4(); setPartMapping(part1, g); break; 163 case 'C': c.curvedness += (2.0 - c.curvedness)*0.25; setPartMapping(part1, g); break; 164 case 'c': c.curvedness += (-2.0 - c.curvedness)*0.25; setPartMapping(part1, g); break; 165 case 'F': c.friction += (4 - c.friction)*0.2; setPartMapping(part1, g); break; 166 case 'f': c.friction -= c.friction*0.2; setPartMapping(part1, g); break; 167 case 'W': c.weight += (2.0 - c.weight)*0.3; setPartMapping(part1, g); break; 168 case 'w': c.weight += (0.5 - c.weight)*0.3; setPartMapping(part1, g); break; 169 case 'E': c.energy += (10.0 - c.energy)*0.1; setPartMapping(part1, g); break; 170 case 'e': c.energy -= c.energy*0.1; setPartMapping(part1, g); break; 171 172 case 'D': c.cred += (1.0 - c.cred)*0.25; setPartMapping(part1, g); break; 173 case 'd': c.cred += (0.0 - c.cred)*0.25; setPartMapping(part1, g); break; 174 case 'G': c.cgreen += (1.0 - c.cgreen)*0.25; setPartMapping(part1, g); break; 175 case 'g': c.cgreen += (0.0 - c.cgreen)*0.25; setPartMapping(part1, g); break; 176 case 'B': c.cblue += (1.0 - c.cblue)*0.25; setPartMapping(part1, g); break; 177 case 'b': c.cblue += (0.0 - c.cblue)*0.25; setPartMapping(part1, g); break; 178 case 'H': c.visual_size += (0.7 - c.visual_size)*0.25; setPartMapping(part1, g); break; 179 case 'h': c.visual_size += (0.05 - c.visual_size)*0.25; setPartMapping(part1, g); break; 180 181 case '[': //neuron 182 // setdebug(g-(char*)geny,DEBUGNEURO | !l_neu); 183 if (model.getJointCount()) 184 g = growNeuro(g + 1, c, hasmuscles); 185 else 186 { 187 logMessage("GenoConv_F1", "grow", 1, "Illegal neuron position (ignored)"); 188 g = skipNeuro(g + 1); 189 } 190 break; 191 case 'X': 192 { 193 int freshpart = 0; 194 //setdebug(g-(char*)geny,DEBUGEST | !l_est); 195 if (part1 < 0) //initial grow 196 { 197 if (model.getPartCount() > 0) 198 part1 = 0; 130 if (c.executeModifier(*g) == 0) 131 { 132 setPartMapping(part1, g); 133 } 134 else 135 { 136 switch (*g) 137 { 138 case 0: return; 139 case ',': case ')': setPartMapping(branching_part, g); return; 140 case 'R': k.x += 0.7853; setPartMapping(part1, g); break; 141 case 'r': k.x -= 0.7853; setPartMapping(part1, g); break; 142 case '[': //neuron 143 // setdebug(g-(char*)geny,DEBUGNEURO | !l_neu); 144 if (model.getJointCount()) 145 g = growNeuro(g + 1, c, hasmuscles); 199 146 else 200 147 { 201 part1 = growPart(c, g); 202 freshpart = 1; 203 if (first_part_mapping) 148 logMessage("GenoConv_F1", "grow", 1, "Illegal neuron position (ignored)"); 149 g = skipNeuro(g + 1); 150 } 151 break; 152 case 'X': 153 { 154 int freshpart = 0; 155 //setdebug(g-(char*)geny,DEBUGEST | !l_est); 156 if (part1 < 0) //initial grow 157 { 158 if (model.getPartCount() > 0) 159 part1 = 0; 160 else 204 161 { 205 //mapping was defined before creating this initial Part -> put it into the Part 206 assert(own_first_part_mapping); 207 model.getPart(part1)->setMapping(*first_part_mapping); 208 delete first_part_mapping; 209 //first_part_mapping can be still used later but from now on it references the internal Part mapping 210 first_part_mapping = model.getPart(part1)->getMapping(); 211 own_first_part_mapping = false; 162 part1 = growPart(c, g); 163 freshpart = 1; 164 if (first_part_mapping) 165 { 166 //mapping was defined before creating this initial Part -> put it into the Part 167 assert(own_first_part_mapping); 168 model.getPart(part1)->setMapping(*first_part_mapping); 169 delete first_part_mapping; 170 //first_part_mapping can be still used later but from now on it references the internal Part mapping 171 first_part_mapping = model.getPart(part1)->getMapping(); 172 own_first_part_mapping = false; 173 } 212 174 } 213 175 } 214 } 215 if (!freshpart) 216 { 217 Part *part = model.getPart(part1); 218 part->density = ((part->mass*part->density) + 1.0 / c.weight) / (part->mass + 1.0); // v=m*d 219 // part->volume+=1.0/c.weight; 220 part->mass += 1.0; 221 } 222 model_energy += 0.9*c.energy + 0.1; 223 model_energy_count++; 224 225 int part2 = growPart(c, g); 226 growJoint(part1, part2, k, c, g); 227 // est* e = new est(*s,*s2,k,c,zz,this); 228 229 // attenuate properties as they are propagated along the structure 230 c.length = 0.5*c.length + 0.5*stdprops.length; 231 c.visual_size = 0.5*c.visual_size + 0.5*stdprops.visual_size; 232 c.curvedness = 0.66*c.curvedness; 233 c.twist = 0.66*c.twist; 234 c.friction = 0.8*c.friction + 0.2*stdprops.friction; 235 236 c.assimilation = 0.8*c.assimilation + 0.2*stdprops.assimilation; 237 c.stamina = 0.8*c.stamina + 0.2*stdprops.stamina; 238 c.muscle_power = 0.8*c.muscle_power + 0.2*stdprops.muscle_power; 239 c.ingestion = 0.8*c.ingestion + 0.2*stdprops.ingestion; 240 c.weight += (stdprops.weight - c.weight)*0.5; 241 c.normalizeBiol4(); 242 243 if (c.muscle_reset_range) c.muscle_bend_range = 1.0; else c.muscle_reset_range = true; 244 model.checkpoint(); 245 grow(part2, g + 1, Pt3D_0, c, branching_part); 246 return; 247 } 248 case '(': 249 { 250 setPartMapping(part1, g); 251 SList ga; 252 int i, count; 253 count = countBranches(g + 1, ga); 254 c.muscle_reset_range = false; 255 c.muscle_bend_range = 1.0 / count; 256 for (i = 0; i < count; i++) 257 grow(part1, (char*)ga(i), k + Pt3D(0, 0, -M_PI + (i + 1)*(2 * M_PI / (count + 1))), c, part1); 258 return; 259 } 260 case ' ': case '\t': case '\n': case '\r': break; 261 default: invalid = 1; return; 176 if (!freshpart) 177 { 178 Part *part = model.getPart(part1); 179 part->density = ((part->mass*part->density) + 1.0 / c.weight) / (part->mass + 1.0); // v=m*d 180 // part->volume+=1.0/c.weight; 181 part->mass += 1.0; 182 } 183 model_energy += 0.9*c.energy + 0.1; 184 model_energy_count++; 185 186 int part2 = growPart(c, g); 187 growJoint(part1, part2, k, c, g); 188 // est* e = new est(*s,*s2,k,c,zz,this); 189 190 // attenuate properties as they are propagated along the structure 191 c.propagateAlong(true); 192 193 model.checkpoint(); 194 grow(part2, g + 1, Pt3D_0, c, branching_part); 195 return; 196 } 197 case '(': 198 { 199 setPartMapping(part1, g); 200 SList ga; 201 int i, count; 202 count = countBranches(g + 1, ga); 203 c.muscle_reset_range = false; 204 c.muscle_bend_range = 1.0 / count; 205 for (i = 0; i < count; i++) 206 grow(part1, (char*)ga(i), k + Pt3D(0, 0, -M_PI + (i + 1)*(2 * M_PI / (count + 1))), c, part1); 207 return; 208 } 209 case ' ': case '\t': case '\n': case '\r': break; 210 default: invalid = 1; return; 211 } 262 212 } 263 213 g++; … … 321 271 } 322 272 323 int Builder::growJoint(int part1, int part2, Pt3D &angle, F1Props &c, const char *g)273 int Builder::growJoint(int part1, int part2, Pt3D &angle, GeneProps &c, const char *g) 324 274 { 325 275 double len = min(2.0, c.length); … … 330 280 } 331 281 332 int Builder::growPart( F1Props &c, const char *g)282 int Builder::growPart(GeneProps &c, const char *g) 333 283 { 334 284 sprintf(tmp, "dn=%lg,fr=%lg,ing=%lg,as=%lg,vs=%g,vr=%g,vg=%g,vb=%g", … … 343 293 } 344 294 345 const char* Builder::growNeuro(const char* t, F1Props& props, int &hasmuscles)295 const char* Builder::growNeuro(const char* t, GeneProps& props, int &hasmuscles) 346 296 { 347 297 const char*neuroend = skipNeuro(t); … … 441 391 return t; 442 392 } 443 void Builder::growConnection(const char* begin, const char* colon, const char* end, F1Props& props)393 void Builder::growConnection(const char* begin, const char* colon, const char* end, GeneProps& props) 444 394 { 445 395 while (*begin && *begin <= ' ') begin++; -
cpp/frams/genetics/f1/conv_f1.h
r732 r759 8 8 #include <frams/genetics/genoconv.h> 9 9 #include <frams/model/model.h> 10 11 struct F1Props12 {13 double length, curvedness, weight, friction, muscle_power, assimilation, stamina, ingestion, twist, energy;14 double muscle_bend_range;15 bool muscle_reset_range;16 double visual_size, cred, cgreen, cblue;17 void normalizeBiol4();18 };19 10 20 11 /**
Note: See TracChangeset
for help on using the changeset viewer.