示例#1
0
bool JanEvolution::buildEvolutionSystem() {

	World *world = new World("Main");
	Population *population = new Population("HumanoidControllers");
	world->addPopulation(population);

	IdentityGenotypePhenotypeMapper *mapper = new IdentityGenotypePhenotypeMapper();
	population->setGenotypePhenotypeMapper(mapper);

	new ENS3EvolutionAlgorithm(world);

	world->setEvaluationMethod(new LocalNetworkInSimulationEvaluationMethod());

	Evolution::getEvolutionManager()->addEvolutionWorld(world);


	//Add statistics calculator.
	Statistics::getStatisticsManager()->addGenerationStatistics(
		new BasicNeuralNetworkStatistics(*population));



	FitnessFunction *fitness = 0;

	CommandLineArgument *fitnessFunctionArg = new CommandLineArgument(
		"fitness", "fit", "<prototypeName> <fitnessFunctionName>", 
		"Creates a copy of <prototypeName> and uses it for the population"
		" with the given <fitnessFunctionName>.", 2, 0, true);

	QList<QString> fitArguments = fitnessFunctionArg->getEntryParameters(0);
	if(fitArguments.size() > 1) {
		fitness = Fitness::getFitnessManager()
			->createFitnessFunctionFromPrototype(fitArguments.at(0), fitArguments.at(1));

		if(fitness == 0) {
			cerr << "Failed to create FitnessFunction prototype ["
				 << fitArguments.at(0).toStdString().c_str() << endl;
		}
	}

	if(fitness == 0) {
		fitness = Fitness::getFitnessManager()
			->createFitnessFunctionFromPrototype("Script", "Script");
	}

	population->addFitnessFunction(fitness);

	TournamentSelectionMethod *tournament = new TournamentSelectionMethod(5);
	PoissonDistributionRanking *poissonDistribution = new PoissonDistributionRanking();
	population->addSelectionMethod(tournament);
	population->addSelectionMethod(poissonDistribution);
	tournament->setResponsibleFitnessFunction(fitness);
	poissonDistribution->setResponsibleFitnessFunction(fitness);
	poissonDistribution->getPopulationProportion()->set(0.0);

	//add individual statistics
	StandardIndividualStatistics indStatistics(population); 
	NeuralNetworkIndividualStatistics neuroStatistics(population);

	return true;
}
/**
 * Constructs a new NeuroEvolutionSelector.
 */
NeuroEvolutionSelector::NeuroEvolutionSelector(EvaluationMethod *evaluationMethod)
{

	//TODO Currently only a single world with a single population is supported.

	//Select evolution algorithm
	CommandLineArgument *evoAlgorithmArg = new CommandLineArgument(
		"evolutionAlgorithm", "evo", "<algorithmName> <controlledAgent> <populationName>", 
		"Uses the evolution algorithm with the given <algorithmName> "
		"to evolve controllers for agent <controlledAgent> (optional). "
		"Optionally the population name can be set with <populationName>.", 1, 2, true);

	CommandLineArgument *fitnessFunctionArg = new CommandLineArgument(
			"fitness", "fit", "<prototypeName> <fitnessFunctionName> <population>", 
			"Creates a copy of <prototypeName> and uses it "
			" with the given <fitnessFunctionName>. The <population>"
			" is a number and specifies the population in which the fitness function is used.", 
			2, 1, true);

	//ensure that there is at least on world, even if not specified via command line.
	bool createDefaultWorld = false;
	if(evoAlgorithmArg->getNumberOfEntries() == 0) {
		createDefaultWorld = true;
	}
	
	int scriptFitnessCounter = 0;
	
	QList<QString> evoWorldNames;

	for(int evoCount = 0; evoCount < evoAlgorithmArg->getNumberOfEntries() || createDefaultWorld; ++evoCount) {

		QString worldName = "Main";
		QString popName = "Controllers";
		if(evoCount > 0) {
			worldName = "Pop" + QString::number(evoCount);
		}
		if(evoCount > 0) {
			popName = "Controllers" + QString::number(evoCount);
		}
		
		QList<QString> evoArguments = evoAlgorithmArg->getEntryParameters(evoCount);
		
		if(evoArguments.size() >= 3) {
			worldName = evoArguments[2];
			QString name = worldName;
			int counter = 1;
			while(evoWorldNames.contains(worldName)) {
				worldName = name + QString::number(counter);
				counter++;
			}
			evoWorldNames.append(worldName);
		}
	
		World *world = new World(worldName);
		Population *population = new Population(popName, world);
		world->addPopulation(population);
		
		if(evoCount == 0) {
			world->setEvaluationMethod(evaluationMethod);
		}
		else {
			world->setEvaluationMethod(0);
		}
		
		
	
		Evolution::getEvolutionManager()->addEvolutionWorld(world);
	
	
		//Install Fitness
		FitnessFunction *fitness = 0;

	
		for(int i = 0; i < fitnessFunctionArg->getNumberOfEntries(); ++i) {
			QList<QString> fitArguments = fitnessFunctionArg->getEntryParameters(i);
			if((evoCount == 0 && fitArguments.size() <= 2)
			  || (fitArguments.size() > 2 
					&& fitArguments.at(2).toInt() == evoCount))
			{
				if(fitArguments.size() > 1) {
					fitness = Fitness::getFitnessManager()
						->createFitnessFunctionFromPrototype(fitArguments.at(0), fitArguments.at(1));
			
					if(fitness == 0) {
						cerr << "Failed to create FitnessFunction prototype ["
							<< fitArguments.at(0).toStdString().c_str() << endl;
					}
				}
				if(fitness != 0) {
					population->addFitnessFunction(fitness);
				}
			}
		}
		if(fitness == 0) {
			QString scriptName = "Script";
			if(scriptFitnessCounter > 0) {
				scriptName += QString::number(scriptFitnessCounter);
			}
			scriptFitnessCounter++;
		
			fitness = Fitness::getFitnessManager()
				->createFitnessFunctionFromPrototype("Script", scriptName);
			population->addFitnessFunction(fitness);
		}
	
		if(evoArguments.size() >= 1 && evoArguments.at(0).trimmed() == "ens3")
		{
			ENS3EvolutionAlgorithm evo(world);
		}
		else if(evoArguments.size() >= 1 && evoArguments.at(0).trimmed() == "neat") {
			NeatAlgorithm evo(world);
		}
		else if(evoArguments.size() < 1 || (evoArguments.size() >= 1 
				&& (evoArguments.at(0).trimmed() == "mens" || evoArguments.at(0).trimmed() == "icone")))
		{
			ModularNeuroEvolution1 evo(world);
		}
		if(evoArguments.size() >= 2) {
			world->getControlleAgentValue()->set(evoArguments.at(1));
		}

		//add individual statistics
		StandardIndividualStatistics indStatistics(population); 
		NeuralNetworkIndividualStatistics neuroStatistics(population);
		
		//Add neuralNetworkStatustic calculator.
		Statistics::getStatisticsManager()->addGenerationStatistics(
			new BasicNeuralNetworkStatistics(*population));

		createDefaultWorld = false;
	}

}