本文整理汇总了C++中individual::Bag类的典型用法代码示例。如果您正苦于以下问题:C++ Bag类的具体用法?C++ Bag怎么用?C++ Bag使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Bag类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。
示例1: lCrowdingSet
/*!
* \brief Evaluate crowding distance of a pool of individuals.
* \param outDistances Evaluated crowding distance and associated indexes.
* \param inIndividualPool Pool of individuals to evaluate distance on.
*/
void EMO::NSGA2Op::evalCrowdingDistance(NSGA2Op::Distances& outDistances,
const Individual::Bag& inIndividualPool) const
{
Beagle_StackTraceBeginM();
outDistances.clear();
if(inIndividualPool.size() == 0) return;
outDistances.resize(inIndividualPool.size());
std::vector<CrowdingPair> lCrowdingSet(inIndividualPool.size());
for(unsigned int i=0; i<inIndividualPool.size(); ++i) {
outDistances[i].first = 0.;
outDistances[i].second = i;
lCrowdingSet[i].first = i;
lCrowdingSet[i].second = inIndividualPool[i]->getFitness();
}
const unsigned int lNumberObjectives = lCrowdingSet.back().second->getNumberOfObjectives();
for(unsigned int j=0; j<lNumberObjectives; ++j) {
std::sort(lCrowdingSet.begin(), lCrowdingSet.end(), IsLessCrowdingPairPredicate(j));
outDistances[lCrowdingSet.front().first].first = DBL_MAX;
outDistances[lCrowdingSet.back().first].first = DBL_MAX;
for(unsigned int k=1; k<(lCrowdingSet.size()-1); ++k) {
if(outDistances[lCrowdingSet[k].first].first < DBL_MAX) {
const double lObjDiff = lCrowdingSet[k+1].second->getObjective(j) -
lCrowdingSet[k-1].second->getObjective(j);
outDistances[lCrowdingSet[k].first].first += lObjDiff;
}
}
}
std::sort(outDistances.begin(), outDistances.end(),
std::greater< std::pair<double,unsigned int> >());
Beagle_StackTraceEndM();
}
开发者ID:GhostGambler,项目名称:beagle,代码行数:36,代码来源:NSGA2Op.cpp
示例2: selectIndividual
/*!
* \brief Select an individual in a population randomly (using an uniform distribution).
* \param ioPool Pool of individuals to use for selection.
* \param ioContext Evolutionary context.
*/
unsigned int SelectRandomOp::selectIndividual(Individual::Bag& ioPool, Context& ioContext)
{
Beagle_StackTraceBeginM();
if(ioPool.size() < 2) return 0;
return ioContext.getSystem().getRandomizer().rollInteger(0, ioPool.size()-1);
Beagle_StackTraceEndM("unsigned int SelectRandomOp::selectIndividual(Individual::Bag& ioPool, Context& ioContext)");
}
开发者ID:splodginald,项目名称:MPI-PACC-OpenBeagle,代码行数:12,代码来源:SelectRandomOp.cpp
示例3: lWeights
/*!
* \brief Recombine individuals by weighted mean to generate a new individual.
* \param inIndivPool Parents being recombined.
* \param ioContext Evolutionary context.
* \return Children generated by recombination.
*/
Individual::Handle SAES::RecombinationWeightedOp::recombine(Individual::Bag& inIndivPool,
Context& ioContext)
{
Beagle_StackTraceBeginM();
// Compute recombination weights.
std::vector<double> lWeights(inIndivPool.size());
for(unsigned int i=0; i<lWeights.size(); ++i) {
lWeights[i] = std::log(double(lWeights.size()+1));
lWeights[i] -= std::log(double(i+1));
}
// Recombine parents to generate new individual.
const Factory& lFactory = ioContext.getSystem().getFactory();
Individual::Alloc::Handle lIndivAlloc =
castHandleT<Individual::Alloc>(lFactory.getConceptAllocator("Individual"));
Genotype::Alloc::Handle lGenotypeAlloc =
castHandleT<Genotype::Alloc>(lFactory.getConceptAllocator("Genotype"));
Individual::Handle lChildIndiv = castHandleT<Individual>(lIndivAlloc->allocate());
std::vector< std::vector<double> > lCountGenoSum;
for(unsigned int i=0; i<inIndivPool.size(); ++i) {
const unsigned int lPoolISize = inIndivPool[i]->size();
const unsigned int lChildSize = lChildIndiv->size();
if(lPoolISize > lChildSize) {
lCountGenoSum.resize(lPoolISize);
lChildIndiv->resize(lPoolISize);
for(unsigned int j=lChildSize; j<lPoolISize; ++j) {
(*lChildIndiv)[j] = castHandleT<Genotype>(lGenotypeAlloc->allocate());
}
}
for(unsigned int j=0; j<lPoolISize; ++j) {
SAES::PairVector::Handle lChildGenoJ = castHandleT<SAES::PairVector>((*lChildIndiv)[j]);
SAES::PairVector::Handle lPoolIGenoJ = castHandleT<SAES::PairVector>((*inIndivPool[i])[j]);
const unsigned int lPoolIGenoJSize = lPoolIGenoJ->size();
if(lPoolIGenoJSize > lChildGenoJ->size()) {
lChildGenoJ->resize(lPoolIGenoJSize,0.0);
lCountGenoSum[j].resize(lPoolIGenoJSize,0);
}
for(unsigned int k=0; k<lPoolIGenoJSize; ++k) {
(*lChildGenoJ)[k].mValue += (lWeights[i] * (*lPoolIGenoJ)[k].mValue);
(*lChildGenoJ)[k].mStrategy += (lWeights[i] * (*lPoolIGenoJ)[k].mStrategy);
lCountGenoSum[j][k] += lWeights[i];
}
}
}
for(unsigned int i=0; i<lChildIndiv->size(); ++i) {
SAES::PairVector::Handle lChildGenoI = castHandleT<SAES::PairVector>((*lChildIndiv)[i]);
for(unsigned int j=0; j<lChildGenoI->size(); ++j) {
(*lChildGenoI)[j].mValue /= lCountGenoSum[i][j];
(*lChildGenoI)[j].mStrategy /= lCountGenoSum[i][j];
}
}
Beagle_LogDebugM(ioContext.getSystem().getLogger(), *lChildIndiv);
return lChildIndiv;
Beagle_StackTraceEndM();
}
开发者ID:AngelGate,项目名称:beagle,代码行数:65,代码来源:RecombinationWeightedOp.cpp
示例4: operate
/*!
* \brief Apply the operation on a deme in the given context.
* \param ioDeme Reference to the deme on which the operation takes place.
* \param ioContext Evolutionary context of the operation.
*/
void GenerationalOp::operate(Deme& ioDeme, Context& ioContext)
{
Beagle_StackTraceBeginM();
Beagle_NonNullPointerAssertM(getRootNode());
Beagle_NonNullPointerAssertM(mElitismKeepSize);
Beagle_ValidateParameterM(mElitismKeepSize->getWrappedValue() <= ioDeme.size(),
"ec.elite.keepsize",
"The elistism keepsize must be less than the deme size!");
Beagle_LogTraceM(
ioContext.getSystem().getLogger(),
"Processing using generational replacement strategy the " <<
uint2ordinal(ioContext.getDemeIndex()+1) << " deme"
);
Beagle_LogTraceM(ioContext.getSystem().getLogger(), (*this));
RouletteT<unsigned int> lRoulette;
buildRoulette(lRoulette, ioContext);
Individual::Bag lOffsprings;
const Factory& lFactory = ioContext.getSystem().getFactory();
if(mElitismKeepSize->getWrappedValue() > 0) {
History::Handle lHistory = castHandleT<History>(ioContext.getSystem().haveComponent("History"));
std::make_heap(ioDeme.begin(), ioDeme.end(), IsLessPointerPredicate());
for(unsigned int i=0; i<mElitismKeepSize->getWrappedValue(); ++i) {
std::string lIndividualType = ioDeme[0]->getType();
Individual::Alloc::Handle lIndividualAlloc =
castHandleT<Individual::Alloc>(lFactory.getAllocator(lIndividualType));
Individual::Handle lEliteIndiv = castHandleT<Individual>(lIndividualAlloc->allocate());
lEliteIndiv->copy(*ioDeme[0], ioContext.getSystem());
lOffsprings.push_back(lEliteIndiv);
if(lHistory != NULL) {
HistoryID::Handle lHID = castHandleT<HistoryID>(ioDeme[0]->getMember("HistoryID"));
std::vector<HistoryID> lParent;
if(lHID != NULL) lParent.push_back(*lHID);
lHistory->allocateNewID(*lEliteIndiv);
lHistory->trace(ioContext, lParent, lEliteIndiv, getName(), "elitism");
}
std::pop_heap(ioDeme.begin(), (ioDeme.end()-i), IsLessPointerPredicate());
}
}
for(unsigned int i=mElitismKeepSize->getWrappedValue(); i<ioDeme.size(); ++i) {
unsigned int lIndexBreeder = lRoulette.select(ioContext.getSystem().getRandomizer());
BreederNode::Handle lSelectedBreeder=getRootNode();
for(unsigned int j=0; j<lIndexBreeder; ++j)
lSelectedBreeder=lSelectedBreeder->getNextSibling();
Beagle_NonNullPointerAssertM(lSelectedBreeder);
Beagle_NonNullPointerAssertM(lSelectedBreeder->getBreederOp());
Individual::Handle lBredIndiv =
lSelectedBreeder->getBreederOp()->breed(ioDeme, lSelectedBreeder->getFirstChild(), ioContext);
Beagle_NonNullPointerAssertM(lBredIndiv);
lOffsprings.push_back(lBredIndiv);
}
for(unsigned int j=0; j<lOffsprings.size(); ++j) ioDeme[j] = lOffsprings[j];
Beagle_StackTraceEndM();
}
开发者ID:falcong,项目名称:beagle-1,代码行数:63,代码来源:GenerationalOp.cpp
示例5: operate
void LogIndividualDataOp::operate(Deme& ioDeme, Context& ioContext) {
Beagle_StackTraceBeginM();
if(mNumberIndividualPerDem->getWrappedValue() <= 0) {
return;
}
// Temporary buffer of individuals.
Individual::Bag lTempPop;
if(ioContext.getGeneration() != mGenerationCalculated) {
mGenerationCalculated = ioContext.getGeneration();
mNbDemesCalculated = 0;
}
if(++mNbDemesCalculated == mPopSize->size() && mOnlyVivarium->getWrappedValue()) {
//Make heap of all individual in the vivarium
for( unsigned int i = 0; i < ioContext.getVivarium().size(); ++i) {
lTempPop.insert(lTempPop.end(), ioContext.getVivarium()[i]->begin(), ioContext.getVivarium()[i]->end());
}
} else if(mOnlyVivarium->getWrappedValue()){
return;
} else {
//Process only this deme
// Insert pointer of all the individuals of the deme in the buffer.
lTempPop.insert(lTempPop.end(), ioDeme.begin(), ioDeme.end());
}
// Make the buffer a STL heap with the fittest individual on the top.
std::make_heap(lTempPop.begin(), lTempPop.end(), IsLessPointerPredicate());
for(unsigned int i = 0; i < mNumberIndividualPerDem->getWrappedValue(); ++i) {
Individual::Handle lIndividual = NULL;
if( !mKeepData->getWrappedValue() ) {
//Strip the simulation data of the individual
lIndividual = castHandleT<Individual>(ioDeme.getTypeAlloc()->cloneData(*lTempPop[0]));
LogFitness::Handle lFitness = castHandleT<LogFitness>(lIndividual->getFitness());
lFitness->clearData();
} else {
lIndividual = lTempPop[0];
}
Beagle_LogObjectM(
ioContext.getSystem().getLogger(),
Logger::eStats,
"history", "Beagle::LogIndividualDataOp",
*lIndividual
);
// STL heap pop of the best individual of the temporary buffer.
std::pop_heap(lTempPop.begin(), lTempPop.end(), IsLessPointerPredicate());
lTempPop.pop_back();
}
Beagle_StackTraceEndM("void LogIndividualDataOp::operate(Deme& ioDeme, Context& ioContext)");
}
开发者ID:ComputationalIntelligenceAndMechatronics,项目名称:HBGGP,代码行数:58,代码来源:LogIndividualDataOp.cpp
示例6: selectNIndividuals
/*!
* \brief Select inN first individuals.
* \param inN Number of individuals to select.
* \param ioPool Pool from which the individuals are choosen.
* \param ioContext Evolutionary context.
* \param outSelections Vector of unsigned ints that say how often an individual was selected.
*
* The output of this method is via outSelection. It produces a
* vector the same size as ioPool, where each index says how many
* times an individual was selected. If this is not the desired
* output, consider using the method convertToList().
*/
void SelectFirstOp::selectNIndividuals(unsigned int inN,
Individual::Bag& ioPool,
Context& ioContext,
std::vector<unsigned int>& outSelections)
{
Beagle_StackTraceBeginM();
const unsigned int lDivNP = inN / ioPool.size();
const unsigned int lModNP = inN % ioPool.size();
outSelections.resize(ioPool.size());
for(unsigned int i=0; i<ioPool.size(); ++i) {
outSelections[i] = (i<lModNP) ? lDivNP+1 : lDivNP;
}
Beagle_StackTraceEndM("void SelectFirstOp::selectNIndividuals(unsigned int,Individual::Bag&,Context&,std::vector<unsigned int>&)");
}
开发者ID:splodginald,项目名称:MPI-PACC-OpenBeagle,代码行数:26,代码来源:SelectFirstOp.cpp
示例7: selectOneIndividual
/*!
* \brief Select an individual using the tournament selection method.
* \param ioPool Individual pool to use for selection.
* \param ioContext Context of the evolution.
* \return Index of the choosen individual in the pool.
*/
unsigned int SelectTournamentOp::selectOneIndividual(Individual::Bag& ioPool, Context& ioContext)
{
Beagle_StackTraceBeginM();
Beagle_ValidateParameterM(mNumberParticipants->getWrappedValue() > 0,
"ec.sel.tournsize", ">0");
unsigned int lChoosenIndividual =
ioContext.getSystem().getRandomizer().rollInteger(0,((unsigned int)ioPool.size())-1);
Beagle_LogDebugM(
ioContext.getSystem().getLogger(),
std::string("Starting by choosing the ")+
uint2ordinal(lChoosenIndividual+1)+" individual"
);
for(unsigned int j=1; j<mNumberParticipants->getWrappedValue(); j++) {
unsigned int lTriedIndividual =
ioContext.getSystem().getRandomizer().rollInteger(0,((unsigned int)ioPool.size())-1);
if(ioPool[lChoosenIndividual]->isLess(*ioPool[lTriedIndividual])) {
lChoosenIndividual = lTriedIndividual;
Beagle_LogDebugM(
ioContext.getSystem().getLogger(),
std::string("Trying the ")+uint2ordinal(lTriedIndividual+1)+
" individual -> choosing it"
);
} else {
Beagle_LogDebugM(
ioContext.getSystem().getLogger(),
std::string("Trying the ")+uint2ordinal(lTriedIndividual+1)+
" individual -> the previously choosen one is better"
);
}
}
Beagle_LogDebugM(
ioContext.getSystem().getLogger(),
"Selecting the " << uint2ordinal(lChoosenIndividual+1) << " individual"
);
Beagle_LogDebugM(ioContext.getSystem().getLogger(), *ioPool[lChoosenIndividual]);
return lChoosenIndividual;
Beagle_StackTraceEndM();
}
开发者ID:AngelGate,项目名称:beagle,代码行数:48,代码来源:SelectTournamentOp.cpp
示例8: selectIndividual
/*!
* \brief Select best individual of a pool of individuals.
* \param ioPool Pool of individuals to use for selection.
* \param ioContext Evolutionary context.
*/
unsigned int SelectBestOp::selectIndividual(Individual::Bag& ioPool, Context& ioContext)
{
Beagle_StackTraceBeginM();
unsigned int lBestIndex = 0;
for(unsigned int i=1; i<ioPool.size(); ++i) {
if(ioPool[lBestIndex]->isLess(*ioPool[i])) {
lBestIndex = i;
}
}
return lBestIndex;
Beagle_StackTraceEndM("unsigned int SelectBestOp::selectIndividual(Individual::Bag&,Context&)");
}
开发者ID:splodginald,项目名称:MPI-PACC-OpenBeagle,代码行数:17,代码来源:SelectBestOp.cpp
示例9: lSortedPop
/*!
* \brief Apply NSGA2 multiobjective selection operator as a standard operator.
* \param ioDeme Deme on which selection operator is applied.
* \param ioContext Evolutionary context.
*/
void EMO::NSGA2Op::applyAsStandardOperator(Deme& ioDeme, Context& ioContext)
{
Beagle_StackTraceBeginM();
// Fast non-dominated sorting, followed by insertion of the first Pareto fronts.
Beagle_LogVerboseM(
ioContext.getSystem().getLogger(),
"Applying fast non-dominated sorting on the whole population"
);
NSGA2Op::Fronts lParetoFronts;
const unsigned int lDesiredPopSize = (*mPopSize)[ioContext.getDemeIndex()];
Individual::Bag lSortedPop(ioDeme);
sortFastND(lParetoFronts, lDesiredPopSize, lSortedPop, ioContext);
unsigned int lIndexDeme=0;
for(unsigned int j=0; j<(lParetoFronts.size()-1); ++j) {
for(unsigned int k=0; k<lParetoFronts[j].size(); ++k) {
ioDeme[lIndexDeme++] = lSortedPop[lParetoFronts[j][k]];
}
}
// Insertion of the last Pareto front, using crowding distance
Individual::Bag lLastFrontIndiv;
for(unsigned int l=0; l<lParetoFronts.back().size(); ++l) {
lLastFrontIndiv.push_back(lSortedPop[lParetoFronts.back()[l]]);
}
NSGA2Op::Distances lDistances;
Beagle_LogVerboseM(
ioContext.getSystem().getLogger(),
"Computing crowding distance on the " << uint2ordinal(lParetoFronts.size()) <<
" Pareto front, which is made of " << uint2ordinal(lParetoFronts.back().size()) << " individuals"
);
evalCrowdingDistance(lDistances, lLastFrontIndiv);
for(unsigned int m=0; lIndexDeme<lDesiredPopSize; ++m) {
ioDeme[lIndexDeme++] = lLastFrontIndiv[lDistances[m].second];
}
ioDeme.resize(lDesiredPopSize);
Beagle_StackTraceEndM();
}
开发者ID:GhostGambler,项目名称:beagle,代码行数:42,代码来源:NSGA2Op.cpp
示例10:
/*!
* \brief Evaluate niche count of an individual over a pool of individuals.
* \param inEvalIndividual Individual for which we need to evaluate crowding distance.
* \param inIndividualPool Pool of individuals to evaluate distance on.
* \return Niche count value.
*/
float NPGA2Op::evalNicheCount(const Individual& inEvalIndividual,
const Individual::Bag& inIndividualPool) const
{
Beagle_StackTraceBeginM();
double lNicheCount = 0.;
const Fitness::Handle lEvalFitness = inEvalIndividual.getFitness();
for(unsigned int i=0; i<inIndividualPool.size(); ++i) {
float lDistance = lEvalFitness->getDistance(*inIndividualPool[i]->getFitness());
if(lDistance < mNicheRadius->getWrappedValue()) {
lNicheCount += (1.0 - (lDistance / mNicheRadius->getWrappedValue()));
}
}
return lNicheCount;
Beagle_StackTraceEndM();
}
开发者ID:AngelGate,项目名称:beagle,代码行数:21,代码来源:NPGA2Op.cpp
示例11: evaluate
/*!
* \brief Evaluate the fitness of the given individual.
* \param inIndividual Current individual to evaluate.
* \param ioContext Evolutionary context.
* \return Handle to the fitness value of the individual.
*
* This method evaluates only a single individual. For this
* operator, it is much better to group individuals into a bag and
* pass the bag to evaluate(Individual::Bag& ioIndividuals, Context::Bag& ioContexts).
*/
Fitness::Handle EvaluationMultipleOp::evaluate(Individual& inIndividual, Context& ioContext)
{
Beagle_StackTraceBeginM();
// Create a bag for the individual
Individual::Bag lIndividuals;
lIndividuals.resize(1);
Beagle_AssertM(inIndividual == ioContext.getIndividual());
lIndividuals[0] = ioContext.getIndividualHandle();
// Create a context for the individual
Context::Bag lContexts;
lContexts.resize(1);
Context::Alloc::Handle lContextAlloc =
castHandleT<Context::Alloc>(ioContext.getSystem().getFactory().getConceptAllocator("Context"));
lContexts[0] = castHandleT<Context>(lContextAlloc->clone(ioContext));
// Call evalutate()
Fitness::Bag::Handle lFitnessBag = evaluateIndividuals(lIndividuals,lContexts);
// Return fitness
Beagle_AssertM( !lFitnessBag->empty() );
return lFitnessBag->at(0);
Beagle_StackTraceEndM();
}
开发者ID:AngelGate,项目名称:beagle,代码行数:34,代码来源:EvaluationMultipleOp.cpp
示例12: double
/*!
* \brief Recombine individuals by averaging to generate a new individual.
* \param inIndivPool Parents being recombined.
* \param ioContext Evolutionary context.
* \return Children generated by recombination.
*/
Individual::Handle Beagle::GA::RecombinationESVecOp::recombine(Individual::Bag& inIndivPool,
Context& ioContext)
{
Beagle_StackTraceBeginM();
// Recombine parents to generate new individual.
const Factory& lFactory = ioContext.getSystem().getFactory();
Individual::Alloc::Handle lIndivAlloc =
castHandleT<Individual::Alloc>(lFactory.getConceptAllocator("Individual"));
Genotype::Alloc::Handle lGenotypeAlloc =
castHandleT<Genotype::Alloc>(lFactory.getConceptAllocator("Genotype"));
Individual::Handle lChildIndiv = castHandleT<Individual>(lIndivAlloc->allocate());
std::vector< std::vector<unsigned int> > lCountGenoSum;
for(unsigned int i=0; i<inIndivPool.size(); ++i) {
const unsigned int lPoolISize = inIndivPool[i]->size();
const unsigned int lChildSize = lChildIndiv->size();
if(lPoolISize > lChildSize) {
lCountGenoSum.resize(lPoolISize);
lChildIndiv->resize(lPoolISize);
for(unsigned int j=lChildSize; j<lPoolISize; ++j) {
(*lChildIndiv)[j] = castHandleT<Genotype>(lGenotypeAlloc->allocate());
}
}
for(unsigned int j=0; j<lPoolISize; ++j) {
GA::ESVector::Handle lChildGenoJ = castHandleT<GA::ESVector>((*lChildIndiv)[j]);
GA::ESVector::Handle lPoolIGenoJ = castHandleT<GA::ESVector>((*inIndivPool[i])[j]);
const unsigned int lPoolIGenoJSize = lPoolIGenoJ->size();
if(lPoolIGenoJSize > lChildGenoJ->size()) {
lChildGenoJ->resize(lPoolIGenoJSize,0.0);
lCountGenoSum[j].resize(lPoolIGenoJSize,0);
}
for(unsigned int k=0; k<lPoolIGenoJSize; ++k) {
(*lChildGenoJ)[k].mValue += (*lPoolIGenoJ)[k].mValue;
(*lChildGenoJ)[k].mStrategy += (*lPoolIGenoJ)[k].mStrategy;
++lCountGenoSum[j][k];
}
}
}
for(unsigned int i=0; i<lChildIndiv->size(); ++i) {
GA::ESVector::Handle lChildGenoI = castHandleT<GA::ESVector>((*lChildIndiv)[i]);
for(unsigned int j=0; j<lChildGenoI->size(); ++j) {
(*lChildGenoI)[j].mValue /= double(lCountGenoSum[i][j]);
(*lChildGenoI)[j].mStrategy /= double(lCountGenoSum[i][j]);
}
}
Beagle_LogDebugM(
ioContext.getSystem().getLogger(),
"crossover", "Beagle::GA::RecombinationESVecOp",
"Individual generated by recombination"
);
Beagle_LogObjectDebugM(
ioContext.getSystem().getLogger(),
"crossover", "Beagle::GA::RecombinationESVecOp",
*lChildIndiv
);
return lChildIndiv;
Beagle_StackTraceEndM("Individual::Handle Beagle::GA::RecombinationESVecOp::recombine(Individual::Bag& inIndivPool,Context& ioContext)");
}
开发者ID:splodginald,项目名称:MPI-PACC-OpenBeagle,代码行数:67,代码来源:RecombinationESVecOp.cpp
示例13: enlargeGroup
/*!
* \brief Add individuals to the bag such that the total equals getIndisPerGroup().
* \param ioIndividuals Bag of individuals to evaluate.
* \param ioContexts Bag of evolutionary context.
* \return The number of individuals added to the bag.
*
* The new individuals are chosen from the current deme.
*/
unsigned int EvaluationMultipleOp::enlargeGroup(Individual::Bag& ioIndividuals,
Context::Bag& ioContexts)
{
Beagle_StackTraceBeginM();
Context& lContext = castObjectT<Context&>(*(ioContexts[0]));
// Calculate the number of individuals to add
unsigned int lNumToAdd = mIndisPerGroup - ioIndividuals.size();
Beagle_LogVerboseM(
lContext.getSystem().getLogger(),
std::string("Adding ")+uint2str(lNumToAdd)+std::string(" individuals to the group (for padding)")
);
// Resize the bags
unsigned int lIndisCounter = ioIndividuals.size();
ioIndividuals.resize(mIndisPerGroup);
ioContexts.resize(mIndisPerGroup);
// Loop through all the individuals in the deme
Deme& lDeme = lContext.getDeme();
std::vector<unsigned int> lSelectableIndis;
lSelectableIndis.resize(lDeme.size());
unsigned int lSelectableIndisCounter = 0;
for (unsigned int i=0; i<lDeme.size(); i++) {
// Loop through all the individuals in the bag
bool lAdd = true;
for(unsigned int j=0; j<ioIndividuals.size(); j++) {
if(lDeme[i] == ioIndividuals[j]) {
lAdd = false;
break;
}
}
// If the individual is not already in the bag, add it as an option
if(lAdd) {
lSelectableIndis[lSelectableIndisCounter] = i;
lSelectableIndisCounter++;
}
}
// Check there are sufficient individuals to choose
if(lSelectableIndis.size() < lNumToAdd) {
throw Beagle_RunTimeExceptionM("There are insufficient individuals in the deme to perform evaluation");
}
// Add individuals
for(unsigned int i=0; i<lNumToAdd; i++) {
unsigned int lIndex =
lContext.getSystem().getRandomizer().rollInteger(0,lSelectableIndisCounter-1);
unsigned int lIndiIndex = lSelectableIndis[lIndex];
Beagle_LogVerboseM(
lContext.getSystem().getLogger(),
std::string("Adding ")+uint2ordinal(lIndiIndex+1)+
std::string(" individual to the group (for padding)")
);
Beagle_AssertM(lIndiIndex < lDeme.size());
ioIndividuals[lIndisCounter] = lDeme[ lIndiIndex ];
Context::Alloc::Handle lContextAlloc =
castHandleT<Context::Alloc>(lContext.getSystem().getFactory().getConceptAllocator("Context"));
ioContexts[lIndisCounter] = castHandleT<Context>(lContextAlloc->clone(*(ioContexts[0])));
ioContexts[lIndisCounter]->setIndividualHandle( ioIndividuals[lIndisCounter] );
ioContexts[lIndisCounter]->setIndividualIndex( lIndiIndex );
lIndisCounter++;
}
Beagle_AssertM( lIndisCounter==ioIndividuals.size() );
return lNumToAdd;
Beagle_StackTraceEndM();
}
开发者ID:AngelGate,项目名称:beagle,代码行数:76,代码来源:EvaluationMultipleOp.cpp
示例14: operate
/*!
* \brief Apply the evaluation process on the invalid individuals of the deme.
* \param ioDeme Deme to process.
* \param ioContext Context of the evolution.
*/
void EvaluationMultipleOp::operate(Deme& ioDeme, Context& ioContext)
{
Beagle_StackTraceBeginM();
Beagle_LogTraceM(
ioContext.getSystem().getLogger(),
"Evaluating the fitness of the individuals in the " <<
uint2ordinal(ioContext.getDemeIndex()+1) << " deme"
);
Beagle_AssertM( ioDeme.size()!=0 );
// Prepare stats
prepareStats(ioDeme,ioContext);
// Generate a vector of indicies into the population
std::vector<unsigned int> lEvalVector;
for(unsigned int i=0; i<ioDeme.size(); i++) {
if((ioDeme[i]->getFitness() == NULL) ||
(ioDeme[i]->getFitness()->isValid() == false)) {
lEvalVector.push_back(i);
Beagle_LogDebugM(
ioContext.getSystem().getLogger(),
"Added " << uint2ordinal(i+1) << " individual for evaluation."
);
}
}
std::random_shuffle(lEvalVector.begin(), lEvalVector.end(),
ioContext.getSystem().getRandomizer());
Beagle_LogDebugM(
ioContext.getSystem().getLogger(),
"There are " << lEvalVector.size() << " individuals to be evaluated."
);
History::Handle lHistory = castHandleT<History>(ioContext.getSystem().haveComponent("History"));
while ( !lEvalVector.empty() ) {
// Put individuals and context into bags.
Individual::Bag lIndividuals;
Context::Bag lContexts;
lIndividuals.resize( mIndisPerGroup );
lContexts.resize( mIndisPerGroup );
unsigned int lIndiCounter =0;
for (unsigned int i=0; i<mIndisPerGroup; i++) {
// Set individual
lIndividuals[i] = ioDeme[lEvalVector.back()];
lIndiCounter++;
// Set context
Context::Alloc::Handle lContextAlloc =
castHandleT<Context::Alloc>(ioContext.getSystem().getFactory().getConceptAllocator("Context"));
Context::Handle lContext = castHandleT<Context>(lContextAlloc->clone(ioContext));
lContext->setIndividualIndex( lEvalVector.back() );
lContext->setIndividualHandle( ioDeme[lEvalVector.back()] );
lContexts[i] = lContext;
// Remove this index from the evaluation vector
lEvalVector.pop_back();
if(lEvalVector.empty()) {
lIndividuals.resize( lIndiCounter );
lContexts.resize( lIndiCounter );
break;
}
}
// Evaluate individuals
std::ostringstream lOSS;
lOSS << "Evaluating the fitness of the ";
for(unsigned int i=0; i<lIndiCounter; i++) {
// Add to message
if (i==lIndiCounter-1) lOSS << " and ";
lOSS << uint2ordinal(lContexts[i]->getIndividualIndex()+1);
if (i<lIndiCounter-2) lOSS << ", ";
}
lOSS << " individuals";
Beagle_LogVerboseM(
ioContext.getSystem().getLogger(),
lOSS.str()
);
Fitness::Bag::Handle lFitnessBag = evaluateIndividuals(lIndividuals, lContexts);
// Assign fitnesses
for (unsigned int i=0; i<lIndiCounter; i++) {
Beagle_LogDebugM(
ioContext.getSystem().getLogger(),
"Considering fitness of the " << uint2ordinal(lContexts[i]->getIndividualIndex()+1) << " individual"
);
Beagle_AssertM( i < lFitnessBag->size() );
Fitness::Handle lFitness = lFitnessBag->at(i);
Beagle_NonNullPointerAssertM( lFitness );
lIndividuals[i]->setFitness( lFitness );
lIndividuals[i]->getFitness()->setValid();
if(lHistory != NULL) {
lHistory->allocateID(*lIndividuals[i]);
lHistory->trace(ioContext, std::vector<HistoryID>(), lIndividuals[i], getName(), "evaluation");
}
//.........这里部分代码省略.........
开发者ID:AngelGate,项目名称:beagle,代码行数:101,代码来源:EvaluationMultipleOp.cpp
示例15: operate
/*!
* \brief Apply the decimation operation on the deme.
* \param ioDeme Current deme of individuals to decimate.
* \param ioContext Context of the evolution.
*/
void DecimateOp::operate(Deme& ioDeme, Context& ioContext)
{
Beagle_StackTraceBeginM();
Beagle_ValidateParameterM((mDecimationRatio->getWrappedValue()<=1.0),
mDecimationRatioName,
"The decimation ratio must be less than or equal to 1.0.");
Beagle_LogTraceM(
ioContext.getSystem().getLogger(),
"decimation", "Beagle::DecimateOp",
std::string("Applying decimation operation on the ")+
uint2ordinal(ioContext.getDemeIndex()+1)+" deme"
);
// Calculate the number of individuals to keep from the deme
unsigned int lMu = 0;
if(mDecimationRatio->getWrappedValue() == -1.0) {
Beagle_AssertM(ioContext.getDemeIndex() < mPopSize->size());
lMu = (*mPopSize)[ioContext.getDemeIndex()];
} else {
lMu = (unsigned int)std::ceil(mDecimationRatio->getWrappedValue()*float(ioDeme.size()));
Beagle_AssertM(ioContext.getDemeIndex() < mPopSize->size());
int lDiffSize = (*mPopSize)[ioContext.getDemeIndex()] - lMu;
if((lDiffSize >= -1) && (lDiffSize <= 1)) lMu = (*mPopSize)[ioContext.getDemeIndex()];
}
Beagle_LogTraceM(
ioContext.getSystem().getLogger(),
"decimation", "Beagle::DecimateOp",
std::string("Keeping ")+uint2str(lMu)+" of the "+uint2str(ioDeme.size())+
" individuals from the "+uint2ordinal(ioContext.getDemeIndex()+1)+" deme"
);
// Check that the number of individuals to keep (mu) isn't greater than the deme size
if(lMu > ioDeme.size()) {
std::ostringstream lOSS;
lOSS << "Warning: the actual population size (" << ioDeme.size();
lOSS << ") is less than the desired decimation size (" << lMu;
lOSS << "). Decimation is thus not applied.";
Beagle_LogBasicM(
ioContext.getSystem().getLogger(),
"decimation", "Beagle::DecimateOp",
lOSS.str()
);
return;
}
// Copy the individuals to be kept into the deme
std::make_heap(ioDeme.begin(), ioDeme.end(), IsLessPointerPredicate());
Individual::Bag lSurvivors;
for(unsigned int i=0; i<lMu; ++i) {
lSurvivors.push_back(ioDeme.front());
std::pop_heap(ioDeme.begin(), (ioDeme.end()-i), IsLessPointerPredicate());
}
ioDeme.clear();
ioDeme.insert(ioDeme.begin(), lSurvivors.begin(), lSurvivors.end());
Beagle_LogDetailedM(
ioContext.getSystem().getLogger(),
"decimation", "Beagle::DecimateOp",
std::string("There are now ")+uint2str(ioDeme.size())+" individuals in the "+
uint2ordinal(ioContext.getDemeIndex()+1)+" deme"
);
Beagle_StackTraceEndM("void DecimateOp::operate(Deme& ioDeme, Context& ioContext)");
}
开发者ID:splodginald,项目名称:MPI-PACC-OpenBeagle,代码行数:68,代码来源:DecimateOp.cpp
示例16: breed
/*!
* \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();
}
开发者ID:AngelGate,项目名称:beagle,代码行数:69,代码来源:RecombinationOp.cpp
示例17: operate
/*!
* \brief Apply the oversize replacement strategy operation on a deme.
* \param ioDeme Reference to the deme on which the operation takes place.
* \param ioContext Evolutionary context of the operation.
*/
void OversizeOp::operate(Deme& ioDeme, Context& ioContext)
{
Beagle_StackTraceBeginM();
Beagle_NonNullPointerAssertM(getRootNode());
Beagle_ValidateParameterM
(mOversizeRatio->getWrappedValue() >= 1.0
|| mOversizeRatio->getWrappedValue() == -1.0,
mOversizeRatioName,
"The oversize ratio must be greater than or equal to 1.0, or equal to -1.0.");
Beagle_LogTraceM(
ioContext.getSystem().getLogger(),
"replacement-strategy", "Beagle::OversizeOp",
string("Using oversize replacement strategy to process the ")+
uint2ordinal(ioContext.getDemeIndex()+1)+" deme"
);
Beagle_LogObjectM(
ioContext.getSystem().getLogger(),
Logger::eTrace,
"replacement-strategy", "Beagle::OversizeOp",
(*this)
);
RouletteT<unsigned int> lRoulette;
buildRoulette(lRoulette, ioContext);
// Calculate the increase in size (lambda)
float lRatio = mOversizeRatio->getWrappedValue();
unsigned int lLambda;
if (lRatio == -1.0) {
// Using special ratio of -1.0 ensures deme grows to size specified in 'ec.pop.size'
if (!ioContext.getSystem().getRegister().isRegistered("ec.pop.size")) {
throw Beagle_RunTimeExceptionM(getName()+" requires register variable 'ec.pop.size'");
}
UIntArray::Handle lPopSize = castHandleT<UIntArray>
(ioContext.getSystem().getRegister().getEntry("ec.pop.size"));
unsigned int lSpecifiedDemeSize = (*lPopSize)[ioContext.getDemeIndex()];
unsigned int lCurrentDemeSize = ioDeme.size();
if (lSpecifiedDemeSize < lCurrentDemeSize) {
throw Beagle_RunTimeExceptionM
(std::string("For the ")+uint2ordinal(ioContext.getDemeIndex()+1)+
" deme, the size specified in 'ec.pop.size' ("+uint2str(lSpecifiedDemeSize)+
") is less than the current deme size ("+uint2str(lCurrentDemeSize)+
"). "+getName()+" can only increase the size of the deme. Consider using DecimateOp "+
"if you wish to decrease the size of the deme");
}
lLambda = lSpecifiedDemeSize - lCurrentDemeSize;
} else {
// Using ratio to scale the deme's population
lLambda = (unsigned int)ceil((lRatio-1.0)*float(ioDeme.size()));
}
Beagle_LogTraceM(
ioContext.getSystem().getLogger(),
"replacement-strategy", "Beagle::OversizeOp",
string("Population will be increased in size by ")+uint2str(lLambda)+" individuals"
);
// Create the new individuals.
Individual::Bag lOffsprings;
for(unsigned int i=0; i<lLambda; ++i) {
unsigned int lIndexBreeder = lRoulette.select(ioContext.getSystem().getRandomizer());
BreederNode::Handle lSelectedBreeder=getRootNode();
for(unsigned int j=0; j<lIndexBreeder; ++j)
lSelectedBreeder=lSelectedBreeder->getNextSibling();
Beagle_NonNullPointerAssertM(lSelectedBreeder);
Beagle_NonNullPointerAssertM(lSelectedBreeder->getBreederOp());
Individual::Handle lBredIndiv =
lSelectedBreeder->getBreederOp()->breed(ioDeme, lSelectedBreeder->getFirstChild(), ioContext);
Beagle_NonNullPointerAssertM(lBredIndiv);
lOffsprings.push_back(lBredIndiv);
}
// Add the new individuals into the deme.
ioDeme.insert(ioDeme.end(), lOffsprings.begin(), lOffsprings.end());
Beagle_LogDetailedM(
ioContext.getSystem().getLogger(),
"replacement-strategy", "Beagle::OversizeOp",
string("There are now ")+uint2str(ioDeme.size())+" individuals in the "+
uint2ordinal(ioContext.getDemeIndex()+1)+" deme"
);
Beagle_StackTraceEndM("void OversizeOp::operate(Deme& ioDeme, Context& ioContext)");
}
开发者ID:splodginald,项目名称:MPI-PACC-OpenBeagle,代码行数:87,代码来源:OversizeOp.cpp
示例18: selectNIndividuals
/*!
* \brief Select inN best individuals.
* \param inN Number of individuals to select.
* \param ioPool Pool from which the individuals are choosen.
* \param ioContext Evolutionary context.
* \param outSelections Vector of unsigned ints that say how often an individual was selected.
*
* The output of this method is via outSelection. It produces a
* vector the same size as ioPool, where each index says how many
* times an individual was selected. If this is not the desired
* output, consider using the method convertToList().
*/
void SelectBestOp::selectNIndividuals(unsigned int inN,
Individual::Bag& ioPool,
Context& ioContext,
std::vector<unsigned int>& outSelections)
{
Beagle_StackTraceBeginM();
// Check that we're not selecting all the individuals
if((inN%ioPool.size()) == 0) {
Beagle_LogBasicM(
ioContext.getSystem().getLogger(),
"selection", "Beagle::SelectBestOp",
std::string("Warning! Selecting the best ")+uint2str(inN)+" individuals from a pool size of "+
uint2str(ioPool.size())+" (during SelectBestOp) means that every individual will be selected "+
uint2str(inN/ioPool.size())+" times, thus applying no selective pressure."
);
}
// Copy the pool, pairing indices and handles to individuals
Beagle_Lo
|
请发表评论