Changeset 969 for cpp/frams/genetics/fS/fS_general.cpp
- Timestamp:
- 06/30/20 00:32:56 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/genetics/fS/fS_general.cpp
r958 r969 10 10 #include "frams/util/rndutil.h" 11 11 #include "frams/neuro/neurolibrary.h" 12 12 #include "../genooperators.h" 13 13 14 14 int fS_Genotype::precision = 4; 15 16 17 double round2(double var) 18 { 19 double value = (int) (var * 100 + .5); 20 return (double) value / 100; 21 } 15 bool fS_Genotype::TURN_WITH_ROTATION = false; 16 22 17 23 18 double fS_stod(const string& str, int start, size_t* size) … … 30 25 { 31 26 throw fS_Exception("Invalid numeric value", start); 27 } 28 catch(const std::out_of_range& ex) 29 { 30 throw fS_Exception("Invalid numeric value; out of range", start); 32 31 } 33 32 } … … 39 38 fr = _state->fr; 40 39 s = _state->s; 40 stif = _state->stif; 41 41 } 42 42 … … 55 55 { 56 56 Orient rotmatrix = Orient_1; 57 rotmatrix.rotate(Pt3D( 58 Convert::toRadians(rotation.x), 59 Convert::toRadians(rotation.y), 60 Convert::toRadians(rotation.z) 61 )); 57 rotmatrix.rotate(rotation); 62 58 vector = rotmatrix.transform(vector); 63 59 } … … 115 111 } 116 112 117 Node::Node(Substring &restOfGeno, bool _modifierMode, bool _paramMode, bool _cycleMode,Node *_parent)113 Node::Node(Substring &restOfGeno, Node *_parent) 118 114 { 119 115 parent = _parent; 120 modifierMode = _modifierMode;121 paramMode = _paramMode;122 cycleMode = _cycleMode;123 116 partDescription = new Substring(restOfGeno); 124 117 … … 293 286 return item->second; 294 287 else 295 return default ParamValues.at(key);288 return defaultValues.at(key); 296 289 } 297 290 … … 415 408 } 416 409 417 double getDistance(Pt3D radiiParent, Pt3D radii, Pt3D vector, Pt3D rotationParent, Pt3D rotation) 418 { 410 double Node::getDistance() 411 { 412 Pt3D size = calculateSize(); 413 Pt3D parentSize = parent->calculateSize(); // Here we are sure that parent is not nullptr 419 414 int parentSphereCount, sphereCount; 420 415 double parentSphereRadius, sphereRadius; 421 Pt3D *centersParent = findSphereCenters(parentSphereCount, parentSphereRadius, radiiParent, rotationParent);422 Pt3D *centers = findSphereCenters(sphereCount, sphereRadius, radii, rotation);416 Pt3D *centersParent = findSphereCenters(parentSphereCount, parentSphereRadius, parentSize, parent->getRotation()); 417 Pt3D *centers = findSphereCenters(sphereCount, sphereRadius, size, getRotation()); 423 418 424 419 double distanceThreshold = sphereRadius + parentSphereRadius; 425 420 double minDistance = 0.0; 426 double maxDistance = 2 * (max3( radiiParent) + max3(radii));421 double maxDistance = 2 * (max3(parentSize) + max3(size)); 427 422 double currentDistance = avg(maxDistance, minDistance); 428 423 int result = -1; 424 int iterationNo = 0; 429 425 while (result != ADJACENT) 430 426 { 431 Pt3D currentVector = vector * currentDistance; 427 iterationNo++; 428 Pt3D currentVector = state->v * currentDistance; 432 429 result = isCollision(centersParent, centers, parentSphereCount, sphereCount, currentVector, distanceThreshold); 430 433 431 if (result == DISJOINT) 434 432 { … … 440 438 currentDistance = avg(maxDistance, currentDistance); 441 439 } 440 441 if(maxDistance <= 0 || iterationNo > 1000) 442 throw fS_Exception("Computing of distances between parts failed", 0); 442 443 if (currentDistance > maxDistance) 444 { 443 445 throw fS_Exception("Internal error; then maximal distance between parts exceeded.", 0); 446 } 444 447 if (currentDistance < minDistance) 445 448 throw fS_Exception("Internal error; the minimal distance between parts exceeded.", 0); … … 449 452 delete[] centersParent; 450 453 delete[] centers; 451 return round2(currentDistance);452 } 453 454 void Node::getState(State *_state , const Pt3D &parentSize)454 return currentDistance; 455 } 456 457 void Node::getState(State *_state) 455 458 { 456 459 if (state != nullptr) … … 473 476 else if (mod == MODIFIERS[2]) 474 477 state->s *= multiplier; 475 } 476 477 Pt3D size = calculateSize(); 478 else if (mod == MODIFIERS[3]) 479 state->stif *= multiplier; 480 } 481 478 482 if (parent != nullptr) 479 483 { … … 481 485 state->rotate(getVectorRotation()); 482 486 483 double distance = getDistance( parentSize, size, state->v, getRotation(), getRotation());487 double distance = getDistance(); 484 488 state->addVector(distance); 485 489 } 486 490 for (int i = 0; i < int(children.size()); i++) 487 children[i]->getState(state , size);491 children[i]->getState(state); 488 492 } 489 493 … … 493 497 for (int i = 0; i < int(branches.size()); i++) 494 498 { 495 children.push_back(new Node(branches[i], modifierMode, paramMode, cycleMode,this));499 children.push_back(new Node(branches[i], this)); 496 500 } 497 501 } … … 593 597 Pt3D Node::getVectorRotation() 594 598 { 595 double rx = getParam(ROT_X); 596 double ry = getParam(ROT_Y); 597 double rz = getParam(ROT_Z); 598 return Pt3D(rx, ry, rz); 599 return Pt3D(getParam(ROT_X), getParam(ROT_Y), getParam(ROT_Z)); 599 600 } 600 601 601 602 Pt3D Node::getRotation() 602 603 { 603 double rx = getParam(RX);604 double ry = getParam(RY);605 double rz = getParam(RZ);606 return Pt3D(rx, ry, rz);604 Pt3D rotation = Pt3D(getParam(RX), getParam(RY), getParam(RZ)); 605 if(fS_Genotype::TURN_WITH_ROTATION) 606 rotation += getVectorRotation(); 607 return rotation; 607 608 } 608 609 … … 614 615 addJointsToModel(model, parent); 615 616 616 617 617 for (int i = 0; i < int(neurons.size()); i++) 618 618 { … … 639 639 { 640 640 part = new Part(partType); 641 part->p = Pt3D( round2(state->location.x),642 round2(state->location.y),643 round2(state->location.z));644 645 part->friction = round2(getParam(FRICTION) * state->fr);646 part->ingest = round2(getParam(INGESTION) * state->ing);641 part->p = Pt3D(state->location.x, 642 state->location.y, 643 state->location.z); 644 645 part->friction = getParam(FRICTION) * state->fr; 646 part->ingest = getParam(INGESTION) * state->ing; 647 647 Pt3D size = calculateSize(); 648 part->scale.x = round2(size.x);649 part->scale.y = round2(size.y);650 part->scale.z = round2(size.z);648 part->scale.x = size.x; 649 part->scale.y = size.y; 650 part->scale.z = size.z; 651 651 part->setRot(getRotation()); 652 652 } … … 655 655 { 656 656 Joint *j = new Joint(); 657 j->stif = getParam(STIFFNESS) * state->stif; 658 j->rotstif = j->stif; 659 657 660 j->attachToParts(parent->part, part); 658 661 switch (joint) … … 698 701 if (i != 0) 699 702 result += NEURON_SEPARATOR; 700 if (n->getClassName() != "N") 701 { 702 result += n->getDetails(); 703 if (!n->inputs.empty()) 704 result += NEURON_INTERNAL_SEPARATOR; 705 } 703 704 result += n->getDetails(); 705 if (!n->inputs.empty()) 706 result += NEURON_INTERNAL_SEPARATOR; 707 706 708 for (auto it = n->inputs.begin(); it != n->inputs.end(); ++it) 707 709 { … … 714 716 result += SString::valueOf(it->second); 715 717 } 716 717 718 } 718 719 } … … 754 755 755 756 756 bool Node::changeSizeParam(string paramKey, double multiplier, bool ensureCircleSection)757 { 758 double oldValue = getParam( paramKey);759 params[ paramKey] = oldValue * multiplier;757 bool Node::changeSizeParam(string key, bool ensureCircleSection) 758 { 759 double oldValue = getParam(key); 760 params[key] = GenoOperators::mutateCreep('f', params[key], minValues.at(key), maxValues.at(key), true); 760 761 if (!ensureCircleSection || isPartSizeValid()) 761 762 return true; 762 763 else 763 764 { 764 params[ paramKey] = oldValue;765 params[key] = oldValue; 765 766 return false; 766 767 } … … 786 787 { 787 788 string geno = genotype.c_str(); 788 // M - modifier mode, S - standard mode 789 size_t modeSeparatorIndex = geno.find(':'); 790 if (modeSeparatorIndex == string::npos) 791 throw fS_Exception("No mode separator", 0); 792 793 string modeStr = geno.substr(0, modeSeparatorIndex).c_str(); 794 bool modifierMode = modeStr.find(MODIFIER_MODE) != string::npos; 795 bool paramMode = modeStr.find(PARAM_MODE) != string::npos; 796 bool cycleMode = modeStr.find(CYCLE_MODE) != string::npos; 797 798 int actualGenoStart = modeSeparatorIndex + 1; 799 Substring substring(geno.c_str(), actualGenoStart, geno.length() - actualGenoStart); 800 startNode = new Node(substring, modifierMode, paramMode, cycleMode, nullptr); 789 Substring substring(geno.c_str(), 0, geno.length()); 790 startNode = new Node(substring, nullptr); 801 791 validateNeuroInputs(); 802 792 } … … 816 806 { 817 807 State *initialState = new State(Pt3D(0), Pt3D(1, 0, 0)); 818 startNode->getState(initialState, Pt3D(1.0)); 819 } 820 821 double fS_Genotype::randomParamMultiplier() 822 { 823 double multiplier = 1 + fabs(RndGen.GaussStd()); 824 if (multiplier > PARAM_MAX_MULTIPLIER) 825 multiplier = PARAM_MAX_MULTIPLIER; 826 if (rndUint(2) == 0) 827 multiplier = 1.0 / multiplier; 828 return multiplier; 808 startNode->getState(initialState); 829 809 } 830 810 … … 833 813 getState(); 834 814 startNode->buildModel(model, nullptr); 835 836 815 buildNeuroConnections(model); 837 838 // Additional joints839 vector<Node*> allNodes = getAllNodes();840 for (int i = 0; i < int(allNodes.size()); i++)841 {842 Node *node = allNodes[i];843 if (node->params.find(JOINT_DISTANCE) != node->params.end())844 {845 Node *otherNode = getNearestNode(allNodes, node);846 if (otherNode != nullptr)847 {848 // If other node is close enough, add a joint849 double distance = node->state->location.distanceTo(otherNode->state->location);850 if (distance < node->params[JOINT_DISTANCE])851 {852 Joint *joint = new Joint();853 joint->attachToParts(node->part, otherNode->part);854 855 joint->shape = Joint::Shape::SHAPE_FIXED;856 model.addJoint(joint);857 }858 }859 }860 }861 816 } 862 817 … … 905 860 SString geno; 906 861 geno.memoryHint(100); // Provide a small buffer from the start to improve performance 907 908 if (startNode->modifierMode)909 geno += MODIFIER_MODE;910 if (startNode->paramMode)911 geno += PARAM_MODE;912 if (startNode->cycleMode)913 geno += CYCLE_MODE;914 915 geno += ':';916 862 startNode->getGeno(geno); 917 863 return geno; … … 1010 956 if (!nodes[i]->isPartSizeValid()) 1011 957 { 1012 return nodes[i]->partDescription->start;958 return 1 + nodes[i]->partDescription->start; 1013 959 } 1014 960 }
Note: See TracChangeset
for help on using the changeset viewer.