/*! * \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(); }