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;
122  DoubleVector& inputs() {
123  return inputdata;
124  };
126  const DoubleVector& inputs() const {
127  return inputdata;
128  };
130  void setOutput( unsigned int neuron, double value );
132  void setOutputs( const DoubleVector& outputs );
134  double getOutput( unsigned int neuron ) const;
136  DoubleVector& outputs() {
137  return outputdata;
138  };
140  const DoubleVector& outputs() const {
141  return outputdata;
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").toLatin1().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  };
213 private:
215  unsigned int numneurons;
217  DoubleVector inputdata;
219  DoubleVector outputdata;
221  std::auto_ptr<OutputFunction> updater;
223  bool needRst;
227  bool accOff;
228 
230  QMap<QString, getStateVectorFuncPtr> stateDelegates;
232  template <class T, DoubleVector& (T::*TMethod)()>
233  static DoubleVector& staticDelegateMethod( Cluster* cluster_ptr ) {
234  T* p = static_cast<T*>(cluster_ptr);
235  //--- call the delegate method using pointer-to-member syntax
236  return (p->*TMethod)();
237  }
238 };
239 
240 }
241 
242 #endif
This file contains the common type defitions used on the whole framework.
getStateVectorFuncPtr getDelegateFor(QString stateVector)
Return the pointer to function for retrieving the DoubleVector representing the state requested...
Definition: cluster.h:182
OutputFunction Class.
Thrown when a user attempt to get a delegate for a state vector that does not exists.
FARSA_UTIL_TEMPLATE const T max(const T &t1, const U &t2)
DoubleVector & inputs()
Get the array of inputs.
Definition: cluster.h:122
Updatables objects.
Definition: updatable.h:37
const DoubleVector & inputs() const
Get the array of inputs.
Definition: cluster.h:126
void setDelegateFor(QString vectorName)
Configure a delegate for a specifing state vector; who implements subclasses of Cluster has to specif...
Definition: cluster.h:204
Define the common interface among Clusters.
Definition: cluster.h:73
This file contains the declaration of the abstract OutputFunction Class.
bool isAccumulate() const
return true if the Cluster will accumulates inputs
Definition: cluster.h:97
const DoubleVector & outputs() const
Get the array of outputs.
Definition: cluster.h:140
OutputFunction * outFunction() const
Get the Output function.
Definition: cluster.h:149
bool needReset()
Return true if inputs needs a reset.
Definition: cluster.h:86
DoubleVector & outputs()
Get the array of outputs.
Definition: cluster.h:136
FARSA_UTIL_TEMPLATE const T min(const T &t1, const U &t2)
unsigned int numNeurons() const
Return the number of neurons (the length of input and output arrays)
Definition: cluster.h:82
void setNeedReset(bool b)
Set the state of 'needReset' Used by subclasses into update implementation.
Definition: cluster.h:210
void setAccumulate(bool mode)
Enable/Disable accumulation mode If accumulation is enabled (true) then linkers attached to this Clu...
Definition: cluster.h:93