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 
41 #include <QCoreApplication>
42 #include <QVector>
43 #include <QMap>
44 #include <QString>
45 
46 namespace farsa {
47 
48 class Evoga;
49 
57 class FARSA_EXPERIMENTS_TEMPLATE WObjectsList : public QVector<WObject*>, public Resource
58 {
59 public:
64  QVector<WObject*>()
65  {
66  }
67 
75  WObjectsList(int size) :
76  QVector<WObject*>(size)
77  {
78  }
79 
88  WObjectsList(int size, WObject* const & value) :
89  QVector<WObject*>(size, value)
90  {
91  }
92 
98  WObjectsList(const QVector<WObject*>& other) :
99  QVector<WObject*>(other)
100  {
101  }
102 };
103 
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();
185  void selectAgent( int agentId );
189  Evonet* getNeuralNetwork( int agentId=0 );
191  void setNetParameters(int *genes);
193  virtual void initGeneration(int generation);
195  virtual void initIndividual(int individual);
197  virtual void initTrial(int trial);
199  virtual void initStep( int step );
201  double getFitness();
213  void stopTrial();
223  void skipTrial();
235  void restartTrial();
248  void endIndividualLife();
253  virtual void afterSensorsUpdate();
257  virtual void beforeMotorsUpdate();
261  virtual void beforeWorldAdvance();
263  virtual void endStep( int step ) = 0;
267  virtual void endTrial(int trial);
270  virtual void endIndividual(int individual);
272  virtual void endGeneration(int generation);
274  void doAllTrialsForIndividual(int individual);
275 
277  int getGenomeLength();
282  Sensor* getSensor( QString sensorName, int agentId=0 );
287  Motor* getMotor( QString motorName, int agentId=0 );
288 
291  return arena;
292  }
293 
295  virtual void setTestingAgentAndSeed( int idindividual, int nreplica );
296 
298  enum Phases {
299  INTEST,
300  INEVOLUTION,
301  NONE
302  };
305  return gaPhase;
306  };
308  void setActivityPhase( Phases newPhase ) {
309  gaPhase = newPhase;
310  };
311 
324  virtual void configure(ConfigurationParameters& params, QString prefix);
325 
336  virtual void save(ConfigurationParameters& params, QString prefix);
337 
345  static void describe( QString type );
346 
351  virtual void postConfigureInitialization();
352 
358  int getNTrials() const
359  {
360  return ntrials;
361  }
367  void setNTrials( int new_ntrials ) {
368  ntrials = new_ntrials;
369  }
370 
376  int getNSteps() const
377  {
378  return nsteps;
379  }
385  void setNSteps( int new_nsteps ) {
386  nsteps = new_nsteps;
387  }
388 
394  int getCurStep() const
395  {
396  return nstep;
397  }
398 
404  int getCurTrial() const
405  {
406  return ntrial;
407  }
408 
409 public slots:
414  void setStepDelay( int delay );
415 
417  int getStepDelay();
418 
420  float getWorldTimeStep() const;
421 
422 protected:
423  int ntrials;
424  int nsteps;
425  int nstep;
426  int ntrial;
427 
436  void recreateWorld();
448  void recreateRobot( int agentId = 0 );
455  void recreateAllRobots();
461  void recreateArena();
472  void recreateNeuralNetwork( int agentId = 0 );
479  void recreateAllNeuralNetworks();
480 
485 
486  //--- it's not anymore const, because createNeuralNetwork may modify some [NET]
487  // parameters; in particual nSensors, nHiddens and nMotors
488  ConfigurationParameters* savedConfigurationParameters;
489  const QString* savedPrefix;
490 
491 private:
493  void doTrial();
495  void doStep();
496 
498  void setWorldTimestep( float timestep );
499 
501  class EmbodiedAgent {
502  public:
504  EmbodiedAgent( int id, QString agentPath, EvoRobotExperiment* exp );
506  ~EmbodiedAgent();
508  void configure();
510  void recreateRobot();
512  void recreateNeuralNetwork();
514  int id;
516  QString agentPath;
518  QString resourcePrefix;
520  Evonet* evonet;
522  EvonetIterator* neuronsIterator;
524  Robot* robot;
526  QVector<Sensor*> sensors;
528  QVector<Motor*> motors;
530  QMap<QString, Sensor*> sensorsMap;
532  QMap<QString, Motor*> motorsMap;
534  EvoRobotExperiment* exp;
535  };
536 
538  Evoga* ga;
540  World* world;
542  float timestep;
544  QList<EmbodiedAgent*> eagents;
546  int agentIdSelected;
548  Phases gaPhase;
550  bool stopCurrentTrial;
552  bool skipCurrentTrial;
554  bool restartCurrentTrial;
556  bool endCurrentIndividualLife;
558  bool batchRunning;
560  Arena* arena;
564  int stepDelay;
565 };
566 
567 } // end namespace farsa
568 
569 #endif