neuroninterfaces.h
1 /********************************************************************************
2  * FARSA Experimentes Library *
3  * Copyright (C) 2007-2012 *
4  * Gianluca Massera <emmegian@yahoo.it> *
5  * Stefano Nolfi <stefano.nolfi@istc.cnr.it> *
6  * Tomassino Ferrauto <tomassino.ferrauto@istc.cnr.it> *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
21  ********************************************************************************/
22 
23 #ifndef NEURONINTERFACES_H
24 #define NEURONINTERFACES_H
25 
26 #include "experimentsconfig.h"
27 #include "resourcesuser.h"
28 #include "parametersettable.h"
29 #include "baseexception.h"
30 #include "mathutils.h"
31 #include <QString>
32 #include <QColor>
33 #include <QMap>
34 
35 // All the suff below is to avoid warnings on Windows about the use of the
36 // unsafe function sprintf and strcpy...
37 #if defined(_MSC_VER)
38  #pragma warning(push)
39  #pragma warning(disable:4996)
40 #endif
41 
42 namespace farsa {
43 
44 class Evonet;
45 
56 class FARSA_EXPERIMENTS_TEMPLATE NeuronsIterator : public Resource {
57 public:
59  virtual ~NeuronsIterator() { /*nothing to do*/ };
69  virtual bool setCurrentBlock( QString blockName ) = 0;
75  virtual bool nextNeuron() = 0;
77  virtual void setInput( double value ) = 0;
79  virtual double getInput() = 0;
81  virtual double getOutput() = 0;
88  virtual void setGraphicProperties( QString label, double minValue, double maxValue, QColor color ) = 0;
89 };
90 
113 class FARSA_EXPERIMENTS_API NoisyDevice : public ParameterSettableInConstructor {
114 public:
121  NoisyDevice( ConfigurationParameters& params, QString prefix );
122 
126  ~NoisyDevice();
127 
134  void save( ConfigurationParameters& params, QString prefix );
135 
141  static void describe( QString type );
142 
143 protected:
156  double applyNoise(double v, double minValue, double maxValue) const;
157 
158 private:
162  enum NoiseTypes {
163  NoNoise,
164  Uniform,
165  Gaussian
166  };
167 
171  NoiseTypes noiseType;
172 
183  double noiseRange;
184 
192  double noiseParameter;
193 };
194 
207 class FARSA_EXPERIMENTS_API Sensor : public NoisyDevice, public ConcurrentResourcesUser {
208 public:
213  Sensor( ConfigurationParameters& params, QString prefix );
215  ~Sensor();
220  void save( ConfigurationParameters& params, QString prefix );
222  static void describe( QString type );
226  QString name();
228  void setName( QString name );
230  virtual void update() = 0;
236  virtual int size() = 0;
237 protected:
241  void checkAllNeededResourcesExist();
245  void resetNeededResourcesCheck();
250  QString actualResourceNameForMultirobot(QString resourceName) const;
251 private:
253  bool allNeededResourcesExist;
255  QString sensorName;
258  QString resourcePrefix;
259 };
260 
272 class FARSA_EXPERIMENTS_API Motor : public NoisyDevice, public ConcurrentResourcesUser {
273 public:
278  Motor( ConfigurationParameters& params, QString prefix );
280  ~Motor();
285  void save( ConfigurationParameters& params, QString prefix );
287  static void describe( QString type );
291  QString name();
293  void setName( QString name );
295  virtual void update() = 0;
301  virtual int size() = 0;
302 protected:
306  void checkAllNeededResourcesExist();
310  void resetNeededResourcesCheck();
315  QString actualResourceNameForMultirobot(QString resourceName) const;
316 private:
318  bool allNeededResourcesExist;
320  QString motorName;
323  QString resourcePrefix;
324 };
325 
333 class FARSA_EXPERIMENTS_API EvonetIterator : public NeuronsIterator {
334 public:
336  EvonetIterator();
338  virtual ~EvonetIterator();
342  void setEvonet( Evonet* evonet );
344  Evonet* getEvonet();
346  typedef enum { InputLayer, HiddenLayer, OutputLayer } layer_t;
354  void defineBlock( QString name, layer_t layer, int startIndex, int size );
365  bool setCurrentBlock( QString blockName );
371  bool nextNeuron();
373  void setInput( double value );
375  double getInput();
377  double getOutput();
384  void setGraphicProperties( QString label, double minValue, double maxValue, QColor color );
385 private:
391  void checkCurrentStatus( const QString& funcName = QString() ) const;
393  class BlockInfo {
394  public:
396  int startIndex;
397  int endIndex;
398  };
400  QMap<QString, BlockInfo> blocks;
402  layer_t currLayer;
406  int currStartIndex;
408  int currEndIndex;
410  int currIndex;
412  Evonet* evonet;
413 };
414 
418 class FARSA_CONF_TEMPLATE EvonetIteratorInvalidStatusException : public BaseException
419 {
420 public:
431  EvonetIteratorInvalidStatusException(const char* function, const char* reason) throw() :
432  BaseException()
433  {
434  strncpy(m_function, function, 256);
435  m_function[255] = '\0';
436  strncpy(m_reason, reason, 256);
437  m_reason[255] = '\0';
438  sprintf(m_errorMessage, "Invalid status for EvonetItarator in function \"%s\", reason: %s", m_function, m_reason);
439  m_errorMessage[1023] = '\0';
440  }
441 
448  BaseException(other)
449  {
450  strncpy(m_function, other.m_function, 256);
451  m_function[255] = '\0';
452  strncpy(m_reason, other.m_reason, 256);
453  m_reason[255] = '\0';
454  strncpy(m_errorMessage, other.m_errorMessage, 1024);
455  m_errorMessage[1023] = '\0';
456  }
457 
464  {
465  if (&other == this) {
466  return *this;
467  }
468 
469  BaseException::operator=(other);
470  strncpy(m_function, other.m_function, 256);
471  m_function[255] = '\0';
472  strncpy(m_reason, other.m_reason, 256);
473  m_reason[255] = '\0';
474  strncpy(m_errorMessage, other.m_errorMessage, 1024);
475  m_errorMessage[1023] = '\0';
476 
477  return *this;
478  }
479 
484  {
485  }
486 
492  virtual const char *what() const throw()
493  {
494  return m_errorMessage;
495  }
496 
502  const char *function() const throw()
503  {
504  return m_function;
505  }
506 
513  const char *reason() const throw()
514  {
515  return m_reason;
516  }
517 
522  EXCEPTION_HELPER_FUNCTIONS(EvonetIteratorInvalidStatusException)
523 
524 private:
528  char m_function[256];
529 
533  char m_reason[256];
534 
538  char m_errorMessage[1024];
539 };
540 
541 } // end namespace farsa
542 
543 // All the suff below is to restore the warning state on Windows
544 #if defined(_MSC_VER)
545  #pragma warning(pop)
546 #endif
547 
548 #endif
This class iterate over the neurons of a Evonet neural network.
virtual ~NeuronsIterator()
Destructor.
layer_t
enum the possible layers on which the blocks can be defined
EvonetIteratorInvalidStatusException & operator=(const EvonetIteratorInvalidStatusException &other)
Copy operator.
The base abstract class for iterating over neurons of a neural network.
The base abstract class for devices (sensors and motors) with noise.
This file contains the common type defitions used on the whole framework.
The base abstract class for the Sensor hierarchy.
virtual ~EvonetIteratorInvalidStatusException()
Destructor.
const char * reason() const
Returns the string explaining why the object is not in a valid status.
EvonetIteratorInvalidStatusException(const char *function, const char *reason)
Constructor.
Evonet is the neural network taken from the EvoRobot.
Definition: evonet.h:121
virtual const char * what() const
Returns a C string describing the exception.
The exception thrown when EvonetIterator is not in a valid status.
The base abstract class for the Motor hierarchy.
EvonetIteratorInvalidStatusException(const EvonetIteratorInvalidStatusException &other)
Copy constructor.