tests.cpp
1 /********************************************************************************
2  * FARSA Experiments Library *
3  * Copyright (C) 2007-2012 *
4  * Piero Savastano <piero.savastano@istc.cnr.it> *
5  * Tomassino Ferrauto <tomassino.ferrauto@istc.cnr.it> *
6  * Stefano Nolfi <stefano.nolfi@istc.cnr.it> *
7  * Onofrio Gigliotta <onofrio.gigliotta@istc.cnr.it> *
8  * Gianluca Massera <emmegian@yahoo.it> *
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  * This program is distributed in the hope that it will be useful, *
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18  * GNU General Public License for more details. *
19  * *
20  * You should have received a copy of the GNU General Public License *
21  * along with this program; if not, write to the Free Software *
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
23  ********************************************************************************/
24 
25 #include "tests.h"
26 #include "factory.h"
27 #include "evorobotcomponent.h"
28 #include "evorobotexperiment.h"
29 #include "logger.h"
30 #include "configurationhelper.h"
31 
32 #include <iostream>
33 
34 namespace farsa {
35 
37  AbstractTest()
38 {
39  m_menuText = "Random Individual";
40  m_tooltip = "Create a random genotype and it will run a test on it";
41  m_iconFilename = QString();
42 }
43 
45 {
46 }
47 
49 {
51  //get genome length and build new array
52  int length = exp->getGenomeLength();
53  int* randomDna = new int[length];
54 
55  //overwrite .phe parameters
56  exp->getNeuralNetwork()->copyPheParameters(randomDna);
57 
58  //randomize genes
59  for(int i=0; i<length; i++)
60  if(randomDna[i] == Evonet::DEFAULT_VALUE)
61  randomDna[i] = rand()%256;
62 
63  return randomDna;
64 }
65 
66 void TestRandom::configure(ConfigurationParameters& params, QString prefix) {
67  AbstractTest::configure( params, prefix );
68 }
69 
70 void TestRandom::save(ConfigurationParameters& params, QString prefix) {
71  AbstractTest::save( params, prefix );
72  params.startObjectParameters( prefix, "TestRandom", this );
73 }
74 
75 void TestRandom::describe( QString type ) {
76  Descriptor d = addTypeDescription( type, "Create a random individual and test it" );
77 }
78 
80 {
81  Logger::info( "Test Random Individual - Starting the test of an agent with a random DNA" );
82 
83  // Setting seed to 0, here with a random DNA the seed number is not important
84  component()->getGA()->setSeed(0);
86  exp->setActivityPhase( EvoRobotExperiment::INTEST );
87  // get a random genotype and set it on the net
88  int* dna = buildRandomDNA();
89  exp->setNetParameters(dna);
90  // initialize the experiment
91  exp->initGeneration(0);
93  exp->endGeneration(0);
94  delete []dna;
95 }
96 
98  AbstractTest(),
99  idIndividual(0),
100  populationFile(),
101  populationLoaded(true)
102 {
103  m_menuText = "Selected Individual";
104  m_tooltip = "Run a test on the individual selected with \"Individual to Test\" view";
105  m_iconFilename = QString();
106 }
107 
109 {
110 }
111 
112 void TestIndividual::configure(ConfigurationParameters& params, QString prefix) {
113  AbstractTest::configure( params, prefix );
114  QString filename = ConfigurationHelper::getString( params, prefix+"populationFile", QString() );
115  if ( !filename.isEmpty() ) {
116  setPopulationToTest( filename );
117  }
118  idIndividual = ConfigurationHelper::getInt( params, prefix+"idIndividual", 0 );
119 }
120 
121 void TestIndividual::save(ConfigurationParameters& params, QString prefix) {
122  AbstractTest::save( params, prefix );
123  params.startObjectParameters( prefix, "TestIndividual", this );
124  if ( !populationFile.isEmpty() ) {
125  params.createParameter( prefix, "populationFile", populationFile );
126  }
127  params.createParameter( prefix, "idIndividual", QString::number(idIndividual) );
128 }
129 
130 void TestIndividual::describe( QString type ) {
131  Descriptor d = addTypeDescription( type, "Test an individual from a saved genome" );
132  d.describeString( "populationFile" ).help( "The name of the file from wich to load the genotypes to test" );
133  d.describeInt( "idIndividual" ).limits( 0, MaxInteger ).help( "The id of the individual to test" );
134 }
135 
137 {
138  if ( !populationLoaded ) {
139  component()->getGA()->loadGenotypes(populationFile);
140  // extract the number of corresponding seed
141  QRegExp reg("[BG][0-9]+S([0-9]+).gen");
142  reg.indexIn( populationFile );
143  component()->getGA()->setSeed( reg.cap(1).toInt() );
144  Logger::info( QString("TestIndividual - Loaded Genome from %1").arg(populationFile) );
145  populationLoaded = true;
146  }
147  if (idIndividual >= int(component()->getGA()->numLoadedGenotypes())) {
148  Logger::error( QString("TestIndividual - Individual %1 doesn't exists (%2 individual have been loaded)").arg(idIndividual).arg(component()->getGA()->numLoadedGenotypes()) );
149  return;
150  }
151  Logger::info( QString("TestIndividual - Start of the Test of Individual %1").arg(idIndividual) );
153  exp->setActivityPhase( EvoRobotExperiment::INTEST );
154  exp->setNetParameters( component()->getGA()->getGenesForIndividual(idIndividual) );
155  exp->initGeneration(0);
156  exp->doAllTrialsForIndividual(idIndividual);
157  exp->endGeneration(0);
158  Logger::info( QString("TestIndividual - End of the Test of Individual %1").arg(idIndividual) );
159 }
160 
161 void TestIndividual::setIndividualToTest( int idIndividual ) {
162  this->idIndividual = idIndividual;
163 }
164 
165 void TestIndividual::setPopulationToTest( QString filename, bool deferLoading ) {
166  if ( populationFile != filename ) {
167  populationFile = filename;
168  populationLoaded = false;
169  }
170  if ( !deferLoading ) {
171  component()->getGA()->loadGenotypes(populationFile);
172  // extract the number of corresponding seed
173  QRegExp reg("[BG][0-9]+S([0-9]+).gen");
174  reg.indexIn( populationFile );
175  component()->getGA()->setSeed( reg.cap(1).toInt() );
176  Logger::info( QString("TestIndividual - Loaded Genome from %1").arg(populationFile) );
177  populationLoaded = true;
178  }
179 }
180 
182  AbstractTest()
183 {
184  m_menuText = "Current Individual";
185  m_tooltip = "Run a test on the current individual";
186  m_iconFilename = QString();
187 }
188 
190 {
191 }
192 
193 void TestCurrent::configure(ConfigurationParameters& params, QString prefix) {
194  AbstractTest::configure( params, prefix );
195 }
196 
197 void TestCurrent::save(ConfigurationParameters& params, QString prefix) {
198  AbstractTest::save( params, prefix );
199  params.startObjectParameters( prefix, "TestCurrent", this );
200 }
201 
202 void TestCurrent::describe( QString type ) {
203  Descriptor d = addTypeDescription( type, "Tests the current individual, i.e. doesn't load nor randomize the genome" );
204 }
205 
207 {
208  Logger::info( QString("TestCurrent - Start of the Test of Current Individual") );
210  exp->setActivityPhase( EvoRobotExperiment::INTEST );
211  exp->initGeneration(0);
212  exp->doAllTrialsForIndividual(0);
213  exp->endGeneration(0);
214  Logger::info( QString("TestCurrent - End of the Test of Current Individual") );
215 }
216 
217 } //end namespace farsa
virtual ~TestRandom()
Destructor.
Definition: tests.cpp:44
TestRandom()
Constructor.
Definition: tests.cpp:36
virtual void setNetParameters(float *genes)
Sets the free parameters of the neural network. This is done for all agents, even disabled ones...
static QString getString(ConfigurationParameters &params, QString paramPath, QString def=QString())
virtual void initGeneration(int generation)
Called at the beginning of a generation. This function is NEVER called concurrently on different obje...
virtual void save(ConfigurationParameters &params, QString prefix)
Save the actual status of parameters into the ConfigurationParameters object passed.
Definition: abstracttest.h:99
void doAllTrialsForIndividual(int individual)
Performs all trials for the given individual.
void setPopulationToTest(QString filename, bool deferLoading=true)
Replace the current population loaded into GA with population loaded from file.
Definition: tests.cpp:165
QString m_menuText
change in in subclasses for setting the text will appear on the menu/toolbar
Definition: abstracttest.h:173
static void describe(QString type)
Describe Function.
Definition: tests.cpp:130
void copyPheParameters(int *pheGene)
transorm floating point parameters normalized in the range [-wrange,range] into integer parameters in...
Definition: evonet.cpp:1120
virtual void save(ConfigurationParameters &params, QString prefix)
Save the actual status of parameters into the ConfigurationParameters object passed.
Definition: tests.cpp:121
virtual void configure(ConfigurationParameters &params, QString prefix)
Configures the object using a ConfigurationParameters object.
Definition: tests.cpp:193
EvoRobotComponent * component()
Returns a pointer to the Component.
Definition: abstracttest.h:137
virtual void runTest()
Run the test on the individual selected.
Definition: tests.cpp:206
virtual void runTest()
Run the test setting random genotype.
Definition: tests.cpp:79
virtual void save(ConfigurationParameters &params, QString prefix)
Save the actual status of parameters into the ConfigurationParameters object passed.
Definition: tests.cpp:197
virtual void save(ConfigurationParameters &params, QString prefix)
Save the actual status of parameters into the ConfigurationParameters object passed.
Definition: tests.cpp:70
static void describe(QString type)
Describe Function.
Definition: tests.cpp:75
static void info(QString msg)
static void error(QString msg)
Evonet * getNeuralNetwork(int agentId=0)
Return the neural network used for the experiment.
bool startObjectParameters(QString groupPath, QString typeName, ParameterSettable *object)
int getGenomeLength()
Returns the length of the genome.
virtual void endGeneration(int generation)
Called at the end of a generation. This function is NEVER called concurrently on different objects...
static void describe(QString type)
Describe Function.
Definition: tests.cpp:202
static Descriptor addTypeDescription(QString type, QString shortHelp, QString longHelp=QString(""))
virtual unsigned int loadGenotypes(QString filename)
Loads genotypes from the given file.
Definition: evoga.cpp:3118
An abstract class for tests of evolved individuals.
Definition: abstracttest.h:50
void setSeed(int s)
Initialize the random number generator.
Definition: evoga.cpp:375
static const int MaxInteger
virtual void runTest()
Run the test on the individual selected.
Definition: tests.cpp:136
virtual EvoRobotExperiment * getEvoRobotExperiment()
Returns a pointer to the EvoRobotExperiment object.
Definition: evoga.cpp:3074
virtual ~TestCurrent()
Destructor.
Definition: tests.cpp:189
virtual ~TestIndividual()
Destructor.
Definition: tests.cpp:108
TestCurrent()
Constructor.
Definition: tests.cpp:181
QString m_iconFilename
change in in subclasses for setting the icon will appear on the menu/toolbar
Definition: abstracttest.h:177
static int getInt(ConfigurationParameters &params, QString paramPath, int def=0)
QString m_tooltip
change in in subclasses for setting the tooltip will appear on the menu/toolbar
Definition: abstracttest.h:175
virtual void configure(ConfigurationParameters &params, QString prefix)
Configures the object using a ConfigurationParameters object.
Definition: tests.cpp:66
virtual void configure(ConfigurationParameters &params, QString prefix)
Configures the object using a ConfigurationParameters object.
Definition: tests.cpp:112
The base common class that evaluate the fitness of a robot.
void createParameter(QString groupPath, QString parameter)
Evoga * getGA()
Return the Evoga.
int * buildRandomDNA()
Create a random genotype.
Definition: tests.cpp:48
virtual void configure(ConfigurationParameters &, QString)
Configures the object using a ConfigurationParameters object.
Definition: abstracttest.h:85
void setActivityPhase(Phases newPhase)
set the new activity phase
void setIndividualToTest(int idIndividual)
Set id of the individual to test.
Definition: tests.cpp:161
TestIndividual()
Constructor.
Definition: tests.cpp:97
static const float DEFAULT_VALUE
DEFAULT_VALUE is used for do not assign values to mut and parameters.
Definition: evonet.h:137