Changeset 1030 for cpp/frams/genetics/fS/fS_general.cpp
- Timestamp:
- 11/26/20 01:30:40 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/genetics/fS/fS_general.cpp
r1017 r1030 15 15 16 16 int fS_Genotype::precision = 4; 17 bool fS_Genotype::TURN_WITH_ROTATION = false;18 17 std::map<string, double> Node::minValues; 19 18 std::map<string, double> Node::defaultValues; … … 33 32 {RY, -M_PI}, 34 33 {RZ, -M_PI}, 35 {S IZE, 0.01},36 {S IZE_X, Model::getMinPart().scale.x},37 {S IZE_Y, Model::getMinPart().scale.y},38 {S IZE_Z, Model::getMinPart().scale.z}34 {SCALE, 0.01}, 35 {SCALE_X, Model::getMinPart().scale.x}, 36 {SCALE_Y, Model::getMinPart().scale.y}, 37 {SCALE_Z, Model::getMinPart().scale.z} 39 38 }; 40 39 } … … 51 50 {RY, M_PI}, 52 51 {RZ, M_PI}, 53 {S IZE, 100.0},54 {S IZE_X, Model::getMaxPart().scale.x},55 {S IZE_Y, Model::getMaxPart().scale.y},56 {S IZE_Z, Model::getMaxPart().scale.z}52 {SCALE, 100.0}, 53 {SCALE_X, Model::getMaxPart().scale.x}, 54 {SCALE_Y, Model::getMaxPart().scale.y}, 55 {SCALE_Z, Model::getMaxPart().scale.z} 57 56 }; 58 57 } … … 68 67 {RY, 0.0}, 69 68 {RZ, 0.0}, 70 {S IZE, 1.0},71 {S IZE_X, Model::getDefPart().scale.x},72 {S IZE_Y, Model::getDefPart().scale.y},73 {S IZE_Z, Model::getDefPart().scale.z}69 {SCALE, 1.0}, 70 {SCALE_X, Model::getDefPart().scale.x}, 71 {SCALE_Y, Model::getDefPart().scale.y}, 72 {SCALE_Z, Model::getDefPart().scale.z} 74 73 }; 75 74 } … … 111 110 } 112 111 112 void rotateVector(Pt3D &vector, const Pt3D &rotation) 113 { 114 Orient rotmatrix = Orient_1; 115 rotmatrix.rotate(rotation); 116 vector = rotmatrix.transform(vector); 117 } 118 113 119 void State::rotate(const Pt3D &rotation) 114 120 { 115 fS_Utils::rotateVector(v, rotation);121 rotateVector(v, rotation); 116 122 v.normalize(); 117 123 } … … 218 224 void Node::extractModifiers(Substring &restOfGenotype) 219 225 { 220 int part TypePosition = getPartPosition(restOfGenotype);221 if (part TypePosition == -1)226 int partShapePosition = getPartPosition(restOfGenotype); 227 if (partShapePosition == -1) 222 228 throw fS_Exception("Part type missing", restOfGenotype.start); 223 229 224 for (int i = 0; i < part TypePosition; i++)230 for (int i = 0; i < partShapePosition; i++) 225 231 { 226 232 // Extract modifiers and joint … … 233 239 throw fS_Exception("Invalid modifier", restOfGenotype.start + i); 234 240 } 235 restOfGenotype.startFrom(part TypePosition);241 restOfGenotype.startFrom(partShapePosition); 236 242 } 237 243 … … 242 248 throw fS_Exception("Invalid part type", restOfGenotype.start); 243 249 244 part Type = itr->second;250 partShape = itr->second; 245 251 restOfGenotype.startFrom(1); 246 252 } … … 327 333 size_t len = length - valueStartIndex; 328 334 double value = fS_stod(val, restOfGenotype.start + start + valueStartIndex, &len); 329 if((key==S IZE_X || key==SIZE_Y || key==SIZE_Z) && value <= 0.0)335 if((key==SCALE_X || key==SCALE_Y || key==SCALE_Z) && value <= 0.0) 330 336 throw fS_Exception("Invalid value of radius parameter", restOfGenotype.start + start + valueStartIndex); 331 337 … … 342 348 if (item != params.end()) 343 349 return item->second; 344 return defaultValues.at(key); 350 351 auto defaultItem = defaultValues.find(key); 352 if(defaultItem == defaultValues.end()) 353 throw fS_Exception("Default value missing", 0); 354 return defaultItem->second; 345 355 } 346 356 … … 382 392 state->rotate(getVectorRotation()); 383 393 384 double distance = getDistance();394 double distance = calculateDistanceFromParent(); 385 395 state->addVector(distance); 386 396 } … … 433 443 } 434 444 435 void Node::calculateS ize(Pt3D &scale)436 { 437 double s izeMultiplier = getParam(SIZE) * state->s;438 scale.x = getParam(S IZE_X) * sizeMultiplier;439 scale.y = getParam(S IZE_Y) * sizeMultiplier;440 scale.z = getParam(S IZE_Z) * sizeMultiplier;445 void Node::calculateScale(Pt3D &scale) 446 { 447 double scaleMultiplier = getParam(SCALE) * state->s; 448 scale.x = getParam(SCALE_X) * scaleMultiplier; 449 scale.y = getParam(SCALE_Y) * scaleMultiplier; 450 scale.z = getParam(SCALE_Z) * scaleMultiplier; 441 451 } 442 452 … … 444 454 { 445 455 double result; 446 Pt3D s ize;447 calculateS ize(size);448 double radiiProduct = s ize.x * size.y * size.z;449 switch (part Type)456 Pt3D scale; 457 calculateScale(scale); 458 double radiiProduct = scale.x * scale.y * scale.z; 459 switch (partShape) 450 460 { 451 461 case Part::Shape::SHAPE_CUBOID: … … 464 474 } 465 475 466 bool Node::isPartS izeValid()467 { 468 Pt3D s ize;469 calculateS ize(size);476 bool Node::isPartScaleValid() 477 { 478 Pt3D scale; 479 calculateScale(scale); 470 480 double volume = calculateVolume(); 471 481 Part_MinMaxDef minP = Model::getMinPart(); … … 474 484 if (volume > maxP.volume || minP.volume > volume) 475 485 return false; 476 if (s ize.x < minP.scale.x || size.y < minP.scale.y || size.z < minP.scale.z)486 if (scale.x < minP.scale.x || scale.y < minP.scale.y || scale.z < minP.scale.z) 477 487 return false; 478 if (s ize.x > maxP.scale.x || size.y > maxP.scale.y || size.z > maxP.scale.z)488 if (scale.x > maxP.scale.x || scale.y > maxP.scale.y || scale.z > maxP.scale.z) 479 489 return false; 480 490 481 if (part Type == Part::Shape::SHAPE_ELLIPSOID && fS_Utils::max3(size) != fS_Utils::min3(size))491 if (partShape == Part::Shape::SHAPE_ELLIPSOID && scale.maxComponentValue() != scale.minComponentValue()) 482 492 // When not all radii have different values 483 493 return false; 484 if (part Type == Part::Shape::SHAPE_CYLINDER && size.y != size.z)494 if (partShape == Part::Shape::SHAPE_CYLINDER && scale.y != scale.z) 485 495 // If base radii have different values 486 496 return false; … … 488 498 } 489 499 490 bool Node::hasPartSizeParam()491 {492 return params.count(SIZE_X) > 0 || params.count(SIZE_Y) > 0 || params.count(SIZE_Z) > 0;493 }494 495 500 Pt3D Node::getVectorRotation() 496 501 { … … 501 506 { 502 507 Pt3D rotation = Pt3D(getParam(RX, 0.0), getParam(RY, 0.0), getParam(RZ, 0.0)); 503 if( fS_Genotype::TURN_WITH_ROTATION)508 if(genotypeParams.turnWithRotation) 504 509 rotation += getVectorRotation(); 505 510 return rotation; … … 537 542 void Node::createPart() 538 543 { 539 part = new Part(part Type);544 part = new Part(partShape); 540 545 part->p = Pt3D(state->location); 541 546 542 547 part->friction = getParam(FRICTION) * state->fr; 543 548 part->ingest = getParam(INGESTION) * state->ing; 544 calculateS ize(part->scale);549 calculateScale(part->scale); 545 550 part->setRot(getRotation()); 546 551 } … … 581 586 result += std::string(count, mod).c_str(); 582 587 } 583 result += SHAPE_TO_GENE.at(part Type);588 result += SHAPE_TO_GENE.at(partShape); 584 589 585 590 if (!neurons.empty()) … … 658 663 } 659 664 660 fS_Genotype::fS_Genotype(const string &geno) 661 { 665 fS_Genotype::fS_Genotype(const string &g) 666 { 667 string geno(g); 668 geno.erase(remove(geno.begin(), geno.end(), ' '), geno.end()); 669 geno.erase(remove(geno.begin(), geno.end(), '\n'), geno.end()); 662 670 try 663 671 { 664 672 GenotypeParams genotypeParams; 665 673 genotypeParams.modifierMultiplier = 1.1; 674 genotypeParams.distanceTolerance = 0.1; 675 genotypeParams.relativeDensity = 10.0; 676 genotypeParams.turnWithRotation = false; 677 genotypeParams.paramMutationStrength = 0.4; 666 678 667 679 size_t modeSeparatorIndex = geno.find(MODE_SEPARATOR); … … 669 681 throw fS_Exception("Genotype parameters missing", 0); 670 682 671 genotypeParams.modifierMultiplier = fS_stod(geno, 0, &modeSeparatorIndex); 683 std::vector<SString> paramStrings; 684 strSplit(SString(geno.c_str(), modeSeparatorIndex), ',', false, paramStrings); 685 686 if(paramStrings.size() >= 1 && paramStrings[0] != "") 687 { 688 size_t len0 = paramStrings[0].length(); 689 genotypeParams.modifierMultiplier = fS_stod(paramStrings[0].c_str(), 0, &len0); 690 } 691 if(paramStrings.size() >= 2 && paramStrings[1] != "") 692 { 693 genotypeParams.turnWithRotation = bool(atoi(paramStrings[1].c_str())); 694 } 695 if(paramStrings.size() >= 3 && paramStrings[2] != "") 696 { 697 size_t len2 = paramStrings[2].length(); 698 genotypeParams.paramMutationStrength = fS_stod(paramStrings[2].c_str(), 0, &len2); 699 } 672 700 673 701 int genoStart = modeSeparatorIndex + 1; … … 680 708 delete startNode; 681 709 throw e; 710 } 711 catch(...) 712 { 713 delete startNode; 714 throw fS_Exception("Unknown exception in fS", 0); 682 715 } 683 716 } … … 753 786 geno.reserve(100); 754 787 755 geno += doubleToString(startNode->genotypeParams.modifierMultiplier, fS_Genotype::precision).c_str(); 788 GenotypeParams gp = startNode->genotypeParams; 789 geno += doubleToString(gp.modifierMultiplier, precision).c_str(); 790 geno += ","; 791 geno += doubleToString(gp.turnWithRotation, precision).c_str(); 792 geno += ","; 793 geno += doubleToString(gp.paramMutationStrength, precision).c_str(); 756 794 geno += MODE_SEPARATOR; 757 795 … … 850 888 for (int i = 0; i < int(nodes.size()); i++) 851 889 { 852 if (!nodes[i]->isPartS izeValid())890 if (!nodes[i]->isPartScaleValid()) 853 891 { 854 892 return 1 + nodes[i]->partDescription->start; … … 884 922 } 885 923 924 double Node::calculateDistanceFromParent() 925 { 926 Pt3D scale; 927 calculateScale(scale); 928 Pt3D parentScale; 929 parent->calculateScale(parentScale); // Here we are sure that parent is not nullptr 930 Part *tmpPart = PartDistanceEstimator::buildTemporaryPart(partShape, scale, getRotation()); 931 Part *parentTmpPart = PartDistanceEstimator::buildTemporaryPart(parent->partShape, parentScale, parent->getRotation()); 932 933 double result; 934 try 935 { 936 tmpPart->p = state->v; 937 result = PartDistanceEstimator::calculateDistance(*tmpPart, *parentTmpPart, genotypeParams.distanceTolerance, genotypeParams.relativeDensity); 938 } 939 catch (...) 940 { 941 throw fS_Exception("Exception thrown while calculating distance from parent", 0); 942 } 943 944 delete tmpPart; 945 delete parentTmpPart; 946 return result; 947 }
Note: See TracChangeset
for help on using the changeset viewer.