Ignore:
Timestamp:
11/26/20 01:30:40 (3 years ago)
Author:
Maciej Komosinski
Message:

fS: refactoring

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/genetics/fS/fS_general.cpp

    r1017 r1030  
    1515
    1616int fS_Genotype::precision = 4;
    17 bool fS_Genotype::TURN_WITH_ROTATION = false;
    1817std::map<string, double> Node::minValues;
    1918std::map<string, double> Node::defaultValues;
     
    3332                                {RY,        -M_PI},
    3433                                {RZ,        -M_PI},
    35                                 {SIZE,      0.01},
    36                                 {SIZE_X,    Model::getMinPart().scale.x},
    37                                 {SIZE_Y,    Model::getMinPart().scale.y},
    38                                 {SIZE_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}
    3938                };
    4039        }
     
    5150                                {RY,        M_PI},
    5251                                {RZ,        M_PI},
    53                                 {SIZE,      100.0},
    54                                 {SIZE_X,    Model::getMaxPart().scale.x},
    55                                 {SIZE_Y,    Model::getMaxPart().scale.y},
    56                                 {SIZE_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}
    5756                };
    5857        }
     
    6867                                {RY,        0.0},
    6968                                {RZ,        0.0},
    70                                 {SIZE,      1.0},
    71                                 {SIZE_X,    Model::getDefPart().scale.x},
    72                                 {SIZE_Y,    Model::getDefPart().scale.y},
    73                                 {SIZE_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}
    7473                };
    7574        }
     
    111110}
    112111
     112void rotateVector(Pt3D &vector, const Pt3D &rotation)
     113{
     114        Orient rotmatrix = Orient_1;
     115        rotmatrix.rotate(rotation);
     116        vector = rotmatrix.transform(vector);
     117}
     118
    113119void State::rotate(const Pt3D &rotation)
    114120{
    115        fS_Utils::rotateVector(v, rotation);
     121       rotateVector(v, rotation);
    116122       v.normalize();
    117123}
     
    218224void Node::extractModifiers(Substring &restOfGenotype)
    219225{
    220         int partTypePosition = getPartPosition(restOfGenotype);
    221         if (partTypePosition == -1)
     226        int partShapePosition = getPartPosition(restOfGenotype);
     227        if (partShapePosition == -1)
    222228                throw fS_Exception("Part type missing", restOfGenotype.start);
    223229
    224         for (int i = 0; i < partTypePosition; i++)
     230        for (int i = 0; i < partShapePosition; i++)
    225231        {
    226232                // Extract modifiers and joint
     
    233239                        throw fS_Exception("Invalid modifier", restOfGenotype.start + i);
    234240        }
    235         restOfGenotype.startFrom(partTypePosition);
     241        restOfGenotype.startFrom(partShapePosition);
    236242}
    237243
     
    242248                throw fS_Exception("Invalid part type", restOfGenotype.start);
    243249
    244         partType = itr->second;
     250        partShape = itr->second;
    245251        restOfGenotype.startFrom(1);
    246252}
     
    327333                size_t len = length - valueStartIndex;
    328334                double value = fS_stod(val, restOfGenotype.start + start + valueStartIndex, &len);
    329                 if((key==SIZE_X || key==SIZE_Y || key==SIZE_Z) && value <= 0.0)
     335                if((key==SCALE_X || key==SCALE_Y || key==SCALE_Z) && value <= 0.0)
    330336                        throw fS_Exception("Invalid value of radius parameter", restOfGenotype.start + start + valueStartIndex);
    331337
     
    342348        if (item != params.end())
    343349                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;
    345355}
    346356
     
    382392                state->rotate(getVectorRotation());
    383393
    384                 double distance = getDistance();
     394                double distance = calculateDistanceFromParent();
    385395                state->addVector(distance);
    386396        }
     
    433443}
    434444
    435 void Node::calculateSize(Pt3D &scale)
    436 {
    437         double sizeMultiplier = getParam(SIZE) * state->s;
    438         scale.x = getParam(SIZE_X) * sizeMultiplier;
    439         scale.y = getParam(SIZE_Y) * sizeMultiplier;
    440         scale.z = getParam(SIZE_Z) * sizeMultiplier;
     445void 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;
    441451}
    442452
     
    444454{
    445455        double result;
    446         Pt3D size;
    447         calculateSize(size);
    448         double radiiProduct = size.x * size.y * size.z;
    449         switch (partType)
     456        Pt3D scale;
     457        calculateScale(scale);
     458        double radiiProduct = scale.x * scale.y * scale.z;
     459        switch (partShape)
    450460        {
    451461                case Part::Shape::SHAPE_CUBOID:
     
    464474}
    465475
    466 bool Node::isPartSizeValid()
    467 {
    468         Pt3D size;
    469         calculateSize(size);
     476bool Node::isPartScaleValid()
     477{
     478        Pt3D scale;
     479        calculateScale(scale);
    470480        double volume = calculateVolume();
    471481        Part_MinMaxDef minP = Model::getMinPart();
     
    474484        if (volume > maxP.volume || minP.volume > volume)
    475485                return false;
    476         if (size.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)
    477487                return false;
    478         if (size.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)
    479489                return false;
    480490
    481         if (partType == Part::Shape::SHAPE_ELLIPSOID && fS_Utils::max3(size) != fS_Utils::min3(size))
     491        if (partShape == Part::Shape::SHAPE_ELLIPSOID && scale.maxComponentValue() != scale.minComponentValue())
    482492                // When not all radii have different values
    483493                return false;
    484         if (partType == Part::Shape::SHAPE_CYLINDER && size.y != size.z)
     494        if (partShape == Part::Shape::SHAPE_CYLINDER && scale.y != scale.z)
    485495                // If base radii have different values
    486496                return false;
     
    488498}
    489499
    490 bool Node::hasPartSizeParam()
    491 {
    492         return params.count(SIZE_X) > 0 || params.count(SIZE_Y) > 0 || params.count(SIZE_Z) > 0;
    493 }
    494 
    495500Pt3D Node::getVectorRotation()
    496501{
     
    501506{
    502507        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)
    504509                rotation += getVectorRotation();
    505510        return rotation;
     
    537542void Node::createPart()
    538543{
    539         part = new Part(partType);
     544        part = new Part(partShape);
    540545        part->p = Pt3D(state->location);
    541546
    542547        part->friction = getParam(FRICTION) * state->fr;
    543548        part->ingest = getParam(INGESTION) * state->ing;
    544         calculateSize(part->scale);
     549        calculateScale(part->scale);
    545550        part->setRot(getRotation());
    546551}
     
    581586                result += std::string(count, mod).c_str();
    582587        }
    583         result += SHAPE_TO_GENE.at(partType);
     588        result += SHAPE_TO_GENE.at(partShape);
    584589
    585590        if (!neurons.empty())
     
    658663}
    659664
    660 fS_Genotype::fS_Genotype(const string &geno)
    661 {
     665fS_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());
    662670        try
    663671        {
    664672                GenotypeParams genotypeParams;
    665673                genotypeParams.modifierMultiplier = 1.1;
     674                genotypeParams.distanceTolerance = 0.1;
     675                genotypeParams.relativeDensity = 10.0;
     676                genotypeParams.turnWithRotation = false;
     677                genotypeParams.paramMutationStrength = 0.4;
    666678
    667679                size_t modeSeparatorIndex = geno.find(MODE_SEPARATOR);
     
    669681                        throw fS_Exception("Genotype parameters missing", 0);
    670682
    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                }
    672700
    673701                int genoStart = modeSeparatorIndex + 1;
     
    680708                delete startNode;
    681709                throw e;
     710        }
     711        catch(...)
     712        {
     713                delete startNode;
     714                throw fS_Exception("Unknown exception in fS", 0);
    682715        }
    683716}
     
    753786        geno.reserve(100);
    754787
    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();
    756794        geno += MODE_SEPARATOR;
    757795
     
    850888        for (int i = 0; i < int(nodes.size()); i++)
    851889        {
    852                 if (!nodes[i]->isPartSizeValid())
     890                if (!nodes[i]->isPartScaleValid())
    853891                {
    854892                        return 1 + nodes[i]->partDescription->start;
     
    884922}
    885923
     924double 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.