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