00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "tests.h"
00026 #include "factory.h"
00027 #include "evorobotcomponent.h"
00028 #include "evorobotexperiment.h"
00029 #include "logger.h"
00030 #include "configurationhelper.h"
00031
00032 #include <iostream>
00033
00034 namespace farsa {
00035
00036 TestRandom::TestRandom() :
00037 AbstractTest()
00038 {
00039 m_menuText = "Random Individual";
00040 m_tooltip = "Create a random genotype and it will run a test on it";
00041 m_iconFilename = QString();
00042 }
00043
00044 TestRandom::~TestRandom()
00045 {
00046 }
00047
00048 int* TestRandom::buildRandomDNA()
00049 {
00050 EvoRobotExperiment* exp = component()->getGA()->getEvoRobotExperiment();
00051
00052 int length = exp->getGenomeLength();
00053 int* randomDna = new int[length];
00054
00055
00056 exp->getNeuralNetwork()->copyPheParameters(randomDna);
00057
00058
00059 for(int i=0; i<length; i++)
00060 if(randomDna[i] == Evonet::DEFAULT_VALUE)
00061 randomDna[i] = rand()%256;
00062
00063 return randomDna;
00064 }
00065
00066 void TestRandom::configure(ConfigurationParameters& params, QString prefix) {
00067 AbstractTest::configure( params, prefix );
00068 }
00069
00070 void TestRandom::save(ConfigurationParameters& params, QString prefix) {
00071 AbstractTest::save( params, prefix );
00072 params.startObjectParameters( prefix, "TestRandom", this );
00073 }
00074
00075 void TestRandom::describe( QString type ) {
00076 Descriptor d = addTypeDescription( type, "Create a random individual and test it" );
00077 }
00078
00079 void TestRandom::runTest()
00080 {
00081 Logger::info( "Test Random Individual - Starting the test of an agent with a random DNA" );
00082
00083
00084 component()->getGA()->setSeed(0);
00085 EvoRobotExperiment* exp = component()->getGA()->getEvoRobotExperiment();
00086 exp->setActivityPhase( EvoRobotExperiment::INTEST );
00087
00088 int* dna = buildRandomDNA();
00089 exp->setNetParameters(dna);
00090
00091 exp->initGeneration(0);
00092 exp->doAllTrialsForIndividual(0);
00093 exp->endGeneration(0);
00094 delete []dna;
00095 }
00096
00097 TestIndividual::TestIndividual() :
00098 AbstractTest(),
00099 idIndividual(0),
00100 populationFile(),
00101 populationLoaded(true)
00102 {
00103 m_menuText = "Selected Individual";
00104 m_tooltip = "Run a test on the individual selected with \"Individual to Test\" view";
00105 m_iconFilename = QString();
00106 }
00107
00108 TestIndividual::~TestIndividual()
00109 {
00110 }
00111
00112 void TestIndividual::configure(ConfigurationParameters& params, QString prefix) {
00113 AbstractTest::configure( params, prefix );
00114 QString filename = ConfigurationHelper::getString( params, prefix+"populationFile", QString() );
00115 if ( !filename.isEmpty() ) {
00116 setPopulationToTest( filename );
00117 }
00118 idIndividual = ConfigurationHelper::getInt( params, prefix+"idIndividual", 0 );
00119 }
00120
00121 void TestIndividual::save(ConfigurationParameters& params, QString prefix) {
00122 AbstractTest::save( params, prefix );
00123 params.startObjectParameters( prefix, "TestIndividual", this );
00124 if ( !populationFile.isEmpty() ) {
00125 params.createParameter( prefix, "populationFile", populationFile );
00126 }
00127 params.createParameter( prefix, "idIndividual", QString::number(idIndividual) );
00128 }
00129
00130 void TestIndividual::describe( QString type ) {
00131 Descriptor d = addTypeDescription( type, "Test an individual from a saved genome" );
00132 d.describeString( "populationFile" ).help( "The name of the file from wich to load the genotypes to test" );
00133 d.describeInt( "idIndividual" ).limits( 0, MaxInteger ).help( "The id of the individual to test" );
00134 }
00135
00136 void TestIndividual::runTest()
00137 {
00138 if ( !populationLoaded ) {
00139 component()->getGA()->loadGenotypes(populationFile);
00140
00141 QRegExp reg("[BG][0-9]+S([0-9]+).gen");
00142 reg.indexIn( populationFile );
00143 component()->getGA()->setSeed( reg.cap(1).toInt() );
00144 Logger::info( QString("TestIndividual - Loaded Genome from %1").arg(populationFile) );
00145 populationLoaded = true;
00146 }
00147 Logger::info( QString("TestIndividual - Start of the Test of Individual %1").arg(idIndividual) );
00148 EvoRobotExperiment* exp = component()->getGA()->getEvoRobotExperiment();
00149 exp->setActivityPhase( EvoRobotExperiment::INTEST );
00150 exp->setNetParameters( component()->getGA()->getGenesForIndividual(idIndividual) );
00151 exp->initGeneration(0);
00152 exp->doAllTrialsForIndividual(idIndividual);
00153 exp->endGeneration(0);
00154 Logger::info( QString("TestIndividual - End of the Test of Individual %1").arg(idIndividual) );
00155 }
00156
00157 void TestIndividual::setIndividualToTest( int idIndividual ) {
00158 this->idIndividual = idIndividual;
00159 }
00160
00161 void TestIndividual::setPopulationToTest( QString filename, bool deferLoading ) {
00162 if ( populationFile != filename ) {
00163 populationFile = filename;
00164 populationLoaded = false;
00165 }
00166 if ( !deferLoading ) {
00167 component()->getGA()->loadGenotypes(populationFile);
00168
00169 QRegExp reg("[BG][0-9]+S([0-9]+).gen");
00170 reg.indexIn( populationFile );
00171 component()->getGA()->setSeed( reg.cap(1).toInt() );
00172 Logger::info( QString("TestIndividual - Loaded Genome from %1").arg(populationFile) );
00173 populationLoaded = true;
00174 }
00175 }
00176
00177 }