/*! * \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(); }
/*! * \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(); }
/*! * \brief Apply the breeding operation on a breeding pool, returning a bred individual. * \param inBreedingPool * \param inChild * \param ioContext Evolutionary context of the breeding operation. * \return Bred individual. */ Individual::Handle InitializationOp::breed(Individual::Bag& inBreedingPool, BreederNode::Handle inChild, Context& ioContext) { Beagle_StackTraceBeginM(); const Factory& lFactory = ioContext.getSystem().getFactory(); Individual::Alloc::Handle lIndivAlloc = castHandleT<Individual::Alloc>(lFactory.getConceptAllocator("Individual")); Individual::Handle lNewIndiv = castHandleT<Individual>(lIndivAlloc->allocate()); initIndividual(*lNewIndiv, ioContext); if(lNewIndiv->getFitness() != NULL) lNewIndiv->getFitness()->setInvalid(); History::Handle lHistory = castHandleT<History>(ioContext.getSystem().haveComponent("History")); if(lHistory != NULL) { lHistory->incrementHistoryVar(*lNewIndiv); lHistory->trace(ioContext, std::vector<HistoryID>(), lNewIndiv, getName(), "initialization"); } ioContext.setIndividualHandle(lNewIndiv); return lNewIndiv; Beagle_StackTraceEndM(); }
/*! * \brief Read individuals of a bag. * \param inIter XML iterator to read the individuals from. * \param ioContext Evolutionary context. */ void IndividualBag::readIndividuals(PACC::XML::ConstIterator inIter, Context& ioContext) { Beagle_StackTraceBeginM(); clear(); Individual::Handle lPrevIndivHandle = ioContext.getIndividualHandle(); unsigned int lPrevIndivIndex = ioContext.getIndividualIndex(); for(PACC::XML::ConstIterator lIter=inIter; lIter; ++lIter) { if(lIter->getType() != PACC::XML::eData) continue; if(lIter->getValue() == "NullHandle") { push_back(NULL); continue; } else if(lIter->getValue() != "Individual") continue; Individual::Alloc::Handle lIndivAlloc = NULL; std::string lIndivTypeName = lIter->getAttribute("type"); const Factory& lFactory = ioContext.getSystem().getFactory(); if(lIndivTypeName.empty()) { lIndivAlloc = castHandleT<Individual::Alloc>(lFactory.getConceptAllocator("Individual")); } else { lIndivAlloc = castHandleT<Individual::Alloc>(lFactory.getAllocator(lIndivTypeName)); } Individual::Handle lReadIndiv = castHandleT<Individual>(lIndivAlloc->allocate()); const unsigned int lBackIndex = size(); push_back(lReadIndiv); ioContext.setIndividualHandle(lReadIndiv); ioContext.setIndividualIndex(lBackIndex); lReadIndiv->readWithContext(lIter, ioContext); } ioContext.setIndividualIndex(lPrevIndivIndex); ioContext.setIndividualHandle(lPrevIndivHandle); Beagle_StackTraceEndM(); }
/*! * \brief Apply the initialization operation on the deme. * \param ioDeme Current deme of individuals to initialize. * \param ioContext Context of the evolution. */ void InitializationOp::operate(Deme& ioDeme, Context& ioContext) { Beagle_StackTraceBeginM(); #ifndef BEAGLE_NDEBUG if(ioContext.getVivariumHandle()!=NULL) { Beagle_AssertM(mPopSize->size() == ioContext.getVivarium().size()); } #endif // BEAGLE_NDEBUG Beagle_LogTraceM( ioContext.getSystem().getLogger(), std::string("Initializing the ")+ uint2ordinal(ioContext.getDemeIndex()+1)+" deme" ); if(!ioDeme.empty()) { Beagle_LogBasicM( ioContext.getSystem().getLogger(), std::string("Warning! Applying '")+getName()+"' will overwrite the "+ uint2str(ioDeme.size())+" individual(s) currently in the deme with newly initialized "+ "individuals. If this is not what you desire consider using OversizeOp instead." ); } Beagle_LogTraceM( ioContext.getSystem().getLogger(), std::string("Resizing the deme from ")+ uint2str(ioDeme.size())+" to "+ uint2str((*mPopSize)[ioContext.getDemeIndex()])+" individuals" ); const Factory& lFactory = ioContext.getSystem().getFactory(); Individual::Alloc::Handle lIndividualAlloc = castHandleT<Individual::Alloc>(lFactory.getConceptAllocator("Individual")); ioDeme.clear(); const unsigned int lDemeSize = (*mPopSize)[ioContext.getDemeIndex()]; for(unsigned int i=0; i<lDemeSize; ++i) { ioDeme.push_back(lIndividualAlloc->allocate()); } unsigned int lSeededIndividuals = 0; if(mSeedsFile->getWrappedValue().empty() == false) { Beagle_LogInfoM( ioContext.getSystem().getLogger(), std::string("Reading seeds file '")+mSeedsFile->getWrappedValue()+ std::string("' to initialize the ")+uint2ordinal(ioContext.getDemeIndex()+1)+ std::string(" deme") ); lSeededIndividuals = readSeeds(mSeedsFile->getWrappedValue(), ioDeme, ioContext); Beagle_LogDetailedM( ioContext.getSystem().getLogger(), uint2str(lSeededIndividuals)+std::string(" individuals read to seed the deme") ); } History::Handle lHistory = castHandleT<History>(ioContext.getSystem().haveComponent("History")); Individual::Handle lOldIndividualHandle = ioContext.getIndividualHandle(); unsigned int lOldIndividualIndex = ioContext.getIndividualIndex(); for(unsigned int i=lSeededIndividuals; i<ioDeme.size(); ++i) { Beagle_LogVerboseM( ioContext.getSystem().getLogger(), std::string("Initializing the ")+uint2ordinal(i+1)+" individual" ); ioContext.setIndividualHandle(ioDeme[i]); ioContext.setIndividualIndex(i); initIndividual(*ioDeme[i], ioContext); if(ioDeme[i]->getFitness() != NULL) { ioDeme[i]->getFitness()->setInvalid(); } if(lHistory != NULL) { lHistory->incrementHistoryVar(*ioDeme[i]); lHistory->trace(ioContext, std::vector<HistoryID>(), ioDeme[i], getName(), "initialization"); } } ioContext.setIndividualIndex(lOldIndividualIndex); ioContext.setIndividualHandle(lOldIndividualHandle); Beagle_StackTraceEndM(); }
/*! * \brief Generate children from the breeder tree. * \param ioDeme Deme to generate children from. * \param ioContext Evolutionary context. * \param lNbChildren Number of children to generate. * \param inN Dimensionality of the problem. * \param ioCMAValues CMA values to use to generate new individual. * \param inSelectionWeights Selection weights used to generate children. */ void CMA::MuWCommaLambdaCMAFltVecOp::generateChildren(Deme& ioDeme, Context& ioContext, unsigned int inNbChildren, unsigned int inN, CMAValues& ioCMAValues, const Vector& inSelectionWeights) const { Beagle_StackTraceBeginM(); // Check parameters and log some information Beagle_NonNullPointerAssertM(mElitismKeepSize); Beagle_ValidateParameterM(mLMRatio->getWrappedValue() >= 1.0, mLMRatioName, "The LM ratio must be higher or equal to 1.0."); Beagle_ValidateParameterM(mElitismKeepSize->getWrappedValue() <= ioDeme.size(), "ec.elite.keepsize", "The elistism keepsize must be less than the deme size!"); Beagle_LogTraceM( ioContext.getSystem().getLogger(), std::string("Using CMA-ES (mu_w,lambda) replacement strategy to process the ")+ uint2ordinal(ioContext.getDemeIndex()+1)+" deme" ); Beagle_LogTraceM(ioContext.getSystem().getLogger(), (*this)); const Factory& lFactory = ioContext.getSystem().getFactory(); // Create weighted mean individual. std::sort(ioDeme.begin(), ioDeme.end(), IsMorePointerPredicate()); Individual::Alloc::Handle lIndividualAlloc = castHandleT<Individual::Alloc>(lFactory.getConceptAllocator("Individual")); Individual::Handle lMeanInd = castHandleT<Individual>(lIndividualAlloc->allocate()); Genotype::Alloc::Handle lGenotypeAlloc = castHandleT<Genotype::Alloc>(lFactory.getConceptAllocator("Genotype")); FltVec::FloatVector::Handle lMeanFloatVec = castHandleT<FltVec::FloatVector>(lGenotypeAlloc->allocate()); lMeanFloatVec->resize(inN); lMeanInd->push_back(lMeanFloatVec); for(unsigned int i=0; i<inN; ++i) (*lMeanFloatVec)[i] = 0.0; if(ioDeme.size()==1) { Beagle_AssertM(ioDeme[0]->size() == 1); FltVec::FloatVector::Handle lInd = castHandleT<FltVec::FloatVector>((*ioDeme[0])[0]); (*lMeanFloatVec) = *lInd; } else { for(unsigned int i=0; i<ioDeme.size(); ++i) { Beagle_AssertM(ioDeme[i]->size()==1); FltVec::FloatVector::Handle lVecI = castHandleT<FltVec::FloatVector>((*ioDeme[i])[0]); Beagle_AssertM(lVecI->size()==inN); for(unsigned int j=0; j<inN; ++j) (*lMeanFloatVec)[j] += (inSelectionWeights[i] * (*lVecI)[j]); } } ioCMAValues.mXmean.resize(inN); for(unsigned int i=0; i<inN; ++i) ioCMAValues.mXmean[i] = (*lMeanFloatVec)[i]; // Generate lambda children with breeder tree, first build breeder roulette RouletteT<unsigned int> lRoulette; buildRoulette(lRoulette, ioContext); // Keep best individuals if elitism is used const unsigned int lElitismKS=mElitismKeepSize->getWrappedValue(); if(lElitismKS > 0) { Individual::Bag lBestInd; History::Handle lHistory = castHandleT<History>(ioContext.getSystem().haveComponent("History")); std::make_heap(ioDeme.begin(), ioDeme.end(), IsLessPointerPredicate()); for(unsigned int i=0; i<lElitismKS; ++i) { 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(*ioDeme[0]); lHistory->trace(ioContext, lParent, ioDeme[0], getName(), "elitism"); } lBestInd.push_back(ioDeme[0]); std::pop_heap(ioDeme.begin(), ioDeme.end(), IsLessPointerPredicate()); ioDeme.pop_back(); } ioDeme.clear(); ioDeme.insert(ioDeme.end(), lBestInd.begin(), lBestInd.end()); } else ioDeme.clear(); // Generate the children Individual::Bag lBagWithMeanInd; lBagWithMeanInd.push_back(lMeanInd); for(unsigned int i=0; i<inNbChildren; ++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(lBagWithMeanInd, lSelectedBreeder->getFirstChild(), ioContext); Beagle_NonNullPointerAssertM(lBredIndiv); ioDeme.push_back(lBredIndiv); } Beagle_StackTraceEndM(); }
/*! * \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)"); }
/*! * \brief Send a receive a deme. * * A deme here is more a sub-deme since it only has a portion of * the total individuals of a deme. * \throw Beagle_InternalExceptionM Bad application name received */ void Master::EvalClient::sendAndReceiveDeme() { Beagle_StackTraceBeginM(); Beagle::String::Handle lAppName= castHandleT<Beagle::String>(mContext->getSystem().getRegister().getEntry("db.app.name")); const Factory& lFactory = mContext->getSystem().getFactory(); Individual::Alloc::Handle lIndividualAlloc = castHandleT<Individual::Alloc>(lFactory.getConceptAllocator("Individual")); std::ostringstream lOSS; lOSS << setprecision(10); if(mDemeStored) { double lTimeEval = mTimeStart.getValue()/(double)mDeme->size(); std::ostringstream lOSSTime; lOSSTime << lTimeEval; PACC::XML::Streamer lStreamer(lOSS, 0); lStreamer.openTag("SG"); lStreamer.insertAttribute("id", Beagle::uint2str(mDemeID)); lStreamer.insertAttribute("app", lAppName->getWrappedValue()); lStreamer.insertAttribute("gen", Beagle::uint2str(mGeneration)); lStreamer.insertAttribute("tf", lOSSTime.str()); // Send evaluated scores for(unsigned int i=0; i<mDeme->size(); ++i) { lStreamer.openTag("J"); //Job lStreamer.insertAttribute("id", Beagle::uint2str(mIndivID[i])); //job id lStreamer.openTag("Sc"); //Score lStreamer.insertAttribute("eval", "no"); //evaluation (*mDeme)[i]->getFitness()->write(lStreamer, false); lStreamer.closeTag(); lStreamer.closeTag(); } lStreamer.closeTag(); } std::string lDemeSerialized = lOSS.str(); DAGS::SubGroup lSubGroupDAGS; std::vector< std::pair<std::string,DAGS::Job> > lJobs; std::vector<std::string> lDontParseVector; lDontParseVector.push_back("Dt"); //Data lDontParseVector.push_back("Sc"); //Score lDontParseVector.push_back("Env"); //Environment doNotParseXMLTags(lDontParseVector); mDemeStored = false; while(true) { int lErrorNbr = getJobs(lDemeSerialized, lSubGroupDAGS, lJobs, lAppName->getWrappedValue()); if(lErrorNbr == DAGS::Client::NoError) break; if(lErrorNbr < -17) logError(getErrorMessage()); lDemeSerialized = std::string(""); PACC::Threading::Thread::sleep(1); } mTimeStart.reset(); std::string lObtAppName = lSubGroupDAGS.getAppName(); if(lObtAppName != lAppName->getWrappedValue()) { std::string lMessage = "Bad application name received (desired: '"; lMessage += lAppName->getWrappedValue(); lMessage += "', obtained: '"; lMessage += lObtAppName; lMessage += "')."; throw Beagle_InternalExceptionM(lMessage); } mDemeID = (unsigned int)(lSubGroupDAGS.getGroupId()); mGeneration = (unsigned int)lSubGroupDAGS.getGeneration(); if(mEnvManager != NULL) { std::string lEnvironment = lSubGroupDAGS.getEnvironment(); std::istringstream lEnvIS(lEnvironment); PACC::XML::Document lParserEnv; lParserEnv.parse(lEnvIS, "environmnent"); PACC::XML::Iterator lEnvNode(lParserEnv.getFirstRoot()); if(lEnvNode) { mEnvManager->readEnvironment(lEnvNode, *mDeme, *mContext); } } // Get jobs individual mIndivID.clear(); const unsigned int lOldDemeSize = mDeme->size(); mDeme->resize(lJobs.size()); for(unsigned int i=lOldDemeSize; i<lJobs.size(); ++i) { (*mDeme)[i] = castHandleT<Individual>(lIndividualAlloc->allocate()); } for(unsigned int i=0; i<mDeme->size(); ++i) { mIndivID.push_back(lJobs[i].second.getId()); std::ostringstream lOSS; lOSS << "<Individual>" << lJobs[i].second.getData() << "</Individual>"; std::istringstream lGenoIS(lOSS.str()); PACC::XML::Document lParser; lParser.parse(lGenoIS); PACC::XML::Iterator lRootNode(lParser.getFirstRoot()); (*mDeme)[i]->readWithContext(lRootNode, *mContext); (*mDeme)[i]->setFitness(NULL); } mDemeStored = true; Beagle_StackTraceEndM(); }
/*! * \brief Apply NPGA2 multiobjective selection operator. * \param ioDeme Deme on which selection operator is applied. * \param ioContext Evolutionary context. */ void NPGA2Op::operate(Deme& ioDeme, Context& ioContext) { Beagle_StackTraceBeginM(); if(ioDeme.size() == 0) return; Beagle_LogTraceM( ioContext.getSystem().getLogger(), std::string("Applying NPGA2 multiobjective selection on the ")+ uint2ordinal(ioContext.getDemeIndex()+1)+" deme" ); std::vector<bool> lAlreadySelectedIndiv(ioDeme.size(), false); Individual::Bag lSelectedIndividual; const Factory& lFactory = ioContext.getSystem().getFactory(); // Generate new generation by selection for(unsigned int i=0; i<ioDeme.size(); ++i) { // First participant unsigned int lFirstParticipant = ioContext.getSystem().getRandomizer().rollInteger(0, ioDeme.size()-1); std::vector<unsigned int> lNDParticipants(1, lFirstParticipant); // Following participants to tournament for(unsigned int j=1; j<mNumberParticipants->getWrappedValue(); ++j) { unsigned int lParticipant = ioContext.getSystem().getRandomizer().rollInteger(0, ioDeme.size()-1); // Test if participant is dominated or dominate other participants bool lIsDominated = false; Fitness::Handle lPartFitness = ioDeme[lParticipant]->getFitness(); for(unsigned int k=0; k<lNDParticipants.size(); ++k) { Fitness::Handle lFitnessNDk = ioDeme[lNDParticipants[k]]->getFitness(); if(lPartFitness->isDominated(*lFitnessNDk)) { lIsDominated = true; } else if(lFitnessNDk->isDominated(*lPartFitness)) { lNDParticipants.erase(lNDParticipants.begin()+k); } } if(lIsDominated==false) lNDParticipants.push_back(lParticipant); } // Test if there is a tie. If so evaluate niche count. Beagle_AssertM(lNDParticipants.size() != 0); unsigned int lWinner = lNDParticipants[0]; if(lNDParticipants.size() > 1) { float lLowestNicheCount = evalNicheCount(*ioDeme[lNDParticipants[0]], lSelectedIndividual); for(unsigned int j=1; j<lNDParticipants.size(); ++j) { float lNicheCount = evalNicheCount(*ioDeme[lNDParticipants[j]], lSelectedIndividual); if(lNicheCount < lLowestNicheCount) { lLowestNicheCount = lNicheCount; lWinner = lNDParticipants[j]; } } } // Put winner in selected individual bag if(lAlreadySelectedIndiv[lWinner]) { std::string lIndividualType = ioDeme[lWinner]->getType(); Individual::Alloc::Handle lIndividualAlloc = castHandleT<Individual::Alloc>(lFactory.getAllocator(lIndividualType)); Individual::Handle lIndividual = castHandleT<Individual>(lIndividualAlloc->allocate()); lIndividual->copy(*ioDeme[lWinner], ioContext.getSystem()); lSelectedIndividual.push_back(lIndividual); Beagle_LogVerboseM( ioContext.getSystem().getLogger(), uint2ordinal(lWinner+1)+" individual selected again in NPGA2 selection process" ); } else { lSelectedIndividual.push_back(ioDeme[lWinner]); lAlreadySelectedIndiv[lWinner] = true; Beagle_LogVerboseM( ioContext.getSystem().getLogger(), uint2ordinal(lWinner+1)+" individual selected in NPGA2 selection process" ); } } // Copy selected individuals into deme for(unsigned int j=0; j<ioDeme.size(); ++j) ioDeme[j] = lSelectedIndividual[j]; Beagle_StackTraceEndM(); }