evorobotexperiment.h
1 /********************************************************************************
2  * FARSA Experiments Library *
3  * Copyright (C) 2007-2012 *
4  * Stefano Nolfi <stefano.nolfi@istc.cnr.it> *
5  * Onofrio Gigliotta <onofrio.gigliotta@istc.cnr.it> *
6  * Gianluca Massera <emmegian@yahoo.it> *
7  * Tomassino Ferrauto <tomassino.ferrauto@istc.cnr.it> *
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17  * GNU General Public License for more details. *
18  * *
19  * You should have received a copy of the GNU General Public License *
20  * along with this program; if not, write to the Free Software *
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
22  ********************************************************************************/
23 
24 #ifndef EVOROBOTEXPERIMENT_H
25 #define EVOROBOTEXPERIMENT_H
26 
27 #include "resourcesuser.h"
28 #include "evonet.h"
29 #include "neuroninterfaces.h"
30 #include "robots.h"
31 #include "world.h"
32 #include "physphere.h"
33 #include "phybox.h"
34 #include "phycylinder.h"
35 #include "configurationparameters.h"
36 #include "parametersettable.h"
37 #include "arena.h"
38 #include "logger.h"
39 #include "simpletimer.h"
40 #include "randomgenerator.h"
41 
42 #include <QCoreApplication>
43 #include <QVector>
44 #include <QMap>
45 #include <QString>
46 
47 namespace farsa {
48 
49 class Evoga;
50 
58 class FARSA_EXPERIMENTS_TEMPLATE WObjectsList : public QVector<WObject*>, public Resource
59 {
60 public:
65  QVector<WObject*>()
66  {
67  }
68 
76  WObjectsList(int size) :
77  QVector<WObject*>(size)
78  {
79  }
80 
89  WObjectsList(int size, WObject* const & value) :
90  QVector<WObject*>(size, value)
91  {
92  }
93 
99  WObjectsList(const QVector<WObject*>& other) :
100  QVector<WObject*>(other)
101  {
102  }
103 };
104 
155 class FARSA_EXPERIMENTS_API EvoRobotExperiment : public QObject, public ParameterSettableWithConfigureFunction, public ConcurrentResourcesUser
156 {
157  Q_OBJECT
158 public:
162  virtual ~EvoRobotExperiment();
163 
165  void setEvoga( Evoga* ga );
167  Evoga* getEvoga();
169  bool inBatchRunning();
172  int getNAgents();
187  bool selectAgent( int agentId );
199  void enableAgent( int agentId );
211  void disableAgent( int agentId );
220  bool agentEnabled( int agentId );
225  Evonet* getNeuralNetwork( int agentId=0 );
227  virtual void setNetParameters(float *genes);
229  virtual void setNetParameters(int *genes);
231  virtual void initGeneration(int generation);
233  virtual void initIndividual(int individual);
236  virtual void initTrial(int trial);
238  virtual void initStep( int step );
240  double getFitness();
242  double getError();
254  void stopTrial();
264  void skipTrial();
276  void restartTrial();
289  void endIndividualLife();
294  virtual void afterSensorsUpdate();
298  virtual void beforeMotorsUpdate();
302  virtual void beforeWorldAdvance();
304  virtual void endStep( int step ) = 0;
308  virtual void endTrial(int trial);
311  virtual void endIndividual(int individual);
313  virtual void endGeneration(int generation);
315  void doAllTrialsForIndividual(int individual);
316 
318  int getGenomeLength();
325  Sensor* getSensor( QString sensorName, int agentId=0 );
332  Motor* getMotor( QString motorName, int agentId=0 );
333 
336  return arena;
337  }
338 
340  virtual void setTestingAgentAndSeed( int idindividual, int nreplica );
341 
343  enum Phases {
344  INTEST,
345  INEVOLUTION,
346  NONE
347  };
350  return gaPhase;
351  };
353  void setActivityPhase( Phases newPhase ) {
354  gaPhase = newPhase;
355  };
356 
369  virtual void configure(ConfigurationParameters& params, QString prefix);
370 
381  virtual void save(ConfigurationParameters& params, QString prefix);
382 
390  static void describe( QString type );
391 
396  virtual void postConfigureInitialization();
397 
403  int getNTrials() const
404  {
405  return ntrials;
406  }
412  void setNTrials( int new_ntrials ) {
413  ntrials = new_ntrials;
414  }
415 
421  int getNSteps() const
422  {
423  return nsteps;
424  }
430  void setNSteps( int new_nsteps ) {
431  nsteps = new_nsteps;
432  }
433 
439  int getCurStep() const
440  {
441  return nstep;
442  }
443 
449  int getCurTrial() const
450  {
451  return ntrial;
452  }
453 
460  void newGASeed(int seed);
461 
467  QVector<int*> m_weightIndividual;
468 
469 public slots:
474  void setStepDelay( int delay );
475 
477  int getStepDelay();
478 
480  float getWorldTimeStep() const;
481 
482 protected:
483  int ntrials;
484  int nsteps;
485  int nstep;
486  int ntrial;
487 
496  void recreateWorld();
510  bool recreateRobot( int agentId = 0 );
517  void recreateAllRobots();
523  void recreateArena();
535  void recreateNeuralNetwork( int agentId = 0 );
542  void recreateAllNeuralNetworks();
543 
553  RandomGenerator* getRNG();
554 
563 
564  //--- it's not anymore const, because createNeuralNetwork may modify some [NET]
565  // parameters; in particual nSensors, nHiddens and nMotors
566  ConfigurationParameters* savedConfigurationParameters;
567  const QString* savedPrefix;
568 
569 private:
571  void doTrial();
573  void doStep();
574 
576  void setWorldTimestep( float timestep );
577 
579  class EmbodiedAgent {
580  public:
582  EmbodiedAgent( int id, QString agentPath, EvoRobotExperiment* exp );
584  ~EmbodiedAgent();
586  void configure();
588  void recreateRobot();
590  void recreateNeuralNetwork();
594  void disable();
598  void enable();
600  int id;
602  bool enabled;
604  QString agentPath;
606  QString resourcePrefix;
608  Evonet* evonet;
610  EvonetIterator* neuronsIterator;
612  Robot* robot;
614  QVector<Sensor*> sensors;
616  QVector<Motor*> motors;
618  QMap<QString, Sensor*> sensorsMap;
620  QMap<QString, Motor*> motorsMap;
622  EvoRobotExperiment* exp;
623  };
624 
626  Evoga* ga;
628  World* world;
630  float timestep;
632  QList<EmbodiedAgent*> eagents;
634  int agentIdSelected;
636  Phases gaPhase;
638  bool stopCurrentTrial;
640  bool skipCurrentTrial;
642  bool restartCurrentTrial;
644  bool endCurrentIndividualLife;
646  bool batchRunning;
648  Arena* arena;
652  int stepDelay;
656  bool sameRandomSequence;
661  RandomGenerator* randomGeneratorInUse;
663  RandomGenerator localRNG;
664 };
665 
666 } // end namespace farsa
667 
668 #endif
This class iterate over the neurons of a Evonet neural network.
QVector< int * > m_weightIndividual
Added by Jonata for the SpecializerSteadyState GA.
A class representing a list of objects.
double totalFitnessValue
the fitness value of the individual all over the trials
Phases getActivityPhase()
return the state of the EvoRobotExperiment
int getCurTrial() const
Returns the current trial.
int getNTrials() const
Returns the number of trials.
The class modelling an arena.
Definition: arena.h:50
double totalErrorValue
the error of the individual all over the trials
An abstract class for robots.
Definition: robots.h:74
The base abstract class for the Sensor hierarchy.
int getCurStep() const
Returns the current step.
int getNSteps() const
Returns the number of steps.
Genetic algorithm from evorobot more or less (spare parts)
Definition: evoga.h:50
Arena * getArena()
Returns the arena. This can returns NULL if no arena object is present.
Phases
this type define the phase of which the EvoRobotExperiment
Evonet is the neural network taken from the EvoRobot.
Definition: evonet.h:121
The base abstract class for the Motor hierarchy.
int nstep
number of cycles
void setNSteps(int new_nsteps)
Sets the number of steps.
WObjectsList()
Constructor.
double trialFitnessValue
the fitness value of the individual during the execution of one trial
double trialErrorValue
the error of the individual during the execution of one trial
WObjectsList(int size)
Constructor.
void setNTrials(int new_ntrials)
Sets the number of trials.
The base common class that evaluate the fitness of a robot.
void setActivityPhase(Phases newPhase)
set the new activity phase
WObjectsList(int size, WObject *const &value)
Constructor.
WObjectsList(const QVector< WObject * > &other)
Copy Constructor.