neuroninterfaces.cpp
1 /********************************************************************************
2  * FARSA Experiments 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 #include "neuroninterfaces.h"
24 #include "evonet.h"
25 #include "configurationhelper.h"
26 #include "logger.h"
27 
28 namespace farsa {
29 
30 Sensor::Sensor( ConfigurationParameters& params, QString prefix ) :
31  ParameterSettableInConstructor(params, prefix),
32  allNeededResourcesExist(false)
33 {
34  setName( ConfigurationHelper::getString( params, prefix+"name", "unnamed" ) );
35  resourcePrefix = ConfigurationHelper::getString( params, prefix + "__resourcePrefix__", "" );
36 }
37 
39 }
40 
41 void Sensor::save( ConfigurationParameters& params, QString prefix ) {
42  params.startObjectParameters( prefix, "Sensor", this );
43  // if Sensor name startsWith "Sensor:" means that the name has been
44  // configured by EvoRobotExperiment automatically
45  if ( sensorName != "unnamed" || !sensorName.startsWith("Sensor:") ) {
46  params.createParameter( prefix, "name", sensorName );
47  }
48  // Here we don't save the __resourcePrefix__ parameter because it is only used internally
49 }
50 
51 void Sensor::describe( QString type ) {
52  Descriptor d = addTypeDescription( type, "Base class for all Sensors" );
53  d.describeString( "name" ).help( "The name of the sensor" );
54  // Here we don't describe the __resourcePrefix__ parameter because it is only used internally
55 }
56 
57 QString Sensor::name() {
58  return sensorName;
59 }
60 
61 void Sensor::setName( QString name ) {
62  sensorName = name;
63 }
64 
66  if (allNeededResourcesExist) {
67  return;
68  }
69 
70  // We do the check only if allNeededResourcesExist is false, if it is true we already did the
71  // check and we will be notified when a resource gets deleted
72  QStringList nonExistingResources;
73  if (!usedResourcesExist(&nonExistingResources)) {
74  ConfigurationHelper::throwUserMissingResourceError(nonExistingResources.join(", "), "Some required resource do not exist, cannot use " + sensorName + " anymore");
75  }
76 
77  allNeededResourcesExist = true;
78 }
79 
81 {
82  allNeededResourcesExist = false;
83 }
84 
85 QString Sensor::actualResourceNameForMultirobot(QString resourceName) const
86 {
87  return resourcePrefix + resourceName;
88 }
89 
90 Motor::Motor( ConfigurationParameters& params, QString prefix ) :
91  ParameterSettableInConstructor(params, prefix),
92  allNeededResourcesExist(false)
93 {
94  setName( ConfigurationHelper::getString( params, prefix+"name", "unnamed" ) );
95  resourcePrefix = ConfigurationHelper::getString( params, prefix + "__resourcePrefix__", "" );
96 }
97 
99 }
100 
101 void Motor::save( ConfigurationParameters& params, QString prefix ) {
102  params.startObjectParameters( prefix, "Motor", this );
103  // if Motor name startsWith "Motor:" means that the name has been
104  // configured by EvoRobotExperiment automatically
105  if ( motorName != "unnamed" || !motorName.startsWith("Motor:") ) {
106  params.createParameter( prefix, "name", motorName );
107  }
108  // Here we don't save the __resourcePrefix__ parameter because it is only used internally
109 }
110 
111 void Motor::describe( QString type ) {
112  Descriptor d = addTypeDescription( type, "Base class for all Motors" );
113  d.describeString( "name" ).help( "The name of the motor" );
114  // Here we don't describe the __resourcePrefix__ parameter because it is only used internally
115 }
116 
117 QString Motor::name() {
118  return motorName;
119 }
120 
121 void Motor::setName( QString name ) {
122  motorName = name;
123 }
124 
126  if (allNeededResourcesExist) {
127  return;
128  }
129 
130  // We do the check only if allNeededResourcesExist is false, if it is true we already did the
131  // check and we will be notified when a resource gets deleted
132  QStringList nonExistingResources;
133  if (!usedResourcesExist(&nonExistingResources)) {
134  ConfigurationHelper::throwUserMissingResourceError(nonExistingResources.join(", "), "Some required resource do not exist, cannot use " + motorName + " anymore");
135  }
136 
137  allNeededResourcesExist = true;
138 }
139 
141 {
142  allNeededResourcesExist = false;
143 }
144 
145 QString Motor::actualResourceNameForMultirobot(QString resourceName) const
146 {
147  return resourcePrefix + resourceName;
148 }
149 
151  blocks(),
152  currLayer(InputLayer),
153  currStartIndex(-1),
154  currEndIndex(0),
155  currIndex(0),
156  evonet(NULL) {
157 }
158 
160  /* nothing to do */
161 }
162 
164  this->evonet = evonet;
165  blocks.clear();
166  currStartIndex = -1;
167  currEndIndex = 0;
168  currIndex = 0;
169 }
170 
172  return evonet;
173 }
174 
175 void EvonetIterator::defineBlock( QString name, layer_t layer, int startIndex, int size ) {
176  BlockInfo binfo;
177  binfo.layer = layer;
178  binfo.startIndex = startIndex;
179  binfo.endIndex = startIndex + size;
180  blocks[name] = binfo;
181 }
182 
183 bool EvonetIterator::setCurrentBlock( QString blockName ) {
184  if ( !blocks.contains( blockName ) ) {
185  Logger::error( QString("EvonetIterator - the block %1 does not exist").arg(blockName) );
186  return false;
187  }
188  BlockInfo binfo = blocks[blockName];
189  currLayer = binfo.layer;
190  currStartIndex = binfo.startIndex;
191  currEndIndex = binfo.endIndex;
192  currIndex = currStartIndex;
193  return true;
194 }
195 
197  checkCurrentStatus( "nextNeuron" );
198 
199  currIndex++;
200  if ( currIndex >= currEndIndex ) {
201  return false;
202  }
203  return true;
204 }
205 
206 void EvonetIterator::setInput( double value ) {
207  checkCurrentStatus( "setInput" );
208 
209  evonet->setInput( currIndex, value );
210 }
211 
213  checkCurrentStatus( "getOutput" );
214 
215  return evonet->getOutput( currIndex );
216 }
217 
218 void EvonetIterator::setGraphicProperties( QString label, double minValue, double maxValue, QColor color ) {
219  checkCurrentStatus( "setGraphicProperties" );
220 
221  label.truncate( 9 );
222  int offsetIndex = 0;
223  if ( currLayer == OutputLayer ) {
224  offsetIndex = evonet->getNoInputs() + evonet->getNoHiddens();
225  }
226  sprintf( evonet->neuronl[ offsetIndex+currIndex ], "%s", label.toAscii().data() );
227  evonet->neuronrange[ offsetIndex+currIndex ][0] = minValue;
228  evonet->neuronrange[ offsetIndex+currIndex ][1] = maxValue;
229  // FIXME: need to change how the color are handled into Evonet
230  evonet->neurondcolor[ offsetIndex+currIndex ] = color;
231 }
232 
233 void EvonetIterator::checkCurrentStatus( const QString& funcName ) const {
234  if ( !evonet ) {
235  throw EvonetIteratorInvalidStatusException( funcName.toAscii().data(), "no Evonet object has ben set");
236  }
237  if ( currStartIndex < 0 ) {
238  throw EvonetIteratorInvalidStatusException( funcName.toAscii().data(), "you should call setCurrentBlock first");
239  }
240  if ( currIndex >= currEndIndex ) {
241  throw EvonetIteratorInvalidStatusException( funcName.toAscii().data(), "attempt to access beyond the size of the current block");
242  }
243 }
244 
245 } // end namespace farsa