⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 crossoverconstrainedop.cpp

📁 非常好的进化算法EC 实现平台 可以实现多种算法 GA GP
💻 CPP
📖 第 1 页 / 共 2 页
字号:
        (lContext1.getSystem().getRandomizer().rollUniform(0.0, 1.0) < lDistrProba);      while((lTree1[lChoosenNode1].mPrimitive->getNumberArguments() != 0) != lTypeNode1) {        lChoosenNode1 = lContext1.getSystem().getRandomizer().rollInteger(0, lTree1.size()-1);      }    }    // Choose type of node (branch or leaf) for the second node.    const bool lTypeNode2 =      (lContext2.getSystem().getRandomizer().rollUniform(0.0, 1.0) < lDistrProba);    // Compute max depth allowable.    lTree1.setContextToNode(lChoosenNode1, lContext1);    const unsigned int lTmpMaxDepth1 = lMaxTreeDepth - lContext1.getCallStackSize();    const unsigned int lTmpMaxDepth2 = lMaxTreeDepth - lTree1.getTreeDepth(lChoosenNode1);    const unsigned int lMaxDepthTree2 = minOf(lTmpMaxDepth1, lTmpMaxDepth2);   // Select a node in second individual for the crossover.   unsigned int lChoosenTree2=0;   unsigned int lChoosenNode2=0;#ifdef BEAGLE_HAVE_RTTI   const std::type_info* lDesiredType = lTree1[lChoosenNode1].mPrimitive->getReturnType(lContext1);   bool lGoodSelect = selectNodeToMateWithType(lChoosenTree2,                                               lChoosenNode2,                                               lTypeNode2,                                               lDesiredType,                                               lPrimitiveSetIndex1,                                               lMaxDepthTree2,                                               UINT_MAX,                                               lIndiv2,                                               lContext2);#else // BEAGLE_HAVE_RTTI    bool lGoodSelect = selectNodeToMate(lChoosenTree2,                                        lChoosenNode2,                                        lTypeNode2,                                        lPrimitiveSetIndex1,                                        lMaxDepthTree2,                                        UINT_MAX,                                        lIndiv2,                                        lContext2);#endif // BEAGLE_HAVE_RTTI    // Check to see that there is at least one node that can be selected    if(lGoodSelect==false) {      Beagle_LogVerboseM(        ioContext1.getSystem().getLogger(),        "crossover", "Beagle::GP::CrossoverConstrainedOp",        string("Crossover attempt failed: it seems there is no corresponding nodes in second ")+        string("individual that would meet all the constraints")      );      continue;    }    // Get reference to the tree the choosen node is in.    Beagle_AssertM(lChoosenTree2 < lIndiv2.size());    GP::Tree& lTree2 = *lIndiv2[lChoosenTree2];    lTree2.setContextToNode(lChoosenNode2, lContext2);    // Mate the trees.    Beagle_LogVerboseM(      ioContext1.getSystem().getLogger(),      "crossover", "Beagle::GP::CrossoverConstrainedOp",      string("Trying to exchange the ")+uint2ordinal(lChoosenNode1+1)+      string(" node of the ")+uint2ordinal(lChoosenTree1+1)+      string(" tree of the first individual with the ")+uint2ordinal(lChoosenNode2+1)+      string(" node of the ")+uint2ordinal(lChoosenTree2+1)+      string(" tree of the second individual")    );    mateTrees(lTree1, lChoosenNode1, lContext1, lTree2, lChoosenNode2, lContext2);    // If one tree is not valid, undo the crossover and do a new crossover attempt.     lContext1.setGenotypeHandle(lIndiv1[lChoosenTree1]);    lContext1.setGenotypeIndex(lChoosenTree1);    lContext2.setGenotypeHandle(lIndiv2[lChoosenTree2]);    lContext2.setGenotypeIndex(lChoosenTree2);    if(lTree1.validateSubTree(lChoosenNode1,lContext1) &&       lTree2.validateSubTree(lChoosenNode2,lContext2)) {      lMatingDone = true;      Beagle_LogVerboseM(        ioContext1.getSystem().getLogger(),        "crossover", "Beagle::GP::CrossoverConstrainedOp",        "Constrained tree GP crossover valid"      );      break; // The crossover is valid.    }    else {   // Undo crossover.      Beagle_LogVerboseM(        ioContext1.getSystem().getLogger(),        "crossover", "Beagle::GP::CrossoverConstrainedOp",        "Crossover attempt failed because one of the resulting trees was invalid"      );      mateTrees(lTree1, lChoosenNode1, lContext1, lTree2, lChoosenNode2, lContext2);      continue;    }  }  // Replace the contexts.  lContext1.setGenotypeHandle(lOldTreeHandle1);  lContext1.setGenotypeIndex(lOldTreeIndex1);  lContext2.setGenotypeHandle(lOldTreeHandle2);  lContext2.setGenotypeIndex(lOldTreeIndex2);  if(lMatingDone) {    Beagle_LogDebugM(      ioContext1.getSystem().getLogger(),      "crossover", "Beagle::GP::CrossoverConstrainedOp",      string("First individual mated (after constrained tree GP crossover): ")+      lIndiv1.serialize()    );    Beagle_LogDebugM(      ioContext1.getSystem().getLogger(),      "crossover", "Beagle::GP::CrossoverConstrainedOp",      string("Second individual mated (after constrained tree GP crossover): ")+      lIndiv2.serialize()    );  }  else {    Beagle_LogVerboseM(      ioContext1.getSystem().getLogger(),      "crossover", "Beagle::GP::CrossoverConstrainedOp",      "No constrained tree GP crossover done"    );  }  return lMatingDone;  Beagle_StackTraceEndM("bool GP::CrossoverConstrainedOp::mate(Individual& ioIndiv1, Context& ioContext1, Individual& ioIndiv2, Context& ioContext2)");}/*! *  \brief Select a node for mating in the given individual, following the constraints penalties. *  \param outSelectTreeIndex Tree index of the selected node. *  \param outSelectNodeIndex Index of the selected node. *  \param inSelectABranch True if node to select must be a branch, false if it must a leaf. *  \param inPrimitSetIndex Primitive set index to which the tree must be associated. *  \param inMaxSubTreeDepth Maximum sub tree depth allowed of the node to be selected. *  \param inMaxSubTreeSize Maximum sub tree size allowed of the node to be selected. *  \param inIndividual Individual to select the node from. *  \param ioContext Evolutionary context. *  \return True if there was node to select, false if no node respected all constraints. */bool GP::CrossoverConstrainedOp::selectNodeToMate(unsigned int& outSelectTreeIndex,                                                  unsigned int& outSelectNodeIndex,                                                  bool inSelectABranch,                                                  unsigned int inPrimitSetIndex,                                                  unsigned int inMaxSubTreeDepth,                                                  unsigned int inMaxSubTreeSize,                                                  GP::Individual& inIndividual,                                                  GP::Context& ioContext) const{  Beagle_StackTraceBeginM();  RouletteT< std::pair<unsigned int,unsigned int> > lRoulette;  GP::Tree::Handle lOldTreeHandle = ioContext.getGenotypeHandle();  const unsigned int lOldTreeIndex = ioContext.getGenotypeIndex();  ioContext.emptyCallStack();  for(unsigned int i=0; i<inIndividual.size(); ++i) {    if(inIndividual[i]->getPrimitiveSetIndex() != inPrimitSetIndex) continue;    ioContext.setGenotypeHandle(inIndividual[i]);    ioContext.setGenotypeIndex(i);    buildRoulette(lRoulette,                  inSelectABranch,                  inMaxSubTreeDepth,                  inMaxSubTreeSize,                  0,                  *inIndividual[i],                  ioContext);  }  ioContext.setGenotypeIndex(lOldTreeIndex);  ioContext.setGenotypeHandle(lOldTreeHandle);  if(lRoulette.size() == 0) return false;  std::pair<unsigned int,unsigned int> lSelectedNode =    lRoulette.select(ioContext.getSystem().getRandomizer());  outSelectTreeIndex = lSelectedNode.first;  outSelectNodeIndex = lSelectedNode.second;  return true;  Beagle_StackTraceEndM("bool GP::CrossoverConstrainedOp::selectNodeToMate(unsigned int& outSelectTreeIndex, unsigned int& outSelectNodeIndex, bool inSelectABranch, unsigned int inPrimitSetIndex, unsigned int inMaxSubTreeDepth, unsigned int inMaxSubTreeSize, GP::Individual& inIndividual, GP::Context& ioContext) const");}#ifdef BEAGLE_HAVE_RTTI/*! *  \brief Select a node for mating in the given individual, following the constraints penalties. *  \param outSelectTreeIndex Tree index of the selected node. *  \param outSelectNodeIndex Index of the selected node. *  \param inSelectABranch True if node to select must be a branch, false if it must a leaf. *  \param inNodeReturnType Desired return type for the nodes to be selected. *  \param inPrimitSetIndex Primitive set index to which the tree must be associated. *  \param inMaxSubTreeDepth Maximum sub tree depth allowed of the node to be selected. *  \param inMaxSubTreeSize Maximum sub tree size allowed of the node to be selected. *  \param inIndividual Individual to select the node from. *  \param ioContext Evolutionary context. *  \return True if there was node to select, false if no node respected all constraints. */bool GP::CrossoverConstrainedOp::selectNodeToMateWithType(unsigned int& outSelectTreeIndex,                                                          unsigned int& outSelectNodeIndex,                                                          bool inSelectABranch,                                                          const std::type_info* inNodeReturnType,                                                          unsigned int inPrimitSetIndex,                                                          unsigned int inMaxSubTreeDepth,                                                          unsigned int inMaxSubTreeSize,                                                          GP::Individual& inIndividual,                                                          GP::Context& ioContext) const{  Beagle_StackTraceBeginM();  RouletteT< std::pair<unsigned int,unsigned int> > lRoulette;  GP::Tree::Handle lOldTreeHandle = ioContext.getGenotypeHandle();  const unsigned int lOldTreeIndex = ioContext.getGenotypeIndex();  ioContext.emptyCallStack();  for(unsigned int i=0; i<inIndividual.size(); ++i) {    if(inIndividual[i]->getPrimitiveSetIndex() != inPrimitSetIndex) continue;    ioContext.setGenotypeHandle(inIndividual[i]);    ioContext.setGenotypeIndex(i);    buildRouletteWithType(lRoulette,                          inSelectABranch,                          inNodeReturnType,                          inMaxSubTreeDepth,                          inMaxSubTreeSize,                          0,                          *inIndividual[i],                          ioContext);  }  ioContext.setGenotypeIndex(lOldTreeIndex);  ioContext.setGenotypeHandle(lOldTreeHandle);  if(lRoulette.size() == 0) return false;  std::pair<unsigned int,unsigned int> lSelectedNode =    lRoulette.select(ioContext.getSystem().getRandomizer());  outSelectTreeIndex = lSelectedNode.first;  outSelectNodeIndex = lSelectedNode.second;  return true;  Beagle_StackTraceEndM("bool GP::CrossoverConstrainedOp::selectNodeToMateWithType(unsigned int& outSelectTreeIndex, unsigned int& outSelectNodeIndex, bool inSelectABranch, const std::type_info* inNodeReturnType, unsigned int inPrimitSetIndex, unsigned int inMaxSubTreeDepth, unsigned int inMaxSubTreeSize, GP::Individual& inIndividual, GP::Context& ioContext) const");}#endif // BEAGLE_HAVE_RTTI

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -