neuralnetiterator.cpp
1 /********************************************************************************
2  * FARSA Experiments Library *
3  * Copyright (C) 2007-2013 *
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 "neuralnetiterator.h"
24 #include "logger.h"
25 
26 namespace farsa {
27 
29  neuralnet(NULL),
30  cluster(NULL),
31  currIndex(-1) {
32 }
33 
35  /* nothing to do */
36 }
37 
38 void NeuralNetIterator::setNeuralNet( NeuralNet* neuralnet ) {
39  this->neuralnet = neuralnet;
40  cluster = NULL;
41  currIndex = -1;
42 }
43 
44 bool NeuralNetIterator::setCurrentBlock( QString blockName ) {
45  neuralnet->byName( blockName, cluster );
46  if ( !cluster ) {
47  currIndex = -1;
48  Logger::error( QString("NeuralNetIterator - the block %1 does not exist").arg(blockName) );
49  return false;
50  }
51  currIndex = 0;
52  return true;
53 }
54 
56  checkCurrentStatus( "nextNeuron" );
57 
58  currIndex++;
59  if ( currIndex >= int(cluster->numNeurons()) ) {
60  return false;
61  }
62  return true;
63 }
64 
65 void NeuralNetIterator::setInput( double value ) {
66  checkCurrentStatus( "setInput" );
67 
68  cluster->setInput( currIndex, value );
69 }
70 
72  checkCurrentStatus( "getInput" );
73 
74  return cluster->getInput( currIndex );
75 }
76 
78  checkCurrentStatus( "getOutput" );
79  return cluster->getOutput( currIndex );
80 }
81 
82 void NeuralNetIterator::setGraphicProperties( QString /*label*/, double /*minValue*/, double /*maxValue*/, QColor /*color*/ ) {
83  checkCurrentStatus( "setGraphicProperties" );
84  Logger::warning("NeuralNetIterator - it is not possible to set graphics properties for NeuralNet");
85 }
86 
87 void NeuralNetIterator::checkCurrentStatus( const QString& funcName ) const {
88  if ( !neuralnet ) {
89  throw EvonetIteratorInvalidStatusException( funcName.toLatin1().data(), "no NeuralNet object has ben set");
90  }
91  if ( !cluster ) {
92  throw EvonetIteratorInvalidStatusException( funcName.toLatin1().data(), "you should call setCurrentBlock first");
93  }
94  if ( currIndex >= int(cluster->numNeurons()) ) {
95  throw EvonetIteratorInvalidStatusException( funcName.toLatin1().data(), "attempt to access beyond the size of the current block");
96  }
97 }
98 
99 } // end namespace farsa