/*!
 *  \return Return selection probability of breeder operator.
 *  \param inChild Child node in the breeder tree.
 */
double InvalidateFitnessOp::getBreedingProba(BreederNode::Handle inChild)
{
	Beagle_StackTraceBeginM();
	Beagle_NonNullPointerAssertM(inChild);
	Beagle_NonNullPointerAssertM(inChild->getBreederOp());
	return inChild->getBreederOp()->getBreedingProba(inChild->getFirstChild());
	Beagle_StackTraceEndM();
}
/*!
 *  \return Return selection probability of breeder operator.
 *  \param inChild Child node in the breeder tree.
 */
double ES::AdaptOneFifthRuleOp::getBreedingProba(BreederNode::Handle inChild)
{
	Beagle_StackTraceBeginM();
	Beagle_NonNullPointerAssertM(inChild);
	Beagle_NonNullPointerAssertM(inChild->getBreederOp());
	return inChild->getBreederOp()->getBreedingProba(inChild->getFirstChild());
	Beagle_StackTraceEndM();
}
Exemple #3
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();
}
/*!
 *  \brief Apply the fitness invalidation operation on a breeding pool, returning a bred individual.
 *  \param inBreedingPool Breeding pool to use for the breeding operation.
 *  \param inChild Node handle associated to child node in the breeder tree.
 *  \param ioContext Evolutionary context of the breeding operation.
 *  \return Invalidated bred individual.
 */
Individual::Handle InvalidateFitnessOp::breed(Individual::Bag& inBreedingPool,
        BreederNode::Handle inChild,
        Context& ioContext)
{
	Beagle_StackTraceBeginM();
	Beagle_NonNullPointerAssertM(inChild);
	Beagle_NonNullPointerAssertM(inChild->getBreederOp());
	Individual::Handle lBredIndividual =
	    inChild->getBreederOp()->breed(inBreedingPool, inChild->getFirstChild(), ioContext);
	if((lBredIndividual->getFitness()!=NULL) && (lBredIndividual->getFitness()->isValid())) {
		Beagle_LogDebugM(
		    ioContext.getSystem().getLogger(),
		    "Invalidating the fitness of the following bred individual"
		);
		Beagle_LogDebugM(ioContext.getSystem().getLogger(), *lBredIndividual);
		lBredIndividual->getFitness()->setInvalid();
	}
	return lBredIndividual;
	Beagle_StackTraceEndM();
}
/*!
 *  \brief Apply the adaptation operation on a breeding pool.
 *  \param inBreedingPool Breeding pool to give to the underlying breeding tree.
 *  \param inChild Node handle associated to child node in the breeder tree.
 *  \param ioContext Evolutionary context of the mutation operation.
 *  \return Mutated individual.
 */
Individual::Handle ES::AdaptOneFifthRuleOp::breed(Individual::Bag& inBreedingPool,
        BreederNode::Handle inChild,
        Context& ioContext)
{
	Beagle_StackTraceBeginM();
	Beagle_NonNullPointerAssertM(inChild);
	Beagle_NonNullPointerAssertM(inChild->getBreederOp());
	Individual::Handle lIndiv =
	    inChild->getBreederOp()->breed(inBreedingPool, inChild->getFirstChild(), ioContext);

	// Look whether the mutation is successful.
	Fitness::Handle lFitParent = inBreedingPool[ioContext.getIndividualIndex()]->getFitness();
	Fitness::Handle lFitChild = lIndiv->getFitness();
	if(lFitChild != NULL) {

		// Check whether the mutation is successful or not.
		const bool lSuccessful = (*lFitChild) > (*lFitParent);
		if(lSuccessful) {
			++mSuccessCount->getWrappedValue();
			Beagle_LogVerboseM(
			    ioContext.getSystem().getLogger(),
			    "Successful mutation"
			);
		} else {
			Beagle_LogVerboseM(
			    ioContext.getSystem().getLogger(),
			    "The mutation is not successful"
			);
		}
		++mMutationsCount->getWrappedValue();

		// Adapt the sigma if the necessary number of observed mutations is reached.
		if(lSuccessful && (mMutationsCount->getWrappedValue()>=mAdaptationPeriod->getWrappedValue())) {
			const double lSuccessRate = double(mSuccessCount->getWrappedValue()) /
			                            double(mMutationsCount->getWrappedValue());
			if(lSuccessRate > 0.2) {
				for(unsigned int i=0; i<mMutateGaussSigma->size(); ++i) {
					(*mMutateGaussSigma)[i] /= mSigmaAdaptFactor->getWrappedValue();
				}
				Beagle_LogDetailedM(
				    ioContext.getSystem().getLogger(),
				    string("Increasing the sigma value by a factor ")+
				    dbl2str(1./mSigmaAdaptFactor->getWrappedValue())
				);
				Beagle_LogVerboseM(
				    ioContext.getSystem().getLogger(),
				    string("Sigma value after the increase: ")+mMutateGaussSigma->serialize()
				);
			} else if(lSuccessRate < 0.2) {
				for(unsigned int i=0; i<mMutateGaussSigma->size(); ++i) {
					(*mMutateGaussSigma)[i] *= mSigmaAdaptFactor->getWrappedValue();
				}
				Beagle_LogDetailedM(
				    ioContext.getSystem().getLogger(),
				    string("Reducing the sigma value by a factor ")+
				    dbl2str(mSigmaAdaptFactor->getWrappedValue())
				);
				Beagle_LogVerboseM(
				    ioContext.getSystem().getLogger(),
				    string("Sigma value after the reduction: ")+mMutateGaussSigma->serialize()
				);
			} else {
				Beagle_LogDetailedM(
				    ioContext.getSystem().getLogger(),
				    "Sigma value unchanged"
				);
			}
		}
	}

	return lIndiv;
	Beagle_StackTraceEndM();
}
Exemple #6
0
/*!
 *  \brief Apply the recombination operation on a breeding pool, returning a recombined individual.
 *  \param inBreedingPool Breeding pool to use for the recombination operation.
 *  \param inChild Node handle associated to child node in the breeder tree.
 *  \param ioContext Evolutionary context of the recombination operation.
 *  \return Recombined individual.
 */
