void TreeSTag::readWithContext(PACC::XML::ConstIterator inIter, Beagle::Context& ioContext){ Beagle_StackTraceBeginM(); GP::Context& lGPContext = castObjectT<GP::Context&>(ioContext); if((inIter->getType()!=PACC::XML::eData) || (inIter->getValue()!="Genotype")) throw Beagle_IOExceptionNodeM(*inIter, "tag <Genotype> expected!"); std::string lType = inIter->getAttribute("type").c_str(); if(lType.empty()) throw Beagle_IOExceptionNodeM(*inIter, "GP tree type of the genotype is not present!"); if(lType != "gptreetag") throw Beagle_IOExceptionNodeM(*inIter, std::string("type of genotype mismatch, expected ") +std::string("\"gptreetag\" but read \"")+lType+std::string("\" instead!")); //Get structure id std::string lStructureID = inIter->getAttribute("structureid").c_str(); mStructureID = str2uint(lStructureID); // Get primitive set index std::string lPrimitiveSetIndexText = inIter->getAttribute("primitiveSetIndex").c_str(); if(lPrimitiveSetIndexText.empty()) { // No primitive set index was specified. This must be an old // tree. Assume index is equal to tree's index unsigned int lGenotypeIndex = ioContext.getGenotypeIndex(); if(lGenotypeIndex >= lGPContext.getSystem().getPrimitiveSuperSet().size()) { throw Beagle_RunTimeExceptionM(std::string("In GP::Tree::readWithContext(): The ")+ std::string("'primitiveSetIndex' attribute was missing from an individual's genotype. ")+ std::string("It would normally be assumed that such a tree was to be mapped to the ")+ std::string("primitive set of the same index as the genotype. In this case that would ")+ std::string("result in an incorrect mapping because there are not enough primitive sets ")+ std::string("in the System. Perhaps this individual was not intended to be read with the ")+ std::string("current set of primitive sets?")); } setPrimitiveSetIndex(lGenotypeIndex); } else { // primitiveSetIndex is a valid attribute. unsigned int lPrimitiveSetIndex = str2uint(lPrimitiveSetIndexText); if(lPrimitiveSetIndex >= lGPContext.getSystem().getPrimitiveSuperSet().size()) { std::string lMessage = std::string("In GP::Tree::readWithContext(): The 'primitiveSetIndex' ")+ std::string("attribute contained the value '") + lPrimitiveSetIndexText + std::string("' which was read as the number '") + uint2str(lPrimitiveSetIndex) + std::string("'. This value is incorrect as there are not enough primitive sets in the ")+ std::string("System. Perhaps this individual was not intended to be read with the current ")+ std::string("set of primitive sets?"); throw Beagle_RunTimeExceptionM(lMessage); } setPrimitiveSetIndex(lPrimitiveSetIndex); } // Get numberArguments attribute std::string lNumberArgumentsText = inIter->getAttribute("numberArguments").c_str(); if(lNumberArgumentsText.empty()) { // numberArguments attribute wasn't defined. This must be an old // tree. Assume the number of arguments is zero. setNumberArguments(0); } else { // numberArguments is a valid attribute. setNumberArguments(str2uint(lNumberArgumentsText)); } // Get size attribute and reserve size accordingly std::string lSizeText = inIter->getAttribute("size").c_str(); if(lSizeText.empty()==false) reserve(str2uint(lSizeText)); PACC::XML::ConstIterator lChild = inIter->getFirstChild(); if((!lChild) || (lChild->getType()!=PACC::XML::eData)) throw Beagle_IOExceptionNodeM(*lChild, "expected a XML tag for the GP tree!"); clear(); readSubTree(lChild, lGPContext); //Read BondGraph if any const PACC::XML::Node* lParent = inIter->getParent(); PACC::XML::ConstFinder lIndivFinder = PACC::XML::ConstIterator(lParent); PACC::XML::ConstIterator lBGTag = lIndivFinder.find("//BondGraph"); if(!lBGTag) { return; } // mBondGraph = new GrowingHybridBondGraph; // mBondGraph->read(lBGTag); Beagle_StackTraceEndM("void GP::Tree::readWithContext(PACC::XML::ConstIterator inIter, Beagle::Context& ioContext)"); }
bool Beagle::GP::MutationEphemeralOpT<T>::mutate(Beagle::Individual& ioIndividual, Beagle::Context& ioContext) { Beagle_StackTraceBeginM(); Beagle_LogDetailedM( ioContext.getSystem().getLogger(), "mutation", "Beagle::GP::MutationEphemeralOpT", std::string("Mutating ")+uint2ordinal(ioContext.getGenotypeIndex()+1)+ std::string(" individual with GP::MutationEphemeralOpT") ); GP::Individual& lIndividual = castObjectT<GP::Individual&>(ioIndividual); GP::Context& lContext = castObjectT<GP::Context&>(ioContext); // Get index of potential primitives with parameters that can be selected for mutation. std::vector< std::pair<unsigned int,unsigned int> > lPotentialParam; for(unsigned int i=0; i<lIndividual.size(); ++i) { GP::Tree& lTree = *lIndividual[i]; for(unsigned int j=0; j<lTree.size(); ++j) { if(lTree[j].mPrimitive->getName() == *mEphemeralName) { lPotentialParam.push_back(std::make_pair(i,j)); } } } // Return if there is not potential parameters. if(lPotentialParam.empty()) return false; // Mutating a primitive Beagle_LogDebugM( ioContext.getSystem().getLogger(), "mutation", "Beagle::GP::MutationEphemeralOpT", "Individual before GP parameters mutation" ); Beagle_LogObjectDebugM( ioContext.getSystem().getLogger(), "mutation", "Beagle::GP::MutationEphemeralOpT", ioIndividual ); // Store original context values unsigned int lOldGenotypeIndex = lContext.getGenotypeIndex(); GP::Tree::Handle lOldGenotypeHandle = lContext.getGenotypeHandle(); // Get reference to primitive to mutate and other objects. const unsigned int lSelectedParam = lContext.getSystem().getRandomizer().rollInteger(0,lPotentialParam.size()-1); GP::Tree::Handle lSelectedTree = lIndividual[lPotentialParam[lSelectedParam].first]; lContext.setGenotypeIndex(lPotentialParam[lSelectedParam].first); lContext.setGenotypeHandle(lSelectedTree); Beagle_LogVerboseM( ioContext.getSystem().getLogger(), "mutation", "Beagle::GP::MutationEphemeralOpT", std::string("Mutating the parameter of the ")+ uint2ordinal(lPotentialParam[lSelectedParam].second+1)+ std::string(" node in the ")+uint2ordinal(lPotentialParam[lSelectedParam].first+1)+ std::string(" tree") ); // Mutate parameter value. GP::Primitive::Handle lSelectedPrimit = (*lSelectedTree)[lPotentialParam[lSelectedParam].second].mPrimitive; typename GP::EphemeralT<T>::Handle lSelectedEphemeral = castHandleT<typename GP::EphemeralT<T> >(lSelectedPrimit); GP::Primitive::Handle lGeneratedPrimit = lSelectedEphemeral->generate(mEphemeralName->getWrappedValue(), lContext); (*lSelectedTree)[lPotentialParam[lSelectedParam].second].mPrimitive = lGeneratedPrimit; Beagle_LogVerboseM( ioContext.getSystem().getLogger(), "mutation", "Beagle::GP::MutationEphemeralOpT", std::string("Changing the ephemeral from ")+lSelectedPrimit->serialize()+ std::string(" to ")+lGeneratedPrimit->serialize() ); // Restore original context values lContext.setGenotypeIndex(lOldGenotypeIndex); lContext.setGenotypeHandle(lOldGenotypeHandle); Beagle_LogDebugM( ioContext.getSystem().getLogger(), "mutation", "Beagle::GP::MutationEphemeralOpT", "Individual after GP parameters mutation" ); Beagle_LogObjectDebugM( ioContext.getSystem().getLogger(), "mutation", "Beagle::GP::MutationEphemeralOpT", ioIndividual ); return true; Beagle_StackTraceEndM("bool GP::MutationEphemeralOpT<T>::mutate(Individual& ioIndividual, Context& ioContext)"); }