/*! * \brief Randomly select a node that takes arguments from a specific * tree in the individual. * \return Randomly selected tree */ unsigned int GP::Individual::chooseRandomNodeWithArgs(unsigned int inTree, GP::Context& ioContext) const { Beagle_StackTraceBeginM(); const GP::Tree& lTree = *(operator[](inTree)); unsigned int lSize = lTree.size(); if (lSize < 2) { std::ostringstream lOSS; lOSS << "In GP::Individual::chooseRandomNodeWithArgs(): "; lOSS << "Could not choose a node with arguments because the specified tree has fewer "; lOSS << "than two (" << lSize << ") nodes, hence there are no such nodes"; lOSS << " in the tree. This occurred while calling chooseRandomNodeWithArgs() with an "; lOSS << "inTree value of " << inTree; throw Beagle_RunTimeExceptionM(lOSS.str()); } // Loop through the tree adding appropriate nodes into the roulette RouletteT<unsigned int> lRoulette; for (unsigned int i=0; i<lSize; i++) { if(lTree[i].mSubTreeSize > 1) lRoulette.insert(i); } // Select node with roulette Beagle_AssertM(!lRoulette.empty()); return lRoulette.select(ioContext.getSystem().getRandomizer()); Beagle_StackTraceEndM(); }
/*! * \brief Randomly select a node that takes no argument from a specific * tree in the individual. * \return Randomly selected tree */ unsigned int GP::Individual::chooseRandomNodeWithoutArgs(unsigned int inTree, GP::Context& ioContext) const { Beagle_StackTraceBeginM(); // Loop through the tree adding appropriate nodes into the roulette unsigned int lSize = operator[](inTree)->size(); RouletteT<unsigned int> lRoulette; for (unsigned int i=0; i<lSize; i++) { if (operator[](inTree)->operator[](i).mSubTreeSize == 1) { lRoulette.insert(i); } } // Select node with roulette Beagle_AssertM(!lRoulette.empty()); return lRoulette.select(ioContext.getSystem().getRandomizer()); Beagle_StackTraceEndM(); }
/*! * \brief Build a roulette of nodes that can be selected following the constraints penalties. * \param ioRoulette Roulette of nodes that can be selected following the constraints given. * \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 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 inActualIndex Index in actual tree of the node processed. * \param inTree Tree processed. * \param ioContext Evolutionary context. * \return Max depth of subtree processed. */ unsigned int STGP::CrossoverConstrainedOp::buildRouletteWithType( RouletteT< std::pair<unsigned int,unsigned int> >& ioRoulette, bool inSelectABranch, const std::type_info* inNodeReturnType, unsigned int inMaxSubTreeDepth, unsigned int inMaxSubTreeSize, unsigned int inActualIndex, GP::Tree& inTree, GP::Context& ioContext) const { Beagle_StackTraceBeginM(); const unsigned int lNbArgs = inTree[inActualIndex].mPrimitive->getNumberArguments(); const unsigned int lSubTreeSize = inTree[inActualIndex].mSubTreeSize; const bool lGoodArity = ((inTree.size()==1) || ((lNbArgs==0) != inSelectABranch)); ioContext.pushCallStack(inActualIndex); const std::type_info* lNodeType = inTree[inActualIndex].mPrimitive->getReturnType(ioContext); const bool lCompatibleTyping = ((inNodeReturnType==NULL) || (lNodeType==NULL) || (inNodeReturnType==lNodeType)); unsigned int lChildIndex = inActualIndex+1; unsigned int lMaxDepthDown = 0; for(unsigned int i=0; i<lNbArgs; ++i) { unsigned int lChildDepth = buildRouletteWithType(ioRoulette, inSelectABranch, inNodeReturnType, inMaxSubTreeDepth, inMaxSubTreeSize, lChildIndex, inTree, ioContext); lChildIndex += inTree[lChildIndex].mSubTreeSize; if(lChildDepth > lMaxDepthDown) lMaxDepthDown = lChildDepth; } ++lMaxDepthDown; const unsigned int lMaxDepthUp = ioContext.getCallStackSize(); ioContext.popCallStack(); if(lGoodArity && lCompatibleTyping && (lSubTreeSize<=inMaxSubTreeSize) && (lMaxDepthDown<=inMaxSubTreeDepth) && (lMaxDepthUp<=inMaxSubTreeDepth)) { std::pair<unsigned int,unsigned int> lPair(ioContext.getGenotypeIndex(), inActualIndex); ioRoulette.insert(lPair, 1.0); } return lMaxDepthDown; Beagle_StackTraceEndM(); }