Changeset 1017 for cpp/frams/genetics/fS/fS_oper.cpp
- Timestamp:
- 07/20/20 16:37:38 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/genetics/fS/fS_oper.cpp
r1006 r1017 24 24 {"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",}, 25 25 {"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 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",}, 27 27 {"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",}, 28 28 {"fS_circle_section", 0, 0, "Ensure circle section", "d 0 1 1", FIELD(ensureCircleSection), "Ensure that ellipsoids and cylinders have circle cross-section"}, … … 35 35 #undef FIELDSTRUCT 36 36 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 73 37 GenoOper_fS::GenoOper_fS() 74 38 { 75 prepareParams();76 39 par.setParamTab(genooper_fS_paramtab); 77 40 par.select(this); … … 332 295 bool GenoOper_fS::addPart(fS_Genotype &geno, const vector <Part::Shape> &availablePartShapes, bool mutateSize) 333 296 { 334 geno.getState( );297 geno.getState(false); 335 298 Node *node = geno.chooseNode(); 336 299 char partType = SHAPE_TO_GENE.at(availablePartShapes[rndUint(availablePartShapes.size())]); … … 375 338 if (mutateSize) 376 339 { 377 geno.getState( );340 geno.getState(false); 378 341 mutateSizeParam(newNode, SIZE_X, true); 379 342 mutateSizeParam(newNode, SIZE_Y, true); … … 387 350 Node *randomNode, *selectedChild; 388 351 // 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++) 390 354 { 391 355 randomNode = geno.chooseNode(); … … 425 389 #endif 426 390 427 geno.getState( );391 geno.getState(false); 428 392 double sizeMultiplier = randomNode->getParam(SIZE) * randomNode->state->s; 429 393 double relativeVolume = randomNode->calculateVolume() / pow(sizeMultiplier, 3.0); … … 485 449 bool isRadius = isRadiusOfBase || key == SIZE_X; 486 450 if (ensureCircleSection && isRadius) 487 if (ensureCircleSection && isRadius)488 451 { 489 452 if (randomNode->partType == Part::Shape::SHAPE_ELLIPSOID) … … 493 456 } 494 457 // 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); 497 461 } 498 462 … … 508 472 auto it = randomNode->params.begin(); 509 473 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 } 512 484 } 513 485 } … … 515 487 } 516 488 489 490 bool 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 517 501 bool GenoOper_fS::changeParam(fS_Genotype &geno) 518 502 { 519 geno.getState( );503 geno.getState(false); 520 504 for (int i = 0; i < mutationTries; i++) 521 505 { … … 526 510 auto it = randomNode->params.begin(); 527 511 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); 536 513 } 537 514 } … … 543 520 Node *randomNode = geno.chooseNode(); 544 521 char randomModifier = MODIFIERS[rndUint(MODIFIERS.length())]; 522 int oldValue = randomNode->modifiers[randomModifier]; 523 545 524 randomNode->modifiers[randomModifier] += rndUint(2) == 0 ? 1 : -1; 546 525 … … 548 527 if (isSizeMod && geno.checkValidityOfPartSizes() != 0) 549 528 { 550 randomNode->modifiers[randomModifier] ++;529 randomNode->modifiers[randomModifier] = oldValue; 551 530 return false; 552 531 } … … 715 694 } 716 695 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); 719 698 720 699 node->params[key] = GenoOperators::mutateCreep('f', node->getParam(key), min, max, true);
Note: See TracChangeset
for help on using the changeset viewer.