Ignore:
Timestamp:
07/20/20 16:37:38 (4 years ago)
Author:
Maciej Komosinski
Message:

fS: faster collision detection, depends on "geometry" algorithms

File:
1 edited

Legend:

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

    r1006 r1017  
    2424                                {"fS_mut_mod_neuro_conn",   0, 0, "Modify neuron connection",    "f 0 100 10", FIELD(prob[FS_MOD_NEURO_CONNECTION]), "mutation: probability of changing a neuron connection",},
    2525                                {"fS_mut_add_neuro_conn",   0, 0, "Add neuron connection",       "f 0 100 10", FIELD(prob[FS_ADD_NEURO_CONNECTION]), "mutation: probability of adding a neuron connection",},
    26                                 {"fS_mut_rem neuro_conn",   0, 0, "Remove neuron connection",    "f 0 100 10", FIELD(prob[FS_REM_NEURO_CONNECTION]), "mutation: probability of removing a neuron connection",},
     26                                {"fS_mut_rem_neuro_conn",   0, 0, "Remove neuron connection",    "f 0 100 10", FIELD(prob[FS_REM_NEURO_CONNECTION]), "mutation: probability of removing a neuron connection",},
    2727                                {"fS_mut_mod_neuro_params", 0, 0, "Modify neuron params",        "f 0 100 10", FIELD(prob[FS_MOD_NEURO_PARAMS]),     "mutation: probability of changing a neuron param",},
    2828                                {"fS_circle_section",       0, 0, "Ensure circle section",       "d 0 1 1",    FIELD(ensureCircleSection),           "Ensure that ellipsoids and cylinders have circle cross-section"},
     
    3535#undef FIELDSTRUCT
    3636
    37 
    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 }
    72 
    7337GenoOper_fS::GenoOper_fS()
    7438{
    75         prepareParams();
    7639        par.setParamTab(genooper_fS_paramtab);
    7740        par.select(this);
     
    332295bool GenoOper_fS::addPart(fS_Genotype &geno, const vector <Part::Shape> &availablePartShapes, bool mutateSize)
    333296{
    334         geno.getState();
     297        geno.getState(false);
    335298        Node *node = geno.chooseNode();
    336299        char partType = SHAPE_TO_GENE.at(availablePartShapes[rndUint(availablePartShapes.size())]);
     
    375338        if (mutateSize)
    376339        {
    377                 geno.getState();
     340                geno.getState(false);
    378341                mutateSizeParam(newNode, SIZE_X, true);
    379342                mutateSizeParam(newNode, SIZE_Y, true);
     
    387350        Node *randomNode, *selectedChild;
    388351        // Choose a parent with children
    389         for (int i = 0; i < mutationTries; i++)
     352        // It may be difficult to choose a eligible node, so the number of tries should be high
     353        for (int i = 0; i < 10 * mutationTries; i++)
    390354        {
    391355                randomNode = geno.chooseNode();
     
    425389#endif
    426390
    427                 geno.getState();
     391                geno.getState(false);
    428392                double sizeMultiplier = randomNode->getParam(SIZE) * randomNode->state->s;
    429393                double relativeVolume = randomNode->calculateVolume() / pow(sizeMultiplier, 3.0);
     
    485449        bool isRadius = isRadiusOfBase || key == SIZE_X;
    486450        if (ensureCircleSection && isRadius)
    487         if (ensureCircleSection && isRadius)
    488451        {
    489452                if (randomNode->partType == Part::Shape::SHAPE_ELLIPSOID)
     
    493456        }
    494457        // Add modified default value for param
    495         randomNode->params[key] = mutateCreep('f', randomNode->defaultValues.at(key), minValues.at(key), maxValues.at(key), true);
    496         return true;
     458        randomNode->params[key] = randomNode->defaultValues.at(key);
     459        geno.getState(false);
     460        return mutateParamValue(randomNode, key);
    497461}
    498462
     
    508472                        auto it = randomNode->params.begin();
    509473                        advance(it, rndUint(paramCount));
    510                         randomNode->params.erase(it->first);
    511                         return true;
     474                        string key = it->first;
     475                        double value = it->second;
     476
     477                        randomNode->params.erase(key);
     478                        if(geno.checkValidityOfPartSizes() == 0)
     479                                return true;
     480                        else
     481                        {
     482                                randomNode->params[key] = value;
     483                        }
    512484                }
    513485        }
     
    515487}
    516488
     489
     490bool GenoOper_fS::mutateParamValue(Node *node, string key)
     491{
     492        // Do not allow invalid changes in part size
     493        if (std::find(SIZE_PARAMS.begin(), SIZE_PARAMS.end(), key) == SIZE_PARAMS.end())
     494        {
     495                node->params[key] = GenoOperators::mutateCreep('f', node->getParam(key), Node::minValues.at(key), Node::maxValues.at(key), true);
     496                return true;
     497        } else
     498                return mutateSizeParam(node, key, ensureCircleSection);
     499}
     500
    517501bool GenoOper_fS::changeParam(fS_Genotype &geno)
    518502{
    519         geno.getState();
     503        geno.getState(false);
    520504        for (int i = 0; i < mutationTries; i++)
    521505        {
     
    526510                        auto it = randomNode->params.begin();
    527511                        advance(it, rndUint(paramCount));
    528 
    529                         // Do not allow invalid changes in part size
    530                         if (std::find(SIZE_PARAMS.begin(), SIZE_PARAMS.end(), it->first) == SIZE_PARAMS.end())
    531                         {
    532                                 it->second = GenoOperators::mutateCreep('f', it->second, minValues.at(it->first), maxValues.at(it->first), true);
    533                                 return true;
    534                         } else
    535                                 return mutateSizeParam(randomNode, it->first, ensureCircleSection);
     512                        return mutateParamValue(randomNode, it->first);
    536513                }
    537514        }
     
    543520        Node *randomNode = geno.chooseNode();
    544521        char randomModifier = MODIFIERS[rndUint(MODIFIERS.length())];
     522        int oldValue = randomNode->modifiers[randomModifier];
     523
    545524        randomNode->modifiers[randomModifier] += rndUint(2) == 0 ? 1 : -1;
    546525
     
    548527        if (isSizeMod && geno.checkValidityOfPartSizes() != 0)
    549528        {
    550                 randomNode->modifiers[randomModifier]++;
     529                randomNode->modifiers[randomModifier] = oldValue;
    551530                return false;
    552531        }
     
    715694        }
    716695
    717         double min = std::max(minValues.at(key), valueAtMinVolume);
    718         double max = std::min(maxValues.at(key), valueAtMaxVolume);
     696        double min = std::max(Node::minValues.at(key), valueAtMinVolume);
     697        double max = std::min(Node::maxValues.at(key), valueAtMaxVolume);
    719698
    720699        node->params[key] = GenoOperators::mutateCreep('f', node->getParam(key), min, max, true);
Note: See TracChangeset for help on using the changeset viewer.