Changeset 1006 for cpp/frams/genetics/fS
- Timestamp:
- 07/14/20 16:04:46 (4 years ago)
- Location:
- cpp/frams/genetics/fS
- Files:
-
- 1 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/genetics/fS/fS_conv.cpp
r958 r1006 18 18 } 19 19 20 Model model; 21 model.open(using_checkpoints); 22 genotype->buildModel(model); 23 model.close(); 20 Model model = genotype->buildModel(using_checkpoints); 24 21 delete genotype; 25 22 -
cpp/frams/genetics/fS/fS_conv.h
r1000 r1006 10 10 11 11 /** 12 * Genotype converter from fS to f0 .12 * Genotype converter from fS to f0s. 13 13 */ 14 14 class GenoConv_fS0 : public GenoConverter -
cpp/frams/genetics/fS/fS_general.cpp
r1000 r1006 17 17 bool fS_Genotype::TURN_WITH_ROTATION = false; 18 18 19 const std::map<string, double> defaultValues = { 20 {INGESTION, 0.5},//Model::getDefPart().ingest }, 21 {FRICTION, 0.5},//Model::getDefPart().friction}, 22 {STIFFNESS, 0.5},//Model::getDefJoint().stif}, 23 {ROT_X, 0.0}, 24 {ROT_Y, 0.0}, 25 {ROT_Z, 0.0}, 26 {RX, 0.0}, 27 {RY, 0.0}, 28 {RZ, 0.0}, 29 {SIZE, 1.0}, 30 {SIZE_X, 0.5},//Model::getDefPart().scale.x}, 31 {SIZE_Y, 0.5},//Model::getDefPart().scale.y}, 32 {SIZE_Z, 0.5}//Model::getDefPart().scale.z} 33 }; 19 void Node::prepareParams() 20 { 21 defaultValues = { 22 {INGESTION, Model::getDefPart().ingest}, 23 {FRICTION, Model::getDefPart().friction}, 24 {STIFFNESS, Model::getDefJoint().stif}, 25 {ROT_X, 0.0}, 26 {ROT_Y, 0.0}, 27 {ROT_Z, 0.0}, 28 {RX, 0.0}, 29 {RY, 0.0}, 30 {RZ, 0.0}, 31 {SIZE, 1.0}, 32 {SIZE_X, Model::getDefPart().scale.x}, 33 {SIZE_Y, Model::getDefPart().scale.y}, 34 {SIZE_Z, Model::getDefPart().scale.z} 35 }; 36 } 34 37 35 38 double fS_stod(const string& str, int start, size_t* size) … … 76 79 77 80 78 fS_Neuron::fS_Neuron(const char *str, int start, int length) 79 { 81 fS_Neuron::fS_Neuron(const char *str, int _start, int length) 82 { 83 start = _start + 1; 84 end = start + length; 80 85 if (length == 0) 81 86 return; … … 123 128 Node::Node(Substring &restOfGeno, Node *_parent, GenotypeParams _genotypeParams) 124 129 { 130 prepareParams(); 125 131 partDescription = new Substring(restOfGeno); 126 132 genotypeParams = _genotypeParams; … … 165 171 for (int i = 0; i < restOfGenotype.len; i++) 166 172 { 167 if (GENE_TO_SHAPE TYPE.find(restOfGenotype.at(i)) != GENE_TO_SHAPETYPE.end())173 if (GENE_TO_SHAPE.find(restOfGenotype.at(i)) != GENE_TO_SHAPE.end()) 168 174 return i; 169 175 } … … 193 199 void Node::extractPartType(Substring &restOfGenotype) 194 200 { 195 auto itr = GENE_TO_SHAPE TYPE.find(restOfGenotype.at(0));196 if (itr == GENE_TO_SHAPE TYPE.end())201 auto itr = GENE_TO_SHAPE.find(restOfGenotype.at(0)); 202 if (itr == GENE_TO_SHAPE.end()) 197 203 throw fS_Exception("Invalid part type", restOfGenotype.start); 198 204 … … 465 471 Neuro *neuro = new Neuro(*neurons[i]); 466 472 model.addNeuro(neuro); 473 neuro->addMapping(MultiRange(IRange(neurons[i]->start, neurons[i]->end))); 467 474 if (neuro->getClass()->preflocation == NeuroClass::PREFER_JOINT && parent != nullptr) 468 475 { … … 536 543 result += std::string(count, mod).c_str(); 537 544 } 538 result += SHAPE TYPE_TO_GENE.at(partType);545 result += SHAPE_TO_GENE.at(partType); 539 546 540 547 if (!neurons.empty()) … … 649 656 } 650 657 651 void fS_Genotype::buildModel(Model &model) 652 { 658 Model fS_Genotype::buildModel(bool using_checkpoints) 659 { 660 661 Model model; 662 model.open(using_checkpoints); 663 653 664 getState(); 654 665 startNode->buildModel(model, nullptr); 655 666 buildNeuroConnections(model); 667 668 model.close(); 669 return model; 656 670 } 657 671 -
cpp/frams/genetics/fS/fS_general.h
r1000 r1006 27 27 const char NEURON_END = ']'; 28 28 const char NEURON_SEPARATOR = ';'; 29 const SString NEURON_INTERNAL_SEPARATOR(" _");29 const SString NEURON_INTERNAL_SEPARATOR("'"); 30 30 #define NEURON_I_W_SEPARATOR ':' 31 31 //@} … … 69 69 const char CUBOID = 'C'; 70 70 const char CYLINDER = 'R'; 71 const std::unordered_map<Part::Shape, char> SHAPE TYPE_TO_GENE = {71 const std::unordered_map<Part::Shape, char> SHAPE_TO_GENE = { 72 72 {Part::Shape::SHAPE_ELLIPSOID, ELLIPSOID}, 73 73 {Part::Shape::SHAPE_CUBOID, CUBOID}, … … 76 76 77 77 // This map is inverse to SHAPE_TO_SYMBOL. Those two should be compatible 78 const std::unordered_map<char, Part::Shape> GENE_TO_SHAPE TYPE= {78 const std::unordered_map<char, Part::Shape> GENE_TO_SHAPE = { 79 79 {ELLIPSOID, Part::Shape::SHAPE_ELLIPSOID}, 80 80 {CUBOID, Part::Shape::SHAPE_CUBOID}, 81 81 {CYLINDER, Part::Shape::SHAPE_CYLINDER}, 82 82 }; 83 const int SHAPE_COUNT = 3; // This should be the count of SHAPE TYPE_TO_GENE and GENE_TO_SHAPETYPE83 const int SHAPE_COUNT = 3; // This should be the count of SHAPE_TO_GENE and GENE_TO_SHAPE 84 84 85 85 const char DEFAULT_JOINT = 'a'; … … 100 100 }; 101 101 102 extern const std::map<string, double> defaultValues;103 104 102 /** @name Number of tries of performing a mutation before GENOPER_FAIL is returned */ 105 103 #define mutationTries 20 … … 198 196 MultiRange toMultiRange() 199 197 { 200 MultiRange range; 201 range.add(start, start + len - 1); 202 return range; 198 int end = start + len - 1; 199 return MultiRange(IRange(start, end)); 203 200 } 204 201 }; … … 244 241 { 245 242 public: 243 int start, end; 246 244 std::map<int, double> inputs; 247 245 … … 274 272 Part *part; /// A part object built from node. Used in building the Model 275 273 int partCodeLen; /// The length of substring that directly describes the corresponding part 274 std::map<string, double> defaultValues; 276 275 GenotypeParams genotypeParams; 277 278 276 279 277 vector<Node *> children; /// Vector of all direct children 280 278 std::map<char, int> modifiers; /// Vector of all modifiers 281 279 vector<fS_Neuron *> neurons; /// Vector of all the neurons 280 281 void prepareParams(); 282 282 283 283 double getDistance(); … … 398 398 399 399 /** 400 * Change the value of the size parameter by given multiplier401 * Do not change the value if any of the size restrictions is not satisfied402 * @param paramKey403 * @param multiplier404 * @param ensureCircleSection405 * @return True if the parameter value was change, false otherwise406 */407 bool mutateSizeParam(string paramKey, bool ensureCircleSection);408 409 /**410 400 * Counts all the nodes in subtree 411 401 * @return node count … … 521 511 * @param a reference to a model that will contain a built model 522 512 */ 523 void buildModel(Model &model);513 Model buildModel(bool using_checkpoints); 524 514 525 515 /** -
cpp/frams/genetics/fS/fS_oper.cpp
r1000 r1006 36 36 37 37 38 39 const std::map<string, double> minValues = { 40 {INGESTION, 0},//Model::getMinPart().ingest}, 41 {FRICTION, 0},//Model::getMinPart().friction}, 42 {STIFFNESS, 0.1}, 43 {ROT_X, -M_PI}, 44 {ROT_Y, -M_PI}, 45 {ROT_Z, -M_PI}, 46 {RX, -M_PI}, 47 {RY, -M_PI}, 48 {RZ, -M_PI}, 49 {SIZE, 0.01}, 50 {SIZE_X, 0},//Model::getMinPart().scale.x}, 51 {SIZE_Y, 0},//Model::getMinPart().scale.y}, 52 {SIZE_Z, 0}//Model::getMinPart().scale.z} 53 }; 54 55 const std::map<string, double> maxValues = { 56 {INGESTION, 1},//Model::getMaxPart().ingest}, 57 {FRICTION, 1},//Model::getMaxPart().friction}, 58 {STIFFNESS, 0.5}, 59 {ROT_X, M_PI}, 60 {ROT_Y, M_PI}, 61 {ROT_Z, M_PI}, 62 {RX, M_PI}, 63 {RY, M_PI}, 64 {RZ, M_PI}, 65 {SIZE, 100.0}, 66 {SIZE_X, 1},//Model::getMaxPart().scale.x}, 67 {SIZE_Y, 1},//Model::getMaxPart().scale.y}, 68 {SIZE_Z, 1}//Model::getMaxPart().scale.z} 69 }; 70 38 void GenoOper_fS::prepareParams() 39 { 40 minValues = { 41 {INGESTION, Model::getMinPart().ingest}, 42 {FRICTION, Model::getMinPart().friction}, 43 {STIFFNESS, 0.1}, 44 {ROT_X, -M_PI}, 45 {ROT_Y, -M_PI}, 46 {ROT_Z, -M_PI}, 47 {RX, -M_PI}, 48 {RY, -M_PI}, 49 {RZ, -M_PI}, 50 {SIZE, 0.01}, 51 {SIZE_X, Model::getMinPart().scale.x}, 52 {SIZE_Y, Model::getMinPart().scale.y}, 53 {SIZE_Z, Model::getMinPart().scale.z} 54 }; 55 56 maxValues = { 57 {INGESTION, Model::getMaxPart().ingest}, 58 {FRICTION, Model::getMaxPart().friction}, 59 {STIFFNESS, 0.5}, 60 {ROT_X, M_PI}, 61 {ROT_Y, M_PI}, 62 {ROT_Z, M_PI}, 63 {RX, M_PI}, 64 {RY, M_PI}, 65 {RZ, M_PI}, 66 {SIZE, 100.0}, 67 {SIZE_X, Model::getMaxPart().scale.x}, 68 {SIZE_Y, Model::getMaxPart().scale.y}, 69 {SIZE_Z, Model::getMaxPart().scale.z} 70 }; 71 } 71 72 72 73 GenoOper_fS::GenoOper_fS() 73 74 { 75 prepareParams(); 74 76 par.setParamTab(genooper_fS_paramtab); 75 77 par.select(this); … … 332 334 geno.getState(); 333 335 Node *node = geno.chooseNode(); 334 char partType = SHAPE TYPE_TO_GENE.at(availablePartShapes[rndUint(availablePartShapes.size())]);336 char partType = SHAPE_TO_GENE.at(availablePartShapes[rndUint(availablePartShapes.size())]); 335 337 336 338 Substring substring(&partType, 0, 1); … … 374 376 { 375 377 geno.getState(); 376 newNode->mutateSizeParam(SIZE_X, true);377 newNode->mutateSizeParam(SIZE_Y, true);378 newNode->mutateSizeParam(SIZE_Z, true);378 mutateSizeParam(newNode, SIZE_X, true); 379 mutateSizeParam(newNode, SIZE_Y, true); 380 mutateSizeParam(newNode, SIZE_Z, true); 379 381 } 380 382 return true; … … 483 485 bool isRadius = isRadiusOfBase || key == SIZE_X; 484 486 if (ensureCircleSection && isRadius) 487 if (ensureCircleSection && isRadius) 485 488 { 486 489 if (randomNode->partType == Part::Shape::SHAPE_ELLIPSOID) … … 490 493 } 491 494 // Add modified default value for param 492 randomNode->params[key] = mutateCreep('f', defaultValues.at(key), minValues.at(key), maxValues.at(key), true);495 randomNode->params[key] = mutateCreep('f', randomNode->defaultValues.at(key), minValues.at(key), maxValues.at(key), true); 493 496 return true; 494 497 } … … 530 533 return true; 531 534 } else 532 return randomNode->mutateSizeParam(it->first, ensureCircleSection);535 return mutateSizeParam(randomNode, it->first, ensureCircleSection); 533 536 } 534 537 } … … 696 699 } 697 700 698 bool Node::mutateSizeParam(string key, bool ensureCircleSection)699 { 700 double oldValue = getParam(key);701 double volume = calculateVolume();701 bool GenoOper_fS::mutateSizeParam(Node *node, string key, bool ensureCircleSection) 702 { 703 double oldValue = node->getParam(key); 704 double volume = node->calculateVolume(); 702 705 double valueAtMinVolume, valueAtMaxVolume; 703 706 if(key == SIZE) … … 715 718 double max = std::min(maxValues.at(key), valueAtMaxVolume); 716 719 717 params[key] = GenoOperators::mutateCreep('f',getParam(key), min, max, true);718 719 if (!ensureCircleSection || isPartSizeValid())720 node->params[key] = GenoOperators::mutateCreep('f', node->getParam(key), min, max, true); 721 722 if (!ensureCircleSection || node->isPartSizeValid()) 720 723 return true; 721 724 else 722 725 { 723 params[key] = oldValue;724 return false; 725 } 726 } 726 node->params[key] = oldValue; 727 return false; 728 } 729 } -
cpp/frams/genetics/fS/fS_oper.h
r1000 r1006 32 32 const int PARENT_COUNT = 2; 33 33 34 35 34 36 class GenoOper_fS : public GenoOperators 35 37 { … … 40 42 paInt useElli, useCub, useCyl; 41 43 paInt strongAddPart; 44 45 std::map<string, double> minValues; 46 std::map<string, double> maxValues; 42 47 43 48 GenoOper_fS(); … … 129 134 130 135 bool changeNeuroParam(fS_Genotype &geno); 136 137 /** 138 * Change the value of the size parameter by given multiplier 139 * Do not change the value if any of the size restrictions is not satisfied 140 * @param paramKey 141 * @param multiplier 142 * @param ensureCircleSection 143 * @return True if the parameter value was change, false otherwise 144 */ 145 bool mutateSizeParam(Node *node, string key, bool ensureCircleSection); 146 147 void prepareParams(); 131 148 }; 132 149
Note: See TracChangeset
for help on using the changeset viewer.