예제 #1
0
/*!
 *  \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();
}
/*!
 *  \brief Apply the CMA-ES (Mu_W+Lambda) 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.
 *  \throw Beagle::ValidationException If a parameter is missing or have a bad value.
 *  \throw Beagle::AssertException If an invalid condition appears.
 */
void CMA::MuWCommaLambdaCMAFltVecOp::operate(Deme& ioDeme, Context& ioContext)
{
	Beagle_StackTraceBeginM();

	// Get real popsize and size of float vectors from register.
	UIntArray::Handle lPopSize;
	if(ioContext.getSystem().getRegister().isRegistered("ec.pop.size")) {
		lPopSize = castHandleT<UIntArray>(ioContext.getSystem().getRegister()["ec.pop.size"]);
	} else {
		std::ostringstream lOSS;
		lOSS << "Population size parameter 'ec.pop.size' is not found in register!";
		throw ValidationException(lOSS.str());
	}

	const unsigned int lDemeSize = (*lPopSize)[ioContext.getDemeIndex()];
	UInt::Handle lFloatVectorSize;
	if(ioContext.getSystem().getRegister().isRegistered("ga.init.vectorsize")) {
		lFloatVectorSize = castHandleT<UInt>(ioContext.getSystem().getRegister()["ga.init.vectorsize"]);
	} else {
		std::ostringstream lOSS;
		lOSS << "GA::MuWCommaLambdaCMAFltVecOp must be used in fixed-lenght float vector ";
		lOSS << "individuals. Parameter 'ga.init.vectorsize' is not in register, ";
		lOSS << "while it is needed to set initial size of the different CMA-ES matrices ";
		lOSS << "and vectors.";
		throw ValidationException(lOSS.str());
	}
	const unsigned int lN=lFloatVectorSize->getWrappedValue();

	// Get the appropriate CMA values from the CMA holder component.
	CMA::CMAValues& lValues = getCMAValues(ioContext.getDemeIndex(),lN,ioContext);

	// Compute weights and effective mu
	Vector lWeight;
	double lMuEff = 0.0;
	lMuEff = generateSelectionWeights(lDemeSize, lWeight);
	if(ioDeme.size() == 1) lMuEff = 1.;

	// If the replacement strategy possess a breeder tree
	if(getRootNode()!=NULL) {
		// Generate new children.
		const unsigned int lLambda =
		    (unsigned int)std::ceil(mLMRatio->getWrappedValue()*double(lDemeSize));
		generateChildren(ioDeme, ioContext, lLambda, lN, lValues, lWeight);

		// Check if all individuals have known fitness.
		for(unsigned int i=0; i<ioDeme.size(); ++i) {
			// If there is one invalid fitness, we should exit.
			// Evaluation will be taken elsewhere (we hope), and actual operator will be recalled.
			if((ioDeme[i]->getFitness()==NULL) || (ioDeme[i]->getFitness()->isValid()==false)) return;
		}
	}

	// Keep mu best children
	Beagle_AssertM(ioDeme.size() > lDemeSize);
	std::sort(ioDeme.begin(), ioDeme.end(), IsMorePointerPredicate());
	ioDeme.resize(lDemeSize);

	// Update CMA-ES values.
	updateValues(ioDeme, ioContext, lN, lMuEff, lWeight, lValues);

	Beagle_StackTraceEndM();
}