Changeset 1006 for cpp


Ignore:
Timestamp:
07/14/20 16:04:46 (4 years ago)
Author:
Maciej Komosinski
Message:

Improved the fS encoding

Location:
cpp/frams/genetics/fS
Files:
1 added
6 edited

Legend:

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

    r958 r1006  
    1818        }
    1919
    20         Model model;
    21         model.open(using_checkpoints);
    22         genotype->buildModel(model);
    23         model.close();
     20        Model model = genotype->buildModel(using_checkpoints);
    2421        delete genotype;
    2522
  • cpp/frams/genetics/fS/fS_conv.h

    r1000 r1006  
    1010
    1111/**
    12  * Genotype converter from fS to f0.
     12 * Genotype converter from fS to f0s.
    1313 */
    1414class GenoConv_fS0 : public GenoConverter
  • cpp/frams/genetics/fS/fS_general.cpp

    r1000 r1006  
    1717bool fS_Genotype::TURN_WITH_ROTATION = false;
    1818
    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 };
     19void 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}
    3437
    3538double fS_stod(const string&  str, int start, size_t* size)
     
    7679
    7780
    78 fS_Neuron::fS_Neuron(const char *str, int start, int length)
    79 {
     81fS_Neuron::fS_Neuron(const char *str, int _start, int length)
     82{
     83        start = _start + 1;
     84        end = start + length;
    8085        if (length == 0)
    8186                return;
     
    123128Node::Node(Substring &restOfGeno, Node *_parent, GenotypeParams _genotypeParams)
    124129{
     130        prepareParams();
    125131        partDescription = new Substring(restOfGeno);
    126132        genotypeParams = _genotypeParams;
     
    165171        for (int i = 0; i < restOfGenotype.len; i++)
    166172        {
    167                 if (GENE_TO_SHAPETYPE.find(restOfGenotype.at(i)) != GENE_TO_SHAPETYPE.end())
     173                if (GENE_TO_SHAPE.find(restOfGenotype.at(i)) != GENE_TO_SHAPE.end())
    168174                        return i;
    169175        }
     
    193199void Node::extractPartType(Substring &restOfGenotype)
    194200{
    195         auto itr = GENE_TO_SHAPETYPE.find(restOfGenotype.at(0));
    196         if (itr == GENE_TO_SHAPETYPE.end())
     201        auto itr = GENE_TO_SHAPE.find(restOfGenotype.at(0));
     202        if (itr == GENE_TO_SHAPE.end())
    197203                throw fS_Exception("Invalid part type", restOfGenotype.start);
    198204
     
    465471                Neuro *neuro = new Neuro(*neurons[i]);
    466472                model.addNeuro(neuro);
     473                neuro->addMapping(MultiRange(IRange(neurons[i]->start, neurons[i]->end)));
    467474                if (neuro->getClass()->preflocation == NeuroClass::PREFER_JOINT && parent != nullptr)
    468475                {
     
    536543                result += std::string(count, mod).c_str();
    537544        }
    538         result += SHAPETYPE_TO_GENE.at(partType);
     545        result += SHAPE_TO_GENE.at(partType);
    539546
    540547        if (!neurons.empty())
     
    649656}
    650657
    651 void fS_Genotype::buildModel(Model &model)
    652 {
     658Model fS_Genotype::buildModel(bool using_checkpoints)
     659{
     660
     661        Model model;
     662        model.open(using_checkpoints);
     663
    653664        getState();
    654665        startNode->buildModel(model, nullptr);
    655666        buildNeuroConnections(model);
     667
     668        model.close();
     669        return model;
    656670}
    657671
  • cpp/frams/genetics/fS/fS_general.h

    r1000 r1006  
    2727const char NEURON_END = ']';
    2828const char NEURON_SEPARATOR = ';';
    29 const SString NEURON_INTERNAL_SEPARATOR("_");
     29const SString NEURON_INTERNAL_SEPARATOR("'");
    3030#define NEURON_I_W_SEPARATOR ':'
    3131//@}
     
    6969const char CUBOID = 'C';
    7070const char CYLINDER = 'R';
    71 const std::unordered_map<Part::Shape, char> SHAPETYPE_TO_GENE = {
     71const std::unordered_map<Part::Shape, char> SHAPE_TO_GENE = {
    7272                {Part::Shape::SHAPE_ELLIPSOID, ELLIPSOID},
    7373                {Part::Shape::SHAPE_CUBOID,    CUBOID},
     
    7676
    7777// This map is inverse to SHAPE_TO_SYMBOL. Those two should be compatible
    78 const std::unordered_map<char, Part::Shape> GENE_TO_SHAPETYPE = {
     78const std::unordered_map<char, Part::Shape> GENE_TO_SHAPE = {
    7979                {ELLIPSOID, Part::Shape::SHAPE_ELLIPSOID},
    8080                {CUBOID,    Part::Shape::SHAPE_CUBOID},
    8181                {CYLINDER,  Part::Shape::SHAPE_CYLINDER},
    8282};
    83 const int SHAPE_COUNT = 3;    // This should be the count of SHAPETYPE_TO_GENE and GENE_TO_SHAPETYPE
     83const int SHAPE_COUNT = 3;    // This should be the count of SHAPE_TO_GENE and GENE_TO_SHAPE
    8484
    8585const char DEFAULT_JOINT = 'a';
     
    100100};
    101101
    102 extern const std::map<string, double> defaultValues;
    103 
    104102/** @name Number of tries of performing a mutation before GENOPER_FAIL is returned */
    105103#define mutationTries  20
     
    198196        MultiRange toMultiRange()
    199197        {
    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));
    203200        }
    204201};
     
    244241{
    245242public:
     243        int start, end;
    246244        std::map<int, double> inputs;
    247245
     
    274272        Part *part;     /// A part object built from node. Used in building the Model
    275273        int partCodeLen; /// The length of substring that directly describes the corresponding part
     274        std::map<string, double> defaultValues;
    276275        GenotypeParams genotypeParams;
    277 
    278276
    279277        vector<Node *> children;    /// Vector of all direct children
    280278        std::map<char, int> modifiers;     /// Vector of all modifiers
    281279        vector<fS_Neuron *> neurons;    /// Vector of all the neurons
     280
     281        void prepareParams();
    282282
    283283        double getDistance();
     
    398398
    399399        /**
    400          * Change the value of the size parameter by given multiplier
    401          * Do not change the value if any of the size restrictions is not satisfied
    402          * @param paramKey
    403          * @param multiplier
    404          * @param ensureCircleSection
    405          * @return True if the parameter value was change, false otherwise
    406          */
    407         bool mutateSizeParam(string paramKey,  bool ensureCircleSection);
    408 
    409         /**
    410400         * Counts all the nodes in subtree
    411401         * @return node count
     
    521511         * @param a reference to a model that will contain a built model
    522512         */
    523         void buildModel(Model &model);
     513        Model buildModel(bool using_checkpoints);
    524514
    525515        /**
  • cpp/frams/genetics/fS/fS_oper.cpp

    r1000 r1006  
    3636
    3737
    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 
     38void 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}
    7172
    7273GenoOper_fS::GenoOper_fS()
    7374{
     75        prepareParams();
    7476        par.setParamTab(genooper_fS_paramtab);
    7577        par.select(this);
     
    332334        geno.getState();
    333335        Node *node = geno.chooseNode();
    334         char partType = SHAPETYPE_TO_GENE.at(availablePartShapes[rndUint(availablePartShapes.size())]);
     336        char partType = SHAPE_TO_GENE.at(availablePartShapes[rndUint(availablePartShapes.size())]);
    335337
    336338        Substring substring(&partType, 0, 1);
     
    374376        {
    375377                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);
    379381        }
    380382        return true;
     
    483485        bool isRadius = isRadiusOfBase || key == SIZE_X;
    484486        if (ensureCircleSection && isRadius)
     487        if (ensureCircleSection && isRadius)
    485488        {
    486489                if (randomNode->partType == Part::Shape::SHAPE_ELLIPSOID)
     
    490493        }
    491494        // 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);
    493496        return true;
    494497}
     
    530533                                return true;
    531534                        } else
    532                                 return randomNode->mutateSizeParam(it->first, ensureCircleSection);
     535                                return mutateSizeParam(randomNode, it->first, ensureCircleSection);
    533536                }
    534537        }
     
    696699}
    697700
    698 bool Node::mutateSizeParam(string key, bool ensureCircleSection)
    699 {
    700         double oldValue = getParam(key);
    701         double volume = calculateVolume();
     701bool GenoOper_fS::mutateSizeParam(Node *node, string key, bool ensureCircleSection)
     702{
     703        double oldValue = node->getParam(key);
     704        double volume = node->calculateVolume();
    702705        double valueAtMinVolume, valueAtMaxVolume;
    703706        if(key == SIZE)
     
    715718        double max = std::min(maxValues.at(key), valueAtMaxVolume);
    716719
    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())
    720723                return true;
    721724        else
    722725        {
    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  
    3232const int PARENT_COUNT = 2;
    3333
     34
     35
    3436class GenoOper_fS : public GenoOperators
    3537{
     
    4042        paInt useElli, useCub,  useCyl;
    4143        paInt strongAddPart;
     44
     45        std::map<string, double> minValues;
     46        std::map<string, double> maxValues;
    4247
    4348        GenoOper_fS();
     
    129134
    130135        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();
    131148};
    132149
Note: See TracChangeset for help on using the changeset viewer.