/*!
 *  \brief Receive the fitness of an individuals group from evaluator node.
 *  \param ioDeme Deme to update by the receive fitness.
 *  \param ioContext Current context of the evolution.
 */
void HPC::RecvFitnessFromEvaluatorOp::operate(Deme& ioDeme, Context& ioContext)
{
	Beagle_StackTraceBeginM();

	ProcessingBuffer::Handle lBuffer = castHandleT<ProcessingBuffer>(ioDeme.getMember("ProcessingBuffer"));
	Beagle_NonNullPointerAssertM(lBuffer);

	Individual::Handle lOldIndividualHandle = ioContext.getIndividualHandle();
	unsigned int lOldIndividualIndex = ioContext.getIndividualIndex();

	prepareStats(ioDeme,ioContext);

	unsigned int lNbEvaluators = mComm->getNbrOfRelation("Child");

	unsigned int lNbIndividualInt = lBuffer->size() / lNbEvaluators;
	unsigned int lNbIndividualFrac = lBuffer->size() % lNbEvaluators;

	unsigned int lNbIndividualTotal = 0;

	for(int i = 0; i < lNbEvaluators; ++i ){
		int lNbIndividual = lNbIndividualInt;
		if(i < lNbIndividualFrac)
			++lNbIndividual;
		std::string lFitnessString;

		mComm->receive(lFitnessString, "Fitness", "Child", i);

		Beagle_LogDetailedM(
			ioContext.getSystem().getLogger(),
			"receive", "Beagle::HPC::RecvFitnessFromEvaluatorOp",
			std::string("Evolver receive fitness from his ")+uint2ordinal(i+1)+
			std::string(" evaluator") + std::string(" of ") + uint2str(lNbEvaluators)
		);

		std::istringstream lInStream(lFitnessString);
		PACC::XML::Document lDocument(lInStream);

		const Factory& lFactory = ioContext.getSystem().getFactory();
		Fitness::Alloc::Handle lFitnessAlloc =
				castHandleT<Fitness::Alloc>(lFactory.getConceptAllocator("Fitness"));
		for(PACC::XML::ConstIterator lIter = lDocument.getFirstRoot()->getFirstChild(); lIter; ++lIter){
			ioContext.setIndividualIndex(lNbIndividualTotal);
			ioContext.setIndividualHandle(ioDeme[lNbIndividualTotal]);
			if((*lBuffer)[lNbIndividualTotal]->getFitness() == 0)
				(*lBuffer)[lNbIndividualTotal]->setFitness( castHandleT<Fitness>(lFitnessAlloc->allocate()) );
			(*lBuffer)[lNbIndividualTotal]->getFitness()->read(lIter);
			++lNbIndividualTotal;
			updateStats(1,ioContext);
		}
	}
	Beagle_LogDetailedM(
		ioContext.getSystem().getLogger(),
		"receive", "Beagle::HPC::RecvFitnessFromEvaluatorOp",
		std::string("Evolver receive all its fitness.")
	);

	ioContext.setIndividualIndex(lOldIndividualIndex);
	ioContext.setIndividualHandle(lOldIndividualHandle);
	Beagle_StackTraceEndM("void RecvFitnessFromEvaluatorOp::operate(Deme& ioDeme, Context& ioContext)");
}
Exemplo n.º 2
0
/*!
 *  \brief Copy individual using the system.
 *  \param inOriginal Individual to copy into current.
 *  \param ioSystem Evolutionary system.
 */
void Individual::copy(const Individual& inOriginal, System& ioSystem)
{
	Beagle_StackTraceBeginM();
	const Factory& lFactory = ioSystem.getFactory();

	// Copy members
	mMemberMap.clear();
	for(MemberMap::const_iterator lIterMap=inOriginal.mMemberMap.begin();
	        lIterMap!=inOriginal.mMemberMap.end(); ++lIterMap) {
		Member::Handle lOrigMember = castHandleT<Member>(lIterMap->second);
		const std::string& lMemberType = lOrigMember->getType();
		Member::Alloc::Handle lMemberAlloc =
		    castHandleT<Member::Alloc>(lFactory.getAllocator(lMemberType));
		Member::Handle lMember = castHandleT<Member>(lMemberAlloc->allocate());
		lMember->copy(*lOrigMember, ioSystem);
		mMemberMap[lIterMap->first] = lMember;
	}

	// Copy fitness
	if(inOriginal.getFitness() == NULL) {
		mFitness = NULL;
	} else {
		const std::string& lFitnessType = inOriginal.getFitness()->getType();
		Fitness::Alloc::Handle lFitnessAlloc =
		    castHandleT<Fitness::Alloc>(lFactory.getAllocator(lFitnessType));
		mFitness = castHandleT<Fitness>(lFitnessAlloc->allocate());
		mFitness->copy(*inOriginal.getFitness(), ioSystem);
	}

	// Copy genotypes
	resize(inOriginal.size());
	for(unsigned int i=0; i<inOriginal.size(); ++i) {
		const std::string& lGenotypeType = inOriginal[i]->getType();
		Genotype::Alloc::Handle lGenotypeAlloc =
		    castHandleT<Genotype::Alloc>(lFactory.getAllocator(lGenotypeType));
		(*this)[i] = castHandleT<Genotype>(lGenotypeAlloc->allocate());
		(*this)[i]->copy(*inOriginal[i], ioSystem);
	}

	Beagle_StackTraceEndM();
}
Exemplo n.º 3
0
/*!
 *  \brief Read fitness of an individual from an XML node.
 *  \param inIter XML iterator to read the individual's fitness from.
 *  \param ioContext Evolutionary context.
 *  \throw Beagle::IOException If the format is not respected.
 */
void Individual::readFitness(PACC::XML::ConstIterator inIter, Context& ioContext)
{
	Beagle_StackTraceBeginM();
	const Factory& lFactory = ioContext.getSystem().getFactory();
	for(PACC::XML::ConstIterator lIter=inIter; lIter; ++lIter) {
		if((lIter->getType() != PACC::XML::eData) || (lIter->getValue() != "Fitness")) continue;
		const std::string& lFitnessType = lIter->getAttribute("type");
		Fitness::Alloc::Handle lFitnessAlloc = NULL;
		if(lFitnessType.empty()) {
			const std::string& lFitnessValid = lIter->getAttribute("valid");
			if(lFitnessValid == "no") {
				mFitness = NULL;
				break;
			}
			lFitnessAlloc = castHandleT<Fitness::Alloc>(lFactory.getConceptAllocator("Fitness"));
			if(lFitnessAlloc == NULL) {
				std::ostringstream lOSS;
				lOSS << "Fitness object can't be read, ";
				lOSS << "it appears that its type is not given and that there is not ";
				lOSS << "valid concept allocator associated to it!";
				throw Beagle_IOExceptionNodeM(*lIter, lOSS.str());
			}
		} else {
			lFitnessAlloc = castHandleT<Fitness::Alloc>(lFactory.getAllocator(lFitnessType));
			if(lFitnessAlloc == NULL) {
				std::ostringstream lOSS;
				lOSS << "Type '" << lFitnessType << "' associated to fitness object ";
				lOSS << "is not valid!";
				throw Beagle_IOExceptionNodeM(*lIter, lOSS.str());
			}
		}
		mFitness = castHandleT<Fitness>(lFitnessAlloc->allocate());
		mFitness->readWithContext(lIter, ioContext);
		break;
	}
	Beagle_StackTraceEndM();
}