Individual::Handle RecombinationOp::breed(Individual::Bag& inBreedingPool,
        BreederNode::Handle inChild,
        Context& ioContext)
{
	Beagle_StackTraceBeginM();

	// Generate parents for recombination.
	Individual::Bag::Handle lParents = new Individual::Bag;
	if(inChild == NULL) {
		const unsigned int lNbGenerated =
		    (mNumberRecomb->getWrappedValue()==0) ? inBreedingPool.size() :
		    minOf<unsigned int>(inBreedingPool.size(), mNumberRecomb->getWrappedValue());
		if(lNbGenerated == inBreedingPool.size()) (*lParents) = inBreedingPool;
		else {
			std::vector<unsigned int> lIndices(inBreedingPool.size());
			for(unsigned int i=0; i<lIndices.size(); ++i) lIndices[i] = i;
			std::random_shuffle(lIndices.begin(), lIndices.end(),
			                    ioContext.getSystem().getRandomizer());
			for(unsigned int i=0; i<lNbGenerated; ++i) {
				lParents->push_back(inBreedingPool[lIndices[i]]);
			}
		}
	} else {
		Beagle_NonNullPointerAssertM(inChild->getBreederOp());
		const unsigned int lNbGenerated =
		    (mNumberRecomb->getWrappedValue()==0) ? inBreedingPool.size() :
		    minOf<unsigned int>(inBreedingPool.size(), mNumberRecomb->getWrappedValue());
		for(unsigned int i=0; i<lNbGenerated; ++i) {
			Individual::Handle lIndiv = inChild->getBreederOp()->breed(inBreedingPool,
			                            inChild->getFirstChild(),
			                            ioContext);
			lParents->push_back(lIndiv);
		}
	}

	// Log parents selected for recombination.
	Beagle_LogVerboseM(
	    ioContext.getSystem().getLogger(),
	    std::string("Recombining ")+uint2str(lParents->size())+std::string(" individuals together")
	);

	// Do recombination operation on parent and get the resulting child.
	Individual::Handle lChildIndiv = recombine(*lParents, ioContext);
	if(lChildIndiv->getFitness() != NULL) {
		lChildIndiv->getFitness()->setInvalid();
	}

	// Log information to history, if it is used.
	History::Handle lHistory = castHandleT<History>(ioContext.getSystem().haveComponent("History"));
	if(lHistory != NULL) {
		std::vector<HistoryID> lParentNames;
		for(unsigned int i=0; i<lParents->size(); ++i) {
			HistoryID::Handle lHID = castHandleT<HistoryID>(lParents->at(i)->getMember("HistoryID"));
			if(lHID != NULL) lParentNames.push_back(*lHID);
		}
		lHistory->incrementHistoryVar(*lChildIndiv);
		lHistory->trace(ioContext, lParentNames, lChildIndiv, getName(), "recombination");
	}

	return lChildIndiv;
	Beagle_StackTraceEndM();
}
/*!
 *  \brief Apply the evaluation operation on a breeding pool, returning a evaluated bred individual.
 *  \param inBreedingPool Breeding pool to use for the breeding operation.
 *  \param inChild Node handle associated to child node in the breeder tree.
 *  \param ioContext Evolutionary context of the breeding operation.
 *  \return Evaluated bred individual.
 */
