Beispiel #1
0
/*!
 *  \brief Evaluate the individual fitness for the spambase problem.
 *  \param inIndividual Individual to evaluate.
 *  \param ioContext Evolutionary context.
 *  \return Handle to the fitness measure,
 */
Fitness::Handle SpambaseEvalOp::evaluate(GP::Individual& inIndividual, GP::Context& ioContext)
{
	// Get reference to data set
	DataSetClassification::Handle lDataSet =
	    castHandleT<DataSetClassification>(ioContext.getSystem().getComponent("DataSet"));
	if(lDataSet == NULL) {
		throw Beagle_RunTimeExceptionM("Data set is not present in the system, could not proceed further!");
	}

	// Generate indices used as data subset for fitness evaluation
	std::vector<unsigned int> lSubSet(lDataSet->size());
	for(unsigned int i=0; i<lSubSet.size(); ++i) lSubSet[i] = i;
	std::random_shuffle(lSubSet.begin(), lSubSet.end(),
	                    ioContext.getSystem().getRandomizer());
	if(Spambase_TestSize < lSubSet.size()) {
		lSubSet.resize(Spambase_TestSize);
	}

	// Evaluate sampled test cases
	unsigned int lCorrectCount = 0;
	for(unsigned int i=0; i<Spambase_TestSize; ++i) {
		const bool lPositiveID = ((*lDataSet)[lSubSet[i]].first == 1);
		const Beagle::Vector& lData = (*lDataSet)[lSubSet[i]].second;
		for(unsigned int j=0; j<lData.size(); ++j) {
			std::ostringstream lOSS;
			lOSS << "IN" << j;
			setValue(lOSS.str(), Double(lData[j]), ioContext);
		}
		Bool lResult;
		inIndividual.run(lResult, ioContext);
		if(lResult.getWrappedValue() == lPositiveID) ++lCorrectCount;
	}
	double lFitness = double(lCorrectCount) / Spambase_TestSize;
	return new EC::FitnessSimple(lFitness);
}
/*!
 *  \brief Evaluate the individual fitness for the even-6 parity problem.
 *  \param inIndividual Individual to evaluate.
 *  \param ioContext Evolutionary context.
 *  \return Handle to the fitness measure,
 */
Fitness::Handle ParityEvalOp::evaluate(GP::Individual& inIndividual, GP::Context& ioContext)
{
	unsigned int lGood = 0;
	for(unsigned int i=0; i<ParitySizeM; i++) {
		for(unsigned int j=0; j<ParityFanInM; j++) {
			std::string lName = "IN";
			lName += uint2str(j);
			setValue(lName, mInputs[i][j], ioContext);
		}
		Bool lResult;
		inIndividual.run(lResult, ioContext);
		if(lResult.getWrappedValue() == mOutputs[i].getWrappedValue()) lGood++;
	}
	double lFitness = double(lGood) / ParitySizeM;
	return new FitnessSimple(lFitness);
}
/*!
 *  \brief Evaluate the individual fitness for the boolean 11-multiplexer problem.
 *  \param inIndividual Individual to evaluate.
 *  \param ioContext Evolutionary context.
 *  \return Handle to the fitness measure,
 */
Fitness::Handle MultiplexerEvalOp::evaluate(GP::Individual& inIndividual, GP::Context& ioContext)
{
	unsigned int lNbGood = 0;
	for(unsigned int i=0; i<2048; ++i) {
		setValue("A0", mInputs[i][0], ioContext);
		setValue("A1", mInputs[i][1], ioContext);
		setValue("A2", mInputs[i][2], ioContext);
		setValue("D0", mInputs[i][3], ioContext);
		setValue("D1", mInputs[i][4], ioContext);
		setValue("D2", mInputs[i][5], ioContext);
		setValue("D3", mInputs[i][6], ioContext);
		setValue("D4", mInputs[i][7], ioContext);
		setValue("D5", mInputs[i][8], ioContext);
		setValue("D6", mInputs[i][9], ioContext);
		setValue("D7", mInputs[i][10], ioContext);
		Bool lResult;
		inIndividual.run(lResult, ioContext);
		if(lResult.getWrappedValue() == mOutputs[i].getWrappedValue()) ++lNbGood;
	}
	double lFitness = double(lNbGood) / 2048.;
	return new EC::FitnessSimple(lFitness);
}