cluster.h
Go to the documentation of this file.
1 /********************************************************************************
2  * Neural Network Framework. *
3  * Copyright (C) 2005-2011 Gianluca Massera <emmegian@yahoo.it> *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the Free Software *
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
18  ********************************************************************************/
19 
20 #ifndef CLUSTER_H
21 #define CLUSTER_H
22 
27 #include "nnfwconfig.h"
28 #include "updatable.h"
29 #include "outputfunction.h"
30 #include <exception>
31 #include <memory>
32 
33 namespace farsa {
34 
73 class FARSA_NNFW_API Cluster : public Updatable {
74 public:
76  Cluster( unsigned int numNeurons, QString name = "unnamed" );
78  Cluster( ConfigurationParameters& params, QString prefix );
80  virtual ~Cluster();
82  unsigned int numNeurons() const {
83  return numneurons;
84  };
86  bool needReset() {
87  return needRst;
88  };
93  void setAccumulate( bool mode ) {
94  accOff = !mode;
95  };
97  bool isAccumulate() const {
98  return !accOff;
99  };
103  virtual void randomize( double min, double max ) = 0;
107  void setInput( unsigned int neuron, double value );
109  void setInputs( const DoubleVector& inputs );
113  void setAllInputs( double value );
117  void resetInputs();
120  double getInput( unsigned int neuron ) const;
123  return *inputdataptr;
124  };
127  return *inputdataptr;
128  };
130  void setOutput( unsigned int neuron, double value );
132  void setOutputs( const DoubleVector& outputs );
134  double getOutput( unsigned int neuron ) const;
137  return *outputdataptr;
138  };
141  return *outputdataptr;
142  };
147  void setOutFunction( OutputFunction* up );
150  return updater.get();
151  };
160  virtual void save(ConfigurationParameters& params, QString prefix);
162  static void describe( QString type );
175  typedef DoubleVector& (*getStateVectorFuncPtr)( Cluster* );
182  getStateVectorFuncPtr getDelegateFor( QString stateVector ) {
183  if ( stateDelegates.contains( stateVector ) ) {
184  return stateDelegates[stateVector];
185  }
186  throw ClusterStateVectorNotPresent( (QString("The state vector named ") + stateVector + " is not part of this Cluster").toAscii().data() );
187  };
188 protected:
203  template <class T, DoubleVector& (T::*TMethod)()>
204  void setDelegateFor( QString vectorName ) {
205  stateDelegates[vectorName] = &staticDelegateMethod<T, TMethod>;
206  }
210  void setNeedReset( bool b ) {
211  needRst = accOff && b;
212  };
214  DoubleVector* inputdataptr;
217 private:
219  unsigned int numneurons;
221  DoubleVector inputdata;
223  DoubleVector outputdata;
225  std::auto_ptr<OutputFunction> updater;
227  bool needRst;
231  bool accOff;
232 
234  QMap<QString, getStateVectorFuncPtr> stateDelegates;
236  template <class T, DoubleVector& (T::*TMethod)()>
237  static DoubleVector& staticDelegateMethod( Cluster* cluster_ptr ) {
238  T* p = static_cast<T*>(cluster_ptr);
239  //--- call the delegate method using pointer-to-member syntax
240  return (p->*TMethod)();
241  }
242 };
243 
244 }
245 
246 #endif