MultiCoreEvaluationRunner::MultiCoreEvaluationRunner()
	: mDoShutDown(false), mShutDownEvent(0), mMessageValue(0), mMinIndex(-1), mMaxIndex(-1)
	{
		connect(this, SIGNAL(quitMainApplication()),
				QCoreApplication::instance(), SLOT(quit()));
		
		mMessageValue = new StringValue("");
		mMessageValue->setNotifyAllSetAttempts(true);
		Core::getInstance()->getValueManager()->addValue(
			"/Evaluation/Messages", mMessageValue);
		
		mNumberOfProcesses = new IntValue(4);
		Core::getInstance()->getValueManager()->addValue(
			"/Evaluation/NumberOfProcesses", mNumberOfProcesses);
		
		
		CommandLineArgument *jobFileNameArg = new CommandLineArgument("scriptFile", "scriptFile", 
									 "<absoluteFileName>", "The name of the main evaluation script",
									 1, 0, true, true);
		
		if(jobFileNameArg->getEntries().size() > 0) {
			QStringList jobScriptName = jobFileNameArg->getEntryParameters(0);
			if(jobScriptName.size() > 0) {
				mJobFile = jobScriptName.at(0);
			}
		}
		
		CommandLineArgument *indicesArg = new CommandLineArgument("index", "index",
									"<minIndex> [<maxIndex]", "The working directory indices to evaluate.\n"
									"If maxIndex is ommited, then only a single evaluation takes place.",
									1, 1, true, true);
		
		if(indicesArg->getEntries().size() > 0) {
			QStringList indicesList = indicesArg->getEntryParameters(0);
			if(indicesList.size() > 0) {
				bool ok = true;
				int index = indicesList.at(0).toInt(&ok);
				if(ok && index >= 0) {
					mMinIndex = index;
				}
			}
			if(indicesList.size() > 1) {
				bool ok = true;
				int index = indicesList.at(1).toInt(&ok);
				if(ok && index >= 0 && index >= mMinIndex) {
					mMaxIndex = index;
				}
			}
			else {
				mMaxIndex = mMinIndex;
			}
		}
		
		
		Core::getInstance()->addSystemObject(this);
	}
bool NerdMultiCoreEvaluationApplication::setupApplication()
{
	bool ok = true;
	
	CommandLineArgument *evalNameArg = new CommandLineArgument("name", "name", "<evalName>",
							 "The name of this evaluation process.", 1, 0, true, true);

	if(evalNameArg->getEntries().size() > 0) {
		QStringList params = evalNameArg->getEntryParameters(0);
		mEvaluationName = params.at(0);
	}
	
	mRunner = new MultiCoreEvaluationRunner();
	
	Core::log("EvaluationName: " + mEvaluationName, true);

	return ok;
}
/**
 * Constructs a new UniversalNeuroScriptLoader.
 */
