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 
40 #include <QCoreApplication>
41 #include <QVector>
42 #include <QMap>
43 #include <QString>
44 
45 namespace farsa {
46 
47 class Evoga;
48 
56 class FARSA_EXPERIMENTS_TEMPLATE WObjectsList : public QVector<WObject*>, public Resource
57 {
58 public:
63  QVector<WObject*>()
64  {
65  }
66 
74  WObjectsList(int size) :
75  QVector<WObject*>(size)
76  {
77  }
78 
87  WObjectsList(int size, WObject* const & value) :
88  QVector<WObject*>(size, value)
89  {
90  }
91 
97  WObjectsList(const QVector<WObject*>& other) :
98  QVector<WObject*>(other)
99  {
100  }
101 };
102 
154 class FARSA_EXPERIMENTS_API EvoRobotExperiment : public QObject, public ParameterSettableWithConfigureFunction, public ConcurrentResourcesUser
155 {
156  Q_OBJECT
157 public:
161  virtual ~EvoRobotExperiment();
162 
164  void setEvoga( Evoga* ga );
166  Evoga* getEvoga();
168  bool inBatchRunning();
171  int getNAgents();
180  void selectAgent( int agentId );
184  Evonet* getNeuralNetwork( int agentId=0 );
186  void setNetParameters(int *genes);
188  virtual void initGeneration(int generation);
190  virtual void initIndividual(int individual);
192  virtual void initTrial(int trial);
194  virtual void initStep( int step );
196  double getFitness();
208  void stopTrial();
218  void skipTrial();
230  void restartTrial();
243  void endIndividualLife();
248  virtual void afterSensorsUpdate();
252  virtual void beforeMotorsUpdate();
256  virtual void beforeWorldAdvance();
258  virtual void endStep( int step ) = 0;
262  virtual void endTrial(int trial);
265  virtual void endIndividual(int individual);
267  virtual void endGeneration(int generation);
269  void doAllTrialsForIndividual(int individual);
270 
272  int getGenomeLength();
277  Sensor* getSensor( QString sensorName, int agentId=0 );
282  Motor* getMotor( QString motorName, int agentId=0 );
283 
286  return arena;
287  }
288 
290  virtual void setTestingAgentAndSeed( int idindividual, int nreplica );
291 
293  enum Phases {
294  INTEST,
295  INEVOLUTION,
296  NONE
297  };
300  return gaPhase;
301  };
303  void setActivityPhase( Phases newPhase ) {
304  gaPhase = newPhase;
305  };
306 
319  virtual void configure(ConfigurationParameters& params, QString prefix);
320 
331  virtual void save(ConfigurationParameters& params, QString prefix);
332 
340  static void describe( QString type );
341 
346  virtual void postConfigureInitialization();
347 
353  int getNTrials() const
354  {
355  return ntrials;
356  }
362  void setNTrials( int new_ntrials ) {
363  ntrials = new_ntrials;
364  }
365 
371  int getNSteps() const
372  {
373  return nsteps;
374  }
380  void setNSteps( int new_nsteps ) {
381  nsteps = new_nsteps;
382  }
383 
389  int getCurStep() const
390  {
391  return nstep;
392  }
393 
399  int getCurTrial() const
400  {
401  return ntrial;
402  }
403 
404 protected:
405  int ntrials;
406  int nsteps;
407  int nstep;
408  int ntrial;
409 
418  void recreateWorld();
429  void recreateRobot( int agentId = 0 );
435  void recreateArena();
446  void recreateNeuralNetwork( int agentId = 0 );
447 
452 
453  //--- it's not anymore const, because createNeuralNetwork may modify some [NET]
454  // parameters; in particual nSensors, nHiddens and nMotors
455  ConfigurationParameters* savedConfigurationParameters;
456  const QString* savedPrefix;
457 
458 private:
460  void doTrial();
462  void doStep();
463 
465  void setWorldTimestep( float timestep );
467  float getWorldTimeStep() const;
468 
470  class EmbodiedAgent {
471  public:
473  EmbodiedAgent( int id, QString agentPath, EvoRobotExperiment* exp );
475  ~EmbodiedAgent();
477  void configure();
479  void recreateRobot();
481  void recreateNeuralNetwork();
483  int id;
485  QString agentPath;
487  QString resourcePrefix;
489  Evonet* evonet;
491  EvonetIterator* neuronsIterator;
493  Robot* robot;
495  QVector<Sensor*> sensors;
497  QVector<Motor*> motors;
499  QMap<QString, Sensor*> sensorsMap;
501  QMap<QString, Motor*> motorsMap;
503  EvoRobotExperiment* exp;
504  };
505 
507  Evoga* ga;
509  World* world;
511  QList<EmbodiedAgent*> eagents;
513  int agentIdSelected;
515  Phases gaPhase;
517  bool stopCurrentTrial;
519  bool skipCurrentTrial;
521  bool restartCurrentTrial;
523  bool endCurrentIndividualLife;
525  bool batchRunning;
527  Arena* arena;
528 };
529 
530 } // end namespace farsa
531 
532 #endif