experiments/evorobot/include/evorobotexperiment.h

00001 /********************************************************************************
00002  *  FARSA Experiments Library                                                   *
00003  *  Copyright (C) 2007-2012                                                     *
00004  *  Stefano Nolfi <stefano.nolfi@istc.cnr.it>                                   *
00005  *  Onofrio Gigliotta <onofrio.gigliotta@istc.cnr.it>                           *
00006  *  Gianluca Massera <emmegian@yahoo.it>                                        *
00007  *  Tomassino Ferrauto <tomassino.ferrauto@istc.cnr.it>                         *
00008  *                                                                              *
00009  *  This program is free software; you can redistribute it and/or modify        *
00010  *  it under the terms of the GNU General Public License as published by        *
00011  *  the Free Software Foundation; either version 2 of the License, or           *
00012  *  (at your option) any later version.                                         *
00013  *                                                                              *
00014  *  This program is distributed in the hope that it will be useful,             *
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00017  *  GNU General Public License for more details.                                *
00018  *                                                                              *
00019  *  You should have received a copy of the GNU General Public License           *
00020  *  along with this program; if not, write to the Free Software                 *
00021  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00022  ********************************************************************************/
00023 
00024 #ifndef EVOROBOTEXPERIMENT_H
00025 #define EVOROBOTEXPERIMENT_H
00026 
00027 #include "resourcesuser.h"
00028 #include "evonet.h"
00029 #include "neuroninterfaces.h"
00030 #include "robots.h"
00031 #include "world.h"
00032 #include "physphere.h"
00033 #include "phybox.h"
00034 #include "phycylinder.h"
00035 #include "configurationparameters.h"
00036 #include "parametersettable.h"
00037 
00038 #include <QCoreApplication>
00039 #include <QVector>
00040 #include <QMap>
00041 #include <QString>
00042 
00043 namespace farsa {
00044 
00045 class Evoga;
00046 
00054 class FARSA_EXPERIMENTS_TEMPLATE WObjectsList : public QVector<WObject*>, public Resource
00055 {
00056 public:
00060     WObjectsList() :
00061         QVector()
00062     {
00063     }
00064 
00072     WObjectsList(int size) :
00073         QVector(size)
00074     {
00075     }
00076 
00085     WObjectsList(int size, WObject* const & value) :
00086         QVector(size, value)
00087     {
00088     }
00089 
00095     WObjectsList(const QVector<WObject*>& other) :
00096         QVector(other)
00097     {
00098     }
00099 };
00100 
00151 class FARSA_EXPERIMENTS_API EvoRobotExperiment : public QObject, public ParameterSettableWithConfigureFunction, public ConcurrentResourcesUser
00152 {
00153     Q_OBJECT
00154 public:
00156     EvoRobotExperiment();
00158     virtual ~EvoRobotExperiment();
00159     
00161     void setEvoga( Evoga* ga );
00163     Evoga* getEvoga();
00165     bool inBatchRunning();
00167     Evonet* getNeuralNetwork();
00169     void setNetParameters(int *genes);
00171     virtual void initGeneration(int generation);
00173     virtual void initIndividual(int individual);
00175     virtual void initTrial(int trial);
00177     virtual void initStep( int step );
00179     double getFitness();
00191     void stopTrial();
00201     void skipTrial();
00213     void restartTrial();
00226     void endIndividualLife();
00231     virtual void afterSensorsUpdate();
00235     virtual void beforeMotorsUpdate();
00237     virtual void endStep( int step ) = 0;
00241     virtual void endTrial(int trial);
00244     virtual void endIndividual(int individual);
00246     virtual void endGeneration(int generation);
00248     void doAllTrialsForIndividual(int individual);
00249     
00251     int getGenomeLength();
00255     Sensor* getSensor( QString sensorName );
00259     Motor* getMotor( QString motorName );
00260 
00262     virtual void setTestingAgentAndSeed( int idindividual, int nreplica );
00263 
00265     enum Phases {
00266         INTEST,
00267         INEVOLUTION,
00268         NONE
00269     };
00271     Phases getActivityPhase() {
00272         return gaPhase;
00273     };
00275     void setActivityPhase( Phases newPhase ) {
00276         gaPhase = newPhase;
00277     };
00278 
00291     virtual void configure(ConfigurationParameters& params, QString prefix);
00292 
00303     virtual void save(ConfigurationParameters& params, QString prefix);
00304 
00312     static void describe( QString type );
00313 
00318     virtual void postConfigureInitialization();
00319 
00325     int getNTrials() const
00326     {
00327         return ntrials;
00328     }
00329 
00335     int getNSteps() const
00336     {
00337         return nsteps;
00338     }
00339 
00345     int getCurStep() const
00346     {
00347         return nstep;
00348     }
00349 
00355     int getCurTrial() const
00356     {
00357         return ntrial;
00358     }
00359 
00360 protected:
00361     int ntrials;            
00362     int nsteps;         
00363     int nstep;          
00364     int ntrial;         
00365 
00374     void recreateWorld();
00382     void recreateRobot();
00390     void recreateNeuralNetwork();
00391     
00393     double trialFitnessValue;
00395     double totalFitnessValue;
00396 
00397     //--- it's not anymore const, because createNeuralNetwork may modify some [NET]
00398     //    parameters; in particual nSensors, nHiddens and nMotors
00399     ConfigurationParameters* savedConfigurationParameters;
00400     const QString* savedPrefix;
00401 
00402 private:
00404     void doTrial();
00406     void doStep();
00407 
00408     // --- the attributes of the class should be private
00409     Evoga* ga;
00411     Evonet *evonet;
00413     EvonetIterator* neuronsIterator;
00415     World* world;
00417     Robot* robot;
00419     QVector<Sensor*> sensors;
00421     QVector<Motor*> motors;
00423     QMap<QString, Sensor*> sensorsMap;
00425     QMap<QString, Motor*> motorsMap;
00427     Phases gaPhase;
00429     bool stopCurrentTrial;
00431     bool skipCurrentTrial;
00433     bool restartCurrentTrial;
00435     bool endCurrentIndividualLife;
00437     bool batchRunning;
00438 };
00439 
00440 } // end namespace farsa
00441 
00442 #endif