UniversalNeuroScriptLoader::UniversalNeuroScriptLoader()
{
    CommandLineArgument *scriptLoaderArgument = new CommandLineArgument(
        "installScript", "is", "<ScriptName> [<ScriptFileName> <TriggerEvent> <ResetEvent> <AgentInterface>]",
        QString("Installs a new script with name <ScriptName>, that loads file <ScriptFileName> ")
        + "during the bind phase. The script is reset by <ResetEvent> and executed each "
        + "time <TriggerEvent> is triggered.\nThe <AgentInterface> "
        + "specifies the agents whose neural network can be changed.", 1, 4, true);

    QList<QString> scriptNames;

    int numberOfScriptsToInstall = scriptLoaderArgument->getNumberOfEntries();
    for(int i = 0; i < numberOfScriptsToInstall; ++i) {
        QList<QString> entryParams = scriptLoaderArgument->getEntryParameters(i);

        QString name = entryParams.at(0);
        QString fileName = "";
        if(entryParams.size() > 1) {
            fileName = entryParams.at(1);
        }
        QString triggerEvent = "";
        if(entryParams.size() > 2) {
            triggerEvent = entryParams.at(2);
        }
        QString resetEvent = "";
        if(entryParams.size() > 3) {
            resetEvent = entryParams.at(3);
        }
        QString agentName = "";
        if(entryParams.size() > 4) {
            agentName = entryParams.at(4);
        }

        if(name != "" && !scriptNames.contains(name)) {
            new UniversalNeuroScriptingContext(name, fileName, triggerEvent, resetEvent, agentName);
            scriptNames.append(name);
            Core::log("UniversalScriptLoader: Installed UniversalScriptingContext ["
                      + name + "]");
        }
    }
}
Esempio n. 4
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;
}
bool EvolutionOperatorTestApplication::setupApplication()
{
	bool ok = true;
	

	//Choose Physics Engine (or external Yars simulator)
	CommandLineArgument *physicsArg = new CommandLineArgument("physics", "p", "<physicsLibrary>",
			"Uses <physicsLibrary> as physics engine. "
			"Currently there are [ode, yars].", 1,0, true);
	if(physicsArg->getNumberOfEntries() != 0 && !physicsArg->getEntryParameters(0).empty()
			&& physicsArg->getEntryParameters(0).at(0).trimmed() == "yars")
	{
		YarsCommunication();
	}
	else {
		//install ODE PhysicsLayer
		ODE_Physics();
	}


	EvolutionSimulationPrototypes();

	//install file parser
	new SimpleObjectFileParser();

	//Install file interface for HTTPS communication.
	//new LoadAndStoreValueManagerPerGeneration();

	//Install fitness prototypes
	NeuroFitnessPrototypes();
	
	//Install standard NN functions and objects.
	StandardNeuralNetworkFunctions();

	//Install constraint manager.
	StandardConstraintCollection();

	//Install neuro modules
	NeuroModuleCollection();

	//Install network attribute characterizer
	new NeuralNetworkAttributes();

	//Install statistics logger to save the current statistics to a file.
	new StatisticsLogger(Evolution::getEvolutionManager()->getEvolutionWorkingDirectory());

	//Install and initialize the settings logger (logging of settings history).
	SettingsLogger *settingsLogger = new SettingsLogger();
	settingsLogger->addValues("(?!.*/Evo/.*/Fitness/.*/Fitness/.*)(?!.*/Performance/.*)/Evo/.*/Algorithm/.*");
	settingsLogger->addValues("(?!.*/Evo/.*/Fitness/.*/Fitness/.*)(?!.*/Performance/.*)/Evo/.*/Pop/.*");
	settingsLogger->addValues("/Evo/.*/Fitness/.*/Fitness/CalculationMode");
	settingsLogger->addValues("/Control/NumberOfSteps");
	settingsLogger->addValues("/Control/NumberOfTries");

	new FramesPerSecondCounter();
	new SimObjectGroupPrinter();
	new SaveBestNetworksHandler(3); 


	if(!buildSimulationModel()) {
		Core::log("LocalNNSimulatorEvolutionApplication: "
				  "Problem while building the simulation model.");
		ok = false;
	}

	if(!buildEvolutionSystem()) {
		Core::log("LocalNNSimulatorEvolutionApplication: "
				  "Problem while building the evolution system.");
		ok = false;
	}

	mRunner = new EvolutionRunner();

	return ok;

}
/**
 * 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;
	}

}
bool NerdNeuroEvoApplication::setupApplication()
{
	bool ok = true;


	//Choose Physics Engine (or external Yars simulator)
	CommandLineArgument *physicsArg = new CommandLineArgument("physics", "p", "<physicsLibrary>",
			"Uses <physicsLibrary> as physics engine. "
			"Currently there are [ode, yars].", 1,0, true);
	if(physicsArg->getNumberOfEntries() != 0 && !physicsArg->getEntryParameters(0).empty()
			&& physicsArg->getEntryParameters(0).at(0).trimmed() == "yars")
	{
		YarsCommunication();
	}
	else if(physicsArg->getNumberOfEntries() != 0 && !physicsArg->getEntryParameters(0).empty()
			&& physicsArg->getEntryParameters(0).at(0).trimmed() == "s2d")
	{
		Simple2D_Physics();
	}
	else {
		//install ODE PhysicsLayer
		ODE_Physics();
	}


	EvolutionSimulationPrototypes();

	//install file parser
	new SimpleObjectFileParser();

	//Install file interface for HTTPS communication.
	//new LoadAndStoreValueManagerPerGeneration();

	//Install fitness prototypes
	NeuroFitnessPrototypes();

	//Install standard NN functions and objects.
	StandardNeuralNetworkFunctions();

	//Install constraint manager.
	StandardConstraintCollection();

	//Install neuro modules
	NeuroModuleCollection();
	StandardTagCollection();

	//Enable NeuroModulation
	NeuroModulatorManager::getInstance();

	//Install network attribute characterizer
	new NeuralNetworkAttributes();

	new AutoStoreNetworksHelper();

	//Install statistics logger to save the current statistics to a file.
	new StatisticsLogger(Evolution::getEvolutionManager()->getEvolutionWorkingDirectoryValue());

	new EvolutionTerminationTrigger();

	//Install and initialize the settings logger (logging of settings history).
	SettingsLogger *settingsLogger = new SettingsLogger();
	settingsLogger->addValues("(?!.*/Evo/.*/Fitness/.*/Fitness/.*)(?!.*/Performance/.*)/Evo/.*/Algorithm/.*");
	settingsLogger->addValues("(?!.*/Evo/.*/Fitness/.*/Fitness/.*)(?!.*/Performance/.*)/Evo/.*/Pop/.*");
	settingsLogger->addValues("/Evo/.*/Fitness/.*/Fitness/CalculationMode");
	settingsLogger->addValues("/Control/NumberOfSteps");
	settingsLogger->addValues("/Control/NumberOfTries");

	new TerminateTryCollisionRule();

	new StepsPerSecondCounter();
	new SimObjectGroupPrinter();
	new SaveBestNetworksHandler(3);

	//add motor model rotation
	new ModelParameterRotator();

	//allow neural networks for other agents outside of the evolution.
	new NetworkAgentControlParser();

	UniversalNeuroScriptLoader();
	ScriptedModelLoader();

	//Add plugin to calculate the open degrees of freedom of the network during evolution.
	new NetworkDegreeOfFreedomCalculator();

	if(!buildSimulationModel()) {
		Core::log("NerdNeuroEvo: "
				  "Problem while building the simulation model.");
		ok = false;
	}

	if(!buildEvolutionSystem()) {
		Core::log("NerdNeuroEvo: "
				  "Problem while building the evolution system.");
		ok = false;
	}

	mRunner = new EvolutionRunner();

	return ok;

}
bool NetworkDynamicsPlotterApplication::setupApplication() {
	bool ok = true;

	//install network characterizer
	new NeuralNetworkAttributes();
	StandardTagCollection();
	NeuroModuleCollection();

	DynamicsPlotCollection();

	if(mEnableSimulator) {
		new SimObjectGroupPrinter();

		//Choose Physics Engine (or external Yars simulator)
		CommandLineArgument *physicsArg = new CommandLineArgument("physics", "p", "<physicsLibrary>",
											"Uses <physicsLibrary> as physics engine. "
											"Currently there are [ode, yars].", 1,0, true);

		if(physicsArg->getNumberOfEntries() != 0 && !physicsArg->getEntryParameters(0).empty()
			&& physicsArg->getEntryParameters(0).at(0).trimmed() == "yars")
		{
			YarsCommunication();
		}
		else if(physicsArg->getNumberOfEntries() != 0 && !physicsArg->getEntryParameters(0).empty()
			&& physicsArg->getEntryParameters(0).at(0).trimmed() == "s2d")
		{
			Simple2D_Physics();
		}
		else {
			//install ODE PhysicsLayer
			ODE_Physics();
		}

		//install file parser
		new SimpleObjectFileParser();

		new StepsPerSecondCounter();


		//Priovide the -net required to load a network for one or more agents.
		new NetworkAgentControlParser();

		new ModelParameterRotator();

		ScriptedModelLoader();
	}

	UniversalNeuroScriptLoader();


	//load a network if given.
	CommandLineArgument *netArg = new CommandLineArgument("loadNetwork", "net", "<networkFile",
								"Loads a NeuralNetwork to the NetworkEditor",
								1, 0, false, true);

	//Only provide a -net to load a network (or create a default in case no -net is given) when
	//NOT using a physical simulation.
	if(!mEnableSimulator) {
		if(netArg->getNumberOfEntries() > 0) {
			QStringList files = netArg->getEntryParameters(0);
			if(files.size() > 0) {
				QString errorMessage;
				QList<QString> warnings;
				NeuralNetwork *net = NeuralNetworkIO::createNetworkFromFile(files.at(0),
								&errorMessage, &warnings);

				if(errorMessage != "") {
					Core::log("Error while loading net: " + errorMessage, true);
				}
				if(!warnings.empty()) {
					for(QListIterator<QString> j(warnings); j.hasNext();) {
						Core::log("Warning: " + j.next(), true);
					}
				}

				if(dynamic_cast<ModularNeuralNetwork*>(net) != 0) {
					Neuro::getNeuralNetworkManager()->addNeuralNetwork(net);
				}
			}
		}
		else {
			Neuro::getNeuralNetworkManager()->addNeuralNetwork(new ModularNeuralNetwork(
											AdditiveTimeDiscreteActivationFunction(),
											TransferFunctionTanh(),
											SimpleSynapseFunction()));
		}
	}

	mExecutionLoop = new PlotterExecutionLoop();

	new MatlabExporter();

	return ok;
}
bool NerdNeuroSimApplication::setupApplication()
{
	
	//install simulation recorder
	new NetworkSimulationRecorder();
	
	
// 	StandardConstraintCollection();
// 	StandardNeuralNetworkFunctions();
	NeuroAndSimEvaluationStandardGuiApplication::setupApplication();
	
	//Install network characterization
	new NeuralNetworkAttributes();

	//Choose Physics Engine (or external Yars simulator)
	CommandLineArgument *physicsArg = new CommandLineArgument("physics", "p", "<physicsLibrary>",
			"Uses <physicsLibrary> as physics engine. "
			"Currently there are [ode, yars].", 1,0, true);
	if(physicsArg->getNumberOfEntries() != 0 && !physicsArg->getEntryParameters(0).empty()
			&& physicsArg->getEntryParameters(0).at(0).trimmed() == "yars")
	{
		YarsCommunication();
	}
	else if(physicsArg->getNumberOfEntries() != 0 && !physicsArg->getEntryParameters(0).empty()
			&& physicsArg->getEntryParameters(0).at(0).trimmed() == "s2d")
	{
		Simple2D_Physics();
	}
	else {
		//install ODE PhysicsLayer
		ODE_Physics();
	}
	
	
	
	new StepsPerSecondCounter();


	EvolutionSimulationPrototypes();

	//install file parser
	new SimpleObjectFileParser();
	//install neural network control parser.
	new NetworkAgentControlParser();

	new ActivationFrequencyCalculator("A");

	//add motor model rotation
	new ModelParameterRotator();

	//Install neuro modules
	NeuroModuleCollection();
	StandardTagCollection();
	
	//Enable NeuroModulation
	NeuroModulatorManager::getInstance();
	
	Fitness::install();
	new FitnessLogger();
	NeuroFitnessPrototypes();
	Statistics::getStatisticsManager();

	UniversalNeuroScriptLoader();
	ScriptedModelLoader();
	
	//Add plugin to calculate the open degrees of freedom of the network during evolution.
	new NetworkDegreeOfFreedomCalculator();
	
	//add simple fitness logger
	CommandLineArgument *useSimpleFitnessLogger = 
			new CommandLineArgument("simpleFitnessLogger", "sfl", "<logfile name>",
			"Logs all fitness values of all fitness functions "
			"incrementally to a file.", 1, 0, true, false);
	if(useSimpleFitnessLogger->getNumberOfEntries() != 0 
			&& !useSimpleFitnessLogger->getEntryParameters(0).empty())
	{
		new SimpleFitnessLogger(useSimpleFitnessLogger->getEntryParameters(0).at(0));
	}
	else if(NerdConstants::HackMode == true) { //HACK MODE remove!
		new SimpleFitnessLogger("simpleFitnessLog.txt");
	}

	return true;
}