Individual::Handle Beagle::MPI::EvaluationOp::breed(Individual::Bag& inBreedingPool,
													BreederNode::Handle inChild,
													Context& ioContext)
{
	Beagle_NonNullPointerAssertM(inChild);
	
	Deme& lDeme = *ioContext.getDemeHandle();
	if(lDeme.getStats()->isValid()) {
		ioContext.setProcessedDeme(0);
		if((ioContext.getGeneration()!=0) && (lDeme.getStats()->existItem("total-processed"))) {
			ioContext.setTotalProcessedDeme((unsigned int)lDeme.getStats()->getItem("total-processed"));
		}
		else ioContext.setTotalProcessedDeme(0);
		lDeme.getStats()->setInvalid();
		
		if(ioContext.getDemeIndex()==0) {
			Stats& lVivaStats = *ioContext.getVivarium().getStats();
			ioContext.setProcessedVivarium(0);
			if((ioContext.getGeneration()!=0) && (lVivaStats.existItem("total-processed"))) {
				ioContext.setTotalProcessedVivarium((unsigned int)lVivaStats.getItem("total-processed"));
			}
			else ioContext.setTotalProcessedVivarium(0);
			lVivaStats.setInvalid();
		}
	}
	
	Beagle_NonNullPointerAssertM(inChild);
	Beagle_NonNullPointerAssertM(inChild->getBreederOp());
	Individual::Handle lBredIndividual =
    inChild->getBreederOp()->breed(inBreedingPool, inChild->getFirstChild(), ioContext);
    
	if((lBredIndividual->getFitness()==NULL) || (lBredIndividual->getFitness()->isValid()==false)) {
		Beagle_LogVerboseM(
						   ioContext.getSystem().getLogger(),
						   "evaluation", "Beagle::MPIEvaluationOp",
						   "Evaluating the fitness of a new bred individual"
						   );

		individualEvaluation(*lBredIndividual, ioContext);
//		lBredIndividual->setFitness(evaluate(*lBredIndividual, ioContext));
//		lBredIndividual->getFitness()->setValid();
		
		ioContext.setProcessedDeme(ioContext.getProcessedDeme()+1);
		ioContext.setTotalProcessedDeme(ioContext.getTotalProcessedDeme()+1);
		ioContext.setProcessedVivarium(ioContext.getProcessedVivarium()+1);
		ioContext.setTotalProcessedVivarium(ioContext.getTotalProcessedVivarium()+1);
		
		Beagle_LogVerboseM(
						   ioContext.getSystem().getLogger(),
						   "evaluation", "Beagle::MPIEvaluationOp",
						   std::string("The individual fitness value is: ")+
						   lBredIndividual->getFitness()->serialize()
						   );
		
		if(mDemeHOFSize->getWrappedValue() > 0) {
			Beagle_LogVerboseM(
							   ioContext.getSystem().getLogger(),
							   "evaluation", "Beagle::MPIEvaluationOp",
							   "Updating the deme hall-of-fame"
							   );
			lDeme.getHallOfFame().updateWithIndividual(mDemeHOFSize->getWrappedValue(),
													   *lBredIndividual, ioContext);
		}
		if(mVivaHOFSize->getWrappedValue() > 0) {
			Beagle_LogVerboseM(
							   ioContext.getSystem().getLogger(),
							   "evaluation", "Beagle::MPIEvaluationOp",
							   "Updating the vivarium hall-of-fame"
							   );
			ioContext.getVivarium().getHallOfFame().updateWithIndividual(mVivaHOFSize->getWrappedValue(),
																		 *lBredIndividual, ioContext);
		}
	}
	
	return lBredIndividual;
}
/*!
 *  \return Return selection probability of breeder operator.
 *  \param inChild Child node in the breeder tree.
 */
float Beagle::MPI::EvaluationOp::getBreedingProba(BreederNode::Handle inChild)
{
	Beagle_NonNullPointerAssertM(inChild);
	Beagle_NonNullPointerAssertM(inChild->getBreederOp());
	return inChild->getBreederOp()->getBreedingProba(inChild->getFirstChild());
}