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();
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  void setNetParameters(int *genes);
229  virtual void initGeneration(int generation);
231  virtual void initIndividual(int individual);
234  virtual void initTrial(int trial);
236  virtual void initStep( int step );
238  double getFitness();
250  void stopTrial();
260  void skipTrial();
272  void restartTrial();
285  void endIndividualLife();
290  virtual void afterSensorsUpdate();
294  virtual void beforeMotorsUpdate();
298  virtual void beforeWorldAdvance();
300  virtual void endStep( int step ) = 0;
304  virtual void endTrial(int trial);
307  virtual void endIndividual(int individual);
309  virtual void endGeneration(int generation);
311  void doAllTrialsForIndividual(int individual);
312 
314  int getGenomeLength();
321  Sensor* getSensor( QString sensorName, int agentId=0 );
328  Motor* getMotor( QString motorName, int agentId=0 );
329 
332  return arena;
333  }
334 
336  virtual void setTestingAgentAndSeed( int idindividual, int nreplica );
337 
339  enum Phases {
340  INTEST,
341  INEVOLUTION,
342  NONE
343  };
346  return gaPhase;
347  };
349  void setActivityPhase( Phases newPhase ) {
350  gaPhase = newPhase;
351  };
352 
365  virtual void configure(ConfigurationParameters& params, QString prefix);
366 
377  virtual void save(ConfigurationParameters& params, QString prefix);
378 
386  static void describe( QString type );
387 
392  virtual void postConfigureInitialization();
393 
399  int getNTrials() const
400  {
401  return ntrials;
402  }
408  void setNTrials( int new_ntrials ) {
409  ntrials = new_ntrials;
410  }
411 
417  int getNSteps() const
418  {
419  return nsteps;
420  }
426  void setNSteps( int new_nsteps ) {
427  nsteps = new_nsteps;
428  }
429 
435  int getCurStep() const
436  {
437  return nstep;
438  }
439 
445  int getCurTrial() const
446  {
447  return ntrial;
448  }
449 
450 public slots:
455  void setStepDelay( int delay );
456 
458  int getStepDelay();
459 
461  float getWorldTimeStep() const;
462 
463 protected:
464  int ntrials;
465  int nsteps;
466  int nstep;
467  int ntrial;
468 
477  void recreateWorld();
491  bool recreateRobot( int agentId = 0 );
498  void recreateAllRobots();
504  void recreateArena();
516  void recreateNeuralNetwork( int agentId = 0 );
523  void recreateAllNeuralNetworks();
524 
529 
530  //--- it's not anymore const, because createNeuralNetwork may modify some [NET]
531  // parameters; in particual nSensors, nHiddens and nMotors
532  ConfigurationParameters* savedConfigurationParameters;
533  const QString* savedPrefix;
534 
535 private:
537  void doTrial();
539  void doStep();
540 
542  void setWorldTimestep( float timestep );
543 
545  class EmbodiedAgent {
546  public:
548  EmbodiedAgent( int id, QString agentPath, EvoRobotExperiment* exp );
550  ~EmbodiedAgent();
552  void configure();
554  void recreateRobot();
556  void recreateNeuralNetwork();
560  void disable();
564  void enable();
566  int id;
568  bool enabled;
570  QString agentPath;
572  QString resourcePrefix;
574  Evonet* evonet;
576  EvonetIterator* neuronsIterator;
578  Robot* robot;
580  QVector<Sensor*> sensors;
582  QVector<Motor*> motors;
584  QMap<QString, Sensor*> sensorsMap;
586  QMap<QString, Motor*> motorsMap;
588  EvoRobotExperiment* exp;
589  };
590 
592  Evoga* ga;
594  World* world;
596  float timestep;
598  QList<EmbodiedAgent*> eagents;
600  int agentIdSelected;
602  Phases gaPhase;
604  bool stopCurrentTrial;
606  bool skipCurrentTrial;
608  bool restartCurrentTrial;
610  bool endCurrentIndividualLife;
612  bool batchRunning;
614  Arena* arena;
618  int stepDelay;
619 };
620 
621 } // end namespace farsa
622 
623 #endif