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  Logger::info( QString("TestIndividual - Start of the Test of Individual %1").arg(idIndividual) );
149  exp->setActivityPhase( EvoRobotExperiment::INTEST );
150  exp->setNetParameters( component()->getGA()->getGenesForIndividual(idIndividual) );
151  exp->initGeneration(0);
152  exp->doAllTrialsForIndividual(idIndividual);
153  exp->endGeneration(0);
154  Logger::info( QString("TestIndividual - End of the Test of Individual %1").arg(idIndividual) );
155 }
156 
157 void TestIndividual::setIndividualToTest( int idIndividual ) {
158  this->idIndividual = idIndividual;
159 }
160 
161 void TestIndividual::setPopulationToTest( QString filename, bool deferLoading ) {
162  if ( populationFile != filename ) {
163  populationFile = filename;
164  populationLoaded = false;
165  }
166  if ( !deferLoading ) {
167  component()->getGA()->loadGenotypes(populationFile);
168  // extract the number of corresponding seed
169  QRegExp reg("[BG][0-9]+S([0-9]+).gen");
170  reg.indexIn( populationFile );
171  component()->getGA()->setSeed( reg.cap(1).toInt() );
172  Logger::info( QString("TestIndividual - Loaded Genome from %1").arg(populationFile) );
173  populationLoaded = true;
174  }
175 }
176 
177 } //end namespace farsa