示例#1
0
/*!
 *  \brief Apply the crossover operation on a breeding pool, returning a mated individual.
 *  \param inBreedingPool Breeding pool to use for the crossover operation.
 *  \param inChild Node handle associated to child node in the breeder tree.
 *  \param ioContext Evolutionary context of the crossover operation.
 *  \return Mated individual.
 */
Individual::Handle CrossoverOp::breed(Individual::Bag& inBreedingPool,
                                      BreederNode::Handle inChild,
                                      Context& ioContext)
{
	Beagle_StackTraceBeginM();

	Context::Alloc::Handle lContextAlloc =
	    castHandleT<Context::Alloc>(ioContext.getSystem().getFactory().getConceptAllocator("Context"));
	Context::Handle lContext2 = castHandleT<Context>(lContextAlloc->clone(ioContext));

	Beagle_NonNullPointerAssertM(inChild);
	Beagle_NonNullPointerAssertM(inChild->getBreederOp());
	Individual::Handle lIndiv1 = inChild->getBreederOp()->breed(inBreedingPool,
	                             inChild->getFirstChild(),
	                             ioContext);

	Beagle_NonNullPointerAssertM(inChild->getNextSibling());
	Beagle_NonNullPointerAssertM(inChild->getNextSibling()->getBreederOp());
	Individual::Handle lIndiv2 =
	    inChild->getNextSibling()->getBreederOp()->breed(inBreedingPool,
	            inChild->getNextSibling()->getFirstChild(),
	            *lContext2);
	Beagle_LogVerboseM(
	    ioContext.getSystem().getLogger(),
	    std::string("Mating the ")+uint2ordinal(ioContext.getIndividualIndex()+1)+
	    std::string(" individual with the ")+uint2ordinal(lContext2->getIndividualIndex()+1)+
	    " individual"
	);

	if((lIndiv1 != NULL) && (lIndiv2 != NULL)) {
		bool lMated = mate(*lIndiv1, ioContext, *lIndiv2, *lContext2);
		if(lMated) {
			if(lIndiv1->getFitness() != NULL) lIndiv1->getFitness()->setInvalid();
			if(lIndiv2->getFitness() != NULL) lIndiv2->getFitness()->setInvalid();
			History::Handle lHistory = castHandleT<History>(ioContext.getSystem().haveComponent("History"));
			if(lHistory != NULL) {
				std::vector<HistoryID> lParents;
				HistoryID::Handle lHID1 = castHandleT<HistoryID>(lIndiv1->getMember("HistoryID"));
				if(lHID1 != NULL) lParents.push_back(*lHID1);
				HistoryID::Handle lHID2 = castHandleT<HistoryID>(lIndiv2->getMember("HistoryID"));
				if(lHID2 != NULL) lParents.push_back(*lHID2);
				lHistory->incrementHistoryVar(*lIndiv1);
				lHistory->trace(ioContext, lParents, lIndiv1, getName(), "crossover");
			}
		}
	}

	return lIndiv1;
	Beagle_StackTraceEndM();
}